From 9bf1763dd8364f3cc717106131c442755780bedb Mon Sep 17 00:00:00 2001 From: Dnomd343 Date: Mon, 6 Mar 2023 15:03:58 +0800 Subject: [PATCH] feat: multiple sources configure parser, ref #3 --- include/loader/config.h | 6 +-- include/loader/loader.h | 3 +- include/utils/assets.h | 24 +++--------- src/cleardns.c | 73 ++--------------------------------- src/common/json.c | 15 -------- src/loader/config.c | 12 ++---- src/loader/default.c | 2 +- src/loader/loader.c | 18 ++++----- src/loader/parser.c | 13 ++++++- src/utils/assets.c | 84 ++++++++++++++--------------------------- 10 files changed, 68 insertions(+), 182 deletions(-) diff --git a/include/loader/config.h b/include/loader/config.h index 2a2f9bc..db80ff8 100644 --- a/include/loader/config.h +++ b/include/loader/config.h @@ -2,6 +2,7 @@ #define CONFIG_H_ #include +#include "assets.h" typedef struct { uint16_t port; @@ -34,10 +35,9 @@ typedef struct { } adguard_config; typedef struct { - uint8_t disable; // bool value char *cron; - char **update_file; - char **update_url; + uint8_t disable; // bool value + asset **resources; } assets_config; typedef struct { diff --git a/include/loader/loader.h b/include/loader/loader.h index 5369ef7..a7205ac 100644 --- a/include/loader/loader.h +++ b/include/loader/loader.h @@ -15,8 +15,7 @@ struct cleardns { overture *diverter; adguard *filter; crontab *crond; - // TODO: update assets ptr -// assets *resource; + asset **resource; }; extern struct cleardns loader; diff --git a/include/utils/assets.h b/include/utils/assets.h index a73a641..3d6da0d 100644 --- a/include/utils/assets.h +++ b/include/utils/assets.h @@ -7,31 +7,19 @@ typedef struct { } asset; asset* asset_init(const char *name); -//void asset_add_src(asset *res, const char *src); asset** assets_init(); -void assets_append(asset ***asset_list, asset *res); - +void assets_dump(asset **asset_list); void assets_free(asset **asset_list); - -//void asset_free(asset *res); uint32_t assets_size(asset **asset_list); +void assets_append(asset ***asset_list, asset *res); -void assets_dump(asset **asset_list); - -//typedef struct { -// char **update_file; -// char **update_url; -//} assets; - -//assets* assets_init(); -//void assets_free(assets *info); -//void assets_load(assets *info); +void assets_load(asset **info); void assets_extract(); -void assets_log_init(uint8_t verbose); - -uint8_t rust_assets_update(const char *file, char *const *sources, const char *assets_dir); +//void assets_log_init(uint8_t verbose); +// +//uint8_t rust_assets_update(const char *file, char *const *sources, const char *assets_dir); #endif diff --git a/src/cleardns.c b/src/cleardns.c index 4ed8be7..6df1daf 100644 --- a/src/cleardns.c +++ b/src/cleardns.c @@ -66,6 +66,7 @@ void init(int argc, char *argv[]) { // return config file void cleardns() { // cleardns service if (settings.verbose || settings.debug) { + // TODO: rust log module settings LOG_LEVEL = LOG_DEBUG; // enable debug log level } create_folder(EXPOSE_DIR); @@ -87,14 +88,14 @@ void cleardns() { // cleardns service log_info("Start loading process"); process_list_init(); -// assets_load(loader.resource); + assets_load(loader.resource); 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")); overture_free(loader.diverter); dnsproxy_free(loader.domestic); dnsproxy_free(loader.foreign); -// assets_free(loader.resource); + assets_free(loader.resource); if (loader.crond != NULL) { process_list_append(crontab_load(loader.crond)); // free crontab struct later } @@ -118,74 +119,6 @@ void cleardns() { // cleardns service } int main(int argc, char *argv[]) { - - LOG_LEVEL = LOG_DEBUG; - - asset *res_1 = asset_init("demo 1"); - asset *res_2 = asset_init("demo 2"); - asset *res_3 = asset_init("demo 3"); - - string_list_append(&res_1->sources, "item a1"); - string_list_append(&res_1->sources, "item a2"); - string_list_append(&res_1->sources, "item a3"); - -// asset_add_src(res_1, "item a1"); -// asset_add_src(res_1, "item a2"); -// asset_add_src(res_1, "item a3"); - - string_list_append(&res_2->sources, "item b1"); - string_list_append(&res_2->sources, "item b2"); - -// asset_add_src(res_2, "item b1"); -// asset_add_src(res_2, "item b2"); - - string_list_append(&res_3->sources, "item c1"); - -// asset_add_src(res_3, "item c1"); - - asset **res = assets_init(); - log_info("res init size: %d", assets_size(res)); - - assets_append(&res, res_1); - assets_append(&res, res_2); - assets_append(&res, res_3); - - log_info("res size: %d", assets_size(res)); - - assets_dump(res); - -// asset_free(res_1); -// asset_free(res_2); -// asset_free(res_3); - - assets_free(res); - -// char **res_1 = string_list_init(); -// char **res_2 = string_list_init(); -// char **res_3 = string_list_init(); -// -// string_list_append(&res_1, "item a1"); -// string_list_append(&res_1, "item a2"); -// string_list_append(&res_1, "item a3"); -// -// string_list_append(&res_2, "item b1"); -// string_list_append(&res_2, "item b2"); -// -// string_list_append(&res_3, "item c"); - -// char *tmp = string_list_dump(demo); -// log_warn("dump -> %s", tmp); - -// assets_log_init(TRUE); - assets_log_init(FALSE); - -// rust_assets_update("test.txt", demo, ASSETS_DIR); - -// string_list_free(demo); - - log_warn("test end"); - exit(0); - init(argc, argv); log_info("ClearDNS server start (%s)", VERSION); cleardns(); diff --git a/src/common/json.c b/src/common/json.c index 4264824..ac008ab 100644 --- a/src/common/json.c +++ b/src/common/json.c @@ -115,18 +115,3 @@ uint32_t** json_uint32_list_value(char *caption, cJSON *json, uint32_t **uint32_ } return uint32_list; } - -void json_string_map_value(char *caption, cJSON *json, char ***key_list, char ***value_list) { // json string map - if (!cJSON_IsObject(json)) { - log_fatal("`%s` must be map", caption); - } - json = json->child; - while (json != NULL) { // traverse all json field - if (!cJSON_IsString(json)) { - log_fatal("`%s` must be string-string map", caption); - } - string_list_append(key_list, json->string); - string_list_append(value_list, json->valuestring); - json = json->next; - } -} diff --git a/src/loader/config.c b/src/loader/config.c index f462337..4b55a94 100644 --- a/src/loader/config.c +++ b/src/loader/config.c @@ -41,8 +41,7 @@ cleardns_config* config_init() { // init config struct of cleardns config->assets.disable = FALSE; config->assets.cron = strdup(UPDATE_CRON); - config->assets.update_file = string_list_init(); - config->assets.update_url = string_list_init(); + config->assets.resources = assets_init(); config->reject = uint32_list_init(); config->hosts = string_list_init(); @@ -85,10 +84,8 @@ void config_dump(cleardns_config *config) { // dump config info of cleardns log_debug("Assets disable -> %s", show_bool(config->assets.disable)); log_debug("Assets update cron -> `%s`", config->assets.cron); - for (char **file = config->assets.update_file; *file != NULL; ++file) { // show string mapping - char **url = file - config->assets.update_file + config->assets.update_url; - log_debug("Assets file `%s` -> %s", *file, *url); - } + log_debug("Assets with %d resource items", assets_size(config->assets.resources)); + assets_dump(config->assets.resources); uint32_list_debug("DNS reject type", config->reject); string_list_debug("Domain TTL", config->ttl); @@ -113,8 +110,7 @@ void config_free(cleardns_config *config) { // free config struct of cleardns free(config->adguard.password); free(config->assets.cron); - string_list_free(config->assets.update_file); - string_list_free(config->assets.update_url); + assets_free(config->assets.resources); uint32_list_free(config->reject); string_list_free(config->hosts); diff --git a/src/loader/default.c b/src/loader/default.c index e964484..44a13b8 100644 --- a/src/loader/default.c +++ b/src/loader/default.c @@ -51,7 +51,7 @@ assets:\n\ void load_default_config(const char *config_file) { if (is_file_exist(config_file)) { - log_debug("Configure file exist -> skip load default"); + log_debug("Configure file exist -> skip loading default"); return; } log_info("Loading default configure file"); diff --git a/src/loader/loader.c b/src/loader/loader.c index 28285cb..a4aee7a 100644 --- a/src/loader/loader.c +++ b/src/loader/loader.c @@ -127,13 +127,14 @@ crontab* load_crond(cleardns_config *config) { return crond; } -// TODO: new assets manager -//assets* load_assets(cleardns_config *config) { -// assets *resource = assets_init(); -// string_list_update(&resource->update_file, config->assets.update_file); -// string_list_update(&resource->update_url, config->assets.update_url); -// return resource; -//} +asset** load_assets(cleardns_config *config) { + asset **resources = assets_init(); + for (asset **res = config->assets.resources; *res != NULL; ++res) { + assets_append(&resources, *res); // pointer movement + } + *(config->assets.resources) = NULL; // disable old assets list + return resources; +} void load_config(const char *config_file) { // parser and load cleardns configure cleardns_config *config = config_init(); @@ -162,8 +163,7 @@ void load_config(const char *config_file) { // parser and load cleardns configur loader.crond = load_crond(config); log_debug("Crond options parser success"); - // TODO: update assets loader -// loader.resource = load_assets(config); + loader.resource = load_assets(config); log_debug("Assets options parser success"); loader.script = string_list_init(); diff --git a/src/loader/parser.c b/src/loader/parser.c index ca8d9d7..8bb68c2 100644 --- a/src/loader/parser.c +++ b/src/loader/parser.c @@ -130,7 +130,18 @@ void assets_parser(assets_config *config, cJSON *json) { // assets options parse config->cron = json_string_value("assets.cron", json); } if (!strcmp(json->string, "update")) { - json_string_map_value("assets.update", json, &config->update_file, &config->update_url); + if (!cJSON_IsObject(json)) { + log_fatal("`%s` must be map", "assets.update"); + } + cJSON *asset_item = json->child; + while (asset_item != NULL) { // traverse all json field + asset *res = asset_init(asset_item->string); + char *caption = string_join("assets.update.", asset_item->string); + res->sources = json_string_list_value(caption, asset_item, res->sources); + free(caption); + assets_append(&config->resources, res); + asset_item = asset_item->next; + } } json = json->next; // next field } diff --git a/src/utils/assets.c b/src/utils/assets.c index 3be2441..e40086b 100644 --- a/src/utils/assets.c +++ b/src/utils/assets.c @@ -8,25 +8,11 @@ #include "structure.h" #include "assets.h" -//assets update; +asset **update_info; -//void assets_update(); -//void assets_dump(assets *info); +void assets_update_entry(); void extract(const char *file); -//void assets_free(assets *info) { // free assets mapping -// string_list_free(info->update_file); -// string_list_free(info->update_url); -// free(info); -//} - -//assets* assets_init() { // init assets mapping -// assets *info = (assets *)malloc(sizeof(assets)); -// info->update_file = string_list_init(); -// info->update_url = string_list_init(); -// return info; -//} - asset* asset_init(const char *name) { // init asset item asset *res = (asset *)malloc(sizeof(asset)); res->file = strdup(name); @@ -34,23 +20,13 @@ asset* asset_init(const char *name) { // init asset item return res; } -//void asset_add_src(asset *res, const char *src) { // append source for asset item -// string_list_append(&res->sources, src); -//} -// -//void asset_free(asset *res) { // free asset item -// string_list_free(res->sources); -// free(res->file); -// free(res); -//} - asset** assets_init() { // init assets list asset **asset_list = (asset **)malloc(sizeof(asset *)); *asset_list = NULL; // list end sign return asset_list; } -void assets_free(asset **asset_list) { +void assets_free(asset **asset_list) { // free assets list for (asset **res = asset_list; *res != NULL; ++res) { string_list_free((*res)->sources); free((*res)->file); @@ -59,49 +35,46 @@ void assets_free(asset **asset_list) { free(asset_list); } -uint32_t assets_size(asset **asset_list) { // get size of asset list +uint32_t assets_size(asset **asset_list) { // get size of assets list uint32_t num = 0; while(asset_list[num++] != NULL); // get list size return num - 1; } -void assets_dump(asset **asset_list) { +void assets_dump(asset **asset_list) { // dump assets list content into debug log for (asset **res = asset_list; *res != NULL; ++res) { // iterate over each item char *sources = string_list_dump((*res)->sources); - log_info("Asset item `%s` -> %s", (*res)->file, sources); + log_debug("Asset item `%s` -> %s", (*res)->file, sources); free(sources); } } -void assets_append(asset ***asset_list, asset *res) { // append asset item for asset list +void assets_append(asset ***asset_list, asset *res) { // push asset item for assets list uint32_t len = assets_size(*asset_list); *asset_list = (asset **)realloc(*asset_list, sizeof(asset *) * (len + 2)); // extend asset list (*asset_list)[len] = res; (*asset_list)[len + 1] = NULL; // list end sign } -//void assets_dump(assets *info) { // show assets mapping in debug log -// for (char **file = info->update_file; *file != NULL; ++file) { -// char **url = file - info->update_file + info->update_url; -// log_info("Asset `%s` -> %s", *file, *url); -// } -//} +void assets_load(asset **info) { // load assets list + update_info = assets_init(); + for (asset **res = info; *res != NULL; ++res) { + assets_append(&update_info, *res); // pointer movement + } + *info = NULL; // disable old assets list + assets_dump(update_info); + log_info("Remote assets load success"); + signal(SIGALRM, assets_update_entry); // receive SIGALRM signal +} -//void assets_load(assets *info) { // load assets mapping -// update.update_file = string_list_init(); -// update.update_url = string_list_init(); -// string_list_update(&update.update_file, info->update_file); -// string_list_update(&update.update_url, info->update_url); -// signal(SIGALRM, assets_update); // receive SIGALRM signal -// assets_dump(&update); -//} +void assets_update_entry() { // receive SIGALRM for update all assets + if (assets_size(update_info) == 0) { // empty assets list + log_info("Skip update assets"); + return; + } + log_info("Start assets update"); -//void assets_update() { // update all assets -// if (!string_list_len(update.update_file)) { // empty assets mapping -// log_info("Skip update assets"); -// return; -// } -// log_info("Start assets update"); + // TODO: call rust `assets_update` function // for (char **file = update.update_file; *file != NULL; ++file) { // char **url = file - update.update_file + update.update_url; // char *asset_file = string_join(ASSETS_DIR, *file); @@ -109,10 +82,11 @@ void assets_append(asset ***asset_list, asset *res) { // append asset item for a // download_file(asset_file, *url); // download asset from url // free(asset_file); // } -// log_info("Restart overture"); -// run_command("pgrep overture | xargs kill"); // restart overture -// log_info("Assets update complete"); -//} + + log_info("Restart overture"); + run_command("pgrep overture | xargs kill"); // restart overture + log_info("Assets update complete"); +} void assets_extract() { // init assets and load update process log_info("Start loading assets");