Browse Source

update: generate subprocess command

dev
dnomd343 3 years ago
parent
commit
68a9e91e6b
  1. 11
      include/common.h
  2. 37
      src/cleardns.c
  3. 68
      src/common.c

11
include/common.h

@ -0,0 +1,11 @@
#ifndef _COMMON_H_
#define _COMMON_H_
extern char **adguard_command;
extern char **overture_command;
extern char **domestic_dnsproxy_command;
extern char **foreign_dnsproxy_command;
void load_start_command(char *adguard_workdir, char *overture_config, char *upstream_config, int is_debug);
#endif

37
src/cleardns.c

@ -0,0 +1,37 @@
#include <stdio.h>
#include "common.h"
void show_command(char **command) {
int num = 0;
while(command[num] != NULL) {
printf("%s\n", command[num++]);
}
}
int main(int argc, char *argv[]) {
char *adguard_workdir = "/etc/cleardns/AdGuardHome";
char *overture_config = "/etc/overture/config.yml";
char *upstream_config = "/etc/cleardns/upstream.json";
load_start_command(adguard_workdir, overture_config, upstream_config, 0);
printf("[AdGuardHome]\n");
show_command(adguard_command);
printf("\n");
printf("[overture]\n");
show_command(overture_command);
printf("\n");
printf("[domestic dnsproxy]\n");
show_command(domestic_dnsproxy_command);
printf("\n");
printf("[foreign dnsproxy]\n");
show_command(foreign_dnsproxy_command);
printf("\n");
printf("done.\n");
return 0;
}

68
src/common.c

@ -3,14 +3,17 @@
#include <string.h> #include <string.h>
#include "cJSON.h" #include "cJSON.h"
#define TRUE 1 char **adguard_command = NULL;
#define FALSE 0 char **overture_command = NULL;
int debug_mode = TRUE;
char **domestic_dnsproxy_command = NULL; char **domestic_dnsproxy_command = NULL;
char **foreign_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 void error_exit(char *message) { // exit with code 1
fprintf(stderr, "%s\n", message); fprintf(stderr, "%s\n", message);
exit(1); exit(1);
@ -108,22 +111,38 @@ char** dnsproxy_config(char *port, cJSON *json, int is_debug) { // generate dnsp
} }
} }
if (debug_mode) { // verbose mode if (is_debug) { // verbose mode
command_list = command_add_field(command_list, "--verbose"); command_list = command_add_field(command_list, "--verbose");
} }
return command_list; return command_list;
} }
void show_dnsproxy_config(char **config) { void load_start_command(char *adguard_workdir, char *overture_config, char *upstream_config, int is_debug) {
int num = 0; // TODO: crond process
while(config[num] != NULL) {
printf("%s\n", config[num++]); // AdGuardHome command
adguard_command = (char**)malloc(sizeof(char*) * 6);
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)
if (is_debug) {
adguard_command = command_add_field(adguard_command, "--verbose");
}
// overture command
overture_command = (char**)malloc(sizeof(char*) * 6);
overture_command[0] = "overture";
overture_command[1] = "-c";
overture_command[2] = overture_config;
if (is_debug) {
overture_command = command_add_field(overture_command, "-v");
} }
}
int main(int argc, char *argv[]) { // dnsproxy command
char *upstream_config = "/etc/cleardns/upstream.json";
cJSON *json = cJSON_Parse(read_file(upstream_config)); cJSON *json = cJSON_Parse(read_file(upstream_config));
if (json == NULL) { if (json == NULL) {
error_exit("JSON format error."); error_exit("JSON format error.");
@ -134,25 +153,20 @@ int main(int argc, char *argv[]) {
if (!cJSON_IsObject(json)) { if (!cJSON_IsObject(json)) {
error_exit("`domestic` must be a object."); error_exit("`domestic` must be a object.");
} }
domestic_dnsproxy_command = dnsproxy_config("4053", json->child, debug_mode); domestic_dnsproxy_command = dnsproxy_config("4053", json->child, is_debug);
} else if (!strcmp(json->string, "foreign")) { // foreign dnsproxy config } else if (!strcmp(json->string, "foreign")) { // foreign dnsproxy config
if (!cJSON_IsObject(json)) { if (!cJSON_IsObject(json)) {
error_exit("`foreign` must be a object."); error_exit("`foreign` must be a object.");
} }
foreign_dnsproxy_command = dnsproxy_config("6053", json->child, debug_mode); foreign_dnsproxy_command = dnsproxy_config("6053", json->child, is_debug);
} }
json = json->next; // next field json = json->next; // next field
} }
if (domestic_dnsproxy_command == NULL) {
printf("[domestic dnsproxy]\n"); error_exit("Miss domestic DNS settings.");
show_dnsproxy_config(domestic_dnsproxy_command); }
printf("\n"); if (foreign_dnsproxy_command == NULL) {
error_exit("Miss foreign DNS settings.");
printf("[foreign dnsproxy]\n"); }
show_dnsproxy_config(foreign_dnsproxy_command); // TODO: free json object
printf("\n");
printf("decode complete\n");
return 0;
} }

Loading…
Cancel
Save