diff --git a/include/loader.h b/include/loader/config.h similarity index 84% rename from include/loader.h rename to include/loader/config.h index 0510fbe..347f06f 100644 --- a/include/loader.h +++ b/include/loader/config.h @@ -1,5 +1,5 @@ -#ifndef _LOAD_H_ -#define _LOAD_H_ +#ifndef _CONFIG_H_ +#define _CONFIG_H_ #include "common.h" @@ -44,7 +44,8 @@ typedef struct { char **ttl; } cleardns_config; -void load_config(const char *config_file); -void json_config_parser(cleardns_config *config, const char *config_file); +cleardns_config* config_init(); +void config_dump(cleardns_config *config); +void config_free(cleardns_config *config); #endif diff --git a/include/loader/loader.h b/include/loader/loader.h new file mode 100644 index 0000000..e578f45 --- /dev/null +++ b/include/loader/loader.h @@ -0,0 +1,6 @@ +#ifndef _LOADER_H_ +#define _LOADER_H_ + +void load_config(const char *config_file); + +#endif diff --git a/include/loader/parser.h b/include/loader/parser.h new file mode 100644 index 0000000..de25ad7 --- /dev/null +++ b/include/loader/parser.h @@ -0,0 +1,8 @@ +#ifndef _PARSER_H_ +#define _PARSER_H_ + +#include "config.h" + +void config_parser(cleardns_config *config, const char *config_file); + +#endif diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index ac65f3f..b7b86dc 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -2,9 +2,10 @@ cmake_minimum_required(VERSION 2.8.12) include_directories(${PROJECT_SOURCE_DIR}/include) include_directories(${PROJECT_SOURCE_DIR}/include/utils) +include_directories(${PROJECT_SOURCE_DIR}/include/loader) -add_subdirectory(loader) add_subdirectory(utils) +add_subdirectory(loader) add_executable(cleardns cleardns.c dnsproxy.c common.c overture.c) target_link_libraries(cleardns loader utils) diff --git a/src/loader/config.c b/src/loader/config.c index fb6d636..5f69079 100644 --- a/src/loader/config.c +++ b/src/loader/config.c @@ -1,5 +1,5 @@ -#include "loader.h" #include "common.h" +#include "config.h" #include "logger.h" #include "structure.h" @@ -95,16 +95,3 @@ void config_free(cleardns_config *config) { // free config struct of cleardns free(config->adguard.password); free(config); } - -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); - - // TODO: load into process - - config_free(config); -} diff --git a/src/loader/loader.c b/src/loader/loader.c index 2d99bbc..6712725 100644 --- a/src/loader/loader.c +++ b/src/loader/loader.c @@ -1,5 +1,19 @@ #include "loader.h" +#include "logger.h" +#include "config.h" +#include "parser.h" void load_dnsproxy() { } + +void load_config(const char *config_file) { + cleardns_config *config = config_init(); + config_parser(config, config_file); + + config_dump(config); + + // TODO: load into process + + config_free(config); +} diff --git a/src/loader/parser.c b/src/loader/parser.c index 26a0851..af7442e 100644 --- a/src/loader/parser.c +++ b/src/loader/parser.c @@ -1,9 +1,9 @@ #include #include -#include "loader.h" #include "cJSON.h" #include "common.h" #include "logger.h" +#include "config.h" #include "structure.h" int json_int_value(char *key, cJSON *json) { // json int or string value -> int @@ -176,11 +176,10 @@ void json_adguard_parser(adguard_config *config, cJSON *json) { // adguard optio } } -void json_config_parser(cleardns_config *config, const char *config_file) { // JSON format configure - char *config_content = read_file(config_file); +void json_config_parser(cleardns_config *config, const char *config_content) { // JSON format configure cJSON *json = cJSON_Parse(config_content); if (json == NULL) { - log_fatal("JSON configure `%s` format error", config_file); + log_fatal("JSON configure format error"); } json = json->child; while (json != NULL) { @@ -215,3 +214,32 @@ void json_config_parser(cleardns_config *config, const char *config_file) { // J } cJSON_free(json); // free JSON struct } + +void yaml_config_parser(cleardns_config *config, const char *config_content) { // YAML format configure + // TODO: change YAML -> JSON + char *json_content = string_init(config_content); // just demo for now + + json_config_parser(config, json_content); +} + +uint8_t is_json_suffix(const char *file_name) { + if (strlen(file_name) <= 5) { // `.json` + return FALSE; + } + if (!strcmp(file_name + strlen(file_name) - 5, ".json")) { + return TRUE; + } + return FALSE; +} + +void config_parser(cleardns_config *config, const char *config_file) { + char *config_content = read_file(config_file); + if (is_json_suffix(config_file)) { + log_info("Start JSON configure parser"); + json_config_parser(config, config_content); + } else { + log_info("Start YAML configure parser"); + yaml_config_parser(config, config_content); + } + log_info("Configure parser success"); +}