Browse Source

update: new common module

dev
dnomd343 2 years ago
parent
commit
a05dc7b0e6
  1. 6
      include/bcrypt/bcrypt.h
  2. 83
      include/common.h
  3. 48
      include/common/common.h
  4. 18
      include/common/json.h
  5. 14
      include/common/structure.h
  6. 10
      include/common/system.h
  7. 6
      src/CMakeLists.txt
  8. 2
      src/applet/CMakeLists.txt
  9. 5
      src/applet/adguard.c
  10. 1
      src/applet/dnsproxy.c
  11. 1
      src/applet/overture.c
  12. 2
      src/bcrypt/CMakeLists.txt
  13. 19
      src/bcrypt/hash.c
  14. 3
      src/common/CMakeLists.txt
  15. 57
      src/common/common.c
  16. 1
      src/common/json.c
  17. 2
      src/common/structure.c
  18. 77
      src/common/system.c
  19. 1
      src/loader/loader.c
  20. 2
      src/loader/parser.c
  21. 2
      src/utils/CMakeLists.txt

6
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
* -------------

83
include/common.h

@ -1,83 +0,0 @@
#ifndef _COMMON_H_
#define _COMMON_H_
//#include <stdlib.h>
//
//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 <stdint.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"
//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

48
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 <stdint.h>
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

18
include/common/json.h

@ -0,0 +1,18 @@
#ifndef _JSON_H_
#define _JSON_H_
#include <stdint.h>
#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

14
include/utils/structure.h → include/common/structure.h

@ -3,13 +3,6 @@
#include <stdint.h>
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

10
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

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

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

5
src/applet/adguard.c

@ -1,8 +1,11 @@
#include <stdlib.h>
#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);

1
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);

1
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);

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

19
src/bcrypt/hash.c

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

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

57
src/common/common.c

@ -0,0 +1,57 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/time.h>
#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
}

1
src/utils/json.c → 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) {

2
src/utils/structure.c → src/common/structure.c

@ -1,8 +1,8 @@
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#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));

77
src/common.c → src/common/system.c

@ -1,60 +1,7 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/time.h>
#include "common.h"
#include <stdio.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
}
#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) {

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

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

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

Loading…
Cancel
Save