Browse Source

feat: save info file

dev
dnomd343 2 years ago
parent
commit
5277c413b3
  1. 1
      include/common.h
  2. 2
      include/dnsproxy.h
  3. 5
      src/cleardns.c
  4. 13
      src/common.c
  5. 10
      src/dnsproxy.c

1
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

2
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

5
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");

13
src/common.c

@ -1,4 +1,6 @@
#include <stdio.h>
#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);
}

10
src/dnsproxy.c

@ -1,10 +1,8 @@
#include <stdlib.h>
#include <string.h>
#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;
}

Loading…
Cancel
Save