Browse Source

feat: loader module

dev
dnomd343 2 years ago
parent
commit
ecb996c63d
  1. 9
      include/loader/config.h
  2. 6
      include/loader/loader.h
  3. 8
      include/loader/parser.h
  4. 3
      src/CMakeLists.txt
  5. 15
      src/loader/config.c
  6. 14
      src/loader/loader.c
  7. 36
      src/loader/parser.c

9
include/loader.h → include/loader/config.h

@ -1,5 +1,5 @@
#ifndef _LOAD_H_ #ifndef _CONFIG_H_
#define _LOAD_H_ #define _CONFIG_H_
#include "common.h" #include "common.h"
@ -44,7 +44,8 @@ typedef struct {
char **ttl; char **ttl;
} cleardns_config; } cleardns_config;
void load_config(const char *config_file); cleardns_config* config_init();
void json_config_parser(cleardns_config *config, const char *config_file); void config_dump(cleardns_config *config);
void config_free(cleardns_config *config);
#endif #endif

6
include/loader/loader.h

@ -0,0 +1,6 @@
#ifndef _LOADER_H_
#define _LOADER_H_
void load_config(const char *config_file);
#endif

8
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

3
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)
include_directories(${PROJECT_SOURCE_DIR}/include/utils) include_directories(${PROJECT_SOURCE_DIR}/include/utils)
include_directories(${PROJECT_SOURCE_DIR}/include/loader)
add_subdirectory(loader)
add_subdirectory(utils) add_subdirectory(utils)
add_subdirectory(loader)
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 loader utils) target_link_libraries(cleardns loader utils)

15
src/loader/config.c

@ -1,5 +1,5 @@
#include "loader.h"
#include "common.h" #include "common.h"
#include "config.h"
#include "logger.h" #include "logger.h"
#include "structure.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->adguard.password);
free(config); 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);
}

14
src/loader/loader.c

@ -1,5 +1,19 @@
#include "loader.h" #include "loader.h"
#include "logger.h"
#include "config.h"
#include "parser.h"
void load_dnsproxy() { 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);
}

36
src/loader/parser.c

@ -1,9 +1,9 @@
#include <string.h> #include <string.h>
#include <stdlib.h> #include <stdlib.h>
#include "loader.h"
#include "cJSON.h" #include "cJSON.h"
#include "common.h" #include "common.h"
#include "logger.h" #include "logger.h"
#include "config.h"
#include "structure.h" #include "structure.h"
int json_int_value(char *key, cJSON *json) { // json int or string value -> int 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 void json_config_parser(cleardns_config *config, const char *config_content) { // JSON format configure
char *config_content = read_file(config_file);
cJSON *json = cJSON_Parse(config_content); cJSON *json = cJSON_Parse(config_content);
if (json == NULL) { if (json == NULL) {
log_fatal("JSON configure `%s` format error", config_file); log_fatal("JSON configure format error");
} }
json = json->child; json = json->child;
while (json != NULL) { 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 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");
}

Loading…
Cancel
Save