Browse Source

feat: load function of adguard

dev
dnomd343 2 years ago
parent
commit
2e52b679b7
  1. 4
      include/applet/adguard.h
  2. 2
      include/applet/overture.h
  3. 1
      include/common.h
  4. 39
      src/applet/adguard.c
  5. 50
      src/applet/overture.c
  6. 6
      src/common.c

4
include/applet/adguard.h

@ -2,6 +2,7 @@
#define _ADGUARD_H_ #define _ADGUARD_H_
#include <stdint.h> #include <stdint.h>
#include "process.h"
typedef struct { typedef struct {
uint8_t debug; // bool value uint8_t debug; // bool value
@ -12,4 +13,7 @@ typedef struct {
char *password; char *password;
} adguard; } adguard;
adguard* adguard_init();
process* adguard_load(adguard *info, const char *dir);
#endif #endif

2
include/applet/overture.h

@ -19,7 +19,7 @@ typedef struct {
char *domestic_domain_file; char *domestic_domain_file;
} overture; } overture;
overture* overture_init(uint16_t port); overture* overture_init();
process* overture_load(overture *info, const char *file); process* overture_load(overture *info, const char *file);
#endif #endif

1
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); void uint32_list_debug(char *describe, uint32_t **uint32_list);
uint8_t check_port(uint16_t port); uint8_t check_port(uint16_t port);
char* uint32_to_string(uint32_t number);
char* string_init(const char *str); char* string_init(const char *str);
char* string_join(const char *base, const char *add); char* string_join(const char *base, const char *add);

39
src/applet/adguard.c

@ -1,14 +1,51 @@
#include <stdlib.h> #include <stdlib.h>
#include "common.h" #include "common.h"
#include "logger.h"
#include "adguard.h" #include "adguard.h"
adguard* adguard_init() { adguard* adguard_init() {
adguard *info = (adguard *)malloc(sizeof(adguard)); adguard *info = (adguard *)malloc(sizeof(adguard));
char *port_str = uint32_to_string(DIVERTER_PORT);
info->debug = FALSE; info->debug = FALSE;
info->dns_port = DNS_PORT; info->dns_port = DNS_PORT;
info->web_port = ADGUARD_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->username = string_init(ADGUARD_USER);
info->password = string_init(ADGUARD_PASSWD); info->password = string_init(ADGUARD_PASSWD);
free(port_str);
return info; 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;
}

50
src/applet/overture.c

@ -1,4 +1,3 @@
#include <stdio.h> // TODO: use int to string instead of sprintf
#include <stdlib.h> #include <stdlib.h>
#include "cJSON.h" #include "cJSON.h"
#include "common.h" #include "common.h"
@ -6,13 +5,12 @@
#include "overture.h" #include "overture.h"
#include "structure.h" #include "structure.h"
void overture_dump(overture *info); void overture_dump(overture *info);
char* overture_config(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)); overture *info = (overture *)malloc(sizeof(overture));
info->port = port; info->port = DIVERTER_PORT;
info->debug = FALSE; info->debug = FALSE;
info->timeout = 6; // default timeout -> 6s info->timeout = 6; // default timeout -> 6s
info->ttl_file = NULL; info->ttl_file = NULL;
@ -20,10 +18,10 @@ overture* overture_init(uint16_t port) { // init overture options
info->foreign_port = FOREIGN_PORT; info->foreign_port = FOREIGN_PORT;
info->domestic_port = DOMESTIC_PORT; info->domestic_port = DOMESTIC_PORT;
info->reject_type = uint32_list_init(); info->reject_type = uint32_list_init();
info->foreign_ip_file = "/dev/null"; info->foreign_ip_file = string_init("/dev/null");
info->domestic_ip_file = "/dev/null"; info->domestic_ip_file = string_init("/dev/null");
info->foreign_domain_file = "/dev/null"; info->foreign_domain_file = string_init("/dev/null");
info->domestic_domain_file = "/dev/null"; info->domestic_domain_file = string_init("/dev/null");
return info; 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) { 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); 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 = overture_config(info); // string config (JSON format)
char *config_file = string_join(WORK_DIR, file); char *config_file = string_join(WORK_DIR, file);
save_file(config_file, config); 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* overture_config(overture *info) { // generate json configure from overture options
char *port_str;
cJSON *config = cJSON_CreateObject(); 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); cJSON_AddStringToObject(config, "bindAddress", bind_addr);
free(bind_addr);
free(port_str);
cJSON_AddFalseToObject(config, "onlyPrimaryDNS"); cJSON_AddFalseToObject(config, "onlyPrimaryDNS");
cJSON_AddFalseToObject(config, "ipv6UseAlternativeDNS"); cJSON_AddFalseToObject(config, "ipv6UseAlternativeDNS");
cJSON_AddTrueToObject(config, "alternativeDNSConcurrent"); cJSON_AddTrueToObject(config, "alternativeDNSConcurrent");
@ -84,21 +83,29 @@ char* overture_config(overture *info) { // generate json configure from overture
cJSON *primary = cJSON_CreateObject(); cJSON *primary = cJSON_CreateObject();
cJSON *primary_dns = cJSON_CreateArray(); 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, "name", "Domestic");
cJSON_AddStringToObject(primary, "address", domestic_port); cJSON_AddStringToObject(primary, "address", domestic_port);
cJSON_AddStringToObject(primary, "protocol", "udp"); cJSON_AddStringToObject(primary, "protocol", "udp");
cJSON_AddNumberToObject(primary, "timeout", info->timeout); cJSON_AddNumberToObject(primary, "timeout", info->timeout);
cJSON_AddItemToArray(primary_dns, primary); cJSON_AddItemToArray(primary_dns, primary);
cJSON_AddItemToObject(config, "primaryDNS", primary_dns); cJSON_AddItemToObject(config, "primaryDNS", primary_dns);
free(domestic_port);
free(port_str);
cJSON *alternative = cJSON_CreateObject(); cJSON *alternative = cJSON_CreateObject();
cJSON *alternative_dns = cJSON_CreateArray(); 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, "name", "Foreign");
cJSON_AddStringToObject(alternative, "address", foreign_addr); cJSON_AddStringToObject(alternative, "address", foreign_addr);
cJSON_AddStringToObject(alternative, "protocol", "udp"); cJSON_AddStringToObject(alternative, "protocol", "udp");
cJSON_AddNumberToObject(alternative, "timeout", info->timeout); cJSON_AddNumberToObject(alternative, "timeout", info->timeout);
cJSON_AddItemToArray(alternative_dns, alternative); cJSON_AddItemToArray(alternative_dns, alternative);
cJSON_AddItemToObject(config, "alternativeDNS", alternative_dns); cJSON_AddItemToObject(config, "alternativeDNS", alternative_dns);
free(foreign_addr);
free(port_str);
cJSON *ip_file = cJSON_CreateObject(); cJSON *ip_file = cJSON_CreateObject();
cJSON_AddStringToObject(ip_file, "primary", info->domestic_ip_file); 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); char *config_str = cJSON_Print(config);
cJSON_Delete(config); // free json object cJSON_Delete(config); // free json object
free(domestic_port);
free(foreign_addr);
free(bind_addr);
return config_str; return config_str;
} }

6
src/common.c

@ -41,6 +41,12 @@ uint8_t check_port(uint16_t port) {
return FALSE; 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 void save_file(const char *file, const char *content) { // save into file
log_debug("Write into `%s` -> \n%s", file, content); log_debug("Write into `%s` -> \n%s", file, content);
FILE* fp = fopen(file , "w"); FILE* fp = fopen(file , "w");

Loading…
Cancel
Save