diff --git a/include/common.h b/include/common.h index 92db949..5f5db0d 100644 --- a/include/common.h +++ b/include/common.h @@ -21,6 +21,8 @@ #define DOMESTIC_PORT 4053 #define FOREIGN_PORT 6053 +#define DIVERTER_TIMEOUT 6 + #define ADGUARD_USER "admin" #define ADGUARD_PASSWD "adguard" @@ -49,8 +51,15 @@ char* uint32_to_string(uint32_t number); char* gen_bcrypt(const char *data); int run_command(const char *command); +void create_folder(const char *folder); char* string_init(const char *str); char* string_join(const char *base, const char *add); +#include "cJSON.h" + +char* to_json(const char *config_file); +cJSON* json_field_get(cJSON *entry, const char *field); +void json_field_replace(cJSON *entry, const char *field, cJSON *content); + #endif diff --git a/include/utils/toJSON.h b/include/utils/toJSON.h deleted file mode 100644 index 3ea5552..0000000 --- a/include/utils/toJSON.h +++ /dev/null @@ -1,6 +0,0 @@ -#ifndef _TO_JSON_H_ -#define _TO_JSON_H_ - -char* to_json(const char *config_file); - -#endif diff --git a/src/applet/adguard.c b/src/applet/adguard.c index 070151f..1c27f64 100644 --- a/src/applet/adguard.c +++ b/src/applet/adguard.c @@ -1,18 +1,19 @@ #include -#include #include "cJSON.h" #include "common.h" #include "logger.h" -#include "toJSON.h" #include "adguard.h" +void adguard_dump(adguard *info); +char *adguard_config(adguard *info, const char *raw_config); + adguard* adguard_init() { adguard *info = (adguard *)malloc(sizeof(adguard)); char *port_str = uint32_to_string(DIVERTER_PORT); info->debug = FALSE; info->dns_port = DNS_PORT; info->web_port = ADGUARD_PORT; - info->upstream = string_join("127.0.0.1:", port_str); + info->upstream = string_join("127.0.0.1:", port_str); // default upstream info->username = string_init(ADGUARD_USER); info->password = string_init(ADGUARD_PASSWD); free(port_str); @@ -28,38 +29,20 @@ void adguard_dump(adguard *info) { // show adguard info in debug log log_debug("AdGuardHome password -> %s", info->password); } -void json_field_replace(cJSON *entry, const char *field, cJSON *content) { - if (!cJSON_ReplaceItemInObject(entry, field, content)) { // field not exist - cJSON_AddItemToObject(entry, field, content); // add new field - } -} - -cJSON* json_field_get(cJSON *entry, const char *field) { - cJSON *sub = entry->child; - while (sub != NULL) { - if (!strcmp(sub->string, field)) { - return sub; - } - sub = sub->next; // next field - } - cJSON *new = cJSON_CreateObject(); - cJSON_AddItemToObject(entry, field, new); - return new; -} - -char *adguard_config(adguard *info, const char *raw_config) { +char *adguard_config(adguard *info, const char *raw_config) { // modify adguard configure cJSON *json = cJSON_Parse(raw_config); if (json == NULL) { log_fatal("AdGuardHome configure error"); } - // TODO: password use bcencrypt cJSON *user_config = cJSON_CreateObject(); cJSON *users_config = cJSON_CreateArray(); + char *password = gen_bcrypt(info->password); cJSON_AddItemToObject(user_config, "name", cJSON_CreateString(info->username)); - cJSON_AddItemToObject(user_config, "password", cJSON_CreateString(info->password)); + cJSON_AddItemToObject(user_config, "password", cJSON_CreateString(password)); cJSON_AddItemToArray(users_config, user_config); json_field_replace(json, "users", users_config); + free(password); cJSON *dns = json_field_get(json, "dns"); cJSON *upstream = cJSON_CreateArray(); @@ -84,6 +67,7 @@ process* adguard_load(adguard *info, const char *dir) { log_fatal("Invalid web port `%u`", info->web_port); } + create_folder(dir); char *adguard_config_ret; char *adguard_config_file = string_join(dir, "AdGuardHome.yaml"); char *adguard_config_raw = to_json(adguard_config_file); @@ -108,5 +92,6 @@ process* adguard_load(adguard *info, const char *dir) { process_add_arg(proc, "--verbose"); // adguard enable debug mode } free(port_str); + log_info("AdGuardHome load success"); return proc; } diff --git a/src/applet/dnsproxy.c b/src/applet/dnsproxy.c index e13149a..d1cecd5 100644 --- a/src/applet/dnsproxy.c +++ b/src/applet/dnsproxy.c @@ -65,9 +65,10 @@ process* dnsproxy_load(const char *caption, dnsproxy *info, const char *file) { log_fatal("%s without primary dns server", caption); } + create_folder(WORK_DIR); char *config = dnsproxy_config(info); // string config (JSON format) char *config_file = string_join(WORK_DIR, file); - save_file(config_file, config); + save_file(config_file, config); // load dnsproxy configure free(config_file); free(config); @@ -78,6 +79,7 @@ process* dnsproxy_load(const char *caption, dnsproxy *info, const char *file) { process_add_arg(proc, "--verbose"); // dnsproxy enable debug mode } free(option); + log_info("%s load success", caption); return proc; } diff --git a/src/applet/overture.c b/src/applet/overture.c index 518f27f..db5061b 100644 --- a/src/applet/overture.c +++ b/src/applet/overture.c @@ -12,7 +12,7 @@ overture* overture_init() { // init overture options overture *info = (overture *)malloc(sizeof(overture)); info->port = DIVERTER_PORT; info->debug = FALSE; - info->timeout = 6; // default timeout -> 6s + info->timeout = DIVERTER_TIMEOUT; // default timeout -> 6s info->ttl_file = NULL; info->host_file = NULL; info->foreign_port = FOREIGN_PORT; @@ -51,6 +51,7 @@ process* overture_load(overture *info, const char *file) { log_fatal("Timeout of overture with invalid value 0"); } + create_folder(WORK_DIR); char *config = overture_config(info); // string config (JSON format) char *config_file = string_join(WORK_DIR, file); save_file(config_file, config); @@ -63,6 +64,7 @@ process* overture_load(overture *info, const char *file) { if (info->debug) { process_add_arg(proc, "-v"); // overture enable debug mode } + log_info("Overture load success"); return proc; } @@ -124,7 +126,6 @@ char* overture_config(overture *info) { // generate json configure from overture } cJSON_AddStringToObject(host_file, "finder", "regex-list"); cJSON_AddItemToObject(config, "hostsFile", host_file); - if (info->ttl_file != NULL) { cJSON_AddStringToObject(config, "domainTTLFile", info->ttl_file); } diff --git a/src/cleardns.c b/src/cleardns.c index 93155bc..3302a52 100644 --- a/src/cleardns.c +++ b/src/cleardns.c @@ -7,7 +7,6 @@ #include "dnsproxy.h" #include "overture.h" #include "structure.h" -#include "toJSON.h" #include "adguard.h" //#include @@ -41,9 +40,6 @@ int main(int argc, char *argv[]) { // ClearDNS server LOG_LEVEL = LOG_DEBUG; log_info("ClearDNS server start (%s)", VERSION); - char *ret = gen_bcrypt("dnomd343"); - log_info("%s", ret); - // load_config("test.json"); // @@ -94,8 +90,9 @@ int main(int argc, char *argv[]) { // ClearDNS server // log_info("cwd -> %s", p->cwd); -// overture *diverter = overture_init(DIVERTER_PORT); +// overture *diverter = overture_init(); // +// diverter->port = 5454; // diverter->timeout = 8; // diverter->domestic_ip_file = "china-ip.txt"; // diverter->domestic_domain_file = "chinalist.txt"; @@ -113,20 +110,20 @@ int main(int argc, char *argv[]) { // ClearDNS server // 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); + 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; diff --git a/src/common.c b/src/common.c index c542bb7..8a5f81e 100644 --- a/src/common.c +++ b/src/common.c @@ -77,6 +77,13 @@ char* gen_bcrypt(const char *data) { return hash; } +void create_folder(const char *folder) { + log_debug("Create folder -> %s", folder); + char *command = string_join("mkdir -p ", folder); + system(command); + free(command); +} + void save_file(const char *file, const char *content) { // save into file log_debug("Write into `%s` -> \n%s", file, content); FILE* fp = fopen(file , "w"); diff --git a/src/utils/CMakeLists.txt b/src/utils/CMakeLists.txt index 192d2e7..901bb2a 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 toJSON.c logger.c process.c structure.c) +add_library(utils cJSON.c json.c logger.c process.c structure.c) diff --git a/src/utils/toJSON.c b/src/utils/json.c similarity index 58% rename from src/utils/toJSON.c rename to src/utils/json.c index 12bc90a..e6ae4a2 100644 --- a/src/utils/toJSON.c +++ b/src/utils/json.c @@ -1,7 +1,7 @@ #include #include #include -#include "toJSON.h" +#include "cJSON.h" #include "logger.h" #include "common.h" @@ -17,6 +17,7 @@ char* to_json(const char *file) { sprintf(to_json_cmd, "toJSON %s > %s", file, output_file); log_debug("JSON format command -> `%s`", to_json_cmd); if (run_command(to_json_cmd)) { // toJSON return non-zero code + // TODO: try remove output file return NULL; // convert failed } free(to_json_cmd); @@ -28,3 +29,22 @@ char* to_json(const char *file) { free(rm_cmd); return json_content; } + +void json_field_replace(cJSON *entry, const char *field, cJSON *content) { + if (!cJSON_ReplaceItemInObject(entry, field, content)) { // field not exist + cJSON_AddItemToObject(entry, field, content); // add new field + } +} + +cJSON* json_field_get(cJSON *entry, const char *field) { + cJSON *sub = entry->child; + while (sub != NULL) { + if (!strcmp(sub->string, field)) { + return sub; + } + sub = sub->next; // next field + } + cJSON *new = cJSON_CreateObject(); + cJSON_AddItemToObject(entry, field, new); + return new; +}