Browse Source

update: dnsproxy json configure

dev
dnomd343 2 years ago
parent
commit
ee57e9e5e0
  1. 2
      include/dnsproxy.h
  2. 7
      src/cleardns.c
  3. 44
      src/dnsproxy.c
  4. 2
      src/utils/CMakeLists.txt
  5. 2
      src/utils/cJSON.c

2
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_fallback(dnsproxy *info, char *server);
void dnsproxy_add_bootstrap(dnsproxy *info, char *server); void dnsproxy_add_bootstrap(dnsproxy *info, char *server);
void dnsproxy_gen_config(dnsproxy *info);
#endif #endif

7
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://223.6.6.6");
dnsproxy_add_fallback(domestic, "tls://120.53.53.53"); 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_dump("Domestic", domestic);
dnsproxy_gen_config(domestic);
// int debug_mode = 0; // int debug_mode = 0;
// fprintf(stderr, "[ClearDNS] Server start.\n"); // fprintf(stderr, "[ClearDNS] Server start.\n");
// for (char **p = argv; p < argv + argc; ++p) { // for (char **p = argv; p < argv + argc; ++p) {

44
src/dnsproxy.c

@ -1,9 +1,12 @@
#include <stdlib.h> #include <stdlib.h>
#include <string.h>
#include "dnsproxy.h" #include "dnsproxy.h"
#include "logger.h" #include "logger.h"
#include "common.h" #include "common.h"
#include "strList.h" #include "strList.h"
#include "cJSON.h"
dnsproxy* dnsproxy_init(int port) { dnsproxy* dnsproxy_init(int port) {
dnsproxy *info = (dnsproxy*)malloc(sizeof(dnsproxy)); dnsproxy *info = (dnsproxy*)malloc(sizeof(dnsproxy));
info->port = port; info->port = port;
@ -39,3 +42,44 @@ void dnsproxy_add_fallback(dnsproxy *info, char *server) {
void dnsproxy_add_bootstrap(dnsproxy *info, char *server) { void dnsproxy_add_bootstrap(dnsproxy *info, char *server) {
info->bootstrap = string_list_append(info->bootstrap, 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));
}

2
src/utils/CMakeLists.txt

@ -1,3 +1,3 @@
cmake_minimum_required(VERSION 2.8.12) cmake_minimum_required(VERSION 2.8.12)
add_library(utils logger.c strList.c) add_library(utils logger.c strList.c cJSON.c)

2
src/utils/cJSON.c

@ -37,7 +37,7 @@
#pragma warning (disable : 4001) #pragma warning (disable : 4001)
#endif #endif
#include <strList.bak> #include <string.h>
#include <stdio.h> #include <stdio.h>
#include <math.h> #include <math.h>
#include <stdlib.h> #include <stdlib.h>

Loading…
Cancel
Save