From af97ee8d3194bc3b019ab06efe5a3c4e272947a9 Mon Sep 17 00:00:00 2001 From: dnomd343 Date: Mon, 12 Sep 2022 00:39:00 +0800 Subject: [PATCH] feat: overture interface --- include/overture.h | 5 +++-- src/cleardns.c | 8 ++++---- src/overture.c | 21 +++++++++++++++++++++ 3 files changed, 28 insertions(+), 6 deletions(-) diff --git a/include/overture.h b/include/overture.h index c129cb2..8533c08 100644 --- a/include/overture.h +++ b/include/overture.h @@ -1,6 +1,8 @@ #ifndef _OVERTURE_H_ #define _OVERTURE_H_ +#include "process.h" + typedef struct { int port; int timeout; @@ -13,7 +15,6 @@ typedef struct { } overture; overture* overture_init(int port); -void overture_dump(overture *info); -char* overture_gen_config(overture *info); +process* overture_load(overture *info, const char *file); #endif diff --git a/src/cleardns.c b/src/cleardns.c index c3a6202..e66f8dc 100644 --- a/src/cleardns.c +++ b/src/cleardns.c @@ -62,10 +62,10 @@ int main(int argc, char *argv[]) { // ClearDNS server diverter->domestic_domain_file = "chinalist.txt"; diverter->foreign_domain_file = "gfwlist.txt"; - overture_dump(diverter); - - char *config = overture_gen_config(diverter); - log_info("\n%s", config); + process *p = overture_load(diverter, "overture.json"); + log_info("cmd -> %s", string_list_dump(p->cmd)); + log_info("env -> %s", string_list_dump(p->env)); + log_info("cwd -> %s", p->cwd); // int debug_mode = 0; // fprintf(stderr, "[ClearDNS] Server start.\n"); diff --git a/src/overture.c b/src/overture.c index 3db0a03..2614a11 100644 --- a/src/overture.c +++ b/src/overture.c @@ -1,11 +1,15 @@ #include #include #include "overture.h" +#include "process.h" #include "common.h" #include "logger.h" #include "cJSON.h" #include "str.h" +void overture_dump(overture *info); +char* overture_gen_config(overture *info); + overture* overture_init(int port) { // init overture options overture *info = (overture*)malloc(sizeof(overture)); info->port = port; @@ -30,6 +34,23 @@ void overture_dump(overture *info) { // show overture info in debug log log_debug("Overture domestic domain file -> %s", info->domestic_domain_file); } +process* overture_load(overture *info, const char *file) { + overture_dump(info); + char *config = overture_gen_config(info); // string config (JSON format) + char *config_file = string_join(WORK_DIR, file); + save_file(config_file, config); + free(config_file); + free(config); + + process *p = (process*)malloc(sizeof(process)); + p->cmd = string_list_append(string_list_init(), OVERTURE_BIN); + p->cmd = string_list_append(p->cmd, "-c"); + p->cmd = string_list_append(p->cmd, file); + p->env = string_list_init(); + p->cwd = WORK_DIR; + return p; +} + char* overture_gen_config(overture *info) { // generate json configure from overture options cJSON *config = cJSON_CreateObject(); char port_str[12]; // 32-bits (MAX_LEN -> -2147483648 -> 12-bytes)