From 76155b916f9ede9671d811e38e75c682d189c1a2 Mon Sep 17 00:00:00 2001 From: dnomd343 Date: Tue, 13 Sep 2022 15:54:50 +0800 Subject: [PATCH] feat: loader interface --- include/loader/loader.h | 10 ++++++++ include/utils/structure.h | 1 + src/cleardns.c | 3 +++ src/dnsproxy.c | 4 +++ src/loader/loader.c | 54 ++++++++++++++++++++++++++++++++++++--- src/overture.c | 5 ++++ src/utils/structure.c | 7 +++++ 7 files changed, 80 insertions(+), 4 deletions(-) diff --git a/include/loader/loader.h b/include/loader/loader.h index e578f45..34dd326 100644 --- a/include/loader/loader.h +++ b/include/loader/loader.h @@ -1,6 +1,16 @@ #ifndef _LOADER_H_ #define _LOADER_H_ +#include "dnsproxy.h" +#include "overture.h" + +struct { + dnsproxy *domestic; + dnsproxy *foreign; + overture *diverter; + // adguard +} loader; + void load_config(const char *config_file); #endif diff --git a/include/utils/structure.h b/include/utils/structure.h index 2ae345e..43dfd1f 100644 --- a/include/utils/structure.h +++ b/include/utils/structure.h @@ -13,6 +13,7 @@ char** string_list_init(); void string_list_free(char **string_list); char* string_list_dump(char **string_list); uint32_t string_list_len(char **string_list); +char** string_list_update(char **base_list, char **update_list); char** string_list_append(char **string_list, const char *string); #endif diff --git a/src/cleardns.c b/src/cleardns.c index f0f2ec9..7123644 100644 --- a/src/cleardns.c +++ b/src/cleardns.c @@ -38,6 +38,9 @@ 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"); + // char **temp = string_list_init(); // temp = string_list_append(temp, "123"); // temp = string_list_append(temp, "abc"); diff --git a/src/dnsproxy.c b/src/dnsproxy.c index e0363d4..15bf5d3 100644 --- a/src/dnsproxy.c +++ b/src/dnsproxy.c @@ -58,6 +58,10 @@ void dnsproxy_dump(const char *caption, dnsproxy *info) { // show dnsproxy info } process* dnsproxy_load(const char *caption, dnsproxy *info, const char *file) { + + // TODO: check primary server number (non-zero) + // TODO: check port (1 ~ 65535) + dnsproxy_dump(caption, info); char *config = dnsproxy_config(info); // string config (JSON format) char *config_file = string_join(WORK_DIR, file); diff --git a/src/loader/loader.c b/src/loader/loader.c index 6712725..aa5730e 100644 --- a/src/loader/loader.c +++ b/src/loader/loader.c @@ -1,19 +1,65 @@ #include "loader.h" -#include "logger.h" #include "config.h" #include "parser.h" +#include "dnsproxy.h" +#include "structure.h" -void load_dnsproxy() { +dnsproxy* load_domestic(cleardns_config *config) { + dnsproxy *domestic = dnsproxy_init(config->domestic.port); + domestic->verify = config->domestic.verify; + domestic->parallel = config->domestic.parallel; + if (config->cache.enable) { + domestic->cache = config->cache.size; + domestic->optimistic = config->cache.optimistic; + } + domestic->bootstrap = string_list_update(domestic->bootstrap, config->domestic.bootstrap); + domestic->fallback = string_list_update(domestic->fallback, config->domestic.fallback); + domestic->primary = string_list_update(domestic->primary, config->domestic.primary); + return domestic; +} +dnsproxy* load_foreign(cleardns_config *config) { + dnsproxy *foreign = dnsproxy_init(config->foreign.port); + foreign->verify = config->foreign.verify; + foreign->parallel = config->foreign.parallel; + if (config->cache.enable) { + foreign->cache = config->cache.size; + foreign->optimistic = config->cache.optimistic; + } + foreign->bootstrap = string_list_update(foreign->bootstrap, config->foreign.bootstrap); + foreign->fallback = string_list_update(foreign->fallback, config->foreign.fallback); + foreign->primary = string_list_update(foreign->primary, config->foreign.primary); + return foreign; +} + +overture* load_diverter(cleardns_config *config) { + overture *diverter = overture_init(config->diverter.port); + + // timeout + // ttl_file + // host_file + // foreign_port + // domestic_port + + // reject_type + + // foreign_ip_file + // domestic_ip_file + // foreign_domain_file + // domestic_domain_file + return diverter; } void load_config(const char *config_file) { cleardns_config *config = config_init(); config_parser(config, config_file); - config_dump(config); - // TODO: load into process + // TODO: use dns port as diverter when adguard disabled + loader.domestic = load_domestic(config); + loader.foreign = load_foreign(config); + loader.diverter = load_diverter(config); + // load adguard config_free(config); } diff --git a/src/overture.c b/src/overture.c index d1a5acd..e69e689 100644 --- a/src/overture.c +++ b/src/overture.c @@ -45,6 +45,11 @@ void overture_dump(overture *info) { // show overture info in debug log } process* overture_load(overture *info, const char *file) { + + // TODO: check port (1 ~ 65535) + // TODO: check timeout not zero + // TODO: check whether file exist + overture_dump(info); char *config = overture_config(info); // string config (JSON format) char *config_file = string_join(WORK_DIR, file); diff --git a/src/utils/structure.c b/src/utils/structure.c index fa1d8e8..08f4281 100644 --- a/src/utils/structure.c +++ b/src/utils/structure.c @@ -29,6 +29,13 @@ char** string_list_append(char **string_list, const char *string) { // add new s return string_list; } +char** string_list_update(char **base_list, char **update_list) { + for (char **string = update_list; *string != NULL; ++string) { + base_list = string_list_append(base_list, *string); + } + return base_list; +} + void string_list_free(char **string_list) { // free string list for (char **string = string_list; *string != NULL; ++string) { free(*string);