diff --git a/include/bcrypt/bcrypt.h b/include/bcrypt/bcrypt.h index e45b3ca..d7d4df8 100644 --- a/include/bcrypt/bcrypt.h +++ b/include/bcrypt/bcrypt.h @@ -59,6 +59,12 @@ int bcrypt_hashpw(const char *passwd, const char salt[BCRYPT_HASHSIZE], */ int bcrypt_checkpw(const char *passwd, const char hash[BCRYPT_HASHSIZE]); +/* + * This function expects a string and return bcrypt result (random salt) + */ + +char* bcrypt_cal(const char *data); + /* * Brief Example * ------------- diff --git a/include/common.h b/include/common.h deleted file mode 100644 index 4db91aa..0000000 --- a/include/common.h +++ /dev/null @@ -1,83 +0,0 @@ -#ifndef _COMMON_H_ -#define _COMMON_H_ - -//#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; - -#include - -#define VERSION "1.3.0-dev" - -#define TRUE 1 -#define FALSE 0 - -#define DNS_PORT 53 -#define ADGUARD_PORT 80 -#define DIVERTER_PORT 5353 -#define DOMESTIC_PORT 4053 -#define FOREIGN_PORT 6053 - -#define DIVERTER_TIMEOUT 6 - -#define ADGUARD_USER "admin" -#define ADGUARD_PASSWD "adguard" - -#define DNSPROXY_BIN "dnsproxy" -#define OVERTURE_BIN "overture" -#define ADGUARD_BIN "AdGuardHome" - -#define WORK_DIR "/etc/cleardns/" -#define EXPOSE_DIR "/cleardns/" -#define ASSETS_DIR "/cleardns/assets/" -#define ADGUARD_DIR "/cleardns/adguard/" - -#define ASSET_TTL "ttl.txt" -#define ASSET_HOSTS "hosts.txt" -#define ASSET_GFW_LIST "gfwlist.txt" -#define ASSET_CHINA_IP "china-ip.txt" -#define ASSET_CHINA_LIST "chinalist.txt" - -//extern char **adguard_command; -//extern char **overture_command; -//extern char **domestic_dnsproxy_command; -//extern char **foreign_dnsproxy_command; - -//void load_start_command(char *adguard_workdir, char *overture_config, char *upstream_config, int is_debug); - -char* show_bool(uint8_t value); -char* read_file(const char *file); -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); -uint16_t gen_rand_num(uint16_t limit); -char* uint32_to_string(uint32_t number); - -void save_string_list(const char *file, char **string_list); - -char* gen_bcrypt(const char *data); -int run_command(const char *command); -void create_folder(const char *folder); - -char* string_init(const char *str); -char* string_join(const char *base, const char *add); - -#include "cJSON.h" - -char* to_json(const char *config_file); -cJSON* json_field_get(cJSON *entry, const char *field); -void json_field_replace(cJSON *entry, const char *field, cJSON *content); - -uint8_t json_bool_value(char *key, cJSON *json); -uint32_t** json_uint32_list_value(char *key, cJSON *json, uint32_t **uint32_list); -char** json_string_list_value(char *key, cJSON *json, char **string_list); -char* json_string_value(char* key, cJSON *json); -int json_int_value(char *key, cJSON *json); -uint8_t is_json_suffix(const char *file_name); - -#endif diff --git a/include/common/common.h b/include/common/common.h new file mode 100644 index 0000000..6fe2750 --- /dev/null +++ b/include/common/common.h @@ -0,0 +1,48 @@ +#ifndef _COMMON_H_ +#define _COMMON_H_ + +#define VERSION "1.3.0-dev" + +#define TRUE 1 +#define FALSE 0 + +#define DNS_PORT 53 +#define ADGUARD_PORT 80 +#define DIVERTER_PORT 5353 +#define DOMESTIC_PORT 4053 +#define FOREIGN_PORT 6053 + +#define DIVERTER_TIMEOUT 6 + +#define ADGUARD_USER "admin" +#define ADGUARD_PASSWD "adguard" + +#define DNSPROXY_BIN "dnsproxy" +#define OVERTURE_BIN "overture" +#define ADGUARD_BIN "AdGuardHome" + +#define WORK_DIR "/etc/cleardns/" +#define EXPOSE_DIR "/cleardns/" +#define ASSETS_DIR "/cleardns/assets/" +#define ADGUARD_DIR "/cleardns/adguard/" + +#define ASSET_TTL "ttl.txt" +#define ASSET_HOSTS "hosts.txt" +#define ASSET_GFW_LIST "gfwlist.txt" +#define ASSET_CHINA_IP "china-ip.txt" +#define ASSET_CHINA_LIST "chinalist.txt" + +#include + +char* show_bool(uint8_t value); +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); +uint16_t gen_rand_num(uint16_t limit); +char* uint32_to_string(uint32_t number); + +char* string_init(const char *str); +char* string_join(const char *base, const char *add); + +#endif diff --git a/include/common/json.h b/include/common/json.h new file mode 100644 index 0000000..db6c9ba --- /dev/null +++ b/include/common/json.h @@ -0,0 +1,18 @@ +#ifndef _JSON_H_ +#define _JSON_H_ + +#include +#include "cJSON.h" + +char* to_json(const char *config_file); +cJSON* json_field_get(cJSON *entry, const char *field); +void json_field_replace(cJSON *entry, const char *field, cJSON *content); + +uint8_t json_bool_value(char *key, cJSON *json); +uint32_t** json_uint32_list_value(char *key, cJSON *json, uint32_t **uint32_list); +char** json_string_list_value(char *key, cJSON *json, char **string_list); +char* json_string_value(char* key, cJSON *json); +int json_int_value(char *key, cJSON *json); +uint8_t is_json_suffix(const char *file_name); + +#endif diff --git a/include/utils/structure.h b/include/common/structure.h similarity index 100% rename from include/utils/structure.h rename to include/common/structure.h index d308940..403844c 100644 --- a/include/utils/structure.h +++ b/include/common/structure.h @@ -3,13 +3,6 @@ #include -uint32_t** uint32_list_init(); -char* uint32_list_dump(uint32_t **int_list); -void uint32_list_free(uint32_t **uint32_list); -uint32_t uint32_list_len(uint32_t **int_list); -uint32_t** uint32_list_append(uint32_t **int_list, uint32_t number); -uint32_t** uint32_list_update(uint32_t **base_list, uint32_t **update_list); - char** string_list_init(); void string_list_free(char **string_list); char* string_list_dump(char **string_list); @@ -17,4 +10,11 @@ uint32_t string_list_len(char **string_list); char** string_list_update(char **base_list, char **update_list); char** string_list_append(char **string_list, const char *string); +uint32_t** uint32_list_init(); +char* uint32_list_dump(uint32_t **int_list); +void uint32_list_free(uint32_t **uint32_list); +uint32_t uint32_list_len(uint32_t **int_list); +uint32_t** uint32_list_append(uint32_t **int_list, uint32_t number); +uint32_t** uint32_list_update(uint32_t **base_list, uint32_t **update_list); + #endif diff --git a/include/common/system.h b/include/common/system.h new file mode 100644 index 0000000..5dd00d0 --- /dev/null +++ b/include/common/system.h @@ -0,0 +1,10 @@ +#ifndef _SYSTEM_H_ +#define _SYSTEM_H_ + +char* read_file(const char *file); +int run_command(const char *command); +void create_folder(const char *folder); +void save_file(const char *file, const char *content); +void save_string_list(const char *file, char **string_list); + +#endif diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index f0f85d1..08534e5 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -4,12 +4,14 @@ include_directories(${PROJECT_SOURCE_DIR}/include) include_directories(${PROJECT_SOURCE_DIR}/include/utils) include_directories(${PROJECT_SOURCE_DIR}/include/applet) include_directories(${PROJECT_SOURCE_DIR}/include/bcrypt) +include_directories(${PROJECT_SOURCE_DIR}/include/common) include_directories(${PROJECT_SOURCE_DIR}/include/loader) add_subdirectory(utils) add_subdirectory(applet) add_subdirectory(bcrypt) +add_subdirectory(common) add_subdirectory(loader) -add_executable(cleardns cleardns.c common.c) -target_link_libraries(cleardns utils applet bcrypt loader) +add_executable(cleardns cleardns.c) +target_link_libraries(cleardns utils applet bcrypt common loader) diff --git a/src/applet/CMakeLists.txt b/src/applet/CMakeLists.txt index 3394051..64b58e9 100644 --- a/src/applet/CMakeLists.txt +++ b/src/applet/CMakeLists.txt @@ -1,4 +1,4 @@ cmake_minimum_required(VERSION 2.8.12) add_library(applet adguard.c dnsproxy.c overture.c) -target_link_libraries(applet utils) +target_link_libraries(applet bcrypt utils) diff --git a/src/applet/adguard.c b/src/applet/adguard.c index f17726c..6e1aec0 100644 --- a/src/applet/adguard.c +++ b/src/applet/adguard.c @@ -1,8 +1,11 @@ #include #include "cJSON.h" +#include "bcrypt.h" #include "common.h" #include "logger.h" #include "adguard.h" +#include "system.h" +#include "json.h" void adguard_dump(adguard *info); char *adguard_config(adguard *info, const char *raw_config); @@ -44,7 +47,7 @@ char *adguard_config(adguard *info, const char *raw_config) { // modify adguard cJSON *user_config = cJSON_CreateObject(); // setting up username and password cJSON *users_config = cJSON_CreateArray(); - char *password = gen_bcrypt(info->password); + char *password = bcrypt_cal(info->password); cJSON_AddItemToObject(user_config, "name", cJSON_CreateString(info->username)); cJSON_AddItemToObject(user_config, "password", cJSON_CreateString(password)); cJSON_AddItemToArray(users_config, user_config); diff --git a/src/applet/dnsproxy.c b/src/applet/dnsproxy.c index fc0d148..834f4cd 100644 --- a/src/applet/dnsproxy.c +++ b/src/applet/dnsproxy.c @@ -3,6 +3,7 @@ #include "common.h" #include "logger.h" #include "dnsproxy.h" +#include "system.h" #include "structure.h" char* dnsproxy_config(dnsproxy *info); diff --git a/src/applet/overture.c b/src/applet/overture.c index 6c9debb..55cbe8c 100644 --- a/src/applet/overture.c +++ b/src/applet/overture.c @@ -3,6 +3,7 @@ #include "common.h" #include "logger.h" #include "overture.h" +#include "system.h" #include "structure.h" void overture_dump(overture *info); diff --git a/src/bcrypt/CMakeLists.txt b/src/bcrypt/CMakeLists.txt index 470e79b..174d813 100644 --- a/src/bcrypt/CMakeLists.txt +++ b/src/bcrypt/CMakeLists.txt @@ -4,5 +4,5 @@ include_directories(${PROJECT_SOURCE_DIR}/include/bcrypt/blowfish) add_subdirectory(blowfish) -add_library(bcrypt bcrypt.c) +add_library(bcrypt bcrypt.c hash.c) target_link_libraries(bcrypt blowfish) diff --git a/src/bcrypt/hash.c b/src/bcrypt/hash.c new file mode 100644 index 0000000..929ff20 --- /dev/null +++ b/src/bcrypt/hash.c @@ -0,0 +1,19 @@ +#include +#include "bcrypt.h" +#include "logger.h" + +char* bcrypt_cal(const char *data) { + char salt[BCRYPT_HASHSIZE]; + log_debug("BCrypt data -> `%s`", data); + if (bcrypt_gensalt(10, salt)) { + log_fatal("BCrypt generate salt error"); + } + log_debug("BCrypt salt -> `%s`", salt); + + char *hash = (char *)malloc(BCRYPT_HASHSIZE); + if (bcrypt_hashpw(data, salt, hash)) { + log_fatal("BCrypt generate hash error"); + } + log_debug("BCrypt hash -> `%s`", hash); + return hash; +} diff --git a/src/common/CMakeLists.txt b/src/common/CMakeLists.txt new file mode 100644 index 0000000..7766163 --- /dev/null +++ b/src/common/CMakeLists.txt @@ -0,0 +1,3 @@ +cmake_minimum_required(VERSION 2.8.12) + +add_library(common common.c json.c system.c structure.c) diff --git a/src/common/common.c b/src/common/common.c new file mode 100644 index 0000000..fb4521b --- /dev/null +++ b/src/common/common.c @@ -0,0 +1,57 @@ +#include +#include +#include +#include +#include "common.h" +#include "logger.h" +#include "structure.h" +#include "bcrypt.h" + +char* show_bool(uint8_t value) { // return `true` or `false` + if (value) { + return "true"; + } else { + return "false"; + } +} + +char* string_init(const char *str) { // new string + return strcpy((char *)malloc(strlen(str) + 1), str); +} + +char* string_join(const char *base, const char *add) { // combine string + char *ret = (char *)malloc(strlen(base) + strlen(add) + 1); + return strcat(strcpy(ret, base), add); +} + +void string_list_debug(char *describe, char **string_list) { + char *string_ret = string_list_dump(string_list); + log_debug("%s -> %s", describe, string_ret); + free(string_ret); +} + +void uint32_list_debug(char *describe, uint32_t **uint32_list) { + char *string_ret = uint32_list_dump(uint32_list); + log_debug("%s -> %s", describe, string_ret); + free(string_ret); +} + +uint8_t check_port(uint16_t port) { + if (port > 0 && port <= 65535) { // 1 ~ 65535 + return TRUE; + } + return FALSE; +} + +char* uint32_to_string(uint32_t number) { + char to_str[11]; // 32-bits (MAX_LEN -> 4294967296 -> 10-bytes) + sprintf(to_str, "%u", number); + return string_init(to_str); +} + +uint16_t gen_rand_num(uint16_t limit) { // 0 ~ (limit - 1) + struct timeval tv; + gettimeofday(&tv, NULL); + srand(tv.tv_usec); + return rand() % limit; // NOLINT +} diff --git a/src/utils/json.c b/src/common/json.c similarity index 99% rename from src/utils/json.c rename to src/common/json.c index 8500bc4..b5ef3b3 100644 --- a/src/utils/json.c +++ b/src/common/json.c @@ -4,6 +4,7 @@ #include "cJSON.h" #include "logger.h" #include "common.h" +#include "system.h" #include "structure.h" char* to_json(const char *file) { diff --git a/src/utils/structure.c b/src/common/structure.c similarity index 100% rename from src/utils/structure.c rename to src/common/structure.c index ea0416b..78a83e9 100644 --- a/src/utils/structure.c +++ b/src/common/structure.c @@ -1,8 +1,8 @@ #include #include #include -#include "structure.h" #include "common.h" +#include "structure.h" uint32_t* uint32_init(uint32_t number) { // new uint32 (by malloc) uint32_t *data = (uint32_t *)malloc(sizeof(uint32_t)); diff --git a/src/common.c b/src/common/system.c similarity index 50% rename from src/common.c rename to src/common/system.c index 98b443a..110e9b1 100644 --- a/src/common.c +++ b/src/common/system.c @@ -1,60 +1,7 @@ -#include #include -#include -#include -#include "common.h" +#include #include "logger.h" -#include "structure.h" -#include "bcrypt.h" - -char* show_bool(uint8_t value) { // return `true` or `false` - if (value) { - return "true"; - } else { - return "false"; - } -} - -char* string_init(const char *str) { // new string - return strcpy((char *)malloc(strlen(str) + 1), str); -} - -char* string_join(const char *base, const char *add) { // combine string - char *ret = (char *)malloc(strlen(base) + strlen(add) + 1); - return strcat(strcpy(ret, base), add); -} - -void string_list_debug(char *describe, char **string_list) { - char *string_ret = string_list_dump(string_list); - log_debug("%s -> %s", describe, string_ret); - free(string_ret); -} - -void uint32_list_debug(char *describe, uint32_t **uint32_list) { - char *string_ret = uint32_list_dump(uint32_list); - log_debug("%s -> %s", describe, string_ret); - free(string_ret); -} - -uint8_t check_port(uint16_t port) { - if (port > 0 && port <= 65535) { // 1 ~ 65535 - return TRUE; - } - return FALSE; -} - -char* uint32_to_string(uint32_t number) { - char to_str[11]; // 32-bits (MAX_LEN -> 4294967296 -> 10-bytes) - sprintf(to_str, "%u", number); - return string_init(to_str); -} - -uint16_t gen_rand_num(uint16_t limit) { // 0 ~ (limit - 1) - struct timeval tv; - gettimeofday(&tv, NULL); - srand(tv.tv_usec); - return rand() % limit; // NOLINT -} +#include "common.h" int run_command(const char *command) { // running command with system shell log_debug("Run command -> `%s`", command); @@ -64,30 +11,14 @@ int run_command(const char *command) { // running command with system shell return system(command) / 256; } -char* gen_bcrypt(const char *data) { - char salt[BCRYPT_HASHSIZE]; - log_debug("BCrypt data -> `%s`", data); - if (bcrypt_gensalt(10, salt)) { - log_fatal("BCrypt generate salt error"); - } - log_debug("BCrypt salt -> `%s`", salt); - - char *hash = (char *)malloc(BCRYPT_HASHSIZE); - if (bcrypt_hashpw(data, salt, hash)) { - log_fatal("BCrypt generate hash error"); - } - log_debug("BCrypt hash -> `%s`", hash); - return hash; -} - void create_folder(const char *folder) { log_debug("Create folder -> %s", folder); char *command = string_join("mkdir -p ", folder); - system(command); + system(command); // TODO: check if folder exist -> skip or continue free(command); } -void save_file(const char *file, const char *content) { // save into file +void save_file(const char *file, const char *content) { // save content into file log_debug("Write into `%s` -> \n%s", file, content); FILE* fp = fopen(file , "w"); if (fp == NULL) { diff --git a/src/loader/loader.c b/src/loader/loader.c index d7303bd..6619e1b 100644 --- a/src/loader/loader.c +++ b/src/loader/loader.c @@ -4,6 +4,7 @@ #include "parser.h" #include "dnsproxy.h" #include "structure.h" +#include "system.h" #include "common.h" struct cleardns loader; diff --git a/src/loader/parser.c b/src/loader/parser.c index 9cce1ec..ce0d114 100644 --- a/src/loader/parser.c +++ b/src/loader/parser.c @@ -4,6 +4,8 @@ #include "common.h" #include "logger.h" #include "config.h" +#include "system.h" +#include "json.h" void cache_parser(cache_config *config, cJSON *json) { // cache options parser if (!cJSON_IsObject(json)) { diff --git a/src/utils/CMakeLists.txt b/src/utils/CMakeLists.txt index 901bb2a..bab7775 100644 --- a/src/utils/CMakeLists.txt +++ b/src/utils/CMakeLists.txt @@ -1,3 +1,3 @@ cmake_minimum_required(VERSION 2.8.12) -add_library(utils cJSON.c json.c logger.c process.c structure.c) +add_library(utils cJSON.c logger.c process.c)