Browse Source

feat: json configure for overture

dev
dnomd343 2 years ago
parent
commit
1917fd2ae1
  1. 1
      include/overture.h
  2. 8
      src/cleardns.c
  3. 60
      src/overture.c
  4. 2
      src/utils/logger.c

1
include/overture.h

@ -14,5 +14,6 @@ typedef struct {
overture* overture_init(int port); overture* overture_init(int port);
void overture_dump(overture *info); void overture_dump(overture *info);
char* overture_gen_config(overture *info);
#endif #endif

8
src/cleardns.c

@ -57,8 +57,16 @@ int main(int argc, char *argv[]) { // ClearDNS server
overture *diverter = overture_init(DIVERTER_PORT); 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); overture_dump(diverter);
char *config = overture_gen_config(diverter);
log_info("\n%s", config);
// 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) {

60
src/overture.c

@ -1,7 +1,10 @@
#include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include "overture.h" #include "overture.h"
#include "common.h" #include "common.h"
#include "logger.h" #include "logger.h"
#include "cJSON.h"
#include "str.h"
overture* overture_init(int port) { // init overture options overture* overture_init(int port) { // init overture options
overture *info = (overture*)malloc(sizeof(overture)); 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 foreign domain file -> %s", info->foreign_domain_file);
log_debug("Overture domestic domain file -> %s", info->domestic_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;
}

2
src/utils/logger.c

@ -22,6 +22,7 @@ static const char *log_color[] = {
"\x1b[95m", // fatal "\x1b[95m", // fatal
}; };
// TODO: add ms field
void log_printf(int level, const char *fmt, ...) { void log_printf(int level, const char *fmt, ...) {
if (level < LOG_LEVEL) { // skip low log level if (level < LOG_LEVEL) { // skip low log level
return; return;
@ -44,6 +45,7 @@ void log_printf(int level, const char *fmt, ...) {
} }
} }
// TODO: add ms field
void log_perror(char *prefix) { void log_perror(char *prefix) {
time_t t; time_t t;
time(&t); time(&t);

Loading…
Cancel
Save