Browse Source

update: dnsproxy process

dev
dnomd343 2 years ago
parent
commit
43882a9de5
  1. 3
      include/common.h
  2. 4
      include/dnsproxy.h
  3. 10
      include/process.h
  4. 5
      include/utils/str.h
  5. 11
      src/cleardns.c
  6. 2
      src/common.c
  7. 2
      src/common_legacy.c
  8. 16
      src/dnsproxy.c
  9. 0
      src/process_legacy.c
  10. 2
      src/utils/CMakeLists.txt
  11. 15
      src/utils/str.c

3
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

4
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

10
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

5
include/utils/strList.h → 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);

11
src/cleardns.c

@ -2,7 +2,7 @@
#include "common.h"
#include "dnsproxy.h"
#include "strList.h"
#include "str.h"
//#include <stdio.h>
//#include <string.h>
@ -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");

2
src/common.c

@ -1,7 +1,7 @@
#include <stdio.h>
#include "common.h"
#include "logger.h"
#include "strList.h"
#include "str.h"
char* show_bool(int value) {
if (value) {

2
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;

16
src/dnsproxy.c

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

0
src/process.c → src/process_legacy.c

2
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)

15
src/utils/strList.c → src/utils/str.c

@ -1,11 +1,22 @@
#include <stdlib.h>
#include <string.h>
#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) {
@ -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;
Loading…
Cancel
Save