Browse Source

update: crontab interface

dev
Dnomd343 2 years ago
parent
commit
43c5ed98d4
  1. 12
      include/applet/crontab.h
  2. 3
      include/applet/dnsproxy.h
  3. 3
      include/utils/assets.h
  4. 2
      src/applet/CMakeLists.txt
  5. 54
      src/applet/crontab.c
  6. 5
      src/cleardns.c
  7. 26
      src/utils/assets.c

12
include/applet/crontab.h

@ -1,10 +1,16 @@
#ifndef CRONTAB_H_ #ifndef CRONTAB_H_
#define CRONTAB_H_ #define CRONTAB_H_
#include "config.h" #include <stdint.h>
#include "process.h" #include "process.h"
void assets_free(assets_config *info); typedef struct {
process* assets_load(assets_config *info); 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 #endif

3
include/applet/dnsproxy.h

@ -18,9 +18,10 @@ typedef struct {
void dnsproxy_free(dnsproxy *info); void dnsproxy_free(dnsproxy *info);
dnsproxy* dnsproxy_init(uint16_t port); 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_primary(dnsproxy *info, const char *server);
void dnsproxy_add_fallback(dnsproxy *info, const char *server); void dnsproxy_add_fallback(dnsproxy *info, const char *server);
void dnsproxy_add_bootstrap(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 #endif

3
include/utils/assets.h

@ -1,6 +1,7 @@
#ifndef ASSETS_H_ #ifndef ASSETS_H_
#define ASSETS_H_ #define ASSETS_H_
void extract_assets(); void assets_init();
//void assets_update();
#endif #endif

2
src/applet/CMakeLists.txt

@ -1,4 +1,4 @@
cmake_minimum_required(VERSION 2.8.12) cmake_minimum_required(VERSION 2.8.12)
add_library(applet adguard.c dnsproxy.c overture.c crontab.c) add_library(applet adguard.c dnsproxy.c overture.c crontab.c)
target_link_libraries(applet bcrypt utils) target_link_libraries(applet bcrypt)

54
src/applet/crontab.c

@ -1,47 +1,41 @@
#include <stdlib.h> #include <stdlib.h>
#include <signal.h>
#include <stdio.h>
#include "config.h"
#include "logger.h" #include "logger.h"
#include "sundry.h" #include "sundry.h"
#include "system.h" #include "system.h"
#include "crontab.h"
#include "process.h" #include "process.h"
#include "structure.h" #include "constant.h"
char **update_file; void crontab_dump(crontab *info);
char **update_url;
void assets_update() { // update all assets void crontab_free(crontab *info) { // free crontab options
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);
free(info->cron); free(info->cron);
free(info); free(info);
} }
process* assets_load(assets_config *info) { // load assets update options crontab* crontab_init(char *cron) { // init crontab options
update_url = string_list_init(); crontab *info = (crontab *)malloc(sizeof(crontab));
update_file = string_list_init(); info->debug = FALSE;
string_list_update(&update_url, info->update_url); info->cron = string_init(UPDATE_CRON);
string_list_update(&update_file, info->update_file); return info;
signal(SIGALRM, assets_update); // receive SIGALRM signal }
char *cron = string_join(info->cron, " kill -14 1"); void crontab_dump(crontab *info) { // show crontab options in debug log
save_file("/var/spool/cron/crontabs/root", cron); log_debug("Crontab debug -> %s", show_bool(info->debug));
free(cron); 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 *proc = process_init("Crontab", "crond");
process_add_arg(proc, "-f"); // foreground process_add_arg(proc, "-f"); // foreground
if (info->debug) {
process_add_arg(proc, "-l");
process_add_arg(proc, "0"); // verbose mode
}
return proc; return proc;
} }

5
src/cleardns.c

@ -46,11 +46,12 @@ int main(int argc, char *argv[]) { // ClearDNS service
char *config_file = init(argc, argv); char *config_file = init(argc, argv);
log_info("ClearDNS server start (%s)", VERSION); log_info("ClearDNS server start (%s)", VERSION);
create_folder(EXPOSE_DIR); create_folder(EXPOSE_DIR);
// TODO: cd WORK_DIR
create_folder(WORK_DIR); create_folder(WORK_DIR);
extract_assets(); assets_init();
load_config(config_file); load_config(config_file);
free(config_file); free(config_file);
if (LOG_LEVEL == LOG_DEBUG) { // debug mode enabled if (LOG_LEVEL == LOG_DEBUG) { // debug mode enabled
loader.filter->debug = TRUE; loader.filter->debug = TRUE;
loader.diverter->debug = TRUE; loader.diverter->debug = TRUE;

26
src/utils/assets.c

@ -1,20 +1,44 @@
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#include <signal.h>
#include "logger.h" #include "logger.h"
#include "sundry.h" #include "sundry.h"
#include "system.h" #include "system.h"
#include "constant.h" #include "constant.h"
#include "structure.h"
char **update_file;
char **update_url;
void assets_update();
void extract_asset(const char *file); void extract_asset(const char *file);
void extract_assets() { void assets_init() { // init assets and load update process
log_info("Start loading assets"); log_info("Start loading assets");
create_folder(ASSETS_DIR); create_folder(ASSETS_DIR);
extract_asset(ASSET_GFW_LIST); extract_asset(ASSET_GFW_LIST);
extract_asset(ASSET_CHINA_IP); extract_asset(ASSET_CHINA_IP);
extract_asset(ASSET_CHINA_LIST); extract_asset(ASSET_CHINA_LIST);
log_info("Assets loaded complete"); 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) { void extract_asset(const char *file) {

Loading…
Cancel
Save