From d2056b64024e8abb3203c3eccd2b9b301db8348c Mon Sep 17 00:00:00 2001 From: dnomd343 Date: Tue, 13 Sep 2022 23:08:09 +0800 Subject: [PATCH] feat: convert to json format --- include/common.h | 3 +++ include/utils/toJSON.h | 6 ++++++ src/cleardns.c | 8 ++++++++ src/common.c | 13 +++++++++++++ src/utils/CMakeLists.txt | 2 +- src/utils/toJSON.c | 30 ++++++++++++++++++++++++++++++ 6 files changed, 61 insertions(+), 1 deletion(-) create mode 100644 include/utils/toJSON.h create mode 100644 src/utils/toJSON.c diff --git a/include/common.h b/include/common.h index b70aa51..5518d3d 100644 --- a/include/common.h +++ b/include/common.h @@ -44,8 +44,11 @@ void string_list_debug(char *describe, char **string_list); void uint32_list_debug(char *describe, uint32_t **uint32_list); uint8_t check_port(uint16_t port); +uint16_t gen_rand_num(uint16_t limit); char* uint32_to_string(uint32_t number); +int run_command(const char *command); + char* string_init(const char *str); char* string_join(const char *base, const char *add); diff --git a/include/utils/toJSON.h b/include/utils/toJSON.h new file mode 100644 index 0000000..3ea5552 --- /dev/null +++ b/include/utils/toJSON.h @@ -0,0 +1,6 @@ +#ifndef _TO_JSON_H_ +#define _TO_JSON_H_ + +char* to_json(const char *config_file); + +#endif diff --git a/src/cleardns.c b/src/cleardns.c index 56294e1..0837dea 100644 --- a/src/cleardns.c +++ b/src/cleardns.c @@ -1,10 +1,13 @@ #include +#include +#include #include "loader.h" #include "logger.h" #include "common.h" #include "dnsproxy.h" #include "overture.h" #include "structure.h" +#include "toJSON.h" //#include //#include @@ -36,6 +39,11 @@ int main(int argc, char *argv[]) { // ClearDNS server LOG_LEVEL = LOG_DEBUG; log_info("ClearDNS server start (%s)", VERSION); + char *str = to_json("AdGuardHome.yaml"); + log_info("%s", str); + return 0; + + // load_config("test.json"); // // dnsproxy_load("Domestic", loader.domestic, "domestic.json"); diff --git a/src/common.c b/src/common.c index 036e1c6..cce8d0d 100644 --- a/src/common.c +++ b/src/common.c @@ -1,6 +1,8 @@ #include #include #include +#include +#include #include "common.h" #include "logger.h" #include "structure.h" @@ -47,6 +49,17 @@ char* uint32_to_string(uint32_t number) { return string_init(to_str); } +uint16_t gen_rand_num(uint16_t limit) { // 0 ~ (limit - 1) + struct timeval tv; + gettimeofday(&tv, NULL); + srand(tv.tv_usec); + return rand() % limit; // NOLINT +} + +int run_command(const char *command) { + return system(command) / 256; +} + void save_file(const char *file, const char *content) { // save into file log_debug("Write into `%s` -> \n%s", file, content); FILE* fp = fopen(file , "w"); diff --git a/src/utils/CMakeLists.txt b/src/utils/CMakeLists.txt index 3cff9f5..192d2e7 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 cJSON.c logger.c process.c structure.c) +add_library(utils cJSON.c toJSON.c logger.c process.c structure.c) diff --git a/src/utils/toJSON.c b/src/utils/toJSON.c new file mode 100644 index 0000000..12bc90a --- /dev/null +++ b/src/utils/toJSON.c @@ -0,0 +1,30 @@ +#include +#include +#include +#include "toJSON.h" +#include "logger.h" +#include "common.h" + +char* to_json(const char *file) { + char flag[9]; + for (int i = 0; i < 8; ++i) { // generate 8-bits flag + flag[i] = (char)gen_rand_num(10); + flag[i] += 48; + } + flag[8] = '\0'; + char *output_file = string_join("/tmp/tojson-", flag); + char *to_json_cmd = (char *)malloc(strlen(file) + strlen(output_file) + 11); + sprintf(to_json_cmd, "toJSON %s > %s", file, output_file); + log_debug("JSON format command -> `%s`", to_json_cmd); + if (run_command(to_json_cmd)) { // toJSON return non-zero code + return NULL; // convert failed + } + free(to_json_cmd); + + char *json_content = read_file(output_file); + char *rm_cmd = string_join("rm -f ", output_file); + run_command(rm_cmd); + free(output_file); + free(rm_cmd); + return json_content; +}