Browse Source

feat: convert to json format

dev
dnomd343 2 years ago
parent
commit
d2056b6402
  1. 3
      include/common.h
  2. 6
      include/utils/toJSON.h
  3. 8
      src/cleardns.c
  4. 13
      src/common.c
  5. 2
      src/utils/CMakeLists.txt
  6. 30
      src/utils/toJSON.c

3
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); void uint32_list_debug(char *describe, uint32_t **uint32_list);
uint8_t check_port(uint16_t port); uint8_t check_port(uint16_t port);
uint16_t gen_rand_num(uint16_t limit);
char* uint32_to_string(uint32_t number); char* uint32_to_string(uint32_t number);
int run_command(const char *command);
char* string_init(const char *str); char* string_init(const char *str);
char* string_join(const char *base, const char *add); char* string_join(const char *base, const char *add);

6
include/utils/toJSON.h

@ -0,0 +1,6 @@
#ifndef _TO_JSON_H_
#define _TO_JSON_H_
char* to_json(const char *config_file);
#endif

8
src/cleardns.c

@ -1,10 +1,13 @@
#include <stdio.h> #include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include "loader.h" #include "loader.h"
#include "logger.h" #include "logger.h"
#include "common.h" #include "common.h"
#include "dnsproxy.h" #include "dnsproxy.h"
#include "overture.h" #include "overture.h"
#include "structure.h" #include "structure.h"
#include "toJSON.h"
//#include <stdio.h> //#include <stdio.h>
//#include <string.h> //#include <string.h>
@ -36,6 +39,11 @@ int main(int argc, char *argv[]) { // ClearDNS server
LOG_LEVEL = LOG_DEBUG; LOG_LEVEL = LOG_DEBUG;
log_info("ClearDNS server start (%s)", VERSION); log_info("ClearDNS server start (%s)", VERSION);
char *str = to_json("AdGuardHome.yaml");
log_info("%s", str);
return 0;
// load_config("test.json"); // load_config("test.json");
// //
// dnsproxy_load("Domestic", loader.domestic, "domestic.json"); // dnsproxy_load("Domestic", loader.domestic, "domestic.json");

13
src/common.c

@ -1,6 +1,8 @@
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#include <time.h>
#include <sys/time.h>
#include "common.h" #include "common.h"
#include "logger.h" #include "logger.h"
#include "structure.h" #include "structure.h"
@ -47,6 +49,17 @@ char* uint32_to_string(uint32_t number) {
return string_init(to_str); 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 void save_file(const char *file, const char *content) { // save into file
log_debug("Write into `%s` -> \n%s", file, content); log_debug("Write into `%s` -> \n%s", file, content);
FILE* fp = fopen(file , "w"); FILE* fp = fopen(file , "w");

2
src/utils/CMakeLists.txt

@ -1,3 +1,3 @@
cmake_minimum_required(VERSION 2.8.12) 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)

30
src/utils/toJSON.c

@ -0,0 +1,30 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#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;
}
Loading…
Cancel
Save