Browse Source

update: applet interface

dev
dnomd343 2 years ago
parent
commit
77cd339081
  1. 9
      include/common.h
  2. 6
      include/utils/toJSON.h
  3. 35
      src/applet/adguard.c
  4. 4
      src/applet/dnsproxy.c
  5. 5
      src/applet/overture.c
  6. 35
      src/cleardns.c
  7. 7
      src/common.c
  8. 2
      src/utils/CMakeLists.txt
  9. 22
      src/utils/json.c

9
include/common.h

@ -21,6 +21,8 @@
#define DOMESTIC_PORT 4053 #define DOMESTIC_PORT 4053
#define FOREIGN_PORT 6053 #define FOREIGN_PORT 6053
#define DIVERTER_TIMEOUT 6
#define ADGUARD_USER "admin" #define ADGUARD_USER "admin"
#define ADGUARD_PASSWD "adguard" #define ADGUARD_PASSWD "adguard"
@ -49,8 +51,15 @@ char* uint32_to_string(uint32_t number);
char* gen_bcrypt(const char *data); char* gen_bcrypt(const char *data);
int run_command(const char *command); int run_command(const char *command);
void create_folder(const char *folder);
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);
#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 #endif

6
include/utils/toJSON.h

@ -1,6 +0,0 @@
#ifndef _TO_JSON_H_
#define _TO_JSON_H_
char* to_json(const char *config_file);
#endif

35
src/applet/adguard.c

@ -1,18 +1,19 @@
#include <stdlib.h> #include <stdlib.h>
#include <string.h>
#include "cJSON.h" #include "cJSON.h"
#include "common.h" #include "common.h"
#include "logger.h" #include "logger.h"
#include "toJSON.h"
#include "adguard.h" #include "adguard.h"
void adguard_dump(adguard *info);
char *adguard_config(adguard *info, const char *raw_config);
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); 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:", port_str); info->upstream = string_join("127.0.0.1:", port_str); // default upstream
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); 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); log_debug("AdGuardHome password -> %s", info->password);
} }
void json_field_replace(cJSON *entry, const char *field, cJSON *content) { char *adguard_config(adguard *info, const char *raw_config) { // modify adguard configure
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); cJSON *json = cJSON_Parse(raw_config);
if (json == NULL) { if (json == NULL) {
log_fatal("AdGuardHome configure error"); log_fatal("AdGuardHome configure error");
} }
// TODO: password use bcencrypt
cJSON *user_config = cJSON_CreateObject(); cJSON *user_config = cJSON_CreateObject();
cJSON *users_config = cJSON_CreateArray(); cJSON *users_config = cJSON_CreateArray();
char *password = gen_bcrypt(info->password);
cJSON_AddItemToObject(user_config, "name", cJSON_CreateString(info->username)); 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); cJSON_AddItemToArray(users_config, user_config);
json_field_replace(json, "users", users_config); json_field_replace(json, "users", users_config);
free(password);
cJSON *dns = json_field_get(json, "dns"); cJSON *dns = json_field_get(json, "dns");
cJSON *upstream = cJSON_CreateArray(); 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); log_fatal("Invalid web port `%u`", info->web_port);
} }
create_folder(dir);
char *adguard_config_ret; char *adguard_config_ret;
char *adguard_config_file = string_join(dir, "AdGuardHome.yaml"); char *adguard_config_file = string_join(dir, "AdGuardHome.yaml");
char *adguard_config_raw = to_json(adguard_config_file); 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 process_add_arg(proc, "--verbose"); // adguard enable debug mode
} }
free(port_str); free(port_str);
log_info("AdGuardHome load success");
return proc; return proc;
} }

4
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); log_fatal("%s without primary dns server", caption);
} }
create_folder(WORK_DIR);
char *config = dnsproxy_config(info); // string config (JSON format) char *config = dnsproxy_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); // load dnsproxy configure
free(config_file); free(config_file);
free(config); 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 process_add_arg(proc, "--verbose"); // dnsproxy enable debug mode
} }
free(option); free(option);
log_info("%s load success", caption);
return proc; return proc;
} }

5
src/applet/overture.c

@ -12,7 +12,7 @@ overture* overture_init() { // init overture options
overture *info = (overture *)malloc(sizeof(overture)); overture *info = (overture *)malloc(sizeof(overture));
info->port = DIVERTER_PORT; info->port = DIVERTER_PORT;
info->debug = FALSE; info->debug = FALSE;
info->timeout = 6; // default timeout -> 6s info->timeout = DIVERTER_TIMEOUT; // default timeout -> 6s
info->ttl_file = NULL; info->ttl_file = NULL;
info->host_file = NULL; info->host_file = NULL;
info->foreign_port = FOREIGN_PORT; 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"); log_fatal("Timeout of overture with invalid value 0");
} }
create_folder(WORK_DIR);
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);
@ -63,6 +64,7 @@ process* overture_load(overture *info, const char *file) {
if (info->debug) { if (info->debug) {
process_add_arg(proc, "-v"); // overture enable debug mode process_add_arg(proc, "-v"); // overture enable debug mode
} }
log_info("Overture load success");
return proc; return proc;
} }
@ -124,7 +126,6 @@ char* overture_config(overture *info) { // generate json configure from overture
} }
cJSON_AddStringToObject(host_file, "finder", "regex-list"); cJSON_AddStringToObject(host_file, "finder", "regex-list");
cJSON_AddItemToObject(config, "hostsFile", host_file); cJSON_AddItemToObject(config, "hostsFile", host_file);
if (info->ttl_file != NULL) { if (info->ttl_file != NULL) {
cJSON_AddStringToObject(config, "domainTTLFile", info->ttl_file); cJSON_AddStringToObject(config, "domainTTLFile", info->ttl_file);
} }

35
src/cleardns.c

@ -7,7 +7,6 @@
#include "dnsproxy.h" #include "dnsproxy.h"
#include "overture.h" #include "overture.h"
#include "structure.h" #include "structure.h"
#include "toJSON.h"
#include "adguard.h" #include "adguard.h"
//#include <stdio.h> //#include <stdio.h>
@ -41,9 +40,6 @@ int main(int argc, char *argv[]) { // ClearDNS server
LOG_LEVEL = LOG_DEBUG; LOG_LEVEL = LOG_DEBUG;
log_info("ClearDNS server start (%s)", VERSION); log_info("ClearDNS server start (%s)", VERSION);
char *ret = gen_bcrypt("dnomd343");
log_info("%s", ret);
// load_config("test.json"); // load_config("test.json");
// //
@ -94,8 +90,9 @@ int main(int argc, char *argv[]) { // ClearDNS server
// log_info("cwd -> %s", p->cwd); // log_info("cwd -> %s", p->cwd);
// overture *diverter = overture_init(DIVERTER_PORT); // overture *diverter = overture_init();
// //
// diverter->port = 5454;
// diverter->timeout = 8; // diverter->timeout = 8;
// diverter->domestic_ip_file = "china-ip.txt"; // diverter->domestic_ip_file = "china-ip.txt";
// diverter->domestic_domain_file = "chinalist.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); // log_info("cwd -> %s", p->cwd);
// adguard *filter = adguard_init(); adguard *filter = adguard_init();
//
// filter->debug = TRUE; filter->debug = TRUE;
// filter->dns_port = 54; filter->dns_port = 54;
// filter->web_port = 8080; filter->web_port = 8080;
// filter->upstream = "127.0.0.1:5454"; filter->upstream = "127.0.0.1:5454";
//
// filter->username = "dnomd343"; filter->username = "dnomd343";
// filter->password = "password"; filter->password = "password";
//
// process *p = adguard_load(filter, "/cleardns/adguard/"); process *p = adguard_load(filter, "/cleardns/adguard/");
// log_info("cmd -> %s", string_list_dump(p->cmd)); log_info("cmd -> %s", string_list_dump(p->cmd));
// log_info("env -> %s", string_list_dump(p->env)); log_info("env -> %s", string_list_dump(p->env));
// log_info("cwd -> %s", p->cwd); log_info("cwd -> %s", p->cwd);
// int debug_mode = 0; // int debug_mode = 0;

7
src/common.c

@ -77,6 +77,13 @@ char* gen_bcrypt(const char *data) {
return hash; 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 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");

2
src/utils/CMakeLists.txt

@ -1,3 +1,3 @@
cmake_minimum_required(VERSION 2.8.12) 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)

22
src/utils/toJSON.c → src/utils/json.c

@ -1,7 +1,7 @@
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#include "toJSON.h" #include "cJSON.h"
#include "logger.h" #include "logger.h"
#include "common.h" #include "common.h"
@ -17,6 +17,7 @@ char* to_json(const char *file) {
sprintf(to_json_cmd, "toJSON %s > %s", file, output_file); sprintf(to_json_cmd, "toJSON %s > %s", file, output_file);
log_debug("JSON format command -> `%s`", to_json_cmd); log_debug("JSON format command -> `%s`", to_json_cmd);
if (run_command(to_json_cmd)) { // toJSON return non-zero code if (run_command(to_json_cmd)) { // toJSON return non-zero code
// TODO: try remove output file
return NULL; // convert failed return NULL; // convert failed
} }
free(to_json_cmd); free(to_json_cmd);
@ -28,3 +29,22 @@ char* to_json(const char *file) {
free(rm_cmd); free(rm_cmd);
return json_content; 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;
}
Loading…
Cancel
Save