Browse Source

feat: load configure of AdGuardHome

dev
dnomd343 2 years ago
parent
commit
2619cdcf97
  1. 65
      src/applet/adguard.c
  2. 63
      src/cleardns.c

65
src/applet/adguard.c

@ -1,6 +1,9 @@
#include <stdlib.h>
#include <string.h>
#include "cJSON.h"
#include "common.h"
#include "logger.h"
#include "toJSON.h"
#include "adguard.h"
adguard* adguard_init() {
@ -25,6 +28,53 @@ 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) {
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();
cJSON_AddItemToObject(user_config, "name", cJSON_CreateString(info->username));
cJSON_AddItemToObject(user_config, "password", cJSON_CreateString(info->password));
cJSON_AddItemToArray(users_config, user_config);
json_field_replace(json, "users", users_config);
cJSON *dns = json_field_get(json, "dns");
cJSON *upstream = cJSON_CreateArray();
cJSON_AddItemToArray(upstream, cJSON_CreateString(info->upstream));
json_field_replace(dns, "port", cJSON_CreateNumber(info->dns_port));
json_field_replace(dns, "bind_host", cJSON_CreateString("0.0.0.0"));
json_field_replace(dns, "upstream_dns", upstream);
json_field_replace(dns, "upstream_dns_file", cJSON_CreateString(""));
json_field_replace(dns, "bootstrap_dns", cJSON_CreateArray());
char *config = cJSON_Print(json);
cJSON_free(json);
return config;
}
process* adguard_load(adguard *info, const char *dir) {
adguard_dump(info);
if (!check_port(info->dns_port)) { // invalid dns port
@ -34,11 +84,22 @@ process* adguard_load(adguard *info, const char *dir) {
log_fatal("Invalid web port `%u`", info->web_port);
}
// TODO: modify configure file
// inject -> dns_port / upstream / username / password
char *adguard_config_ret;
char *adguard_config_file = string_join(dir, "AdGuardHome.yaml");
char *adguard_config_raw = to_json(adguard_config_file);
if (adguard_config_raw == NULL) { // configure not exist
log_info("Create AdGuardHome configure");
adguard_config_ret = adguard_config(info, "{}"); // begin with empty json
} else {
adguard_config_ret = adguard_config(info, adguard_config_raw);
free(adguard_config_raw);
}
save_file(adguard_config_file, adguard_config_ret); // save modified configure
free(adguard_config_file);
process *proc = process_init("AdGuardHome", ADGUARD_BIN);
char *port_str = uint32_to_string(info->web_port);
process_add_arg(proc, "--no-check-update");
process_add_arg(proc, "--work-dir");
process_add_arg(proc, dir);
process_add_arg(proc, "--port");

63
src/cleardns.c

@ -8,6 +8,7 @@
#include "overture.h"
#include "structure.h"
#include "toJSON.h"
#include "adguard.h"
//#include <stdio.h>
//#include <string.h>
@ -39,10 +40,6 @@ int main(int argc, char *argv[]) { // ClearDNS server
LOG_LEVEL = LOG_DEBUG;
log_info("ClearDNS server start (%s)", VERSION);
char *str = to_json("AdGuardHome.yaml");
log_info("%s", str);
return 0;
// load_config("test.json");
//
@ -70,27 +67,27 @@ int main(int argc, char *argv[]) { // ClearDNS server
// 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);
// 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);
@ -112,6 +109,22 @@ 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);
// int debug_mode = 0;
// fprintf(stderr, "[ClearDNS] Server start.\n");
// for (char **p = argv; p < argv + argc; ++p) {

Loading…
Cancel
Save