diff --git a/include/loader/default.h b/include/loader/default.h new file mode 100644 index 0000000..d2ce92e --- /dev/null +++ b/include/loader/default.h @@ -0,0 +1,6 @@ +#ifndef DEFAULT_H_ +#define DEFAULT_H_ + +void load_default_config(const char *file); + +#endif diff --git a/src/cleardns.c b/src/cleardns.c index 1902bc9..4f43175 100644 --- a/src/cleardns.c +++ b/src/cleardns.c @@ -67,6 +67,8 @@ int main(int argc, char *argv[]) { // ClearDNS server LOG_LEVEL = LOG_DEBUG; // TODO: dev only log_info("ClearDNS server start (%s)", VERSION); + create_folder(WORK_DIR); + load_config(config_file); free(config_file); diff --git a/src/loader/CMakeLists.txt b/src/loader/CMakeLists.txt index dd1ef74..90e826d 100644 --- a/src/loader/CMakeLists.txt +++ b/src/loader/CMakeLists.txt @@ -1,4 +1,4 @@ cmake_minimum_required(VERSION 2.8.12) -add_library(loader config.c loader.c parser.c) +add_library(loader config.c default.c loader.c parser.c) target_link_libraries(loader applet) diff --git a/src/loader/default.c b/src/loader/default.c new file mode 100644 index 0000000..2815373 --- /dev/null +++ b/src/loader/default.c @@ -0,0 +1,63 @@ +#include +#include "json.h" +#include "logger.h" +#include "system.h" +#include "sundry.h" + +#define DEFAULT_CONFIG "\ +port: 53\n\ +\n\ +diverter:\n\ + port: 5353\n\ +\n\ +adguard:\n\ + enable: true\n\ + port: 80\n\ + username: admin\n\ + password: cleardns\n\ +\n\ +cache:\n\ + enable: true\n\ + size: 4194304\n\ + optimistic: true\n\ +\n\ +domestic:\n\ + port: 4053\n\ + bootstrap: 223.5.5.5\n\ + primary:\n\ + - tls://dns.alidns.com\n\ + - https://doh.pub/dns-query\n\ + fallback:\n\ + - 223.6.6.6\n\ + - 119.29.29.29\n\ +\n\ +foreign:\n\ + port: 6053\n\ + bootstrap: 8.8.8.8\n\ + primary:\n\ + - tls://dns.google\n\ + - https://dns.cloudflare.com/dns-query\n\ + fallback:\n\ + - 1.1.1.1\n\ + - 8.8.4.4\n\ +" + +void load_default_config(const char *config_file) { + if (is_file_exist(config_file)) { + log_debug("Configure file exist -> skip load default"); + return; + } + log_info("Loading default configure file"); + char *config_content = NULL; + if (is_json_suffix(config_file)) { // convert to json format + char temp_file[] = "temp.yml"; + save_file(temp_file, DEFAULT_CONFIG); + config_content = to_json(temp_file); + remove_file(temp_file); + } + if (config_content == NULL) { + config_content = string_init(DEFAULT_CONFIG); + } + save_file(config_file, config_content); + free(config_content); +} diff --git a/src/loader/loader.c b/src/loader/loader.c index d19aadb..49b45c5 100644 --- a/src/loader/loader.c +++ b/src/loader/loader.c @@ -7,6 +7,8 @@ #include "dnsproxy.h" #include "constant.h" #include "structure.h" +#include "default.h" +#include "logger.h" struct cleardns loader; @@ -105,13 +107,11 @@ adguard* load_filter(cleardns_config *config) { void load_config(const char *config_file) { cleardns_config *config = config_init(); - // TODO: load default configure + load_default_config(config_file); config_parser(config, config_file); config_dump(config); - // TODO: mkdir -p ${WORK_DIR} - if (!config->adguard.enable) { config->diverter.port = config->port; // override diverter port by dns port } diff --git a/src/loader/parser.c b/src/loader/parser.c index 7c8f2fe..e69a76c 100644 --- a/src/loader/parser.c +++ b/src/loader/parser.c @@ -155,19 +155,18 @@ void config_parser(cleardns_config *config, const char *config_file) { // TODO: load custom script - char *config_content = read_file(config_file); + char *config_content; if (is_json_suffix(config_file)) { // JSON format log_info("Start JSON configure parser"); - cleardns_parser(config, config_content); // configure parser + config_content = read_file(config_file); } else { // YAML or TOML format log_info("Start configure parser"); - char *json_content = to_json(config_content); // convert to json format - if (json_content == NULL) { + config_content = to_json(config_file); // convert to json format + if (config_content == NULL) { log_fatal("Configure parser error"); } - cleardns_parser(config, json_content); // configure parser - free(json_content); } + cleardns_parser(config, config_content); // configure parser free(config_content); log_info("Configure parser success"); }