Add cli binary

This commit is contained in:
Melon 2021-04-24 11:19:17 +01:00
parent fe7e922022
commit 9ed779b8ae
2 changed files with 136 additions and 0 deletions

79
src/impl/cli/main.c Normal file
View File

@ -0,0 +1,79 @@
#include <stdio.h>
#include <string.h>
#include <stdbool.h>
#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;
}

57
src/impl/cli/open_gui.c Normal file
View File

@ -0,0 +1,57 @@
#include <stdio.h>
#include <dlfcn.h>
#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;
}