mirror of https://github.com/dnomd343/ClearDNS
dnomd343
2 years ago
8 changed files with 208 additions and 25 deletions
@ -0,0 +1,50 @@ |
|||
#ifndef _LOAD_H_ |
|||
#define _LOAD_H_ |
|||
|
|||
#include "common.h" |
|||
|
|||
typedef struct { |
|||
uint16_t port; |
|||
uint8_t verify; // bool value
|
|||
uint8_t parallel; // bool value
|
|||
char **bootstrap; |
|||
char **fallback; |
|||
char **primary; |
|||
} upstream_config; |
|||
|
|||
typedef struct { |
|||
uint16_t port; |
|||
char **gfwlist; |
|||
char **china_ip; |
|||
char **chinalist; |
|||
} diverter_config; |
|||
|
|||
typedef struct { |
|||
uint32_t size; |
|||
uint8_t enable; // bool value
|
|||
uint8_t optimistic; // bool value
|
|||
} cache_config; |
|||
|
|||
typedef struct { |
|||
uint16_t port; |
|||
uint8_t enable; |
|||
char *username; |
|||
char *password; |
|||
} adguard_config; |
|||
|
|||
typedef struct { |
|||
uint16_t port; |
|||
cache_config cache; |
|||
upstream_config domestic; |
|||
upstream_config foreign; |
|||
diverter_config diverter; |
|||
adguard_config adguard; |
|||
uint32_t **reject; |
|||
char **hosts; |
|||
char **ttl; |
|||
} cleardns_config; |
|||
|
|||
void load_config(const char *config_file); |
|||
void json_config_parser(cleardns_config *config, const char *config_file); |
|||
|
|||
#endif |
@ -0,0 +1,3 @@ |
|||
cmake_minimum_required(VERSION 2.8.12) |
|||
|
|||
add_library(load load.c json.c) |
@ -0,0 +1,22 @@ |
|||
#include <string.h> |
|||
#include "load.h" |
|||
#include "cJSON.h" |
|||
#include "common.h" |
|||
#include "logger.h" |
|||
|
|||
void json_config_parser(cleardns_config *config, const char *config_file) { // JSON format configure
|
|||
char *config_content = read_file(config_file); |
|||
cJSON *json = cJSON_Parse(config_content); |
|||
if (json == NULL) { |
|||
log_fatal("JSON format error"); |
|||
} |
|||
json = json->child; |
|||
while (json != NULL) { |
|||
if (!strcmp(json->string, "port")) { |
|||
// get dns port
|
|||
} |
|||
|
|||
json = json->next; // next field
|
|||
} |
|||
cJSON_free(json); // free JSON struct
|
|||
} |
@ -0,0 +1,85 @@ |
|||
#include "load.h" |
|||
#include "common.h" |
|||
#include "logger.h" |
|||
#include "structure.h" |
|||
|
|||
cleardns_config* config_init() { |
|||
cleardns_config *config = (cleardns_config *)malloc(sizeof(cleardns_config)); |
|||
config->port = DNS_PORT; |
|||
config->cache.size = 0; |
|||
config->cache.enable = FALSE; |
|||
config->cache.optimistic = FALSE; |
|||
|
|||
config->domestic.port = DOMESTIC_PORT; |
|||
config->domestic.verify = TRUE; |
|||
config->domestic.parallel = TRUE; |
|||
config->domestic.bootstrap = string_list_init(); |
|||
config->domestic.fallback = string_list_init(); |
|||
config->domestic.primary = string_list_init(); |
|||
|
|||
config->foreign.port = FOREIGN_PORT; |
|||
config->foreign.verify = TRUE; |
|||
config->foreign.parallel = TRUE; |
|||
config->foreign.bootstrap = string_list_init(); |
|||
config->foreign.fallback = string_list_init(); |
|||
config->foreign.primary = string_list_init(); |
|||
|
|||
config->diverter.port = DIVERTER_PORT; |
|||
config->diverter.gfwlist = string_list_init(); |
|||
config->diverter.china_ip = string_list_init(); |
|||
config->diverter.chinalist = string_list_init(); |
|||
|
|||
config->adguard.port = ADGUARD_PORT; |
|||
config->adguard.enable = TRUE; |
|||
config->adguard.username = ADGUARD_USER; |
|||
config->adguard.password = ADGUARD_PASSWD; |
|||
|
|||
config->reject = uint32_list_init(); |
|||
config->hosts = string_list_init(); |
|||
config->ttl = string_list_init(); |
|||
return config; |
|||
} |
|||
|
|||
void config_dump(cleardns_config *config) { |
|||
log_debug("DNS port -> %u", config->port); |
|||
log_debug("Cache size -> %u", config->cache.size); |
|||
log_debug("Cache enable -> %s", show_bool(config->cache.enable)); |
|||
log_debug("Cache optimistic -> %s", show_bool(config->cache.optimistic)); |
|||
|
|||
log_debug("Domestic port -> %u", config->domestic.port); |
|||
log_debug("Domestic verify -> %s", show_bool(config->domestic.verify)); |
|||
log_debug("Domestic parallel -> %s", show_bool(config->domestic.parallel)); |
|||
string_list_debug("Domestic bootstrap", config->domestic.bootstrap); |
|||
string_list_debug("Domestic fallback", config->domestic.fallback); |
|||
string_list_debug("Domestic primary", config->domestic.primary); |
|||
|
|||
log_debug("Foreign port -> %u", config->foreign.port); |
|||
log_debug("Foreign verify -> %s", show_bool(config->foreign.verify)); |
|||
log_debug("Foreign parallel -> %s", show_bool(config->foreign.parallel)); |
|||
string_list_debug("Foreign bootstrap", config->foreign.bootstrap); |
|||
string_list_debug("Foreign fallback", config->foreign.fallback); |
|||
string_list_debug("Foreign primary", config->foreign.primary); |
|||
|
|||
log_debug("Diverter port -> %u", config->diverter.port); |
|||
string_list_debug("Diverter gfwlist", config->diverter.gfwlist); |
|||
string_list_debug("Diverter china-ip", config->diverter.china_ip); |
|||
string_list_debug("Diverter chinalist", config->diverter.chinalist); |
|||
|
|||
log_debug("AdGuardHome port -> %u", config->adguard.port); |
|||
log_debug("AdGuardHome enable -> %s", show_bool(config->adguard.enable)); |
|||
log_debug("AdGuardHome username -> %s", config->adguard.username); |
|||
log_debug("AdGuardHome password -> %s", config->adguard.password); |
|||
|
|||
uint32_list_debug("DNS reject type", config->reject); |
|||
string_list_debug("Domain TTL", config->ttl); |
|||
string_list_debug("Hosts", config->hosts); |
|||
} |
|||
|
|||
void load_config(const char *config_file) { |
|||
cleardns_config *config = config_init(); |
|||
|
|||
json_config_parser(config, config_file); |
|||
|
|||
log_info("Configure load success"); |
|||
config_dump(config); |
|||
} |
Loading…
Reference in new issue