Browse Source

update: multi utils

dev
dnomd343 2 years ago
parent
commit
61c64dc1f0
  1. 12
      include/common.h
  2. 14
      src/CMakeLists.txt
  3. 80
      src/cleardns.c
  4. 3
      src/utils/CMakeLists.txt
  5. 4
      src/utils/logger.c

12
include/common.h

@ -1,11 +1,13 @@
#ifndef _COMMON_H_
#define _COMMON_H_
extern char **adguard_command;
extern char **overture_command;
extern char **domestic_dnsproxy_command;
extern char **foreign_dnsproxy_command;
#define VERSION "1.3.0-dev"
void load_start_command(char *adguard_workdir, char *overture_config, char *upstream_config, int is_debug);
//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

14
src/CMakeLists.txt

@ -1,13 +1,9 @@
cmake_minimum_required(VERSION 2.8.12)
include_directories(${PROJECT_SOURCE_DIR}/include)
include_directories(/usr/lib/i386-linux-gnu/glib-2.0/include)
include_directories(/usr/lib/x86_64-linux-gnu/glib-2.0/include)
include_directories(/usr/lib/aarch64-linux-gnu/glib-2.0/include)
include_directories(/usr/lib64/glib-2.0/include)
include_directories(/usr/lib/glib-2.0/include)
include_directories(/usr/include/glib-2.0)
include_directories(${PROJECT_SOURCE_DIR}/include/utils)
aux_source_directory(${PROJECT_SOURCE_DIR}/src SRC)
add_executable(cleardns ${SRC})
target_link_libraries(cleardns glib-2.0)
add_subdirectory(utils)
add_executable(cleardns cleardns.c)
target_link_libraries(cleardns utils)

80
src/cleardns.c

@ -1,47 +1,53 @@
#include <stdio.h>
#include <string.h>
#include "logger.h"
#include "common.h"
#include "process.h"
char *init_script = "/usr/bin/load";
char *custom_script = "/etc/cleardns/custom.sh";
//#include <stdio.h>
//#include <string.h>
//#include "common.h"
//#include "process.h"
char *adguard_workdir = "/etc/cleardns/AdGuardHome";
char *overture_config = "/etc/overture/config.yml";
char *upstream_config = "/etc/cleardns/upstream.json";
//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";
void show_command(char *title, char **command) {
int num = 0;
fprintf(stderr, "%s => \"", title);
while(command[num] != NULL) {
fprintf(stderr, "%s", command[num++]);
if (command[num] != NULL) {
fprintf(stderr, " ");
}
}
fprintf(stderr, "\"\n");
}
//void show_command(char *title, char **command) {
// int num = 0;
// fprintf(stderr, "%s => \"", title);
// while(command[num] != NULL) {
// fprintf(stderr, "%s", command[num++]);
// if (command[num] != NULL) {
// fprintf(stderr, " ");
// }
// }
// fprintf(stderr, "\"\n");
//}
int main(int argc, char *argv[]) { // ClearDNS server
int debug_mode = 0;
fprintf(stderr, "[ClearDNS] Server start.\n");
for (char **p = argv; p < argv + argc; ++p) {
if (!strcmp(*p, "--debug")) { // --debug option
++debug_mode;
}
}
init_server(init_script, custom_script); // run init script and custom script
load_start_command(adguard_workdir, overture_config, upstream_config, debug_mode); // generate commands
if (debug_mode) { // show exec command
fprintf(stderr, "[ClearDNS] Debug mode.\n");
show_command("[ClearDNS] adguard", adguard_command);
show_command("[ClearDNS] overture", overture_command);
show_command("[ClearDNS] dnsproxy (domestic)", domestic_dnsproxy_command);
show_command("[ClearDNS] dnsproxy (foreign)", foreign_dnsproxy_command);
}
log_info("ClearDNS server start (%s)", VERSION);
server_daemon(); // run as daemon to manage process in docker
// int debug_mode = 0;
// fprintf(stderr, "[ClearDNS] Server start.\n");
// for (char **p = argv; p < argv + argc; ++p) {
// if (!strcmp(*p, "--debug")) { // --debug option
// ++debug_mode;
// }
// }
//
// init_server(init_script, custom_script); // run init script and custom script
//
// load_start_command(adguard_workdir, overture_config, upstream_config, debug_mode); // generate commands
// if (debug_mode) { // show exec command
// fprintf(stderr, "[ClearDNS] Debug mode.\n");
// show_command("[ClearDNS] adguard", adguard_command);
// show_command("[ClearDNS] overture", overture_command);
// show_command("[ClearDNS] dnsproxy (domestic)", domestic_dnsproxy_command);
// show_command("[ClearDNS] dnsproxy (foreign)", foreign_dnsproxy_command);
// }
//
// server_daemon(); // run as daemon to manage process in docker
return 0;
}

3
src/utils/CMakeLists.txt

@ -0,0 +1,3 @@
cmake_minimum_required(VERSION 2.8.12)
add_library(utils cJSON.c logger.c)

4
src/utils/logger.c

@ -32,7 +32,7 @@ void log_printf(int level, const char *fmt, ...) {
va_list ap;
va_start(ap, fmt);
fprintf(stderr, "\x1b[36m[Bootstrap]\x1b[0m \x1b[90m%s\x1b[0m", time_str); // show log prefix
fprintf(stderr, "\x1b[36m[ClearDNS]\x1b[0m \x1b[90m%s\x1b[0m", time_str); // show log prefix
fprintf(stderr, " %s%s\x1b[0m ", log_color[level], log_string[level]); // show log level
vfprintf(stderr, fmt, ap); // output log content
fprintf(stderr, "\n"); // add LF after line
@ -48,7 +48,7 @@ void log_perror(char *prefix) {
time_t t;
time(&t);
struct tm *lt = localtime(&t);
fprintf(stderr, "\x1b[36m[Bootstrap]\x1b[0m ");
fprintf(stderr, "\x1b[36m[ClearDNS]\x1b[0m ");
fprintf(stderr, "\x1b[90m%04d-%02d-%02d %02d:%02d:%02d\x1b[0m",
lt->tm_year + 1900, lt->tm_mon, lt->tm_mday, lt->tm_hour, lt->tm_min, lt->tm_sec);
fprintf(stderr, " %s%s\x1b[0m ", log_color[3], log_string[3]); // error level

Loading…
Cancel
Save