diff --git a/include/constant.h b/include/constant.h index 480843c..3bc8dbb 100644 --- a/include/constant.h +++ b/include/constant.h @@ -1,11 +1,12 @@ #ifndef CONSTANT_H_ #define CONSTANT_H_ -#define VERSION "1.3.0-dev" - #define TRUE 1 #define FALSE 0 +#define VERSION "1.3.0-dev" +#define CONFIG_FILE "test.yml" + #define DNS_PORT 53 #define ADGUARD_PORT 80 #define DIVERTER_PORT 5353 @@ -15,22 +16,31 @@ #define DIVERTER_TIMEOUT 6 #define ADGUARD_USER "admin" -#define ADGUARD_PASSWD "adguard" +#define ADGUARD_PASSWD "cleardns" #define DNSPROXY_BIN "dnsproxy" #define OVERTURE_BIN "overture" #define ADGUARD_BIN "AdGuardHome" -#define ASSETS_PKG "/assets.tar.xz" #define EXPOSE_DIR "/cleardns/" #define WORK_DIR "/etc/cleardns/" #define ASSETS_DIR "/cleardns/assets/" #define ADGUARD_DIR "/cleardns/adguard/" +#define UPDATE_CRON "0 4 * * *" +#define ASSETS_PKG "/assets.tar.xz" + #define ASSET_TTL "ttl.txt" #define ASSET_HOSTS "hosts.txt" #define ASSET_GFW_LIST "gfwlist.txt" #define ASSET_CHINA_IP "china-ip.txt" #define ASSET_CHINA_LIST "chinalist.txt" +#define HELP_MSG "\ +ClearDNS usage \n\ +--debug ...\n\ +--config ...\n\ +--help ...\n\ +" + #endif diff --git a/include/loader/config.h b/include/loader/config.h index cbb1657..760b50b 100644 --- a/include/loader/config.h +++ b/include/loader/config.h @@ -32,6 +32,12 @@ typedef struct { char *password; } adguard_config; +typedef struct { + char *cron; + char **update_file; + char **update_url; +} assets_config; + typedef struct { uint16_t port; cache_config cache; @@ -39,6 +45,7 @@ typedef struct { upstream_config foreign; diverter_config diverter; adguard_config adguard; + assets_config assets; uint32_t **reject; char **hosts; char **ttl; diff --git a/include/loader/loader.h b/include/loader/loader.h index 7661b8b..c455f70 100644 --- a/include/loader/loader.h +++ b/include/loader/loader.h @@ -1,16 +1,18 @@ #ifndef LOADER_H_ #define LOADER_H_ +#include "config.h" #include "adguard.h" #include "dnsproxy.h" #include "overture.h" struct cleardns { + char **script; dnsproxy *domestic; dnsproxy *foreign; overture *diverter; adguard *filter; - char **script; + assets_config *assets; }; extern struct cleardns loader; diff --git a/src/cleardns.c b/src/cleardns.c index d65a561..9f1a975 100644 --- a/src/cleardns.c +++ b/src/cleardns.c @@ -11,15 +11,6 @@ #include "system.h" #include "sundry.h" -#define HELP_MSG "\ -ClearDNS usage \n\ ---debug ...\n\ ---config ...\n\ ---help ...\n\ -" - -#define CONFIG_FILE "test.json" - char* init(int argc, char *argv[]) { // return config file char *config = string_init(CONFIG_FILE); for (int i = 0; i < argc; ++i) { @@ -47,8 +38,7 @@ char* init(int argc, char *argv[]) { // return config file return config; } - -int main(int argc, char *argv[]) { // ClearDNS server +int main(int argc, char *argv[]) { // ClearDNS service char *config_file = init(argc, argv); @@ -56,26 +46,22 @@ int main(int argc, char *argv[]) { // ClearDNS server log_info("ClearDNS server start (%s)", VERSION); // process *test = process_init("TEST", "lls"); - process *test = process_init("TEST", "ls"); - - process_add_arg(test, "-al"); - +// process *test = process_init("TEST", "ls"); +// process_add_arg(test, "-al"); // test->cwd = "/etc/cleardns/fuck"; +// int pid = process_exec(test); +// log_info("PID = %d", pid); - int pid = process_exec(test); - - log_info("PID = %d", pid); - - int status; - wait(&status); - - return 0; +// int status; +// wait(&status); +// return 0; create_folder(WORK_DIR); -// load_config(config_file); -// free(config_file); -// + // TODO: load assets first + load_config(config_file); + free(config_file); + // process_list_init(); // process_list_append(dnsproxy_load("Domestic", loader.domestic, "domestic.json")); // process_list_append(dnsproxy_load("Foreign", loader.foreign, "foreign.json")); @@ -86,7 +72,7 @@ int main(int argc, char *argv[]) { // ClearDNS server // TODO: running custom script - process_list_run(); +// process_list_run(); // init_server(init_script, custom_script); // run init script and custom script diff --git a/src/common/structure.c b/src/common/structure.c index 0229cf9..90b89cb 100644 --- a/src/common/structure.c +++ b/src/common/structure.c @@ -17,6 +17,7 @@ uint32_t string_list_len(char **string_list) { // get len of string list return num - 1; } +// TODO: use char *** with void return char** string_list_append(char **string_list, const char *string) { // add new string at the end of list uint32_t len = string_list_len(string_list); string_list = (char **)realloc(string_list, sizeof(char *) * (len + 2)); // extend string list diff --git a/src/loader/CMakeLists.txt b/src/loader/CMakeLists.txt index 90e826d..4969c3a 100644 --- a/src/loader/CMakeLists.txt +++ b/src/loader/CMakeLists.txt @@ -1,4 +1,4 @@ cmake_minimum_required(VERSION 2.8.12) add_library(loader config.c default.c loader.c parser.c) -target_link_libraries(loader applet) +target_link_libraries(loader applet common) diff --git a/src/loader/config.c b/src/loader/config.c index 61741a0..89a452e 100644 --- a/src/loader/config.c +++ b/src/loader/config.c @@ -36,6 +36,10 @@ 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.cron = string_init(UPDATE_CRON); + config->assets.update_file = string_list_init(); + config->assets.update_url = string_list_init(); + config->reject = uint32_list_init(); config->hosts = string_list_init(); config->ttl = string_list_init(); @@ -73,6 +77,12 @@ 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 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); + } + uint32_list_debug("DNS reject type", config->reject); string_list_debug("Domain TTL", config->ttl); string_list_debug("Hosts", config->hosts); diff --git a/src/loader/default.c b/src/loader/default.c index 8f407f3..a1da955 100644 --- a/src/loader/default.c +++ b/src/loader/default.c @@ -40,6 +40,13 @@ foreign:\n\ fallback:\n\ - 1.1.1.1\n\ - 8.8.4.4\n\ +\n\ +assets:\n\ + cron: \"0 4 * * *\"\n\ + update:\n\ + gfwlist.txt: https://res.dnomd343.top/Share/gfwlist/gfwlist.txt\n\ + china-ip.txt: https://res.dnomd343.top/Share/chinalist/china-ip.txt\n\ + chinalist.txt: https://res.dnomd343.top/Share/chinalist/chinalist.txt\n\ " void load_default_config(const char *config_file) { diff --git a/src/loader/loader.c b/src/loader/loader.c index 6d9b1bc..8b58ad4 100644 --- a/src/loader/loader.c +++ b/src/loader/loader.c @@ -117,6 +117,10 @@ void load_config(const char *config_file) { cleardns_config *config = config_init(); load_default_config(config_file); // load default configure config_parser(config, config_file); // configure parser + + // insert code (remove after test) + config->assets.update_file = string_list_append(config->assets.update_file, "geoip.dat"); + config->assets.update_url = string_list_append(config->assets.update_url, "https://test.net/geoip.dat"); config_dump(config); log_info("Loading configure options"); diff --git a/src/loader/parser.c b/src/loader/parser.c index 3ea5581..5e19f7a 100644 --- a/src/loader/parser.c +++ b/src/loader/parser.c @@ -112,6 +112,24 @@ void adguard_parser(adguard_config *config, cJSON *json) { // adguard options pa } } +void assets_parser(assets_config *config, cJSON *json) { // assets options parser + + if (!cJSON_IsObject(json)) { + log_fatal("`assets` must be array"); + } + json = json->child; + while (json != NULL) { + if (!strcmp(json->string, "cron")) { + config->cron = json_string_value("assets.cron", json); + } + if (!strcmp(json->string, "update")) { + log_warn("GET UPDATE KEY"); + // TODO: get string map + } + json = json->next; // next field + } +} + void cleardns_parser(cleardns_config *config, const char *config_content) { // JSON format configure cJSON *json = cJSON_Parse(config_content); if (json == NULL) { @@ -137,6 +155,9 @@ void cleardns_parser(cleardns_config *config, const char *config_content) { // J if (!strcmp(json->string, "adguard")) { adguard_parser(&config->adguard, json); } + if (!strcmp(json->string, "assets")) { + assets_parser(&config->assets, json); + } if (!strcmp(json->string, "reject")) { config->reject = json_uint32_list_value("reject", json, config->reject); }