diff --git a/include/common/constant.h b/include/constant.h similarity index 95% rename from include/common/constant.h rename to include/constant.h index bbcc630..480843c 100644 --- a/include/common/constant.h +++ b/include/constant.h @@ -20,9 +20,10 @@ #define DNSPROXY_BIN "dnsproxy" #define OVERTURE_BIN "overture" #define ADGUARD_BIN "AdGuardHome" +#define ASSETS_PKG "/assets.tar.xz" -#define WORK_DIR "/etc/cleardns/" #define EXPOSE_DIR "/cleardns/" +#define WORK_DIR "/etc/cleardns/" #define ASSETS_DIR "/cleardns/assets/" #define ADGUARD_DIR "/cleardns/adguard/" diff --git a/include/utils/assets.h b/include/utils/assets.h new file mode 100644 index 0000000..d1814d7 --- /dev/null +++ b/include/utils/assets.h @@ -0,0 +1,6 @@ +#ifndef ASSETS_H_ +#define ASSETS_H_ + +void load_assets(); + +#endif diff --git a/src/cleardns.c b/src/cleardns.c index 0626768..2199168 100644 --- a/src/cleardns.c +++ b/src/cleardns.c @@ -10,6 +10,7 @@ #include "structure.h" #include "adguard.h" #include "system.h" +#include "assets.h" //#include //#include @@ -35,7 +36,7 @@ // fprintf(stderr, "\"\n"); //} - +// TODO: load `--debug`, `--config ...`, `--version` int main(int argc, char *argv[]) { // ClearDNS server @@ -43,13 +44,10 @@ int main(int argc, char *argv[]) { // ClearDNS server log_info("ClearDNS server start (%s)", VERSION); -// char **temp = string_list_init(); -// -// temp = string_list_append(temp, "a"); -// temp = string_list_append(temp, "b"); -// temp = string_list_append(temp, "c"); -// -// save_string_list("test.txt", temp); + // TODO: load assets -> extract `/assets.tar.xz` -> ${ASSETS_DIR}*.txt + + load_assets(); + return 0; load_config("test.json"); @@ -60,86 +58,6 @@ int main(int argc, char *argv[]) { // ClearDNS server adguard_load(loader.filter, ADGUARD_DIR); -// char **temp = string_list_init(); -// temp = string_list_append(temp, "123"); -// temp = string_list_append(temp, "abc"); -// temp = string_list_append(temp, "ok"); -// -// char *str = string_list_dump(temp); -// log_info("`%s`", str); -// free(str); -// string_list_free(temp); - -// uint32_t **temp = uint32_list_init(); -// temp = uint32_list_append(temp, 123); -// temp = uint32_list_append(temp, 456); -// temp = uint32_list_append(temp, 789); -// -// char *str = uint32_list_dump(temp); -// log_info("`%s`\n", str); -// free(str); -// uint32_list_free(temp); - - -// dnsproxy *domestic = dnsproxy_init(DOMESTIC_PORT); -// -// dnsproxy_add_bootstrap(domestic, "1.1.1.1"); -// dnsproxy_add_bootstrap(domestic, "8.8.8.8"); -// -// dnsproxy_add_primary(domestic, "223.5.5.5"); -// dnsproxy_add_primary(domestic, "tls://dns.pub"); -// -// dnsproxy_add_fallback(domestic, "tls://223.6.6.6"); -// dnsproxy_add_fallback(domestic, "tls://120.53.53.53"); -// -// domestic->verify = FALSE; -// domestic->parallel = FALSE; -// domestic->optimistic = TRUE; -// domestic->debug = TRUE; -// domestic->cache = 4194304; // 4MiB -// -// process *p = dnsproxy_load("Domestic", domestic, "domestic.json"); -// log_info("cmd -> %s", string_list_dump(p->cmd)); -// log_info("env -> %s", string_list_dump(p->env)); -// log_info("cwd -> %s", p->cwd); - - -// overture *diverter = overture_init(); -// -// diverter->port = 5454; -// diverter->timeout = 8; -// diverter->domestic_ip_file = "china-ip.txt"; -// diverter->domestic_domain_file = "chinalist.txt"; -// diverter->foreign_domain_file = "gfwlist.txt"; -// -// diverter->debug = TRUE; -// diverter->ttl_file = "domain_ttl.txt"; -// diverter->host_file = "hosts.txt"; -// diverter->reject_type = uint32_list_append(diverter->reject_type, 255); -// diverter->reject_type = uint32_list_append(diverter->reject_type, 254); -// -// process *p = overture_load(diverter, "overture.json"); -// log_info("cmd -> %s", string_list_dump(p->cmd)); -// log_info("env -> %s", string_list_dump(p->env)); -// log_info("cwd -> %s", p->cwd); - - -// adguard *filter = adguard_init(); -// -// filter->debug = TRUE; -// filter->dns_port = 54; -// filter->web_port = 8080; -// filter->upstream = "127.0.0.1:5454"; -// -// filter->username = "dnomd343"; -// filter->password = "password"; -// -// process *p = adguard_load(filter, "/cleardns/adguard/"); -// log_info("cmd -> %s", string_list_dump(p->cmd)); -// log_info("env -> %s", string_list_dump(p->env)); -// log_info("cwd -> %s", p->cwd); - - // int debug_mode = 0; // fprintf(stderr, "[ClearDNS] Server start.\n"); // for (char **p = argv; p < argv + argc; ++p) { diff --git a/src/loader/loader.c b/src/loader/loader.c index 6d9a946..d19aadb 100644 --- a/src/loader/loader.c +++ b/src/loader/loader.c @@ -104,10 +104,12 @@ adguard* load_filter(cleardns_config *config) { void load_config(const char *config_file) { cleardns_config *config = config_init(); + + // TODO: load default configure + config_parser(config, config_file); config_dump(config); - // TODO: extract assets file // TODO: mkdir -p ${WORK_DIR} if (!config->adguard.enable) { diff --git a/src/loader/parser.c b/src/loader/parser.c index d081dd1..7c8f2fe 100644 --- a/src/loader/parser.c +++ b/src/loader/parser.c @@ -152,6 +152,9 @@ void cleardns_parser(cleardns_config *config, const char *config_content) { // J } void config_parser(cleardns_config *config, const char *config_file) { + + // TODO: load custom script + char *config_content = read_file(config_file); if (is_json_suffix(config_file)) { // JSON format log_info("Start JSON configure parser"); diff --git a/src/utils/CMakeLists.txt b/src/utils/CMakeLists.txt index bab7775..bb1d68d 100644 --- a/src/utils/CMakeLists.txt +++ b/src/utils/CMakeLists.txt @@ -1,3 +1,3 @@ cmake_minimum_required(VERSION 2.8.12) -add_library(utils cJSON.c logger.c process.c) +add_library(utils cJSON.c assets.c logger.c process.c) diff --git a/src/utils/assets.c b/src/utils/assets.c new file mode 100644 index 0000000..01eafe6 --- /dev/null +++ b/src/utils/assets.c @@ -0,0 +1,40 @@ +#include +#include +#include +#include "logger.h" +#include "sundry.h" +#include "system.h" +#include "constant.h" + +void load_asset(const char *file); + +// TODO: assets update -> crontab task + +void load_assets() { + log_info("Start loading assets"); + create_folder(ASSETS_DIR); + load_asset(ASSET_GFW_LIST); + load_asset(ASSET_CHINA_IP); + load_asset(ASSET_CHINA_LIST); + log_info("Assets loaded complete"); +} + +void load_asset(const char *file) { + log_debug("Start extract `%s`", file); + char *output_file = string_join(ASSETS_DIR, file); + if (is_file_exist(output_file)) { + log_debug("Assets `%s` exist -> skip extract", file); + free(output_file); + return; + } + free(output_file); + + char *extract_cmd = (char *)malloc(strlen(ASSETS_PKG) + strlen(file) + strlen(ASSETS_DIR) + 15); + sprintf(extract_cmd, "tar xf %s ./%s -C %s", ASSETS_PKG, file, ASSETS_DIR); + if (run_command(extract_cmd)) { + log_warn("Extract asset `%s` failed", file); + } else { + log_info("Extract asset `%s` success", file); + } + free(extract_cmd); +}