From ee57e9e5e04278b93a0b5c7d370af5f339057f4a Mon Sep 17 00:00:00 2001 From: dnomd343 Date: Sun, 11 Sep 2022 18:57:32 +0800 Subject: [PATCH] update: dnsproxy json configure --- include/dnsproxy.h | 2 ++ src/cleardns.c | 7 +++++++ src/dnsproxy.c | 44 ++++++++++++++++++++++++++++++++++++++++ src/utils/CMakeLists.txt | 2 +- src/utils/cJSON.c | 2 +- 5 files changed, 55 insertions(+), 2 deletions(-) diff --git a/include/dnsproxy.h b/include/dnsproxy.h index 484c347..877321d 100644 --- a/include/dnsproxy.h +++ b/include/dnsproxy.h @@ -19,4 +19,6 @@ void dnsproxy_add_primary(dnsproxy *info, char *server); void dnsproxy_add_fallback(dnsproxy *info, char *server); void dnsproxy_add_bootstrap(dnsproxy *info, char *server); +void dnsproxy_gen_config(dnsproxy *info); + #endif diff --git a/src/cleardns.c b/src/cleardns.c index 32a3264..6ed014d 100644 --- a/src/cleardns.c +++ b/src/cleardns.c @@ -45,8 +45,15 @@ int main(int argc, char *argv[]) { // ClearDNS server dnsproxy_add_fallback(domestic, "tls://223.6.6.6"); dnsproxy_add_fallback(domestic, "tls://120.53.53.53"); + domestic->verify = FALSE; +// domestic->parallel = FALSE; + domestic->optimistic = TRUE; +// domestic->cache = "0"; + dnsproxy_dump("Domestic", domestic); + dnsproxy_gen_config(domestic); + // int debug_mode = 0; // fprintf(stderr, "[ClearDNS] Server start.\n"); // for (char **p = argv; p < argv + argc; ++p) { diff --git a/src/dnsproxy.c b/src/dnsproxy.c index fefcf2a..ab378ea 100644 --- a/src/dnsproxy.c +++ b/src/dnsproxy.c @@ -1,9 +1,12 @@ #include +#include #include "dnsproxy.h" #include "logger.h" #include "common.h" #include "strList.h" +#include "cJSON.h" + dnsproxy* dnsproxy_init(int port) { dnsproxy *info = (dnsproxy*)malloc(sizeof(dnsproxy)); info->port = port; @@ -39,3 +42,44 @@ void dnsproxy_add_fallback(dnsproxy *info, char *server) { void dnsproxy_add_bootstrap(dnsproxy *info, char *server) { info->bootstrap = string_list_append(info->bootstrap, server); } + +void dnsproxy_gen_config(dnsproxy *info) { + cJSON *config = cJSON_CreateObject(); + if (!info->verify) { + cJSON_AddTrueToObject(config, "insecure"); // insecure --(default)--> `false` + } + if (info->parallel) { + cJSON_AddTrueToObject(config, "all-servers"); + } + if (strcmp(info->cache, "0") != 0) { + cJSON_AddTrueToObject(config, "cache"); + cJSON_AddStringToObject(config, "cache-size", info->cache); + } + if (info->optimistic) { + cJSON_AddTrueToObject(config, "cache-optimistic"); + } + + cJSON *port = cJSON_CreateArray(); + cJSON_AddItemToArray(port, cJSON_CreateNumber(info->port)); + cJSON_AddItemToObject(config, "listen-ports", port); + + cJSON *bootstrap = cJSON_CreateArray(); + for (char **server = info->bootstrap; *server != NULL; ++server) { + cJSON_AddItemToArray(bootstrap, cJSON_CreateString(*server)); + } + cJSON_AddItemToObject(config, "bootstrap", bootstrap); + + cJSON *fallback = cJSON_CreateArray(); + for (char **server = info->fallback; *server != NULL; ++server) { + cJSON_AddItemToArray(fallback, cJSON_CreateString(*server)); + } + cJSON_AddItemToObject(config, "fallback", fallback); + + cJSON *primary = cJSON_CreateArray(); + for (char **server = info->primary; *server != NULL; ++server) { + cJSON_AddItemToArray(primary, cJSON_CreateString(*server)); + } + cJSON_AddItemToObject(config, "upstream", primary); + + log_info("\n%s", cJSON_Print(config)); +} diff --git a/src/utils/CMakeLists.txt b/src/utils/CMakeLists.txt index b82db9b..4fd3913 100644 --- a/src/utils/CMakeLists.txt +++ b/src/utils/CMakeLists.txt @@ -1,3 +1,3 @@ cmake_minimum_required(VERSION 2.8.12) -add_library(utils logger.c strList.c) +add_library(utils logger.c strList.c cJSON.c) diff --git a/src/utils/cJSON.c b/src/utils/cJSON.c index e40e4bb..3063f74 100644 --- a/src/utils/cJSON.c +++ b/src/utils/cJSON.c @@ -37,7 +37,7 @@ #pragma warning (disable : 4001) #endif -#include +#include #include #include #include