Browse Source

feat: process interface

dev
dnomd343 2 years ago
parent
commit
63f3e500c7
  1. 1
      include/loader/config.h
  2. 1
      include/loader/loader.h
  3. 3
      include/utils/process.h
  4. 27
      src/cleardns.c
  5. 171
      src/common_legacy.c
  6. 4
      src/loader/config.c
  7. 12
      src/loader/loader.c
  8. 6
      src/loader/parser.c
  9. 23
      src/utils/process.c

1
include/loader/config.h

@ -42,6 +42,7 @@ typedef struct {
uint32_t **reject;
char **hosts;
char **ttl;
char **script;
} cleardns_config;
cleardns_config* config_init();

1
include/loader/loader.h

@ -10,6 +10,7 @@ struct cleardns {
dnsproxy *foreign;
overture *diverter;
adguard *filter;
char **script;
};
extern struct cleardns loader;

3
include/utils/process.h

@ -8,6 +8,9 @@ typedef struct {
char *cwd;
} process;
void process_list_run();
void process_list_init();
void process_list_append(process *proc);
void process_add_arg(process *proc, const char *arg);
process* process_init(const char *caption, const char *bin);

27
src/cleardns.c

@ -1,26 +1,15 @@
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <unistd.h>
#include <string.h>
#include "loader.h"
#include "logger.h"
#include "constant.h"
#include "dnsproxy.h"
#include "overture.h"
#include "structure.h"
#include "adguard.h"
#include "system.h"
#include "assets.h"
#include "sundry.h"
//char *init_script = "/usr/bin/load";
//char *custom_script = "/etc/cleardns/custom.sh";
//
//char *adguard_workdir = "/etc/cleardns/AdGuardHome";
//char *overture_config = "/etc/overture/config.yml";
//char *upstream_config = "/etc/cleardns/upstream.json";
#define HELP_MSG "\
ClearDNS usage \n\
--debug ...\n\
@ -30,8 +19,6 @@ ClearDNS usage \n\
#define CONFIG_FILE "test.json"
// TODO: load `--debug`, `--config ...`, `--version`, `--help`
char* init(int argc, char *argv[]) { // return config file
char *config = string_init(CONFIG_FILE);
for (int i = 0; i < argc; ++i) {
@ -64,7 +51,6 @@ int main(int argc, char *argv[]) { // ClearDNS server
char *config_file = init(argc, argv);
LOG_LEVEL = LOG_DEBUG; // TODO: dev only
log_info("ClearDNS server start (%s)", VERSION);
create_folder(WORK_DIR);
@ -72,13 +58,18 @@ int main(int argc, char *argv[]) { // ClearDNS server
load_config(config_file);
free(config_file);
dnsproxy_load("Domestic", loader.domestic, "domestic.json");
dnsproxy_load("Foreign", loader.foreign, "foreign.json");
overture_load(loader.diverter, "overture.json");
adguard_load(loader.filter, ADGUARD_DIR);
process_list_init();
process_list_append(dnsproxy_load("Domestic", loader.domestic, "domestic.json"));
process_list_append(dnsproxy_load("Foreign", loader.foreign, "foreign.json"));
process_list_append(overture_load(loader.diverter, "overture.json"));
if (loader.filter != NULL) {
process_list_append(adguard_load(loader.filter, ADGUARD_DIR));
}
// TODO: running custom script
process_list_run();
// init_server(init_script, custom_script); // run init script and custom script
//

171
src/common_legacy.c

@ -1,171 +0,0 @@
#include <stdio.h>
#include <stdlib.h>
#include <strList.bak>
#include "cJSON.h"
#include "flag_legacy.h"
char **adguard_command = NULL;
char **overture_command = NULL;
char **domestic_dnsproxy_command = NULL;
char **foreign_dnsproxy_command = NULL;
void error_exit(char *message);
char* read_file(char *file_name);
char** command_add_field(char **command_list, char *field);
char** dnsproxy_config(char *port, cJSON *json, int is_debug);
void load_start_command(char *adguard_workdir, char *overture_config, char *upstream_config, int is_debug);
void error_exit(char *message) { // exit with code 1
fprintf(stderr, "[ClearDNS] %s\n", message);
exit(EXIT_FILE_ERROR);
}
char* read_file(char *file_name) { // read file content
FILE *pfile = fopen(file_name, "rb");
if (pfile == NULL) { // open failed
error_exit("File open failed.");
}
fseek(pfile, 0, SEEK_END);
long file_length = ftell(pfile); // get file length
char *file_content = (char*)malloc(file_length + 1); // malloc new memory
if (file_content == NULL) {
error_exit("No enough memory."); // file too large
}
rewind(pfile);
fread(file_content, 1, file_length, pfile); // read file stream
file_content[file_length] = '\0'; // set end flag
fclose(pfile);
return file_content;
}
char** command_add_field(char **command_list, char *field) { // add field into command
int num = 0;
while(command_list[num++] != NULL); // get options number
command_list = (char**)realloc(command_list, sizeof(char*) * (num + 1));
command_list[num - 1] = strcpy((char*)malloc(strlen(field) + 1), field);
command_list[num] = NULL; // end sign
return command_list;
}
char** dnsproxy_config(char *port, cJSON *json, int is_debug) { // generate dnsproxy command
char *bootstrap_dns = NULL;
cJSON *primary_dns = NULL;
cJSON *fallback_dns = NULL;
char **command_list = (char**)malloc(sizeof(char*) * 9);
command_list[0] = "dnsproxy";
command_list[1] = "--port";
command_list[2] = port; // listen port
command_list[3] = "--all-servers"; // parallel queries to all servers
command_list[4] = "--cache"; // enable DNS cache
command_list[5] = "--cache-size";
command_list[6] = "4194304"; // cache size -> 4MiB
command_list[7] = "--cache-optimistic"; // optimistic DNS cache
command_list[8] = NULL; // end sign
while (json != NULL) {
if (!strcmp(json->string, "bootstrap")) { // bootstrap DNS server
if (!cJSON_IsString(json)) {
error_exit("`bootstrap` must be a string.");
}
bootstrap_dns = json->valuestring;
} else if (!strcmp(json->string, "primary")) { // primary DNS server
if (!cJSON_IsArray(json)) {
error_exit("`primary` must be a array.");
}
primary_dns = json->child;
} else if (!strcmp(json->string, "fallback")) { // fallback DNS server
if (!cJSON_IsArray(json)) {
error_exit("`fallback` must be a array.");
}
fallback_dns = json->child;
}
json = json->next; // next field
}
if (bootstrap_dns != NULL) { // add bootstrap DNS server
command_list = command_add_field(command_list, "--bootstrap");
command_list = command_add_field(command_list, bootstrap_dns);
}
if (primary_dns == NULL) { // primary DNS server required
error_exit("Miss primary DNS server.");
}
while (primary_dns != NULL) { // iterate over primary DNS server list
if (!cJSON_IsString(primary_dns)) {
error_exit("DNS Server should be a string.");
}
command_list = command_add_field(command_list, "--upstream");
command_list = command_add_field(command_list, primary_dns->valuestring);
primary_dns = primary_dns->next;
}
while (fallback_dns != NULL) { // iterate over fallback DNS server list
if (!cJSON_IsString(fallback_dns)) {
error_exit("DNS Server should be a string.");
}
command_list = command_add_field(command_list, "--fallback");
command_list = command_add_field(command_list, fallback_dns->valuestring);
fallback_dns = fallback_dns->next;
}
if (is_debug) { // debug mode
command_list = command_add_field(command_list, "--verbose");
}
return command_list;
}
void load_start_command(char *adguard_workdir, char *overture_config, char *upstream_config, int is_debug) {
// AdGuardHome command
adguard_command = (char**)malloc(sizeof(char*) * 7);
adguard_command[0] = "AdGuardHome";
adguard_command[1] = "-w";
adguard_command[2] = adguard_workdir; // workdir for AdGuardHome
adguard_command[3] = "-p";
adguard_command[4] = "80"; // port for web manage
adguard_command[5] = "--no-check-update"; // skip check update (invalid in docker)
adguard_command[6] = NULL; // end sign
if (is_debug) { // debug mode
adguard_command = command_add_field(adguard_command, "--verbose");
}
// overture command
overture_command = (char**)malloc(sizeof(char*) * 4);
overture_command[0] = "overture";
overture_command[1] = "-c";
overture_command[2] = overture_config;
overture_command[3] = NULL; // end sign
if (is_debug) { // debug mode
overture_command = command_add_field(overture_command, "-v");
}
// dnsproxy command
cJSON *json = NULL;
cJSON *json_root = cJSON_Parse(read_file(upstream_config));
if (json_root == NULL) {
error_exit("JSON format error.");
} else {
json = json_root->child;
}
while (json != NULL) {
if (!strcmp(json->string, "domestic")) { // domestic dnsproxy config
if (!cJSON_IsObject(json)) {
error_exit("`domestic` must be a object.");
}
domestic_dnsproxy_command = dnsproxy_config("4053", json->child, is_debug);
} else if (!strcmp(json->string, "foreign")) { // foreign dnsproxy config
if (!cJSON_IsObject(json)) {
error_exit("`foreign` must be a object.");
}
foreign_dnsproxy_command = dnsproxy_config("6053", json->child, is_debug);
}
json = json->next; // next field
}
if (domestic_dnsproxy_command == NULL) {
error_exit("Miss domestic DNS settings.");
}
if (foreign_dnsproxy_command == NULL) {
error_exit("Miss foreign DNS settings.");
}
cJSON_free(json_root); // free cJSON object
}

4
src/loader/config.c

@ -39,6 +39,7 @@ cleardns_config* config_init() { // init config struct of cleardns
config->reject = uint32_list_init();
config->hosts = string_list_init();
config->ttl = string_list_init();
config->script = string_list_init();
return config;
}
@ -75,6 +76,7 @@ void config_dump(cleardns_config *config) { // dump config info of cleardns
uint32_list_debug("DNS reject type", config->reject);
string_list_debug("Domain TTL", config->ttl);
string_list_debug("Hosts", config->hosts);
string_list_debug("Custom script", config->script);
}
void config_free(cleardns_config *config) { // free config struct of cleardns
@ -93,6 +95,8 @@ void config_free(cleardns_config *config) { // free config struct of cleardns
uint32_list_free(config->reject);
string_list_free(config->hosts);
string_list_free(config->ttl);
string_list_free(config->script);
free(config->adguard.username);
free(config->adguard.password);
free(config);

12
src/loader/loader.c

@ -9,6 +9,7 @@
#include "dnsproxy.h"
#include "constant.h"
#include "structure.h"
#include "logger.h"
struct cleardns loader;
@ -114,16 +115,23 @@ adguard* load_filter(cleardns_config *config) {
void load_config(const char *config_file) {
cleardns_config *config = config_init();
load_default_config(config_file);
config_parser(config, config_file);
load_default_config(config_file); // load default configure
config_parser(config, config_file); // configure parser
config_dump(config);
log_info("Loading configure options");
if (!config->adguard.enable) {
config->diverter.port = config->port; // override diverter port by dns port
}
loader.domestic = load_domestic(config);
log_debug("Domestic options parser success");
loader.foreign = load_foreign(config);
log_debug("Foreign options parser success");
loader.diverter = load_diverter(config);
log_debug("Diverter options parser success");
loader.filter = load_filter(config);
log_debug("Filter options parser success");
loader.script = string_list_init();
loader.script = string_list_update(loader.script, config->script);
config_free(config);
}

6
src/loader/parser.c

@ -146,15 +146,15 @@ void cleardns_parser(cleardns_config *config, const char *config_content) { // J
if (!strcmp(json->string, "ttl")) {
config->ttl = json_string_list_value("ttl", json, config->ttl);
}
if (!strcmp(json->string, "custom")) {
config->script = json_string_list_value("custom", json, config->script);
}
json = json->next; // next field
}
cJSON_free(json); // free JSON struct
}
void config_parser(cleardns_config *config, const char *config_file) {
// TODO: load custom script
char *config_content;
if (is_json_suffix(config_file)) { // JSON format
log_info("Start JSON configure parser");

23
src/utils/process.c

@ -3,6 +3,9 @@
#include "process.h"
#include "constant.h"
#include "structure.h"
#include "logger.h"
process **process_list;
process* process_init(const char *caption, const char *bin) { // init process struct
process *proc = (process *)malloc(sizeof(process));
@ -16,3 +19,23 @@ process* process_init(const char *caption, const char *bin) { // init process st
void process_add_arg(process *proc, const char *arg) { // add argument for process
proc->cmd = string_list_append(proc->cmd, arg);
}
void process_list_init() { // init process list
process_list = (process **)malloc(sizeof(process *));
*process_list = NULL;
}
void process_list_append(process *proc) { // add new process into process list
int proc_num = 0;
for (process **p = process_list; *p != NULL; ++p, ++proc_num); // get process number
process_list = (process **)realloc(process_list, sizeof(process *) * (proc_num + 2));
process_list[proc_num++] = proc; // add into process list
process_list[proc_num] = NULL;
log_debug("%s process added", proc->name);
}
void process_list_run() {
for (process **p = process_list; *p != NULL; ++p) {
log_warn("proc -> %s", (*p)->name);
}
}

Loading…
Cancel
Save