Browse Source

perf: dnsproxy module

dev
dnomd343 2 years ago
parent
commit
6c5685b029
  1. 13
      include/dnsproxy.h
  2. 6
      include/utils/str.h
  3. 19
      src/cleardns.c
  4. 74
      src/dnsproxy.c
  5. 40
      src/utils/str.c

13
include/dnsproxy.h

@ -15,14 +15,9 @@ typedef struct {
} dnsproxy; } dnsproxy;
dnsproxy* dnsproxy_init(int port); dnsproxy* dnsproxy_init(int port);
void dnsproxy_dump(char *caption, dnsproxy *info); void dnsproxy_add_primary(dnsproxy *info, const char *server);
void dnsproxy_add_fallback(dnsproxy *info, const char *server);
void dnsproxy_add_primary(dnsproxy *info, char *server); void dnsproxy_add_bootstrap(dnsproxy *info, const char *server);
void dnsproxy_add_fallback(dnsproxy *info, char *server); process* dnsproxy_load(const char *caption, dnsproxy *info, const char *file);
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 #endif

6
include/utils/str.h

@ -1,12 +1,12 @@
#ifndef _STR_H_ #ifndef _STR_H_
#define _STR_H_ #define _STR_H_
char* string_init(char *str); char* string_init(const char *str);
char* string_join(char *base, const char *add, int is_new); char* string_join(const char *base, const char *add);
char** string_list_init(); char** string_list_init();
int string_list_len(char **string_list); int string_list_len(char **string_list);
char* string_list_dump(char **string_list); char* string_list_dump(char **string_list);
char** string_list_append(char **string_list, char *string); char** string_list_append(char **string_list, const char *string);
#endif #endif

19
src/cleardns.c

@ -1,7 +1,6 @@
#include "logger.h" #include "logger.h"
#include "common.h" #include "common.h"
#include "dnsproxy.h" #include "dnsproxy.h"
#include "str.h" #include "str.h"
//#include <stdio.h> //#include <stdio.h>
@ -31,7 +30,6 @@
int main(int argc, char *argv[]) { // ClearDNS server 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);
dnsproxy *domestic = dnsproxy_init(DOMESTIC_PORT); dnsproxy *domestic = dnsproxy_init(DOMESTIC_PORT);
@ -47,18 +45,13 @@ int main(int argc, char *argv[]) { // ClearDNS server
// domestic->verify = FALSE; // domestic->verify = FALSE;
// domestic->parallel = FALSE; // domestic->parallel = FALSE;
// domestic->optimistic = TRUE; domestic->optimistic = TRUE;
domestic->cache = 0; // domestic->cache = 0;
dnsproxy_dump("Domestic", domestic);
// char *config = dnsproxy_gen_config(domestic);
// log_info("\n%s", config);
// log_info("%s", string_join(WORK_DIR, "domestic.json", TRUE));
process *p = dnsproxy_load("Domestic", domestic, WORK_DIR, "domestic.json"); process *p = dnsproxy_load("Domestic", domestic, "domestic.json");
log_info("%s", string_list_dump(p->cmd)); log_info("cmd -> %s", string_list_dump(p->cmd));
log_info("env -> %s", string_list_dump(p->env));
log_info("cwd -> %s", p->cwd);
// int debug_mode = 0; // int debug_mode = 0;
// fprintf(stderr, "[ClearDNS] Server start.\n"); // fprintf(stderr, "[ClearDNS] Server start.\n");

74
src/dnsproxy.c

@ -6,7 +6,22 @@
#include "common.h" #include "common.h"
#include "cJSON.h" #include "cJSON.h"
dnsproxy* dnsproxy_init(int port) { char* dnsproxy_gen_config(dnsproxy *info);
void dnsproxy_dump(const char *caption, dnsproxy *info);
void dnsproxy_add_primary(dnsproxy *info, const char *server) { // add primary dns server
info->primary = string_list_append(info->primary, server);
}
void dnsproxy_add_fallback(dnsproxy *info, const char *server) { // add fallback dns server
info->fallback = string_list_append(info->fallback, server);
}
void dnsproxy_add_bootstrap(dnsproxy *info, const char *server) { // add bootstrap dns server
info->bootstrap = string_list_append(info->bootstrap, server);
}
dnsproxy* dnsproxy_init(int port) { // init dnsproxy options
dnsproxy *info = (dnsproxy*)malloc(sizeof(dnsproxy)); dnsproxy *info = (dnsproxy*)malloc(sizeof(dnsproxy));
info->port = port; info->port = port;
info->verify = TRUE; info->verify = TRUE;
@ -19,7 +34,7 @@ dnsproxy* dnsproxy_init(int port) {
return info; return info;
} }
void dnsproxy_dump(char *caption, dnsproxy *info) { void dnsproxy_dump(const char *caption, dnsproxy *info) { // show dnsproxy info in debug log
log_debug("%s listen port -> %d", caption, info->port); log_debug("%s listen port -> %d", caption, info->port);
log_debug("%s verify -> %s", caption, show_bool(info->verify)); log_debug("%s verify -> %s", caption, show_bool(info->verify));
log_debug("%s parallel -> %s", caption, show_bool(info->parallel)); log_debug("%s parallel -> %s", caption, show_bool(info->parallel));
@ -30,70 +45,63 @@ void dnsproxy_dump(char *caption, dnsproxy *info) {
log_debug("%s cache -> %d", caption, info->cache); log_debug("%s cache -> %d", caption, info->cache);
} }
void dnsproxy_add_primary(dnsproxy *info, char *server) { process* dnsproxy_load(const char *caption, dnsproxy *info, const char *file) {
info->primary = string_list_append(info->primary, server); dnsproxy_dump(caption, info);
} char *config = dnsproxy_gen_config(info); // string config (JSON format)
char *config_file = string_join(WORK_DIR, file);
save_file(config_file, config);
free(config_file);
free(config);
void dnsproxy_add_fallback(dnsproxy *info, char *server) { char *option = string_join("--config-path=", file);
info->fallback = string_list_append(info->fallback, server); process *p = (process*)malloc(sizeof(process));
} p->cmd = command_init(DNSPROXY_BIN);
p->cmd = string_list_append(p->cmd, option);
void dnsproxy_add_bootstrap(dnsproxy *info, char *server) { p->env = string_list_init();
info->bootstrap = string_list_append(info->bootstrap, server); p->cwd = WORK_DIR;
free(option);
return p;
} }
char* dnsproxy_gen_config(dnsproxy *info) { char* dnsproxy_gen_config(dnsproxy *info) { // generate json configure from dnsproxy options
cJSON *config = cJSON_CreateObject(); cJSON *config = cJSON_CreateObject();
if (!info->verify) { if (!info->verify) {
cJSON_AddTrueToObject(config, "insecure"); // insecure --(default)--> `false` cJSON_AddTrueToObject(config, "insecure"); // insecure --(default)--> `false`
} }
if (info->parallel) { if (info->parallel) {
cJSON_AddTrueToObject(config, "all-servers"); cJSON_AddTrueToObject(config, "all-servers"); // all-servers --(default)--> `false`
} }
if (info->cache) { if (info->cache) {
cJSON_AddTrueToObject(config, "cache"); cJSON_AddTrueToObject(config, "cache"); // cache --(default)--> `false`
cJSON_AddNumberToObject(config, "cache-size", info->cache); cJSON_AddNumberToObject(config, "cache-size", info->cache);
} }
if (info->optimistic) { if (info->optimistic) {
cJSON_AddTrueToObject(config, "cache-optimistic"); cJSON_AddTrueToObject(config, "cache-optimistic"); // cache-optimistic --(default)--> `false`
} }
cJSON *port = cJSON_CreateArray(); cJSON *port = cJSON_CreateArray();
cJSON_AddItemToArray(port, cJSON_CreateNumber(info->port)); cJSON_AddItemToArray(port, cJSON_CreateNumber(info->port));
cJSON_AddItemToObject(config, "listen-ports", port); cJSON_AddItemToObject(config, "listen-ports", port); // listen-ports <=> port
cJSON *bootstrap = cJSON_CreateArray(); cJSON *bootstrap = cJSON_CreateArray();
for (char **server = info->bootstrap; *server != NULL; ++server) { for (char **server = info->bootstrap; *server != NULL; ++server) {
cJSON_AddItemToArray(bootstrap, cJSON_CreateString(*server)); cJSON_AddItemToArray(bootstrap, cJSON_CreateString(*server));
} }
cJSON_AddItemToObject(config, "bootstrap", bootstrap); cJSON_AddItemToObject(config, "bootstrap", bootstrap); // bootstrap <=> bootstrap
cJSON *fallback = cJSON_CreateArray(); cJSON *fallback = cJSON_CreateArray();
for (char **server = info->fallback; *server != NULL; ++server) { for (char **server = info->fallback; *server != NULL; ++server) {
cJSON_AddItemToArray(fallback, cJSON_CreateString(*server)); cJSON_AddItemToArray(fallback, cJSON_CreateString(*server));
} }
cJSON_AddItemToObject(config, "fallback", fallback); cJSON_AddItemToObject(config, "fallback", fallback); // fallback <=> fallback
cJSON *primary = cJSON_CreateArray(); cJSON *primary = cJSON_CreateArray();
for (char **server = info->primary; *server != NULL; ++server) { for (char **server = info->primary; *server != NULL; ++server) {
cJSON_AddItemToArray(primary, cJSON_CreateString(*server)); cJSON_AddItemToArray(primary, cJSON_CreateString(*server));
} }
cJSON_AddItemToObject(config, "upstream", primary); cJSON_AddItemToObject(config, "upstream", primary); // upstream <=> primary
char *config_str = cJSON_Print(config); char *config_str = cJSON_Print(config);
cJSON_Delete(config); cJSON_Delete(config); // free json object
return config_str; 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;
}

40
src/utils/str.c

@ -2,19 +2,25 @@
#include <string.h> #include <string.h>
#include "str.h" #include "str.h"
char* string_init(char *str) { char* string_init(const char *str) {
return strcpy((char*)malloc(strlen(str) + 1), str); return strcpy((char*)malloc(strlen(str) + 1), str);
} }
char* string_join(char *base, const char *add, int is_new) { char* string_join(const char *base, const char *add) {
unsigned long string_len = strlen(base) + strlen(add); char *ret = (char*)malloc(sizeof(char) * (strlen(base) + strlen(add) + 1));
if (is_new) { return strcat(strcpy(ret, base), add);
char *ret = (char*)malloc(sizeof(char) * (string_len + 1)); }
return strcat(strcpy(ret, base), add);
} else { char** string_list_init() {
base = (char*)realloc(base, sizeof(char) * (string_len + 1)); char **string_list = (char**)malloc(sizeof(char*));
return strcat(base, add); *string_list = NULL;
} return string_list;
}
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_dump(char **string_list) { // ['a', 'b', 'c', ...] char* string_list_dump(char **string_list) { // ['a', 'b', 'c', ...]
@ -37,19 +43,7 @@ char* string_list_dump(char **string_list) { // ['a', 'b', 'c', ...]
return string_ret; return string_ret;
} }
int string_list_len(char **string_list) { char** string_list_append(char **string_list, const char *string) {
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); 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] = string_init(string);

Loading…
Cancel
Save