From 5277c413b33b3eb3d2827fa43ba003d495788f4e Mon Sep 17 00:00:00 2001 From: dnomd343 Date: Sun, 11 Sep 2022 19:25:38 +0800 Subject: [PATCH] feat: save info file --- include/common.h | 1 + include/dnsproxy.h | 2 +- src/cleardns.c | 5 ++++- src/common.c | 13 +++++++++++++ src/dnsproxy.c | 10 +++++----- 5 files changed, 24 insertions(+), 7 deletions(-) diff --git a/include/common.h b/include/common.h index 6e6b71c..870ba1a 100644 --- a/include/common.h +++ b/include/common.h @@ -23,5 +23,6 @@ char* show_bool(int value); char** command_init(char *bin); +void save_file(char *file, char *data); #endif diff --git a/include/dnsproxy.h b/include/dnsproxy.h index b193d22..076e00d 100644 --- a/include/dnsproxy.h +++ b/include/dnsproxy.h @@ -19,6 +19,6 @@ void dnsproxy_add_primary(dnsproxy *info, char *server); void dnsproxy_add_fallback(dnsproxy *info, char *server); void dnsproxy_add_bootstrap(dnsproxy *info, char *server); -void dnsproxy_gen_config(dnsproxy *info); +char* dnsproxy_gen_config(dnsproxy *info); #endif diff --git a/src/cleardns.c b/src/cleardns.c index b8c5dfc..ad96ea0 100644 --- a/src/cleardns.c +++ b/src/cleardns.c @@ -52,7 +52,10 @@ int main(int argc, char *argv[]) { // ClearDNS server dnsproxy_dump("Domestic", domestic); - dnsproxy_gen_config(domestic); + char *config = dnsproxy_gen_config(domestic); + log_info("\n%s", config); + + save_file("/tmp/test.txt", config); // int debug_mode = 0; // fprintf(stderr, "[ClearDNS] Server start.\n"); diff --git a/src/common.c b/src/common.c index e09e394..543529a 100644 --- a/src/common.c +++ b/src/common.c @@ -1,4 +1,6 @@ +#include #include "common.h" +#include "logger.h" #include "strList.h" char* show_bool(int value) { @@ -12,3 +14,14 @@ char* show_bool(int value) { char** command_init(char *bin) { return string_list_append(string_list_init(), bin); } + +void save_file(char *file, char *content) { + log_debug("Write into `%s` -> \n%s", file, content); + FILE* fp = fopen(file , "w"); + if (fp == NULL) { + log_fatal("Fail to open file -> %s", file); + } + fputs(content, fp); + fclose(fp); + log_debug("Save `%s` success", file); +} diff --git a/src/dnsproxy.c b/src/dnsproxy.c index 9d39ec7..534a5a4 100644 --- a/src/dnsproxy.c +++ b/src/dnsproxy.c @@ -1,10 +1,8 @@ #include -#include #include "dnsproxy.h" +#include "strList.h" #include "logger.h" #include "common.h" -#include "strList.h" - #include "cJSON.h" dnsproxy* dnsproxy_init(int port) { @@ -43,7 +41,7 @@ void dnsproxy_add_bootstrap(dnsproxy *info, char *server) { info->bootstrap = string_list_append(info->bootstrap, server); } -void dnsproxy_gen_config(dnsproxy *info) { +char* dnsproxy_gen_config(dnsproxy *info) { cJSON *config = cJSON_CreateObject(); if (!info->verify) { cJSON_AddTrueToObject(config, "insecure"); // insecure --(default)--> `false` @@ -81,5 +79,7 @@ void dnsproxy_gen_config(dnsproxy *info) { } cJSON_AddItemToObject(config, "upstream", primary); - log_info("\n%s", cJSON_Print(config)); + char *config_str = cJSON_Print(config); + cJSON_Delete(config); + return config_str; }