From 43c5ed98d4f7bd86efc505b7c1e62f8c83ca90ef Mon Sep 17 00:00:00 2001 From: Dnomd343 Date: Mon, 26 Sep 2022 16:46:09 +0800 Subject: [PATCH] update: crontab interface --- include/applet/crontab.h | 12 ++++++--- include/applet/dnsproxy.h | 3 ++- include/utils/assets.h | 3 ++- src/applet/CMakeLists.txt | 2 +- src/applet/crontab.c | 54 +++++++++++++++++---------------------- src/cleardns.c | 5 ++-- src/utils/assets.c | 26 ++++++++++++++++++- 7 files changed, 66 insertions(+), 39 deletions(-) diff --git a/include/applet/crontab.h b/include/applet/crontab.h index ce583e5..53499e0 100644 --- a/include/applet/crontab.h +++ b/include/applet/crontab.h @@ -1,10 +1,16 @@ #ifndef CRONTAB_H_ #define CRONTAB_H_ -#include "config.h" +#include #include "process.h" -void assets_free(assets_config *info); -process* assets_load(assets_config *info); +typedef struct { + uint8_t debug; // bool value + char *cron; // cron expression +} crontab; + +void crontab_free(crontab *info); +crontab* crontab_init(char *cron); +process* crontab_load(crontab *info); #endif diff --git a/include/applet/dnsproxy.h b/include/applet/dnsproxy.h index 564d320..b112689 100644 --- a/include/applet/dnsproxy.h +++ b/include/applet/dnsproxy.h @@ -18,9 +18,10 @@ typedef struct { void dnsproxy_free(dnsproxy *info); dnsproxy* dnsproxy_init(uint16_t port); +process* dnsproxy_load(const char *caption, dnsproxy *info, const char *file); + void dnsproxy_add_primary(dnsproxy *info, const char *server); void dnsproxy_add_fallback(dnsproxy *info, const char *server); void dnsproxy_add_bootstrap(dnsproxy *info, const char *server); -process* dnsproxy_load(const char *caption, dnsproxy *info, const char *file); #endif diff --git a/include/utils/assets.h b/include/utils/assets.h index 5690bba..5323c49 100644 --- a/include/utils/assets.h +++ b/include/utils/assets.h @@ -1,6 +1,7 @@ #ifndef ASSETS_H_ #define ASSETS_H_ -void extract_assets(); +void assets_init(); +//void assets_update(); #endif diff --git a/src/applet/CMakeLists.txt b/src/applet/CMakeLists.txt index c6ddac0..8271386 100644 --- a/src/applet/CMakeLists.txt +++ b/src/applet/CMakeLists.txt @@ -1,4 +1,4 @@ cmake_minimum_required(VERSION 2.8.12) add_library(applet adguard.c dnsproxy.c overture.c crontab.c) -target_link_libraries(applet bcrypt utils) +target_link_libraries(applet bcrypt) diff --git a/src/applet/crontab.c b/src/applet/crontab.c index 16b0662..e59019f 100644 --- a/src/applet/crontab.c +++ b/src/applet/crontab.c @@ -1,47 +1,41 @@ #include -#include -#include -#include "config.h" #include "logger.h" #include "sundry.h" #include "system.h" +#include "crontab.h" #include "process.h" -#include "structure.h" +#include "constant.h" -char **update_file; -char **update_url; +void crontab_dump(crontab *info); -void assets_update() { // update all assets - log_info("Start assets update"); - for (char **file = update_file; *file != NULL; ++file) { - char **url = file - update_file + update_url; - log_info("Update asset `%s` -> %s", *file, *url); - download_file(*file, *url); // download asset from url - } - log_info("Restart overture"); - run_command("pgrep overture | xargs kill"); // restart overture - log_info("Assets update complete"); -} - -void assets_free(assets_config *info) { // free assets config - string_list_free(info->update_file); - string_list_free(info->update_url); +void crontab_free(crontab *info) { // free crontab options free(info->cron); free(info); } -process* assets_load(assets_config *info) { // load assets update options - update_url = string_list_init(); - update_file = string_list_init(); - string_list_update(&update_url, info->update_url); - string_list_update(&update_file, info->update_file); - signal(SIGALRM, assets_update); // receive SIGALRM signal +crontab* crontab_init(char *cron) { // init crontab options + crontab *info = (crontab *)malloc(sizeof(crontab)); + info->debug = FALSE; + info->cron = string_init(UPDATE_CRON); + return info; +} - char *cron = string_join(info->cron, " kill -14 1"); - save_file("/var/spool/cron/crontabs/root", cron); - free(cron); +void crontab_dump(crontab *info) { // show crontab options in debug log + log_debug("Crontab debug -> %s", show_bool(info->debug)); + log_debug("Crontab expression -> `%s`", info->cron); +} + +process* crontab_load(crontab *info) { // load crontab options + // TODO: avoid the case that PID of cleardns is not 1 (docker --pid) + char *cron_cmd = string_join(info->cron, " kill -14 1"); // SIGALRM -> 14 + save_file("/var/spool/cron/crontabs/root", cron_cmd); + free(cron_cmd); process *proc = process_init("Crontab", "crond"); process_add_arg(proc, "-f"); // foreground + if (info->debug) { + process_add_arg(proc, "-l"); + process_add_arg(proc, "0"); // verbose mode + } return proc; } diff --git a/src/cleardns.c b/src/cleardns.c index 89fe179..0b78341 100644 --- a/src/cleardns.c +++ b/src/cleardns.c @@ -46,11 +46,12 @@ int main(int argc, char *argv[]) { // ClearDNS service char *config_file = init(argc, argv); log_info("ClearDNS server start (%s)", VERSION); create_folder(EXPOSE_DIR); + // TODO: cd WORK_DIR create_folder(WORK_DIR); - extract_assets(); + assets_init(); + load_config(config_file); free(config_file); - if (LOG_LEVEL == LOG_DEBUG) { // debug mode enabled loader.filter->debug = TRUE; loader.diverter->debug = TRUE; diff --git a/src/utils/assets.c b/src/utils/assets.c index 28044cb..2f4cc04 100644 --- a/src/utils/assets.c +++ b/src/utils/assets.c @@ -1,20 +1,44 @@ #include #include #include +#include #include "logger.h" #include "sundry.h" #include "system.h" #include "constant.h" +#include "structure.h" +char **update_file; +char **update_url; + +void assets_update(); void extract_asset(const char *file); -void extract_assets() { +void assets_init() { // init assets and load update process log_info("Start loading assets"); create_folder(ASSETS_DIR); extract_asset(ASSET_GFW_LIST); extract_asset(ASSET_CHINA_IP); extract_asset(ASSET_CHINA_LIST); log_info("Assets loaded complete"); + + update_url = string_list_init(); + update_file = string_list_init(); + signal(SIGALRM, assets_update); // receive SIGALRM signal +} + +// TODO: inject update file and url + +void assets_update() { // update all assets + log_info("Start assets update"); + for (char **file = update_file; *file != NULL; ++file) { + char **url = file - update_file + update_url; + log_info("Update asset `%s` -> %s", *file, *url); + download_file(*file, *url); // download asset from url + } + log_info("Restart overture"); + run_command("pgrep overture | xargs kill"); // restart overture + log_info("Assets update complete"); } void extract_asset(const char *file) {