From 63f3e500c7815224c98c6b17350e908d084bb581 Mon Sep 17 00:00:00 2001 From: dnomd343 Date: Fri, 16 Sep 2022 16:42:15 +0800 Subject: [PATCH] feat: process interface --- include/loader/config.h | 1 + include/loader/loader.h | 1 + include/utils/process.h | 3 + src/cleardns.c | 27 +++---- src/common_legacy.c | 171 ---------------------------------------- src/loader/config.c | 4 + src/loader/loader.c | 12 ++- src/loader/parser.c | 6 +- src/utils/process.c | 23 ++++++ 9 files changed, 54 insertions(+), 194 deletions(-) delete mode 100644 src/common_legacy.c diff --git a/include/loader/config.h b/include/loader/config.h index b14e3e2..cbb1657 100644 --- a/include/loader/config.h +++ b/include/loader/config.h @@ -42,6 +42,7 @@ typedef struct { uint32_t **reject; char **hosts; char **ttl; + char **script; } cleardns_config; cleardns_config* config_init(); diff --git a/include/loader/loader.h b/include/loader/loader.h index 3f5f359..7661b8b 100644 --- a/include/loader/loader.h +++ b/include/loader/loader.h @@ -10,6 +10,7 @@ struct cleardns { dnsproxy *foreign; overture *diverter; adguard *filter; + char **script; }; extern struct cleardns loader; diff --git a/include/utils/process.h b/include/utils/process.h index b4b5969..b979ead 100644 --- a/include/utils/process.h +++ b/include/utils/process.h @@ -8,6 +8,9 @@ typedef struct { char *cwd; } process; +void process_list_run(); +void process_list_init(); +void process_list_append(process *proc); void process_add_arg(process *proc, const char *arg); process* process_init(const char *caption, const char *bin); diff --git a/src/cleardns.c b/src/cleardns.c index 4f43175..45034b7 100644 --- a/src/cleardns.c +++ b/src/cleardns.c @@ -1,26 +1,15 @@ #include #include -#include -#include #include #include "loader.h" #include "logger.h" #include "constant.h" #include "dnsproxy.h" #include "overture.h" -#include "structure.h" #include "adguard.h" #include "system.h" -#include "assets.h" #include "sundry.h" -//char *init_script = "/usr/bin/load"; -//char *custom_script = "/etc/cleardns/custom.sh"; -// -//char *adguard_workdir = "/etc/cleardns/AdGuardHome"; -//char *overture_config = "/etc/overture/config.yml"; -//char *upstream_config = "/etc/cleardns/upstream.json"; - #define HELP_MSG "\ ClearDNS usage \n\ --debug ...\n\ @@ -30,8 +19,6 @@ ClearDNS usage \n\ #define CONFIG_FILE "test.json" -// TODO: load `--debug`, `--config ...`, `--version`, `--help` - char* init(int argc, char *argv[]) { // return config file char *config = string_init(CONFIG_FILE); for (int i = 0; i < argc; ++i) { @@ -64,7 +51,6 @@ int main(int argc, char *argv[]) { // ClearDNS server char *config_file = init(argc, argv); - LOG_LEVEL = LOG_DEBUG; // TODO: dev only log_info("ClearDNS server start (%s)", VERSION); create_folder(WORK_DIR); @@ -72,13 +58,18 @@ int main(int argc, char *argv[]) { // ClearDNS server load_config(config_file); free(config_file); - dnsproxy_load("Domestic", loader.domestic, "domestic.json"); - dnsproxy_load("Foreign", loader.foreign, "foreign.json"); - overture_load(loader.diverter, "overture.json"); - adguard_load(loader.filter, ADGUARD_DIR); + process_list_init(); + process_list_append(dnsproxy_load("Domestic", loader.domestic, "domestic.json")); + process_list_append(dnsproxy_load("Foreign", loader.foreign, "foreign.json")); + process_list_append(overture_load(loader.diverter, "overture.json")); + if (loader.filter != NULL) { + process_list_append(adguard_load(loader.filter, ADGUARD_DIR)); + } // TODO: running custom script + process_list_run(); + // init_server(init_script, custom_script); // run init script and custom script // diff --git a/src/common_legacy.c b/src/common_legacy.c deleted file mode 100644 index e27044e..0000000 --- a/src/common_legacy.c +++ /dev/null @@ -1,171 +0,0 @@ -#include -#include -#include -#include "cJSON.h" -#include "flag_legacy.h" - -char **adguard_command = NULL; -char **overture_command = NULL; -char **domestic_dnsproxy_command = NULL; -char **foreign_dnsproxy_command = NULL; - -void error_exit(char *message); -char* read_file(char *file_name); -char** command_add_field(char **command_list, char *field); -char** dnsproxy_config(char *port, cJSON *json, int is_debug); -void load_start_command(char *adguard_workdir, char *overture_config, char *upstream_config, int is_debug); - -void error_exit(char *message) { // exit with code 1 - fprintf(stderr, "[ClearDNS] %s\n", message); - exit(EXIT_FILE_ERROR); -} - -char* read_file(char *file_name) { // read file content - FILE *pfile = fopen(file_name, "rb"); - if (pfile == NULL) { // open failed - error_exit("File open failed."); - } - fseek(pfile, 0, SEEK_END); - long file_length = ftell(pfile); // get file length - char *file_content = (char*)malloc(file_length + 1); // malloc new memory - if (file_content == NULL) { - error_exit("No enough memory."); // file too large - } - rewind(pfile); - fread(file_content, 1, file_length, pfile); // read file stream - file_content[file_length] = '\0'; // set end flag - fclose(pfile); - return file_content; -} - -char** command_add_field(char **command_list, char *field) { // add field into command - int num = 0; - while(command_list[num++] != NULL); // get options number - command_list = (char**)realloc(command_list, sizeof(char*) * (num + 1)); - command_list[num - 1] = strcpy((char*)malloc(strlen(field) + 1), field); - command_list[num] = NULL; // end sign - return command_list; -} - -char** dnsproxy_config(char *port, cJSON *json, int is_debug) { // generate dnsproxy command - char *bootstrap_dns = NULL; - cJSON *primary_dns = NULL; - cJSON *fallback_dns = NULL; - char **command_list = (char**)malloc(sizeof(char*) * 9); - - command_list[0] = "dnsproxy"; - command_list[1] = "--port"; - command_list[2] = port; // listen port - command_list[3] = "--all-servers"; // parallel queries to all servers - command_list[4] = "--cache"; // enable DNS cache - command_list[5] = "--cache-size"; - command_list[6] = "4194304"; // cache size -> 4MiB - command_list[7] = "--cache-optimistic"; // optimistic DNS cache - command_list[8] = NULL; // end sign - - while (json != NULL) { - if (!strcmp(json->string, "bootstrap")) { // bootstrap DNS server - if (!cJSON_IsString(json)) { - error_exit("`bootstrap` must be a string."); - } - bootstrap_dns = json->valuestring; - } else if (!strcmp(json->string, "primary")) { // primary DNS server - if (!cJSON_IsArray(json)) { - error_exit("`primary` must be a array."); - } - primary_dns = json->child; - } else if (!strcmp(json->string, "fallback")) { // fallback DNS server - if (!cJSON_IsArray(json)) { - error_exit("`fallback` must be a array."); - } - fallback_dns = json->child; - } - json = json->next; // next field - } - - if (bootstrap_dns != NULL) { // add bootstrap DNS server - command_list = command_add_field(command_list, "--bootstrap"); - command_list = command_add_field(command_list, bootstrap_dns); - } - - if (primary_dns == NULL) { // primary DNS server required - error_exit("Miss primary DNS server."); - } - while (primary_dns != NULL) { // iterate over primary DNS server list - if (!cJSON_IsString(primary_dns)) { - error_exit("DNS Server should be a string."); - } - command_list = command_add_field(command_list, "--upstream"); - command_list = command_add_field(command_list, primary_dns->valuestring); - primary_dns = primary_dns->next; - } - - while (fallback_dns != NULL) { // iterate over fallback DNS server list - if (!cJSON_IsString(fallback_dns)) { - error_exit("DNS Server should be a string."); - } - command_list = command_add_field(command_list, "--fallback"); - command_list = command_add_field(command_list, fallback_dns->valuestring); - fallback_dns = fallback_dns->next; - } - - if (is_debug) { // debug mode - command_list = command_add_field(command_list, "--verbose"); - } - return command_list; -} - -void load_start_command(char *adguard_workdir, char *overture_config, char *upstream_config, int is_debug) { - // AdGuardHome command - adguard_command = (char**)malloc(sizeof(char*) * 7); - adguard_command[0] = "AdGuardHome"; - adguard_command[1] = "-w"; - adguard_command[2] = adguard_workdir; // workdir for AdGuardHome - adguard_command[3] = "-p"; - adguard_command[4] = "80"; // port for web manage - adguard_command[5] = "--no-check-update"; // skip check update (invalid in docker) - adguard_command[6] = NULL; // end sign - if (is_debug) { // debug mode - adguard_command = command_add_field(adguard_command, "--verbose"); - } - - // overture command - overture_command = (char**)malloc(sizeof(char*) * 4); - overture_command[0] = "overture"; - overture_command[1] = "-c"; - overture_command[2] = overture_config; - overture_command[3] = NULL; // end sign - if (is_debug) { // debug mode - overture_command = command_add_field(overture_command, "-v"); - } - - // dnsproxy command - cJSON *json = NULL; - cJSON *json_root = cJSON_Parse(read_file(upstream_config)); - if (json_root == NULL) { - error_exit("JSON format error."); - } else { - json = json_root->child; - } - while (json != NULL) { - if (!strcmp(json->string, "domestic")) { // domestic dnsproxy config - if (!cJSON_IsObject(json)) { - error_exit("`domestic` must be a object."); - } - domestic_dnsproxy_command = dnsproxy_config("4053", json->child, is_debug); - } else if (!strcmp(json->string, "foreign")) { // foreign dnsproxy config - if (!cJSON_IsObject(json)) { - error_exit("`foreign` must be a object."); - } - foreign_dnsproxy_command = dnsproxy_config("6053", json->child, is_debug); - } - json = json->next; // next field - } - if (domestic_dnsproxy_command == NULL) { - error_exit("Miss domestic DNS settings."); - } - if (foreign_dnsproxy_command == NULL) { - error_exit("Miss foreign DNS settings."); - } - cJSON_free(json_root); // free cJSON object -} diff --git a/src/loader/config.c b/src/loader/config.c index 9fe84a6..61741a0 100644 --- a/src/loader/config.c +++ b/src/loader/config.c @@ -39,6 +39,7 @@ cleardns_config* config_init() { // init config struct of cleardns config->reject = uint32_list_init(); config->hosts = string_list_init(); config->ttl = string_list_init(); + config->script = string_list_init(); return config; } @@ -75,6 +76,7 @@ void config_dump(cleardns_config *config) { // dump config info of cleardns uint32_list_debug("DNS reject type", config->reject); string_list_debug("Domain TTL", config->ttl); string_list_debug("Hosts", config->hosts); + string_list_debug("Custom script", config->script); } void config_free(cleardns_config *config) { // free config struct of cleardns @@ -93,6 +95,8 @@ void config_free(cleardns_config *config) { // free config struct of cleardns uint32_list_free(config->reject); string_list_free(config->hosts); string_list_free(config->ttl); + string_list_free(config->script); + free(config->adguard.username); free(config->adguard.password); free(config); diff --git a/src/loader/loader.c b/src/loader/loader.c index 6d5c0e2..6d9b1bc 100644 --- a/src/loader/loader.c +++ b/src/loader/loader.c @@ -9,6 +9,7 @@ #include "dnsproxy.h" #include "constant.h" #include "structure.h" +#include "logger.h" struct cleardns loader; @@ -114,16 +115,23 @@ adguard* load_filter(cleardns_config *config) { void load_config(const char *config_file) { cleardns_config *config = config_init(); - load_default_config(config_file); - config_parser(config, config_file); + load_default_config(config_file); // load default configure + config_parser(config, config_file); // configure parser config_dump(config); + log_info("Loading configure options"); if (!config->adguard.enable) { config->diverter.port = config->port; // override diverter port by dns port } loader.domestic = load_domestic(config); + log_debug("Domestic options parser success"); loader.foreign = load_foreign(config); + log_debug("Foreign options parser success"); loader.diverter = load_diverter(config); + log_debug("Diverter options parser success"); loader.filter = load_filter(config); + log_debug("Filter options parser success"); + loader.script = string_list_init(); + loader.script = string_list_update(loader.script, config->script); config_free(config); } diff --git a/src/loader/parser.c b/src/loader/parser.c index e69a76c..3ea5581 100644 --- a/src/loader/parser.c +++ b/src/loader/parser.c @@ -146,15 +146,15 @@ void cleardns_parser(cleardns_config *config, const char *config_content) { // J if (!strcmp(json->string, "ttl")) { config->ttl = json_string_list_value("ttl", json, config->ttl); } + if (!strcmp(json->string, "custom")) { + config->script = json_string_list_value("custom", json, config->script); + } json = json->next; // next field } cJSON_free(json); // free JSON struct } void config_parser(cleardns_config *config, const char *config_file) { - - // TODO: load custom script - char *config_content; if (is_json_suffix(config_file)) { // JSON format log_info("Start JSON configure parser"); diff --git a/src/utils/process.c b/src/utils/process.c index 9c52502..31bc46e 100644 --- a/src/utils/process.c +++ b/src/utils/process.c @@ -3,6 +3,9 @@ #include "process.h" #include "constant.h" #include "structure.h" +#include "logger.h" + +process **process_list; process* process_init(const char *caption, const char *bin) { // init process struct process *proc = (process *)malloc(sizeof(process)); @@ -16,3 +19,23 @@ process* process_init(const char *caption, const char *bin) { // init process st void process_add_arg(process *proc, const char *arg) { // add argument for process proc->cmd = string_list_append(proc->cmd, arg); } + +void process_list_init() { // init process list + process_list = (process **)malloc(sizeof(process *)); + *process_list = NULL; +} + +void process_list_append(process *proc) { // add new process into process list + int proc_num = 0; + for (process **p = process_list; *p != NULL; ++p, ++proc_num); // get process number + process_list = (process **)realloc(process_list, sizeof(process *) * (proc_num + 2)); + process_list[proc_num++] = proc; // add into process list + process_list[proc_num] = NULL; + log_debug("%s process added", proc->name); +} + +void process_list_run() { + for (process **p = process_list; *p != NULL; ++p) { + log_warn("proc -> %s", (*p)->name); + } +}