From 64385fefd9aef13ffefc4f0ede921e46ac91bf5f Mon Sep 17 00:00:00 2001 From: Dnomd343 Date: Mon, 26 Sep 2022 17:37:51 +0800 Subject: [PATCH] update: assets framework --- include/applet/crontab.h | 2 +- include/loader/config.h | 1 + include/loader/loader.h | 5 +++- include/utils/assets.h | 12 ++++++-- src/applet/CMakeLists.txt | 2 +- src/applet/crontab.c | 2 +- src/cleardns.c | 13 ++++++--- src/loader/config.c | 9 ++++-- src/loader/loader.c | 40 ++++++++++++++++++------- src/loader/parser.c | 3 ++ src/utils/assets.c | 61 ++++++++++++++++++++++++++++----------- 11 files changed, 110 insertions(+), 40 deletions(-) diff --git a/include/applet/crontab.h b/include/applet/crontab.h index 53499e0..ba0016a 100644 --- a/include/applet/crontab.h +++ b/include/applet/crontab.h @@ -9,8 +9,8 @@ typedef struct { char *cron; // cron expression } crontab; +crontab* crontab_init(); void crontab_free(crontab *info); -crontab* crontab_init(char *cron); process* crontab_load(crontab *info); #endif diff --git a/include/loader/config.h b/include/loader/config.h index 760b50b..d0c5f33 100644 --- a/include/loader/config.h +++ b/include/loader/config.h @@ -33,6 +33,7 @@ typedef struct { } adguard_config; typedef struct { + uint8_t disable; // bool value char *cron; char **update_file; char **update_url; diff --git a/include/loader/loader.h b/include/loader/loader.h index c455f70..fb68648 100644 --- a/include/loader/loader.h +++ b/include/loader/loader.h @@ -5,6 +5,8 @@ #include "adguard.h" #include "dnsproxy.h" #include "overture.h" +#include "crontab.h" +#include "assets.h" struct cleardns { char **script; @@ -12,7 +14,8 @@ struct cleardns { dnsproxy *foreign; overture *diverter; adguard *filter; - assets_config *assets; + crontab *crond; + assets *resource; }; extern struct cleardns loader; diff --git a/include/utils/assets.h b/include/utils/assets.h index 5323c49..5e1b47b 100644 --- a/include/utils/assets.h +++ b/include/utils/assets.h @@ -1,7 +1,15 @@ #ifndef ASSETS_H_ #define ASSETS_H_ -void assets_init(); -//void assets_update(); +typedef struct { + char **update_file; + char **update_url; +} assets; + +assets* assets_init(); +void assets_free(assets *info); +void assets_load(assets *info); + +void assets_extract(); #endif diff --git a/src/applet/CMakeLists.txt b/src/applet/CMakeLists.txt index 8271386..c6ddac0 100644 --- a/src/applet/CMakeLists.txt +++ b/src/applet/CMakeLists.txt @@ -1,4 +1,4 @@ cmake_minimum_required(VERSION 2.8.12) add_library(applet adguard.c dnsproxy.c overture.c crontab.c) -target_link_libraries(applet bcrypt) +target_link_libraries(applet bcrypt utils) diff --git a/src/applet/crontab.c b/src/applet/crontab.c index e59019f..01ee399 100644 --- a/src/applet/crontab.c +++ b/src/applet/crontab.c @@ -13,7 +13,7 @@ void crontab_free(crontab *info) { // free crontab options free(info); } -crontab* crontab_init(char *cron) { // init crontab options +crontab* crontab_init() { // init crontab options crontab *info = (crontab *)malloc(sizeof(crontab)); info->debug = FALSE; info->cron = string_init(UPDATE_CRON); diff --git a/src/cleardns.c b/src/cleardns.c index 0b78341..8e566e1 100644 --- a/src/cleardns.c +++ b/src/cleardns.c @@ -46,9 +46,9 @@ int main(int argc, char *argv[]) { // ClearDNS service char *config_file = init(argc, argv); log_info("ClearDNS server start (%s)", VERSION); create_folder(EXPOSE_DIR); - // TODO: cd WORK_DIR create_folder(WORK_DIR); - assets_init(); + // TODO: cd EXPOSE_DIR + assets_extract(); // extract built-in resource load_config(config_file); free(config_file); @@ -57,21 +57,26 @@ int main(int argc, char *argv[]) { // ClearDNS service loader.diverter->debug = TRUE; loader.domestic->debug = TRUE; loader.foreign->debug = TRUE; + loader.crond->debug = TRUE; } process_list_init(); - process_list_append(assets_load(loader.assets)); + 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.assets); + assets_free(loader.resource); if (loader.filter != NULL) { process_list_append(adguard_load(loader.filter, ADGUARD_DIR)); adguard_free(loader.filter); } + if (loader.crond != NULL) { + process_list_append(crontab_load(loader.crond)); + crontab_free(loader.crond); + } for (char **script = loader.script; *script != NULL; ++script) { // run custom script log_info("Run custom script -> `%s`", *script); diff --git a/src/loader/config.c b/src/loader/config.c index 653760a..483a585 100644 --- a/src/loader/config.c +++ b/src/loader/config.c @@ -36,6 +36,7 @@ cleardns_config* config_init() { // init config struct of cleardns config->adguard.username = string_init(ADGUARD_USER); config->adguard.password = string_init(ADGUARD_PASSWD); + config->assets.disable = FALSE; config->assets.cron = string_init(UPDATE_CRON); config->assets.update_file = string_list_init(); config->assets.update_url = string_list_init(); @@ -77,6 +78,7 @@ void config_dump(cleardns_config *config) { // dump config info of cleardns log_debug("AdGuardHome username -> %s", config->adguard.username); log_debug("AdGuardHome password -> %s", config->adguard.password); + 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; @@ -102,6 +104,10 @@ void config_free(cleardns_config *config) { // free config struct of cleardns string_list_free(config->diverter.china_ip); string_list_free(config->diverter.chinalist); + free(config->adguard.username); + free(config->adguard.password); + + free(config->assets.cron); string_list_free(config->assets.update_file); string_list_free(config->assets.update_url); @@ -109,8 +115,5 @@ void config_free(cleardns_config *config) { // free config struct of cleardns 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->assets.cron); free(config); } diff --git a/src/loader/loader.c b/src/loader/loader.c index 2d826b3..0861d44 100644 --- a/src/loader/loader.c +++ b/src/loader/loader.c @@ -94,7 +94,11 @@ overture* load_diverter(cleardns_config *config) { free(gfwlist); uint32_list_update(&diverter->reject_type, config->reject); - load_diverter_assets(); + if (!config->assets.disable) { + // TODO: extract assets + + load_diverter_assets(); + } return diverter; } @@ -113,14 +117,20 @@ adguard* load_filter(cleardns_config *config) { return filter; } -assets_config* load_assets(cleardns_config *config) { - assets_config *assets = (assets_config *)malloc(sizeof(assets_config)); - assets->update_url = string_list_init(); - assets->update_file = string_list_init(); - assets->cron = string_init(config->assets.cron); - string_list_update(&assets->update_url, config->assets.update_url); - string_list_update(&assets->update_file, config->assets.update_file); - return assets; +crontab* load_crond(cleardns_config *config) { + if (config->assets.disable) { + return NULL; // disable crond + } + crontab *crond = crontab_init(); + crond->cron = string_init(config->assets.cron); + return crond; +} + +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; } void load_config(const char *config_file) { // parser and load cleardns configure @@ -133,18 +143,28 @@ void load_config(const char *config_file) { // parser and load cleardns configur 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.assets = load_assets(config); + + loader.crond = load_crond(config); + log_debug("Crond options parser success"); + + loader.resource = load_assets(config); log_debug("Assets options parser success"); + loader.script = string_list_init(); string_list_update(&loader.script, config->script); log_debug("Custom script parser success"); + config_free(config); } diff --git a/src/loader/parser.c b/src/loader/parser.c index 69de082..74b0e72 100644 --- a/src/loader/parser.c +++ b/src/loader/parser.c @@ -118,6 +118,9 @@ void assets_parser(assets_config *config, cJSON *json) { // assets options parse } json = json->child; while (json != NULL) { + if (!strcmp(json->string, "disable")) { + config->disable = json_bool_value("assets.disable", json); + } if (!strcmp(json->string, "cron")) { config->cron = json_string_value("assets.cron", json); } diff --git a/src/utils/assets.c b/src/utils/assets.c index 2f4cc04..6199cbe 100644 --- a/src/utils/assets.c +++ b/src/utils/assets.c @@ -7,32 +7,50 @@ #include "system.h" #include "constant.h" #include "structure.h" +#include "assets.h" -char **update_file; -char **update_url; +assets update; void assets_update(); -void extract_asset(const char *file); +void assets_dump(assets *info); +void extract(const char *file); -void assets_init() { // init assets and load update process - log_info("Start loading assets"); - create_folder(ASSETS_DIR); - extract_asset(ASSET_GFW_LIST); - extract_asset(ASSET_CHINA_IP); - extract_asset(ASSET_CHINA_LIST); - log_info("Assets loaded complete"); +void assets_free(assets *info) { // free assets mapping + string_list_free(info->update_file); + string_list_free(info->update_url); + free(info); +} - update_url = string_list_init(); - update_file = string_list_init(); - signal(SIGALRM, assets_update); // receive SIGALRM signal +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; } -// TODO: inject update file and url +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(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() { // update all assets + + // TODO: skip when assets map is empty + log_info("Start assets update"); - for (char **file = update_file; *file != NULL; ++file) { - char **url = file - update_file + update_url; + for (char **file = update.update_file; *file != NULL; ++file) { + char **url = file - update.update_file + update.update_url; log_info("Update asset `%s` -> %s", *file, *url); download_file(*file, *url); // download asset from url } @@ -41,7 +59,16 @@ void assets_update() { // update all assets log_info("Assets update complete"); } -void extract_asset(const char *file) { +void assets_extract() { // init assets and load update process + log_info("Start loading assets"); + create_folder(ASSETS_DIR); + extract(ASSET_GFW_LIST); + extract(ASSET_CHINA_IP); + extract(ASSET_CHINA_LIST); + log_info("Assets loaded complete"); +} + +void extract(const char *file) { // extract one asset file from `.tar.xz` file log_debug("Start extract `%s`", file); char *output_file = string_join(ASSETS_DIR, file); if (is_file_exist(output_file)) {