From ae89e93bc45cb0b9b8571a7f4f81ed9440215b87 Mon Sep 17 00:00:00 2001 From: dnomd343 Date: Wed, 14 Sep 2022 17:49:24 +0800 Subject: [PATCH] update: loader of diverter and filter --- include/common.h | 10 +++++- include/loader/loader.h | 6 ++-- include/utils/structure.h | 1 + src/cleardns.c | 7 ++-- src/common.c | 2 +- src/loader/loader.c | 68 +++++++++++++++++++++++---------------- src/utils/json.c | 2 +- src/utils/structure.c | 7 ++++ 8 files changed, 69 insertions(+), 34 deletions(-) diff --git a/include/common.h b/include/common.h index 9524aa6..c8e9c46 100644 --- a/include/common.h +++ b/include/common.h @@ -30,7 +30,15 @@ #define OVERTURE_BIN "overture" #define ADGUARD_BIN "AdGuardHome" -#define WORK_DIR "/etc/cleardns/" +#define WORK_DIR "/etc/cleardns/" +#define EXPOSE_DIR "/cleardns/" +#define ADGUARD_DIR "/cleardns/adguard/" + +#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" //extern char **adguard_command; //extern char **overture_command; diff --git a/include/loader/loader.h b/include/loader/loader.h index d4c54db..5b01f08 100644 --- a/include/loader/loader.h +++ b/include/loader/loader.h @@ -5,12 +5,14 @@ #include "dnsproxy.h" #include "overture.h" -extern struct { +struct cleardns { dnsproxy *domestic; dnsproxy *foreign; overture *diverter; adguard *filter; -} loader; +}; + +extern struct cleardns loader; void load_config(const char *config_file); diff --git a/include/utils/structure.h b/include/utils/structure.h index 660171a..d308940 100644 --- a/include/utils/structure.h +++ b/include/utils/structure.h @@ -8,6 +8,7 @@ char* uint32_list_dump(uint32_t **int_list); void uint32_list_free(uint32_t **uint32_list); uint32_t uint32_list_len(uint32_t **int_list); uint32_t** uint32_list_append(uint32_t **int_list, uint32_t number); +uint32_t** uint32_list_update(uint32_t **base_list, uint32_t **update_list); char** string_list_init(); void string_list_free(char **string_list); diff --git a/src/cleardns.c b/src/cleardns.c index 944e263..356fef9 100644 --- a/src/cleardns.c +++ b/src/cleardns.c @@ -43,8 +43,11 @@ int main(int argc, char *argv[]) { // ClearDNS server load_config("test.json"); -// dnsproxy_load("Domestic", loader.domestic, "domestic.json"); -// dnsproxy_load("Foreign", loader.foreign, "foreign.json"); + 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); + // char **temp = string_list_init(); // temp = string_list_append(temp, "123"); diff --git a/src/common.c b/src/common.c index 8a5f81e..7983a52 100644 --- a/src/common.c +++ b/src/common.c @@ -1,7 +1,6 @@ #include #include #include -#include #include #include "common.h" #include "logger.h" @@ -58,6 +57,7 @@ uint16_t gen_rand_num(uint16_t limit) { // 0 ~ (limit - 1) } int run_command(const char *command) { + log_debug("Run command -> `%s`", command); return system(command) / 256; } diff --git a/src/loader/loader.c b/src/loader/loader.c index a6c7f28..0c3c3f4 100644 --- a/src/loader/loader.c +++ b/src/loader/loader.c @@ -1,8 +1,12 @@ +#include #include "loader.h" #include "config.h" #include "parser.h" #include "dnsproxy.h" #include "structure.h" +#include "common.h" + +struct cleardns loader; dnsproxy* load_domestic(cleardns_config *config) { dnsproxy *domestic = dnsproxy_init(config->domestic.port); @@ -34,35 +38,45 @@ dnsproxy* load_foreign(cleardns_config *config) { overture* load_diverter(cleardns_config *config) { overture *diverter = overture_init(); - - // ttl_file - // host_file - // foreign_port - // domestic_port - - // reject_type - - // foreign_ip_file - // domestic_ip_file - // foreign_domain_file - // domestic_domain_file - - // TODO: load assets file - + diverter->port = config->diverter.port; + 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); + + 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 + + diverter->reject_type = uint32_list_update(diverter->reject_type, config->reject); return diverter; } adguard* load_filter(cleardns_config *config) { + if (!config->adguard.enable) { + return NULL; // disable adguard + } adguard *filter = adguard_init(); - - // dns_port - // web_port - - // upstream - - // username - // password - + filter->dns_port = config->port; + filter->web_port = config->adguard.port; + filter->username = config->adguard.username; + filter->password = config->adguard.password; + char *diverter_port = uint32_to_string(config->diverter.port); + filter->upstream = string_join("127.0.0.1:", diverter_port); + free(diverter_port); return filter; } @@ -70,9 +84,9 @@ void load_config(const char *config_file) { cleardns_config *config = config_init(); config_parser(config, config_file); config_dump(config); - - // TODO: use dns port as diverter when adguard disabled - + if (!config->adguard.enable) { + config->diverter.port = config->port; // override diverter port by dns port + } loader.domestic = load_domestic(config); loader.foreign = load_foreign(config); loader.diverter = load_diverter(config); diff --git a/src/utils/json.c b/src/utils/json.c index 4250e66..8500bc4 100644 --- a/src/utils/json.c +++ b/src/utils/json.c @@ -16,7 +16,7 @@ char* to_json(const char *file) { char *output_file = string_join("/tmp/tojson-", flag); char *to_json_cmd = (char *)malloc(strlen(file) + strlen(output_file) + 11); sprintf(to_json_cmd, "toJSON %s > %s", file, output_file); - log_debug("JSON format command -> `%s`", to_json_cmd); +// log_debug("JSON format command -> `%s`", to_json_cmd); if (run_command(to_json_cmd)) { // toJSON return non-zero code // TODO: try remove output file return NULL; // convert failed diff --git a/src/utils/structure.c b/src/utils/structure.c index 87f549d..ea0416b 100644 --- a/src/utils/structure.c +++ b/src/utils/structure.c @@ -37,6 +37,13 @@ char** string_list_update(char **base_list, char **update_list) { return base_list; } +uint32_t** uint32_list_update(uint32_t **base_list, uint32_t **update_list) { + for (uint32_t **number = update_list; *number != NULL; ++number) { + base_list = uint32_list_append(base_list, **number); + } + return base_list; +} + void string_list_free(char **string_list) { // free string list for (char **string = string_list; *string != NULL; ++string) { free(*string);