From 2e52b679b705b18336575f0b24dfa5bded0205d4 Mon Sep 17 00:00:00 2001 From: dnomd343 Date: Tue, 13 Sep 2022 18:57:25 +0800 Subject: [PATCH] feat: load function of adguard --- include/applet/adguard.h | 4 ++++ include/applet/overture.h | 2 +- include/common.h | 1 + src/applet/adguard.c | 39 +++++++++++++++++++++++++++++- src/applet/overture.c | 50 +++++++++++++++++++++------------------ src/common.c | 6 +++++ 6 files changed, 77 insertions(+), 25 deletions(-) diff --git a/include/applet/adguard.h b/include/applet/adguard.h index 9d8af96..5c749c9 100644 --- a/include/applet/adguard.h +++ b/include/applet/adguard.h @@ -2,6 +2,7 @@ #define _ADGUARD_H_ #include +#include "process.h" typedef struct { uint8_t debug; // bool value @@ -12,4 +13,7 @@ typedef struct { char *password; } adguard; +adguard* adguard_init(); +process* adguard_load(adguard *info, const char *dir); + #endif diff --git a/include/applet/overture.h b/include/applet/overture.h index 610e148..1c4f046 100644 --- a/include/applet/overture.h +++ b/include/applet/overture.h @@ -19,7 +19,7 @@ typedef struct { char *domestic_domain_file; } overture; -overture* overture_init(uint16_t port); +overture* overture_init(); process* overture_load(overture *info, const char *file); #endif diff --git a/include/common.h b/include/common.h index aa12ca1..b70aa51 100644 --- a/include/common.h +++ b/include/common.h @@ -44,6 +44,7 @@ void string_list_debug(char *describe, char **string_list); void uint32_list_debug(char *describe, uint32_t **uint32_list); uint8_t check_port(uint16_t port); +char* uint32_to_string(uint32_t number); char* string_init(const char *str); char* string_join(const char *base, const char *add); diff --git a/src/applet/adguard.c b/src/applet/adguard.c index ae1d237..c0023de 100644 --- a/src/applet/adguard.c +++ b/src/applet/adguard.c @@ -1,14 +1,51 @@ #include #include "common.h" +#include "logger.h" #include "adguard.h" 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:", "5353"); // TODO: use DIVERTER_PORT + info->upstream = string_join("127.0.0.1:", port_str); info->username = string_init(ADGUARD_USER); info->password = string_init(ADGUARD_PASSWD); + free(port_str); return info; } + +void adguard_dump(adguard *info) { // show adguard info in debug log + log_debug("AdGuardHome debug -> %s", show_bool(info->debug)); + log_debug("AdGuardHome dns port -> %u", info->dns_port); + log_debug("AdGuardHome web port -> %u", info->web_port); + log_debug("AdGuardHome upstream -> %s", info->upstream); + log_debug("AdGuardHome username -> %s", info->username); + log_debug("AdGuardHome password -> %s", info->password); +} + +process* adguard_load(adguard *info, const char *dir) { + adguard_dump(info); + if (!check_port(info->dns_port)) { // invalid dns port + log_fatal("Invalid dns port `%u`", info->dns_port); + } + if (!check_port(info->web_port)) { // invalid web port + log_fatal("Invalid web port `%u`", info->web_port); + } + + // TODO: modify configure file + // inject -> dns_port / upstream / username / password + + process *proc = process_init("AdGuardHome", ADGUARD_BIN); + char *port_str = uint32_to_string(info->web_port); + process_add_arg(proc, "--work-dir"); + process_add_arg(proc, dir); + process_add_arg(proc, "--port"); + process_add_arg(proc, port_str); + if (info->debug) { + process_add_arg(proc, "--verbose"); // adguard enable debug mode + } + free(port_str); + return proc; +} diff --git a/src/applet/overture.c b/src/applet/overture.c index 0e71f4d..518f27f 100644 --- a/src/applet/overture.c +++ b/src/applet/overture.c @@ -1,4 +1,3 @@ -#include // TODO: use int to string instead of sprintf #include #include "cJSON.h" #include "common.h" @@ -6,13 +5,12 @@ #include "overture.h" #include "structure.h" - void overture_dump(overture *info); char* overture_config(overture *info); -overture* overture_init(uint16_t port) { // init overture options +overture* overture_init() { // init overture options overture *info = (overture *)malloc(sizeof(overture)); - info->port = port; + info->port = DIVERTER_PORT; info->debug = FALSE; info->timeout = 6; // default timeout -> 6s info->ttl_file = NULL; @@ -20,10 +18,10 @@ overture* overture_init(uint16_t port) { // init overture options info->foreign_port = FOREIGN_PORT; info->domestic_port = DOMESTIC_PORT; info->reject_type = uint32_list_init(); - info->foreign_ip_file = "/dev/null"; - info->domestic_ip_file = "/dev/null"; - info->foreign_domain_file = "/dev/null"; - info->domestic_domain_file = "/dev/null"; + info->foreign_ip_file = string_init("/dev/null"); + info->domestic_ip_file = string_init("/dev/null"); + info->foreign_domain_file = string_init("/dev/null"); + info->domestic_domain_file = string_init("/dev/null"); return info; } @@ -45,12 +43,14 @@ void overture_dump(overture *info) { // show overture info in debug log } process* overture_load(overture *info, const char *file) { - - // TODO: check port (1 ~ 65535) - // TODO: check timeout not zero - // TODO: check whether file exist - overture_dump(info); + if (!check_port(info->port)) { // invalid server port + log_fatal("Invalid port `%u`", info->port); + } + if (info->timeout == 0) { + log_fatal("Timeout of overture with invalid value 0"); + } + char *config = overture_config(info); // string config (JSON format) char *config_file = string_join(WORK_DIR, file); save_file(config_file, config); @@ -67,16 +67,15 @@ process* overture_load(overture *info, const char *file) { } char* overture_config(overture *info) { // generate json configure from overture options + char *port_str; cJSON *config = cJSON_CreateObject(); - char port_str[12]; // 32-bits (MAX_LEN -> -2147483648 -> 12-bytes) - sprintf(port_str, "%u", info->port); - char *bind_addr = string_join(":", port_str); - sprintf(port_str, "%u", info->foreign_port); - char *foreign_addr = string_join("127.0.0.1:", port_str); - sprintf(port_str, "%u", info->domestic_port); - char *domestic_port = string_join("127.0.0.1:", port_str); + port_str = uint32_to_string(info->port); + char *bind_addr = string_join(":", port_str); cJSON_AddStringToObject(config, "bindAddress", bind_addr); + free(bind_addr); + free(port_str); + cJSON_AddFalseToObject(config, "onlyPrimaryDNS"); cJSON_AddFalseToObject(config, "ipv6UseAlternativeDNS"); cJSON_AddTrueToObject(config, "alternativeDNSConcurrent"); @@ -84,21 +83,29 @@ char* overture_config(overture *info) { // generate json configure from overture cJSON *primary = cJSON_CreateObject(); cJSON *primary_dns = cJSON_CreateArray(); + port_str = uint32_to_string(info->domestic_port); + char *domestic_port = string_join("127.0.0.1:", port_str); cJSON_AddStringToObject(primary, "name", "Domestic"); cJSON_AddStringToObject(primary, "address", domestic_port); cJSON_AddStringToObject(primary, "protocol", "udp"); cJSON_AddNumberToObject(primary, "timeout", info->timeout); cJSON_AddItemToArray(primary_dns, primary); cJSON_AddItemToObject(config, "primaryDNS", primary_dns); + free(domestic_port); + free(port_str); cJSON *alternative = cJSON_CreateObject(); cJSON *alternative_dns = cJSON_CreateArray(); + port_str = uint32_to_string(info->foreign_port); + char *foreign_addr = string_join("127.0.0.1:", port_str); cJSON_AddStringToObject(alternative, "name", "Foreign"); cJSON_AddStringToObject(alternative, "address", foreign_addr); cJSON_AddStringToObject(alternative, "protocol", "udp"); cJSON_AddNumberToObject(alternative, "timeout", info->timeout); cJSON_AddItemToArray(alternative_dns, alternative); cJSON_AddItemToObject(config, "alternativeDNS", alternative_dns); + free(foreign_addr); + free(port_str); cJSON *ip_file = cJSON_CreateObject(); cJSON_AddStringToObject(ip_file, "primary", info->domestic_ip_file); @@ -132,8 +139,5 @@ char* overture_config(overture *info) { // generate json configure from overture char *config_str = cJSON_Print(config); cJSON_Delete(config); // free json object - free(domestic_port); - free(foreign_addr); - free(bind_addr); return config_str; } diff --git a/src/common.c b/src/common.c index bff9288..036e1c6 100644 --- a/src/common.c +++ b/src/common.c @@ -41,6 +41,12 @@ uint8_t check_port(uint16_t port) { return FALSE; } +char* uint32_to_string(uint32_t number) { + char to_str[11]; // 32-bits (MAX_LEN -> 4294967296 -> 10-bytes) + sprintf(to_str, "%u", number); + return string_init(to_str); +} + 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");