From 8d31a49cdc406ba26685fbdddc7c19836f9461d0 Mon Sep 17 00:00:00 2001 From: dnomd343 Date: Wed, 14 Sep 2022 18:42:59 +0800 Subject: [PATCH] update: load diverter assets --- include/common.h | 3 +++ src/cleardns.c | 9 +++++++++ src/common.c | 20 +++++++++++++++++++- src/loader/loader.c | 44 ++++++++++++++++++++++++++++++++++---------- 4 files changed, 65 insertions(+), 11 deletions(-) diff --git a/include/common.h b/include/common.h index c8e9c46..4db91aa 100644 --- a/include/common.h +++ b/include/common.h @@ -32,6 +32,7 @@ #define WORK_DIR "/etc/cleardns/" #define EXPOSE_DIR "/cleardns/" +#define ASSETS_DIR "/cleardns/assets/" #define ADGUARD_DIR "/cleardns/adguard/" #define ASSET_TTL "ttl.txt" @@ -57,6 +58,8 @@ uint8_t check_port(uint16_t port); uint16_t gen_rand_num(uint16_t limit); char* uint32_to_string(uint32_t number); +void save_string_list(const char *file, char **string_list); + char* gen_bcrypt(const char *data); int run_command(const char *command); void create_folder(const char *folder); diff --git a/src/cleardns.c b/src/cleardns.c index 356fef9..4d3e07a 100644 --- a/src/cleardns.c +++ b/src/cleardns.c @@ -41,6 +41,15 @@ int main(int argc, char *argv[]) { // ClearDNS server log_info("ClearDNS server start (%s)", VERSION); +// char **temp = string_list_init(); +// +// temp = string_list_append(temp, "a"); +// temp = string_list_append(temp, "b"); +// temp = string_list_append(temp, "c"); +// +// save_string_list("test.txt", temp); + + load_config("test.json"); dnsproxy_load("Domestic", loader.domestic, "domestic.json"); diff --git a/src/common.c b/src/common.c index 7983a52..98b443a 100644 --- a/src/common.c +++ b/src/common.c @@ -56,8 +56,11 @@ uint16_t gen_rand_num(uint16_t limit) { // 0 ~ (limit - 1) return rand() % limit; // NOLINT } -int run_command(const char *command) { +int run_command(const char *command) { // running command with system shell log_debug("Run command -> `%s`", command); + + // TODO: add non-zero return code warning + return system(command) / 256; } @@ -114,3 +117,18 @@ char* read_file(const char *file) { // read file content log_debug("File `%s` read success ->\n%s", file, content); return content; } + +void save_string_list(const char *file, char **string_list) { // save string list into file + log_debug("Write string list into `%s`", file); + FILE* fp = fopen(file , "w"); + if (fp == NULL) { + log_fatal("Fail to open file -> %s", file); + } + for (char **string = string_list; *string != NULL; ++string) { + log_debug("File append -> `%s`", *string); + fputs(*string, fp); + fputs("\n", fp); + } + fclose(fp); + log_debug("Save `%s` success", file); +} diff --git a/src/loader/loader.c b/src/loader/loader.c index 0c3c3f4..d7303bd 100644 --- a/src/loader/loader.c +++ b/src/loader/loader.c @@ -8,6 +8,12 @@ struct cleardns loader; +void load_diverter_assets() { + // ${ASSETS_DIR}${ASSET_GFW_LIST} >> ${WORK_DIR}${ASSET_GFW_LIST} + // ${ASSETS_DIR}${ASSET_CHINA_IP} >> ${WORK_DIR}${ASSET_CHINA_IP} + // ${ASSETS_DIR}${ASSET_CHINA_LIST} >> ${WORK_DIR}${ASSET_CHINA_LIST} +} + dnsproxy* load_domestic(cleardns_config *config) { dnsproxy *domestic = dnsproxy_init(config->domestic.port); domestic->verify = config->domestic.verify; @@ -42,26 +48,40 @@ overture* load_diverter(cleardns_config *config) { diverter->foreign_port = config->foreign.port; diverter->domestic_port = config->domestic.port; - free(diverter->ttl_file); - free(diverter->host_file); - // TODO: load into ASSET_TTL - // TODO: load into ASSET_HOSTS - diverter->ttl_file = string_init(ASSET_TTL); - diverter->host_file = string_init(ASSET_HOSTS); + if (string_list_len(config->ttl)) { + free(diverter->ttl_file); + diverter->ttl_file = string_init(ASSET_TTL); + char *ttl_file = string_join(WORK_DIR, ASSET_TTL); + save_string_list(ttl_file, config->ttl); + free(ttl_file); + } + if (string_list_len(config->hosts)) { + free(diverter->host_file); + diverter->host_file = string_init(ASSET_HOSTS); + char *hosts_file = string_join(WORK_DIR, ASSET_HOSTS); + save_string_list(hosts_file, config->hosts); + free(hosts_file); + } free(diverter->domestic_ip_file); free(diverter->foreign_domain_file); free(diverter->domestic_domain_file); - // TODO: load into ASSET_CHINA_IP - // TODO: load into ASSET_GFW_LIST - // TODO: load into ASSET_CHINA_LIST diverter->domestic_ip_file = string_init(ASSET_CHINA_IP); diverter->foreign_domain_file = string_init(ASSET_GFW_LIST); diverter->domestic_domain_file = string_init(ASSET_CHINA_LIST); - // TODO: assets file append + char *gfwlist = string_join(WORK_DIR, ASSET_GFW_LIST); + char *china_ip = string_join(WORK_DIR, ASSET_CHINA_IP); + char *chinalist = string_join(WORK_DIR, ASSET_CHINA_LIST); + save_string_list(gfwlist, config->diverter.gfwlist); + save_string_list(china_ip, config->diverter.china_ip); + save_string_list(chinalist, config->diverter.chinalist); + free(chinalist); + free(china_ip); + free(gfwlist); diverter->reject_type = uint32_list_update(diverter->reject_type, config->reject); + load_diverter_assets(); return diverter; } @@ -84,6 +104,10 @@ void load_config(const char *config_file) { cleardns_config *config = config_init(); config_parser(config, config_file); config_dump(config); + + // TODO: extract assets file + // TODO: mkdir -p ${WORK_DIR} + if (!config->adguard.enable) { config->diverter.port = config->port; // override diverter port by dns port }