From 61c64dc1f03320838229ecde6614d117cb9adb22 Mon Sep 17 00:00:00 2001 From: dnomd343 Date: Sat, 10 Sep 2022 20:18:47 +0800 Subject: [PATCH] update: multi utils --- include/common.h | 12 +++--- src/CMakeLists.txt | 14 +++---- src/cleardns.c | 80 +++++++++++++++++++++------------------- src/utils/CMakeLists.txt | 3 ++ src/utils/logger.c | 4 +- 5 files changed, 60 insertions(+), 53 deletions(-) create mode 100644 src/utils/CMakeLists.txt diff --git a/include/common.h b/include/common.h index 18699c2..7393511 100644 --- a/include/common.h +++ b/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 diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 9105852..d303d47 100644 --- a/src/CMakeLists.txt +++ b/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) diff --git a/src/cleardns.c b/src/cleardns.c index a8a126a..6cef540 100644 --- a/src/cleardns.c +++ b/src/cleardns.c @@ -1,47 +1,53 @@ -#include -#include +#include "logger.h" #include "common.h" -#include "process.h" -char *init_script = "/usr/bin/load"; -char *custom_script = "/etc/cleardns/custom.sh"; +//#include +//#include +//#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; } diff --git a/src/utils/CMakeLists.txt b/src/utils/CMakeLists.txt new file mode 100644 index 0000000..cc3bdc1 --- /dev/null +++ b/src/utils/CMakeLists.txt @@ -0,0 +1,3 @@ +cmake_minimum_required(VERSION 2.8.12) + +add_library(utils cJSON.c logger.c) diff --git a/src/utils/logger.c b/src/utils/logger.c index 73b68b5..ac64260 100644 --- a/src/utils/logger.c +++ b/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