From 1917fd2ae1c85146fb37cc2371cebe5b2596c612 Mon Sep 17 00:00:00 2001 From: dnomd343 Date: Mon, 12 Sep 2022 00:28:06 +0800 Subject: [PATCH] feat: json configure for overture --- include/overture.h | 1 + src/cleardns.c | 8 +++++++ src/overture.c | 60 ++++++++++++++++++++++++++++++++++++++++++++++ src/utils/logger.c | 2 ++ 4 files changed, 71 insertions(+) diff --git a/include/overture.h b/include/overture.h index db5877c..c129cb2 100644 --- a/include/overture.h +++ b/include/overture.h @@ -14,5 +14,6 @@ typedef struct { overture* overture_init(int port); void overture_dump(overture *info); +char* overture_gen_config(overture *info); #endif diff --git a/src/cleardns.c b/src/cleardns.c index 5b17a87..c3a6202 100644 --- a/src/cleardns.c +++ b/src/cleardns.c @@ -57,8 +57,16 @@ int main(int argc, char *argv[]) { // ClearDNS server 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"; + overture_dump(diverter); + char *config = overture_gen_config(diverter); + log_info("\n%s", config); + // int debug_mode = 0; // fprintf(stderr, "[ClearDNS] Server start.\n"); // for (char **p = argv; p < argv + argc; ++p) { diff --git a/src/overture.c b/src/overture.c index 478f1db..3db0a03 100644 --- a/src/overture.c +++ b/src/overture.c @@ -1,7 +1,10 @@ +#include #include #include "overture.h" #include "common.h" #include "logger.h" +#include "cJSON.h" +#include "str.h" overture* overture_init(int port) { // init overture options overture *info = (overture*)malloc(sizeof(overture)); @@ -26,3 +29,60 @@ void overture_dump(overture *info) { // show overture info in debug log log_debug("Overture foreign domain file -> %s", info->foreign_domain_file); log_debug("Overture domestic domain file -> %s", info->domestic_domain_file); } + +char* overture_gen_config(overture *info) { // generate json configure from overture options + cJSON *config = cJSON_CreateObject(); + char port_str[12]; // 32-bits (MAX_LEN -> -2147483648 -> 12-bytes) + sprintf(port_str, "%d", info->port); + char *bind_addr = string_join(":", port_str); + sprintf(port_str, "%d", info->foreign_port); + char *foreign_addr = string_join("127.0.0.1:", port_str); + sprintf(port_str, "%d", info->domestic_port); + char *domestic_port = string_join("127.0.0.1:", port_str); + + cJSON_AddStringToObject(config, "bindAddress", bind_addr); + cJSON_AddFalseToObject(config, "onlyPrimaryDNS"); + cJSON_AddFalseToObject(config, "ipv6UseAlternativeDNS"); + cJSON_AddTrueToObject(config, "alternativeDNSConcurrent"); + cJSON_AddStringToObject(config, "whenPrimaryDNSAnswerNoneUse", "alternativeDNS"); + + cJSON *primary = cJSON_CreateObject(); + cJSON *primary_dns = cJSON_CreateArray(); + cJSON_AddStringToObject(primary, "name", "Domestic"); + cJSON_AddStringToObject(primary, "address", domestic_port); + cJSON_AddStringToObject(primary, "protocol", "udp"); + cJSON_AddNumberToObject(primary, "timeout", info->timeout); + cJSON_AddItemToArray(primary_dns, primary); + cJSON_AddItemToObject(config, "primaryDNS", primary_dns); + + cJSON *alternative = cJSON_CreateObject(); + cJSON *alternative_dns = cJSON_CreateArray(); + cJSON_AddStringToObject(alternative, "name", "Foreign"); + cJSON_AddStringToObject(alternative, "address", foreign_addr); + cJSON_AddStringToObject(alternative, "protocol", "udp"); + cJSON_AddNumberToObject(alternative, "timeout", info->timeout); + cJSON_AddItemToArray(alternative_dns, alternative); + cJSON_AddItemToObject(config, "alternativeDNS", alternative_dns); + + cJSON *ip_file = cJSON_CreateObject(); + cJSON_AddStringToObject(ip_file, "primary", info->domestic_ip_file); + cJSON_AddStringToObject(ip_file, "alternative", info->foreign_ip_file); + cJSON_AddItemToObject(config, "ipNetworkFile", ip_file); + + cJSON *domain_file = cJSON_CreateObject(); + cJSON_AddStringToObject(domain_file, "primary", info->domestic_domain_file); + cJSON_AddStringToObject(domain_file, "alternative", info->foreign_domain_file); + cJSON_AddStringToObject(domain_file, "matcher", "suffix-tree"); + cJSON_AddItemToObject(config, "domainFile", domain_file); + + cJSON *host_file = cJSON_CreateObject(); + cJSON_AddStringToObject(host_file, "finder", "full-map"); + cJSON_AddItemToObject(config, "hostsFile", host_file); + + char *config_str = cJSON_Print(config); + cJSON_Delete(config); // free json object + free(domestic_port); + free(foreign_addr); + free(bind_addr); + return config_str; +} diff --git a/src/utils/logger.c b/src/utils/logger.c index ac64260..e9850ba 100644 --- a/src/utils/logger.c +++ b/src/utils/logger.c @@ -22,6 +22,7 @@ static const char *log_color[] = { "\x1b[95m", // fatal }; +// TODO: add ms field void log_printf(int level, const char *fmt, ...) { if (level < LOG_LEVEL) { // skip low log level return; @@ -44,6 +45,7 @@ void log_printf(int level, const char *fmt, ...) { } } +// TODO: add ms field void log_perror(char *prefix) { time_t t; time(&t);