diff --git a/include/common.h b/include/common.h index 870ba1a..6f7ef33 100644 --- a/include/common.h +++ b/include/common.h @@ -13,6 +13,7 @@ #define OVERTURE_BIN "overture" #define ADGUARD_BIN "AdGuardHome" +#define WORK_DIR "/etc/cleardns/" //extern char **adguard_command; //extern char **overture_command; @@ -23,6 +24,6 @@ char* show_bool(int value); char** command_init(char *bin); -void save_file(char *file, char *data); +void save_file(char *file, char *content); #endif diff --git a/include/dnsproxy.h b/include/dnsproxy.h index 076e00d..261db00 100644 --- a/include/dnsproxy.h +++ b/include/dnsproxy.h @@ -1,6 +1,8 @@ #ifndef _DNSPROXY_H_ #define _DNSPROXY_H_ +#include "process.h" + typedef struct { int port; int cache; @@ -21,4 +23,6 @@ void dnsproxy_add_bootstrap(dnsproxy *info, char *server); char* dnsproxy_gen_config(dnsproxy *info); +process* dnsproxy_load(char *caption, dnsproxy *info, char *work_dir, char *file); + #endif diff --git a/include/process.h b/include/process.h index ec5254b..3488308 100644 --- a/include/process.h +++ b/include/process.h @@ -1,7 +1,13 @@ #ifndef _PROCESS_H_ #define _PROCESS_H_ -void server_daemon(); -void init_server(char *init_script, char *custom_script); +//void server_daemon(); +//void init_server(char *init_script, char *custom_script); + +typedef struct { + char **cmd; + char **env; + char *cwd; +} process; #endif diff --git a/include/utils/strList.h b/include/utils/str.h similarity index 69% rename from include/utils/strList.h rename to include/utils/str.h index 02ede62..94ae2b5 100644 --- a/include/utils/strList.h +++ b/include/utils/str.h @@ -1,7 +1,8 @@ -#ifndef _STRLIST_H_ -#define _STRLIST_H_ +#ifndef _STR_H_ +#define _STR_H_ char* string_init(char *str); +char* string_join(char *base, const char *add, int is_new); char** string_list_init(); int string_list_len(char **string_list); diff --git a/src/cleardns.c b/src/cleardns.c index ad96ea0..ccabe72 100644 --- a/src/cleardns.c +++ b/src/cleardns.c @@ -2,7 +2,7 @@ #include "common.h" #include "dnsproxy.h" -#include "strList.h" +#include "str.h" //#include //#include @@ -52,10 +52,13 @@ int main(int argc, char *argv[]) { // ClearDNS server dnsproxy_dump("Domestic", domestic); - char *config = dnsproxy_gen_config(domestic); - log_info("\n%s", config); +// char *config = dnsproxy_gen_config(domestic); +// log_info("\n%s", config); - save_file("/tmp/test.txt", config); +// log_info("%s", string_join(WORK_DIR, "domestic.json", TRUE)); + + process *p = dnsproxy_load("Domestic", domestic, WORK_DIR, "domestic.json"); + log_info("%s", string_list_dump(p->cmd)); // int debug_mode = 0; // fprintf(stderr, "[ClearDNS] Server start.\n"); diff --git a/src/common.c b/src/common.c index 543529a..d3c33ba 100644 --- a/src/common.c +++ b/src/common.c @@ -1,7 +1,7 @@ #include #include "common.h" #include "logger.h" -#include "strList.h" +#include "str.h" char* show_bool(int value) { if (value) { diff --git a/src/common_legacy.c b/src/common_legacy.c index 0803a4e..ae9d59e 100644 --- a/src/common_legacy.c +++ b/src/common_legacy.c @@ -41,7 +41,7 @@ char* read_file(char *file_name) { // read file content char** command_add_field(char **command_list, char *field) { // add field into command int num = 0; while(command_list[num++] != NULL); // get options number - command_list = (char**)realloc(command_list, sizeof(char**) * (num + 1)); + command_list = (char**)realloc(command_list, sizeof(char*) * (num + 1)); command_list[num - 1] = strcpy((char*)malloc(strlen(field) + 1), field); command_list[num] = NULL; // end sign return command_list; diff --git a/src/dnsproxy.c b/src/dnsproxy.c index 534a5a4..64d80cd 100644 --- a/src/dnsproxy.c +++ b/src/dnsproxy.c @@ -1,6 +1,7 @@ #include #include "dnsproxy.h" -#include "strList.h" +#include "str.h" +#include "process.h" #include "logger.h" #include "common.h" #include "cJSON.h" @@ -83,3 +84,16 @@ char* dnsproxy_gen_config(dnsproxy *info) { cJSON_Delete(config); return config_str; } + +process* dnsproxy_load(char *caption, dnsproxy *info, char *work_dir, char *file) { + char *config = dnsproxy_gen_config(info); + + save_file(string_join(work_dir, file, TRUE), config); + + process *demo = (process*)malloc(sizeof(process)); + demo->cmd = command_init(DNSPROXY_BIN); + demo->env = string_list_init(); + demo->cwd = work_dir; + + return demo; +} diff --git a/src/process.c b/src/process_legacy.c similarity index 100% rename from src/process.c rename to src/process_legacy.c diff --git a/src/utils/CMakeLists.txt b/src/utils/CMakeLists.txt index 4fd3913..1c11971 100644 --- a/src/utils/CMakeLists.txt +++ b/src/utils/CMakeLists.txt @@ -1,3 +1,3 @@ cmake_minimum_required(VERSION 2.8.12) -add_library(utils logger.c strList.c cJSON.c) +add_library(utils logger.c str.c cJSON.c) diff --git a/src/utils/strList.c b/src/utils/str.c similarity index 69% rename from src/utils/strList.c rename to src/utils/str.c index a2ee359..936fe76 100644 --- a/src/utils/strList.c +++ b/src/utils/str.c @@ -1,11 +1,22 @@ #include #include -#include "strList.h" +#include "str.h" char* string_init(char *str) { return strcpy((char*)malloc(strlen(str) + 1), str); } +char* string_join(char *base, const char *add, int is_new) { + unsigned long string_len = strlen(base) + strlen(add); + if (is_new) { + char *ret = (char*)malloc(sizeof(char) * (string_len + 1)); + return strcat(strcpy(ret, base), add); + } else { + base = (char*)realloc(base, sizeof(char) * (string_len + 1)); + return strcat(base, add); + } +} + char* string_list_dump(char **string_list) { // ['a', 'b', 'c', ...] unsigned long string_len = 0; for (char **string = string_list; *string != NULL; ++string) { @@ -14,7 +25,7 @@ char* string_list_dump(char **string_list) { // ['a', 'b', 'c', ...] if (string_len == 0) { // empty string string_len = 2; } - char *string_ret = (char *)malloc(sizeof(char) * (string_len + 1)); + char *string_ret = (char*)malloc(sizeof(char) * (string_len + 1)); string_ret[0] = '['; string_ret[1] = 0x00; for (char **string = string_list; *string != NULL; ++string) { @@ -40,7 +51,7 @@ char** string_list_init() { char** string_list_append(char **string_list, char *string) { int len = string_list_len(string_list); - string_list = (char**)realloc(string_list, sizeof(char**) * (len + 2)); + string_list = (char**)realloc(string_list, sizeof(char*) * (len + 2)); string_list[len] = string_init(string); string_list[len + 1] = NULL; // list end sign return string_list;