From 9ed779b8ae17c4a03c41528f8b9a8054ac06011c Mon Sep 17 00:00:00 2001 From: MrMelon Date: Sat, 24 Apr 2021 11:19:17 +0100 Subject: [PATCH] Add cli binary --- src/impl/cli/main.c | 79 +++++++++++++++++++++++++++++++++++++++++ src/impl/cli/open_gui.c | 57 +++++++++++++++++++++++++++++ 2 files changed, 136 insertions(+) create mode 100644 src/impl/cli/main.c create mode 100644 src/impl/cli/open_gui.c diff --git a/src/impl/cli/main.c b/src/impl/cli/main.c new file mode 100644 index 0000000..1c325a5 --- /dev/null +++ b/src/impl/cli/main.c @@ -0,0 +1,79 @@ +#include +#include +#include + +#include "states.h" +#include "open_gui.h" + +#ifndef PROJ_VERSION + #define PROJ_VERSION "unknown" +#endif + +States currentstate; +char *version = PROJ_VERSION; + +void update_state(States state) { + currentstate = state; +} + +int is_arg(int ac, char **argv, char *arg) { + if (ac < 2) { + return 0; + } + for(int x=1; x < ac; x++) { + if (0 == strcmp(argv[x], arg)) { + return x; // return position of arg + } + } + return 0; // arg not present +} + +void display_version(bool is_debug) { + if(is_debug) printf("v%s-beta\n", version); + else printf("v%s\n", version); +} + +void display_about(bool is_debug) { + printf("Melon VPN\n"); + display_version(is_debug); + printf("Copyright OnPointCoding\n"); + printf("https://software.onpointcoding.net\n"); + printf("OnPointCoding.net\n"); + printf("Author: MrMelon54\n"); + printf("Artist: MrMelon54\n"); +} + +void display_help(bool is_debug) { + printf("Melon VPN Help\n"); + printf("-h, --help Display this help message\n"); + printf("--about About this program\n"); +} + +int main (int argc, char **argv) { + int status; + currentstate = DISCONNECTED; + + bool is_debug = is_arg(argc, argv, "--debug"); + + if(is_arg(argc, argv, "--help") || is_arg(argc, argv, "-h")) { + display_help(is_debug); + return 0; + } + + if(is_arg(argc, argv, "--version") || is_arg(argc, argv, "-v")) { + display_version(is_debug); + return 0; + } + + if(is_arg(argc, argv, "--about")) { + display_about(is_debug); + return 0; + } + + if(is_arg(argc, argv, "--gui")) { + open_gui(argc, argv, is_debug); + return 0; + } + + return status; +} diff --git a/src/impl/cli/open_gui.c b/src/impl/cli/open_gui.c new file mode 100644 index 0000000..9995a0a --- /dev/null +++ b/src/impl/cli/open_gui.c @@ -0,0 +1,57 @@ +#include +#include + +#include "magic_code.h" +#include "open_gui.h" + +int open_gui(int argc, char **argv, bool is_debug) { + void *lib_handle; + void (*fn)(int*, int*); + int (*gui)(int, char**); + int a = 0; + int b = 0; + char *error; + char *p; + + if(is_debug) p = "./libmelonvpngui.so"; + else p = "/usr/lib/melonvpn/libmelonvpngui.so"; + + lib_handle = dlopen(p, RTLD_LAZY); + if (!lib_handle) { + fprintf(stderr, "Failed to open Melon VPN gui\n"); + fprintf(stderr, "Try installing the package 'melonvpngui'\n"); + fprintf(stderr, "%s\n", dlerror()); + return 1; + } + + fn = dlsym(lib_handle, "get_magic_code"); + if ((error = dlerror()) != NULL) { + fprintf(stderr, "Failed to open Melon VPN gui\n"); + fprintf(stderr, "The function 'get_magic_code' couldn't be found\n"); + fprintf(stderr, "Check the package 'melonvpngui' is installed correctly\n"); + fprintf(stderr, "%s\n", error); + return 1; + } + + (*fn)(&a,&b); + + if(a==MAGIC_CODE_A && b==MAGIC_CODE_B) { + gui = dlsym(lib_handle, "run_gui"); + if((error = dlerror()) != NULL) { + fprintf(stderr, "Failed to open Melon VPN gui\n"); + fprintf(stderr, "The function 'run_gui' couldn't be found\n"); + fprintf(stderr, "Check the package 'melonvpngui' is installed correctly\n"); + fprintf(stderr, "%s\n", error); + return 1; + } + + int fake_argc = 1; + char *fake_argv[1] = {0}; + fake_argv[0] = argv[0]; + + (*gui)(fake_argc,fake_argv); + } + + dlclose(lib_handle); + return 0; +}