Browse Source

update: complete json parser

dev
dnomd343 2 years ago
parent
commit
f3bc8862fb
  1. 0
      include/loader.h
  2. 4
      src/CMakeLists.txt
  3. 2
      src/cleardns.c
  4. 2
      src/loader/CMakeLists.txt
  5. 7
      src/loader/config.c
  6. 75
      src/loader/parser.c

0
include/load.h → include/loader.h

4
src/CMakeLists.txt

@ -3,8 +3,8 @@ cmake_minimum_required(VERSION 2.8.12)
include_directories(${PROJECT_SOURCE_DIR}/include) include_directories(${PROJECT_SOURCE_DIR}/include)
include_directories(${PROJECT_SOURCE_DIR}/include/utils) include_directories(${PROJECT_SOURCE_DIR}/include/utils)
add_subdirectory(load) add_subdirectory(loader)
add_subdirectory(utils) add_subdirectory(utils)
add_executable(cleardns cleardns.c dnsproxy.c common.c overture.c) add_executable(cleardns cleardns.c dnsproxy.c common.c overture.c)
target_link_libraries(cleardns load utils) target_link_libraries(cleardns loader utils)

2
src/cleardns.c

@ -1,5 +1,5 @@
#include <stdio.h> #include <stdio.h>
#include "load.h" #include "loader.h"
#include "logger.h" #include "logger.h"
#include "common.h" #include "common.h"
#include "dnsproxy.h" #include "dnsproxy.h"

2
src/load/CMakeLists.txt → src/loader/CMakeLists.txt

@ -1,3 +1,3 @@
cmake_minimum_required(VERSION 2.8.12) cmake_minimum_required(VERSION 2.8.12)
add_library(load load.c json.c) add_library(loader config.c parser.c)

7
src/load/load.c → src/loader/config.c

@ -1,4 +1,4 @@
#include "load.h" #include "loader.h"
#include "common.h" #include "common.h"
#include "logger.h" #include "logger.h"
#include "structure.h" #include "structure.h"
@ -82,4 +82,9 @@ void load_config(const char *config_file) {
log_info("Configure load success"); log_info("Configure load success");
config_dump(config); config_dump(config);
// TODO: load into process
// TODO: free config struct
} }

75
src/load/json.c → src/loader/parser.c

@ -1,6 +1,6 @@
#include <string.h> #include <string.h>
#include <stdlib.h> #include <stdlib.h>
#include "load.h" #include "loader.h"
#include "cJSON.h" #include "cJSON.h"
#include "common.h" #include "common.h"
#include "logger.h" #include "logger.h"
@ -41,7 +41,7 @@ char** json_string_list_value(char *key, cJSON *json, char **string_list) {
string_list = string_list_append(string_list, json->valuestring); string_list = string_list_append(string_list, json->valuestring);
json = json->next; // next field json = json->next; // next field
} }
} else { } else if (!cJSON_IsNull(json)) {
log_fatal("`%s` must be array or string", key); log_fatal("`%s` must be array or string", key);
} }
return string_list; return string_list;
@ -59,7 +59,7 @@ uint32_t** json_uint32_list_value(char *key, cJSON *json, uint32_t **uint32_list
uint32_list = uint32_list_append(uint32_list, json->valueint); uint32_list = uint32_list_append(uint32_list, json->valueint);
json = json->next; // next field json = json->next; // next field
} }
} else { } else if (!cJSON_IsNull(json)) {
log_fatal("`%s` must be array or number", key); log_fatal("`%s` must be array or number", key);
} }
return uint32_list; return uint32_list;
@ -73,6 +73,10 @@ uint8_t json_bool_value(char *key, cJSON *json) { // json bool value -> bool
} }
void json_cache_parser(cache_config *config, cJSON *json) { // cache options parser void json_cache_parser(cache_config *config, cJSON *json) { // cache options parser
if (!cJSON_IsObject(json)) {
log_fatal("`cache` must be object");
}
json = json->child;
while (json != NULL) { while (json != NULL) {
if (!strcmp(json->string, "size")) { if (!strcmp(json->string, "size")) {
config->size = json_int_value("cache.size", json); config->size = json_int_value("cache.size", json);
@ -87,7 +91,52 @@ void json_cache_parser(cache_config *config, cJSON *json) { // cache options par
} }
} }
void json_upstream_parser(char *caption, upstream_config *config, cJSON *json) {
if (!cJSON_IsObject(json)) {
log_fatal("`%s` must be object", caption);
}
char *key_name;
json = json->child;
while (json != NULL) {
if (!strcmp(json->string, "port")) {
key_name = string_join(caption, ".port");
config->port = json_int_value(key_name, json);
free(key_name);
}
if (!strcmp(json->string, "verify")) {
key_name = string_join(caption, ".verify");
config->verify = json_bool_value(key_name, json);
free(key_name);
}
if (!strcmp(json->string, "parallel")) {
key_name = string_join(caption, ".parallel");
config->parallel = json_bool_value(key_name, json);
free(key_name);
}
if (!strcmp(json->string, "bootstrap")) {
key_name = string_join(caption, ".bootstrap");
config->bootstrap = json_string_list_value(key_name, json, config->bootstrap);
free(key_name);
}
if (!strcmp(json->string, "fallback")) {
key_name = string_join(caption, ".fallback");
config->fallback = json_string_list_value(key_name, json, config->fallback);
free(key_name);
}
if (!strcmp(json->string, "primary")) {
key_name = string_join(caption, ".primary");
config->primary = json_string_list_value(key_name, json, config->primary);
free(key_name);
}
json = json->next; // next field
}
}
void json_diverter_parser(diverter_config *config, cJSON *json) { // diverter options parser void json_diverter_parser(diverter_config *config, cJSON *json) { // diverter options parser
if (!cJSON_IsObject(json)) {
log_fatal("`diverter` must be object");
}
json = json->child;
while (json != NULL) { while (json != NULL) {
if (!strcmp(json->string, "port")) { if (!strcmp(json->string, "port")) {
config->port = json_int_value("diverter.port", json); config->port = json_int_value("diverter.port", json);
@ -106,6 +155,10 @@ void json_diverter_parser(diverter_config *config, cJSON *json) { // diverter op
} }
void json_adguard_parser(adguard_config *config, cJSON *json) { // adguard options parser void json_adguard_parser(adguard_config *config, cJSON *json) { // adguard options parser
if (!cJSON_IsObject(json)) {
log_fatal("`adguard` must be array");
}
json = json->child;
while (json != NULL) { while (json != NULL) {
if (!strcmp(json->string, "port")) { if (!strcmp(json->string, "port")) {
config->port = json_int_value("adguard.port", json); config->port = json_int_value("adguard.port", json);
@ -135,17 +188,19 @@ void json_config_parser(cleardns_config *config, const char *config_file) { // J
config->port = json_int_value("port", json); config->port = json_int_value("port", json);
} }
if (!strcmp(json->string, "cache")) { if (!strcmp(json->string, "cache")) {
json_cache_parser(&config->cache, json->child); json_cache_parser(&config->cache, json);
}
if (!strcmp(json->string, "domestic")) {
json_upstream_parser("domestic", &config->domestic, json);
}
if (!strcmp(json->string, "foreign")) {
json_upstream_parser("foreign", &config->foreign, json);
} }
// domestic
// foreign
if (!strcmp(json->string, "diverter")) { if (!strcmp(json->string, "diverter")) {
json_diverter_parser(&config->diverter, json->child); json_diverter_parser(&config->diverter, json);
} }
if (!strcmp(json->string, "adguard")) { if (!strcmp(json->string, "adguard")) {
json_adguard_parser(&config->adguard, json->child); json_adguard_parser(&config->adguard, json);
} }
if (!strcmp(json->string, "reject")) { if (!strcmp(json->string, "reject")) {
config->reject = json_uint32_list_value("reject", json, config->reject); config->reject = json_uint32_list_value("reject", json, config->reject);
Loading…
Cancel
Save