diff --git a/include/common.h b/include/common.h index 9ba173d..f4882d7 100644 --- a/include/common.h +++ b/include/common.h @@ -13,9 +13,14 @@ typedef u_int64_t uint64_t; #define TRUE 1 #define FALSE 0 +#define DNS_PORT 53 +#define ADGUARD_PORT 80 +#define DIVERTER_PORT 5353 #define DOMESTIC_PORT 4053 #define FOREIGN_PORT 6053 -#define DIVERTER_PORT 5353 + +#define ADGUARD_USER "admin" +#define ADGUARD_PASSWD "adguard" #define DNSPROXY_BIN "dnsproxy" #define OVERTURE_BIN "overture" @@ -30,8 +35,10 @@ typedef u_int64_t uint64_t; //void load_start_command(char *adguard_workdir, char *overture_config, char *upstream_config, int is_debug); -char* show_bool(int value); -char* read_file(char *file); -void save_file(char *file, char *content); +char* show_bool(uint8_t value); +char* read_file(const char *file); +void save_file(const char *file, const char *content); +void string_list_debug(char *describe, char **string_list); +void uint32_list_debug(char *describe, uint32_t **uint32_list); #endif diff --git a/include/load.h b/include/load.h new file mode 100644 index 0000000..0510fbe --- /dev/null +++ b/include/load.h @@ -0,0 +1,50 @@ +#ifndef _LOAD_H_ +#define _LOAD_H_ + +#include "common.h" + +typedef struct { + uint16_t port; + uint8_t verify; // bool value + uint8_t parallel; // bool value + char **bootstrap; + char **fallback; + char **primary; +} upstream_config; + +typedef struct { + uint16_t port; + char **gfwlist; + char **china_ip; + char **chinalist; +} diverter_config; + +typedef struct { + uint32_t size; + uint8_t enable; // bool value + uint8_t optimistic; // bool value +} cache_config; + +typedef struct { + uint16_t port; + uint8_t enable; + char *username; + char *password; +} adguard_config; + +typedef struct { + uint16_t port; + cache_config cache; + upstream_config domestic; + upstream_config foreign; + diverter_config diverter; + adguard_config adguard; + uint32_t **reject; + char **hosts; + char **ttl; +} cleardns_config; + +void load_config(const char *config_file); +void json_config_parser(cleardns_config *config, const char *config_file); + +#endif diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 458b539..1ac2906 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -3,7 +3,8 @@ cmake_minimum_required(VERSION 2.8.12) include_directories(${PROJECT_SOURCE_DIR}/include) include_directories(${PROJECT_SOURCE_DIR}/include/utils) +add_subdirectory(load) add_subdirectory(utils) add_executable(cleardns cleardns.c dnsproxy.c common.c overture.c) -target_link_libraries(cleardns utils) +target_link_libraries(cleardns load utils) diff --git a/src/cleardns.c b/src/cleardns.c index 5a85afb..242fc12 100644 --- a/src/cleardns.c +++ b/src/cleardns.c @@ -1,4 +1,5 @@ #include +#include "load.h" #include "logger.h" #include "common.h" #include "dnsproxy.h" @@ -35,6 +36,7 @@ int main(int argc, char *argv[]) { // ClearDNS server LOG_LEVEL = LOG_DEBUG; log_info("ClearDNS server start (%s)", VERSION); + load_config("test.json"); // char **temp = string_list_init(); // temp = string_list_append(temp, "123"); @@ -80,23 +82,23 @@ int main(int argc, char *argv[]) { // ClearDNS server // log_info("cwd -> %s", p->cwd); - overture *diverter = overture_init(DIVERTER_PORT); - - diverter->timeout = 8; - diverter->domestic_ip_file = "china-ip.txt"; - diverter->domestic_domain_file = "chinalist.txt"; - diverter->foreign_domain_file = "gfwlist.txt"; - - diverter->debug = TRUE; - diverter->ttl_file = "domain_ttl.txt"; - diverter->host_file = "hosts.txt"; - diverter->reject_type = uint32_list_append(diverter->reject_type, 255); - diverter->reject_type = uint32_list_append(diverter->reject_type, 254); - - process *p = overture_load(diverter, "overture.json"); - log_info("cmd -> %s", string_list_dump(p->cmd)); - log_info("env -> %s", string_list_dump(p->env)); - log_info("cwd -> %s", p->cwd); +// overture *diverter = overture_init(DIVERTER_PORT); +// +// diverter->timeout = 8; +// diverter->domestic_ip_file = "china-ip.txt"; +// diverter->domestic_domain_file = "chinalist.txt"; +// diverter->foreign_domain_file = "gfwlist.txt"; +// +// diverter->debug = TRUE; +// diverter->ttl_file = "domain_ttl.txt"; +// diverter->host_file = "hosts.txt"; +// diverter->reject_type = uint32_list_append(diverter->reject_type, 255); +// diverter->reject_type = uint32_list_append(diverter->reject_type, 254); +// +// process *p = overture_load(diverter, "overture.json"); +// log_info("cmd -> %s", string_list_dump(p->cmd)); +// log_info("env -> %s", string_list_dump(p->env)); +// log_info("cwd -> %s", p->cwd); // int debug_mode = 0; diff --git a/src/common.c b/src/common.c index 5db4bec..5d30b6f 100644 --- a/src/common.c +++ b/src/common.c @@ -2,8 +2,9 @@ #include #include "common.h" #include "logger.h" +#include "structure.h" -char* show_bool(int value) { // return `true` or `false` +char* show_bool(uint8_t value) { // return `true` or `false` if (value) { return "true"; } else { @@ -11,7 +12,19 @@ char* show_bool(int value) { // return `true` or `false` } } -void save_file(char *file, char *content) { // save into file +void string_list_debug(char *describe, char **string_list) { + char *string_ret = string_list_dump(string_list); + log_debug("%s -> %s", describe, string_ret); + free(string_ret); +} + +void uint32_list_debug(char *describe, uint32_t **uint32_list) { + char *string_ret = uint32_list_dump(uint32_list); + log_debug("%s -> %s", describe, string_ret); + free(string_ret); +} + +void save_file(const char *file, const char *content) { // save into file log_debug("Write into `%s` -> \n%s", file, content); FILE* fp = fopen(file , "w"); if (fp == NULL) { @@ -22,7 +35,7 @@ void save_file(char *file, char *content) { // save into file log_debug("Save `%s` success", file); } -char* read_file(char *file) { // read file content +char* read_file(const char *file) { // read file content log_debug("Read file -> %s", file); FILE *fp = fopen(file, "rb"); if (fp == NULL) { // file open failed diff --git a/src/load/CMakeLists.txt b/src/load/CMakeLists.txt new file mode 100644 index 0000000..fc14767 --- /dev/null +++ b/src/load/CMakeLists.txt @@ -0,0 +1,3 @@ +cmake_minimum_required(VERSION 2.8.12) + +add_library(load load.c json.c) diff --git a/src/load/json.c b/src/load/json.c new file mode 100644 index 0000000..7095216 --- /dev/null +++ b/src/load/json.c @@ -0,0 +1,22 @@ +#include +#include "load.h" +#include "cJSON.h" +#include "common.h" +#include "logger.h" + +void json_config_parser(cleardns_config *config, const char *config_file) { // JSON format configure + char *config_content = read_file(config_file); + cJSON *json = cJSON_Parse(config_content); + if (json == NULL) { + log_fatal("JSON format error"); + } + json = json->child; + while (json != NULL) { + if (!strcmp(json->string, "port")) { + // get dns port + } + + json = json->next; // next field + } + cJSON_free(json); // free JSON struct +} diff --git a/src/load/load.c b/src/load/load.c new file mode 100644 index 0000000..727a2c5 --- /dev/null +++ b/src/load/load.c @@ -0,0 +1,85 @@ +#include "load.h" +#include "common.h" +#include "logger.h" +#include "structure.h" + +cleardns_config* config_init() { + cleardns_config *config = (cleardns_config *)malloc(sizeof(cleardns_config)); + config->port = DNS_PORT; + config->cache.size = 0; + config->cache.enable = FALSE; + config->cache.optimistic = FALSE; + + config->domestic.port = DOMESTIC_PORT; + config->domestic.verify = TRUE; + config->domestic.parallel = TRUE; + config->domestic.bootstrap = string_list_init(); + config->domestic.fallback = string_list_init(); + config->domestic.primary = string_list_init(); + + config->foreign.port = FOREIGN_PORT; + config->foreign.verify = TRUE; + config->foreign.parallel = TRUE; + config->foreign.bootstrap = string_list_init(); + config->foreign.fallback = string_list_init(); + config->foreign.primary = string_list_init(); + + config->diverter.port = DIVERTER_PORT; + config->diverter.gfwlist = string_list_init(); + config->diverter.china_ip = string_list_init(); + config->diverter.chinalist = string_list_init(); + + config->adguard.port = ADGUARD_PORT; + config->adguard.enable = TRUE; + config->adguard.username = ADGUARD_USER; + config->adguard.password = ADGUARD_PASSWD; + + config->reject = uint32_list_init(); + config->hosts = string_list_init(); + config->ttl = string_list_init(); + return config; +} + +void config_dump(cleardns_config *config) { + log_debug("DNS port -> %u", config->port); + log_debug("Cache size -> %u", config->cache.size); + log_debug("Cache enable -> %s", show_bool(config->cache.enable)); + log_debug("Cache optimistic -> %s", show_bool(config->cache.optimistic)); + + log_debug("Domestic port -> %u", config->domestic.port); + log_debug("Domestic verify -> %s", show_bool(config->domestic.verify)); + log_debug("Domestic parallel -> %s", show_bool(config->domestic.parallel)); + string_list_debug("Domestic bootstrap", config->domestic.bootstrap); + string_list_debug("Domestic fallback", config->domestic.fallback); + string_list_debug("Domestic primary", config->domestic.primary); + + log_debug("Foreign port -> %u", config->foreign.port); + log_debug("Foreign verify -> %s", show_bool(config->foreign.verify)); + log_debug("Foreign parallel -> %s", show_bool(config->foreign.parallel)); + string_list_debug("Foreign bootstrap", config->foreign.bootstrap); + string_list_debug("Foreign fallback", config->foreign.fallback); + string_list_debug("Foreign primary", config->foreign.primary); + + log_debug("Diverter port -> %u", config->diverter.port); + string_list_debug("Diverter gfwlist", config->diverter.gfwlist); + string_list_debug("Diverter china-ip", config->diverter.china_ip); + string_list_debug("Diverter chinalist", config->diverter.chinalist); + + log_debug("AdGuardHome port -> %u", config->adguard.port); + log_debug("AdGuardHome enable -> %s", show_bool(config->adguard.enable)); + log_debug("AdGuardHome username -> %s", config->adguard.username); + log_debug("AdGuardHome password -> %s", config->adguard.password); + + uint32_list_debug("DNS reject type", config->reject); + string_list_debug("Domain TTL", config->ttl); + string_list_debug("Hosts", config->hosts); +} + +void load_config(const char *config_file) { + cleardns_config *config = config_init(); + + json_config_parser(config, config_file); + + log_info("Configure load success"); + config_dump(config); +}