Browse Source

update: add init and crond command

dev
dnomd343 3 years ago
parent
commit
e55f612b23
  1. 2
      include/common.h
  2. 25
      src/cleardns.c
  3. 40
      src/common.c

2
include/common.h

@ -1,6 +1,8 @@
#ifndef _COMMON_H_ #ifndef _COMMON_H_
#define _COMMON_H_ #define _COMMON_H_
extern char **init_command;
extern char **crond_command;
extern char **adguard_command; extern char **adguard_command;
extern char **overture_command; extern char **overture_command;
extern char **domestic_dnsproxy_command; extern char **domestic_dnsproxy_command;

25
src/cleardns.c

@ -1,6 +1,13 @@
#include <stdio.h> #include <stdio.h>
#include <string.h>
#include "common.h" #include "common.h"
int debug_mode = 0;
char *adguard_workdir = "/etc/cleardns/AdGuardHome";
char *overture_config = "/etc/overture/config.yml";
char *upstream_config = "/etc/cleardns/upstream.json";
void show_command(char **command) { void show_command(char **command) {
int num = 0; int num = 0;
while(command[num] != NULL) { while(command[num] != NULL) {
@ -9,11 +16,21 @@ void show_command(char **command) {
} }
int main(int argc, char *argv[]) { int main(int argc, char *argv[]) {
char *adguard_workdir = "/etc/cleardns/AdGuardHome"; for (char **p = argv; p < argv + argc; ++p) {
char *overture_config = "/etc/overture/config.yml"; if (!strcmp(*p, "--debug")) { // --debug option
char *upstream_config = "/etc/cleardns/upstream.json"; ++debug_mode;
}
}
load_start_command(adguard_workdir, overture_config, upstream_config, debug_mode);
load_start_command(adguard_workdir, overture_config, upstream_config, 0); printf("[init]\n");
show_command(init_command);
printf("\n");
printf("[crond]\n");
show_command(crond_command);
printf("\n");
printf("[AdGuardHome]\n"); printf("[AdGuardHome]\n");
show_command(adguard_command); show_command(adguard_command);

40
src/common.c

@ -3,6 +3,8 @@
#include <string.h> #include <string.h>
#include "cJSON.h" #include "cJSON.h"
char **init_command = NULL;
char **crond_command = NULL;
char **adguard_command = NULL; char **adguard_command = NULL;
char **overture_command = NULL; char **overture_command = NULL;
char **domestic_dnsproxy_command = NULL; char **domestic_dnsproxy_command = NULL;
@ -111,7 +113,7 @@ char** dnsproxy_config(char *port, cJSON *json, int is_debug) { // generate dnsp
} }
} }
if (is_debug) { // verbose mode if (is_debug) { // debug mode
command_list = command_add_field(command_list, "--verbose"); command_list = command_add_field(command_list, "--verbose");
} }
@ -119,35 +121,53 @@ char** dnsproxy_config(char *port, cJSON *json, int is_debug) { // generate dnsp
} }
void load_start_command(char *adguard_workdir, char *overture_config, char *upstream_config, int is_debug) { void load_start_command(char *adguard_workdir, char *overture_config, char *upstream_config, int is_debug) {
// TODO: crond process // init command
init_command = (char**)malloc(sizeof(char*) * 3);
init_command[0] = "sh";
init_command[1] = "/usr/bin/load";
init_command[2] = NULL;
// crond command
crond_command = (char**)malloc(sizeof(char*) * 2);
crond_command[0] = "crond";
crond_command[1] = NULL; // end sign
if (is_debug) { // debug mode
crond_command = command_add_field(crond_command, "-f"); // run in foreground
crond_command = command_add_field(crond_command, "-L");
crond_command = command_add_field(crond_command, "/dev/stdout");
}
// AdGuardHome command // AdGuardHome command
adguard_command = (char**)malloc(sizeof(char*) * 6); adguard_command = (char**)malloc(sizeof(char*) * 7);
adguard_command[0] = "AdGuardHome"; adguard_command[0] = "AdGuardHome";
adguard_command[1] = "-w"; adguard_command[1] = "-w";
adguard_command[2] = adguard_workdir; // workdir for AdGuardHome adguard_command[2] = adguard_workdir; // workdir for AdGuardHome
adguard_command[3] = "-p"; adguard_command[3] = "-p";
adguard_command[4] = "80"; // port for web manage adguard_command[4] = "80"; // port for web manage
adguard_command[5] = "--no-check-update"; // skip check update (invalid in docker) adguard_command[5] = "--no-check-update"; // skip check update (invalid in docker)
if (is_debug) { adguard_command[6] = NULL; // end sign
if (is_debug) { // debug mode
adguard_command = command_add_field(adguard_command, "--verbose"); adguard_command = command_add_field(adguard_command, "--verbose");
} }
// overture command // overture command
overture_command = (char**)malloc(sizeof(char*) * 6); overture_command = (char**)malloc(sizeof(char*) * 4);
overture_command[0] = "overture"; overture_command[0] = "overture";
overture_command[1] = "-c"; overture_command[1] = "-c";
overture_command[2] = overture_config; overture_command[2] = overture_config;
if (is_debug) { overture_command[3] = NULL; // end sign
if (is_debug) { // debug mode
overture_command = command_add_field(overture_command, "-v"); overture_command = command_add_field(overture_command, "-v");
} }
// dnsproxy command // dnsproxy command
cJSON *json = cJSON_Parse(read_file(upstream_config)); cJSON *json = NULL;
if (json == NULL) { cJSON *json_root = cJSON_Parse(read_file(upstream_config));
if (json_root == NULL) {
error_exit("JSON format error."); error_exit("JSON format error.");
} else {
json = json_root->child;
} }
json = json->child;
while (json != NULL) { while (json != NULL) {
if (!strcmp(json->string, "domestic")) { // domestic dnsproxy config if (!strcmp(json->string, "domestic")) { // domestic dnsproxy config
if (!cJSON_IsObject(json)) { if (!cJSON_IsObject(json)) {
@ -168,5 +188,5 @@ void load_start_command(char *adguard_workdir, char *overture_config, char *upst
if (foreign_dnsproxy_command == NULL) { if (foreign_dnsproxy_command == NULL) {
error_exit("Miss foreign DNS settings."); error_exit("Miss foreign DNS settings.");
} }
// TODO: free json object cJSON_free(json_root);
} }

Loading…
Cancel
Save