diff --git a/include/applet/adguard.h b/include/applet/adguard.h index b3ebb61..9d8af96 100644 --- a/include/applet/adguard.h +++ b/include/applet/adguard.h @@ -1,8 +1,7 @@ #ifndef _ADGUARD_H_ #define _ADGUARD_H_ -#include "common.h" -#include "process.h" +#include typedef struct { uint8_t debug; // bool value diff --git a/include/applet/dnsproxy.h b/include/applet/dnsproxy.h index 56b8979..c8597de 100644 --- a/include/applet/dnsproxy.h +++ b/include/applet/dnsproxy.h @@ -1,7 +1,7 @@ #ifndef _DNSPROXY_H_ #define _DNSPROXY_H_ -#include "common.h" +#include #include "process.h" typedef struct { diff --git a/include/applet/overture.h b/include/applet/overture.h index 2cdd5d7..610e148 100644 --- a/include/applet/overture.h +++ b/include/applet/overture.h @@ -1,7 +1,7 @@ #ifndef _OVERTURE_H_ #define _OVERTURE_H_ -#include "common.h" +#include #include "process.h" typedef struct { diff --git a/include/common.h b/include/common.h index a513baa..aa12ca1 100644 --- a/include/common.h +++ b/include/common.h @@ -1,12 +1,14 @@ #ifndef _COMMON_H_ #define _COMMON_H_ -#include +//#include +// +//typedef u_int8_t uint8_t; +//typedef u_int16_t uint16_t; +//typedef u_int32_t uint32_t; +//typedef u_int64_t uint64_t; -typedef u_int8_t uint8_t; -typedef u_int16_t uint16_t; -typedef u_int32_t uint32_t; -typedef u_int64_t uint64_t; +#include #define VERSION "1.3.0-dev" @@ -41,6 +43,8 @@ void save_file(const char *file, const char *content); void string_list_debug(char *describe, char **string_list); void uint32_list_debug(char *describe, uint32_t **uint32_list); +uint8_t check_port(uint16_t port); + char* string_init(const char *str); char* string_join(const char *base, const char *add); diff --git a/include/utils/process.h b/include/utils/process.h index ace4522..ce9d8c5 100644 --- a/include/utils/process.h +++ b/include/utils/process.h @@ -1,9 +1,6 @@ #ifndef _PROCESS_H_ #define _PROCESS_H_ -//void server_daemon(); -//void init_server(char *init_script, char *custom_script); - typedef struct { char *name; char **cmd; diff --git a/include/utils/structure.h b/include/utils/structure.h index 43dfd1f..660171a 100644 --- a/include/utils/structure.h +++ b/include/utils/structure.h @@ -1,7 +1,7 @@ -#ifndef _STR_H_ -#define _STR_H_ +#ifndef _STRUCTURE_H_ +#define _STRUCTURE_H_ -#include "common.h" +#include uint32_t** uint32_list_init(); char* uint32_list_dump(uint32_t **int_list); diff --git a/src/applet/CMakeLists.txt b/src/applet/CMakeLists.txt index afc98b7..3394051 100644 --- a/src/applet/CMakeLists.txt +++ b/src/applet/CMakeLists.txt @@ -1,3 +1,4 @@ cmake_minimum_required(VERSION 2.8.12) add_library(applet adguard.c dnsproxy.c overture.c) +target_link_libraries(applet utils) diff --git a/src/applet/adguard.c b/src/applet/adguard.c index df49112..ae1d237 100644 --- a/src/applet/adguard.c +++ b/src/applet/adguard.c @@ -1,3 +1,5 @@ +#include +#include "common.h" #include "adguard.h" adguard* adguard_init() { diff --git a/src/applet/dnsproxy.c b/src/applet/dnsproxy.c index 15bf5d3..e13149a 100644 --- a/src/applet/dnsproxy.c +++ b/src/applet/dnsproxy.c @@ -1,10 +1,9 @@ #include +#include "cJSON.h" +#include "common.h" +#include "logger.h" #include "dnsproxy.h" #include "structure.h" -#include "process.h" -#include "logger.h" -#include "common.h" -#include "cJSON.h" char* dnsproxy_config(dnsproxy *info); void dnsproxy_dump(const char *caption, dnsproxy *info); @@ -58,11 +57,14 @@ void dnsproxy_dump(const char *caption, dnsproxy *info) { // show dnsproxy info } process* dnsproxy_load(const char *caption, dnsproxy *info, const char *file) { - - // TODO: check primary server number (non-zero) - // TODO: check port (1 ~ 65535) - dnsproxy_dump(caption, info); + if (!check_port(info->port)) { // invalid server port + log_fatal("Invalid port `%u`", info->port); + } + if (string_list_len(info->primary) == 0) { // without primary dns server + log_fatal("%s without primary dns server", caption); + } + char *config = dnsproxy_config(info); // string config (JSON format) char *config_file = string_join(WORK_DIR, file); save_file(config_file, config); diff --git a/src/applet/overture.c b/src/applet/overture.c index 2405c5f..0e71f4d 100644 --- a/src/applet/overture.c +++ b/src/applet/overture.c @@ -1,12 +1,12 @@ -#include +#include // TODO: use int to string instead of sprintf #include -#include "overture.h" -#include "process.h" +#include "cJSON.h" #include "common.h" #include "logger.h" -#include "cJSON.h" +#include "overture.h" #include "structure.h" + void overture_dump(overture *info); char* overture_config(overture *info); diff --git a/src/cleardns.c b/src/cleardns.c index 7123644..56294e1 100644 --- a/src/cleardns.c +++ b/src/cleardns.c @@ -36,10 +36,10 @@ int main(int argc, char *argv[]) { // ClearDNS server LOG_LEVEL = LOG_DEBUG; log_info("ClearDNS server start (%s)", VERSION); - load_config("test.json"); - - dnsproxy_load("Domestic", loader.domestic, "domestic.json"); - dnsproxy_load("Foreign", loader.foreign, "foreign.json"); +// load_config("test.json"); +// +// dnsproxy_load("Domestic", loader.domestic, "domestic.json"); +// dnsproxy_load("Foreign", loader.foreign, "foreign.json"); // char **temp = string_list_init(); // temp = string_list_append(temp, "123"); @@ -62,27 +62,27 @@ int main(int argc, char *argv[]) { // ClearDNS server // uint32_list_free(temp); -// dnsproxy *domestic = dnsproxy_init(DOMESTIC_PORT); -// -// dnsproxy_add_bootstrap(domestic, "1.1.1.1"); -// dnsproxy_add_bootstrap(domestic, "8.8.8.8"); -// -// dnsproxy_add_primary(domestic, "223.5.5.5"); -// dnsproxy_add_primary(domestic, "tls://dns.pub"); -// -// dnsproxy_add_fallback(domestic, "tls://223.6.6.6"); -// dnsproxy_add_fallback(domestic, "tls://120.53.53.53"); -// -// domestic->verify = FALSE; -// domestic->parallel = FALSE; -// domestic->optimistic = TRUE; -// domestic->debug = TRUE; -// domestic->cache = 4194304; // 4MiB -// -// process *p = dnsproxy_load("Domestic", domestic, "domestic.json"); -// log_info("cmd -> %s", string_list_dump(p->cmd)); -// log_info("env -> %s", string_list_dump(p->env)); -// log_info("cwd -> %s", p->cwd); + dnsproxy *domestic = dnsproxy_init(DOMESTIC_PORT); + + dnsproxy_add_bootstrap(domestic, "1.1.1.1"); + dnsproxy_add_bootstrap(domestic, "8.8.8.8"); + + dnsproxy_add_primary(domestic, "223.5.5.5"); + dnsproxy_add_primary(domestic, "tls://dns.pub"); + + dnsproxy_add_fallback(domestic, "tls://223.6.6.6"); + dnsproxy_add_fallback(domestic, "tls://120.53.53.53"); + + domestic->verify = FALSE; + domestic->parallel = FALSE; + domestic->optimistic = TRUE; + domestic->debug = TRUE; + domestic->cache = 4194304; // 4MiB + + process *p = dnsproxy_load("Domestic", domestic, "domestic.json"); + log_info("cmd -> %s", string_list_dump(p->cmd)); + log_info("env -> %s", string_list_dump(p->env)); + log_info("cwd -> %s", p->cwd); // overture *diverter = overture_init(DIVERTER_PORT); diff --git a/src/common.c b/src/common.c index d4c5466..bff9288 100644 --- a/src/common.c +++ b/src/common.c @@ -34,6 +34,13 @@ void uint32_list_debug(char *describe, uint32_t **uint32_list) { free(string_ret); } +uint8_t check_port(uint16_t port) { + if (port > 0 && port <= 65535) { // 1 ~ 65535 + return TRUE; + } + return FALSE; +} + void save_file(const char *file, const char *content) { // save into file log_debug("Write into `%s` -> \n%s", file, content); FILE* fp = fopen(file , "w"); diff --git a/src/loader/config.c b/src/loader/config.c index 5f69079..d47c2b6 100644 --- a/src/loader/config.c +++ b/src/loader/config.c @@ -1,3 +1,4 @@ +#include #include "common.h" #include "config.h" #include "logger.h" diff --git a/src/loader/loader.c b/src/loader/loader.c index aa5730e..0019858 100644 --- a/src/loader/loader.c +++ b/src/loader/loader.c @@ -1,3 +1,4 @@ +#include #include "loader.h" #include "config.h" #include "parser.h" diff --git a/src/utils/process.c b/src/utils/process.c index f4ad4a1..d63aa90 100644 --- a/src/utils/process.c +++ b/src/utils/process.c @@ -1,6 +1,7 @@ -#include "common.h" +#include #include "process.h" #include "structure.h" +#include "common.h" process* process_init(const char *caption, const char *bin) { // init process struct process *proc = (process *)malloc(sizeof(process)); diff --git a/src/utils/structure.c b/src/utils/structure.c index 08f4281..95f081d 100644 --- a/src/utils/structure.c +++ b/src/utils/structure.c @@ -1,7 +1,9 @@ #include -#include + #include +#include #include "structure.h" +#include "common.h" uint32_t* uint32_init(uint32_t number) { // new uint32 (by malloc) uint32_t *data = (uint32_t *)malloc(sizeof(uint32_t));