Browse Source

feat: load default configure

dev
dnomd343 2 years ago
parent
commit
f326d5b008
  1. 6
      include/loader/default.h
  2. 2
      src/cleardns.c
  3. 2
      src/loader/CMakeLists.txt
  4. 63
      src/loader/default.c
  5. 6
      src/loader/loader.c
  6. 11
      src/loader/parser.c

6
include/loader/default.h

@ -0,0 +1,6 @@
#ifndef DEFAULT_H_
#define DEFAULT_H_
void load_default_config(const char *file);
#endif

2
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);

2
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)

63
src/loader/default.c

@ -0,0 +1,63 @@
#include <stdlib.h>
#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);
}

6
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
}

11
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");
}

Loading…
Cancel
Save