Browse Source

feat: dnsproxy framework

dev
dnomd343 2 years ago
parent
commit
6db861ed61
  1. 13
      include/common.h
  2. 18
      include/dnsproxy.h
  3. 4
      include/utils/logger.h
  4. 11
      include/utils/strList.h
  5. 2
      src/CMakeLists.txt
  6. 6
      src/cleardns.c
  7. 2
      src/common.c
  8. 54
      src/dnsproxy.c
  9. 2
      src/utils/CMakeLists.txt
  10. 2
      src/utils/cJSON.c
  11. 47
      src/utils/strList.c

13
include/common.h

@ -3,11 +3,22 @@
#define VERSION "1.3.0-dev" #define VERSION "1.3.0-dev"
#define TRUE 1
#define FALSE 0
#define DOMESTIC_PORT 4053
#define FOREIGN_PORT 6053
#define DNSPROXY_BIN "dnsproxy"
#define OVERTURE_BIN "overture"
#define ADGUARD_BIN "AdGuardHome"
//extern char **adguard_command; //extern char **adguard_command;
//extern char **overture_command; //extern char **overture_command;
//extern char **domestic_dnsproxy_command; //extern char **domestic_dnsproxy_command;
//extern char **foreign_dnsproxy_command; //extern char **foreign_dnsproxy_command;
//
//void load_start_command(char *adguard_workdir, char *overture_config, char *upstream_config, int is_debug); //void load_start_command(char *adguard_workdir, char *overture_config, char *upstream_config, int is_debug);
#endif #endif

18
include/dnsproxy.h

@ -0,0 +1,18 @@
#ifndef _DNSPROXY_H_
#define _DNSPROXY_H_
typedef struct {
int port;
int verify; // bool value
int parallel; // bool value
int optimistic; // bool value
char **bootstrap;
char **fallback;
char **primary;
char *cache;
} dnsproxy;
dnsproxy* dnsproxy_init(int port);
void dnsproxy_dump(char *caption, dnsproxy *info);
#endif

4
include/utils/logger.h

@ -1,5 +1,5 @@
#ifndef _LOG_H #ifndef _LOGGER_H_
#define _LOG_H #define _LOGGER_H_
enum { enum {
LOG_DEBUG, LOG_DEBUG,

11
include/utils/strList.h

@ -0,0 +1,11 @@
#ifndef _STRLIST_H_
#define _STRLIST_H_
char* string_init(char *str);
char** string_list_init();
int string_list_len(char **string_list);
char* string_list_dump(char **string_list);
char** string_list_append(char **string_list, char *string);
#endif

2
src/CMakeLists.txt

@ -5,5 +5,5 @@ include_directories(${PROJECT_SOURCE_DIR}/include/utils)
add_subdirectory(utils) add_subdirectory(utils)
add_executable(cleardns cleardns.c) add_executable(cleardns cleardns.c dnsproxy.c)
target_link_libraries(cleardns utils) target_link_libraries(cleardns utils)

6
src/cleardns.c

@ -1,5 +1,6 @@
#include "logger.h" #include "logger.h"
#include "common.h" #include "common.h"
#include "dnsproxy.h"
//#include <stdio.h> //#include <stdio.h>
//#include <string.h> //#include <string.h>
@ -27,8 +28,13 @@
int main(int argc, char *argv[]) { // ClearDNS server int main(int argc, char *argv[]) { // ClearDNS server
LOG_LEVEL = LOG_DEBUG;
log_info("ClearDNS server start (%s)", VERSION); log_info("ClearDNS server start (%s)", VERSION);
dnsproxy *domestic = dnsproxy_init(DOMESTIC_PORT);
dnsproxy_dump("Domestic", domestic);
// int debug_mode = 0; // int debug_mode = 0;
// fprintf(stderr, "[ClearDNS] Server start.\n"); // fprintf(stderr, "[ClearDNS] Server start.\n");
// for (char **p = argv; p < argv + argc; ++p) { // for (char **p = argv; p < argv + argc; ++p) {

2
src/common.c

@ -1,6 +1,6 @@
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <strList.bak>
#include "cJSON.h" #include "cJSON.h"
#include "flag.h" #include "flag.h"

54
src/dnsproxy.c

@ -0,0 +1,54 @@
#include <stdlib.h>
#include "dnsproxy.h"
#include "logger.h"
#include "common.h"
#include "strList.h"
char* show_bool(int value) {
if (value) {
return "true";
} else {
return "false";
}
}
char** command_init(char *file) {
return string_list_append(string_list_init(), file);
}
dnsproxy* dnsproxy_init(int port) {
dnsproxy *info = (dnsproxy*)malloc(sizeof(dnsproxy));
info->port = port;
info->verify = TRUE;
info->parallel = TRUE;
info->optimistic = FALSE;
info->bootstrap = string_list_init();
info->fallback = string_list_init();
info->primary = string_list_init();
info->cache = "4194304"; // 4MiB
return info;
}
void dnsproxy_dump(char *caption, dnsproxy *info) {
log_debug("%s listen port -> %d", caption, info->port);
log_debug("%s verify -> %s", caption, show_bool(info->verify));
log_debug("%s parallel -> %s", caption, show_bool(info->parallel));
log_debug("%s optimistic -> %s", caption, show_bool(info->optimistic));
log_debug("%s bootstrap -> %s", caption, string_list_dump(info->bootstrap));
log_debug("%s fallback -> %s", caption, string_list_dump(info->fallback));
log_debug("%s primary -> %s", caption, string_list_dump(info->primary));
log_debug("%s cache -> %s", caption, info->cache);
char **test = string_list_init();
test = string_list_append(test, "0");
test = string_list_append(test, "1");
test = string_list_append(test, "2");
// char *test[] = {NULL};
// char *test[] = {"dnomd343", "2333", NULL};
log_warn("%s", string_list_dump(test));
log_warn("%d", string_list_len(test));
}

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) add_library(utils logger.c strList.c)

2
src/utils/cJSON.c

@ -37,7 +37,7 @@
#pragma warning (disable : 4001) #pragma warning (disable : 4001)
#endif #endif
#include <string.h> #include <strList.bak>
#include <stdio.h> #include <stdio.h>
#include <math.h> #include <math.h>
#include <stdlib.h> #include <stdlib.h>

47
src/utils/strList.c

@ -0,0 +1,47 @@
#include <stdlib.h>
#include <string.h>
#include "strList.h"
char* string_init(char *str) {
return strcpy((char*)malloc(strlen(str) + 1), str);
}
char* string_list_dump(char **string_list) { // ['a', 'b', 'c', ...]
unsigned long string_len = 0;
for (char **string = string_list; *string != NULL; ++string) {
string_len += strlen(*string) + 4;
}
if (string_len == 0) { // empty string
string_len = 2;
}
char *string_ret = (char *)malloc(sizeof(char) * (string_len + 1));
string_ret[0] = '[';
string_ret[1] = 0x00;
for (char **string = string_list; *string != NULL; ++string) {
string_ret = strcat(strcat(string_ret, "'"), *string);
string_ret = strcat(string_ret, "', ");
}
string_ret[string_len - 1] = ']';
string_ret[string_len] = 0x00;
return string_ret;
}
int string_list_len(char **string_list) {
int num = 0;
while(string_list[num++] != NULL); // get string list size
return num - 1;
}
char** string_list_init() {
char **string_list = (char**)malloc(sizeof(char*));
*string_list = NULL;
return string_list;
}
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[len] = string_init(string);
string_list[len + 1] = NULL; // list end sign
return string_list;
}
Loading…
Cancel
Save