Browse Source

refactor: common module

dev
dnomd343 2 years ago
parent
commit
2678ca1a90
  1. 17
      include/common/constant.h
  2. 12
      include/common/json.h
  3. 16
      include/common/sundry.h
  4. 4
      include/common/system.h
  5. 8
      src/applet/adguard.c
  6. 5
      src/applet/dnsproxy.c
  7. 5
      src/applet/overture.c
  8. 13
      src/cleardns.c
  9. 2
      src/common/CMakeLists.txt
  10. 102
      src/common/json.c
  11. 32
      src/common/structure.c
  12. 24
      src/common/sundry.c
  13. 41
      src/common/system.c
  14. 3
      src/loader/config.c
  15. 7
      src/loader/loader.c
  16. 7
      src/loader/parser.c
  17. 2
      src/process_legacy.c
  18. 3
      src/utils/process.c

17
include/common/common.h → include/common/constant.h

@ -1,5 +1,5 @@
#ifndef _COMMON_H_ #ifndef _CONSTANT_H_
#define _COMMON_H_ #define _CONSTANT_H_
#define VERSION "1.3.0-dev" #define VERSION "1.3.0-dev"
@ -32,17 +32,4 @@
#define ASSET_CHINA_IP "china-ip.txt" #define ASSET_CHINA_IP "china-ip.txt"
#define ASSET_CHINA_LIST "chinalist.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 #endif

12
include/common/json.h

@ -5,14 +5,14 @@
#include "cJSON.h" #include "cJSON.h"
char* to_json(const char *config_file); char* to_json(const char *config_file);
cJSON* json_field_get(cJSON *entry, const char *field); uint8_t is_json_suffix(const char *file_name);
void json_field_replace(cJSON *entry, const char *field, cJSON *content); cJSON* json_field_get(cJSON *entry, const char *key);
void json_field_replace(cJSON *entry, const char *key, cJSON *content);
int json_int_value(char *key, cJSON *json);
uint8_t json_bool_value(char *key, cJSON *json); 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); char* json_string_value(char* key, cJSON *json);
int json_int_value(char *key, cJSON *json); char** json_string_list_value(char *key, cJSON *json, char **string_list);
uint8_t is_json_suffix(const char *file_name); uint32_t** json_uint32_list_value(char *key, cJSON *json, uint32_t **uint32_list);
#endif #endif

16
include/common/sundry.h

@ -0,0 +1,16 @@
#ifndef _SUNDRY_H_
#define _SUNDRY_H_
#include <stdint.h>
char* show_bool(uint8_t value);
char* string_init(const char *str);
char* uint32_to_string(uint32_t number);
char* string_join(const char *base, const char *add);
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);
#endif

4
include/common/system.h

@ -1,10 +1,14 @@
#ifndef _SYSTEM_H_ #ifndef _SYSTEM_H_
#define _SYSTEM_H_ #define _SYSTEM_H_
#include <stdint.h>
char* read_file(const char *file); char* read_file(const char *file);
int run_command(const char *command); int run_command(const char *command);
void create_folder(const char *folder); void create_folder(const char *folder);
uint8_t is_file_exist(const char *file);
void save_file(const char *file, const char *content); void save_file(const char *file, const char *content);
void save_string_list(const char *file, char **string_list); void save_string_list(const char *file, char **string_list);
#endif #endif

8
src/applet/adguard.c

@ -1,11 +1,11 @@
#include <stdlib.h> #include <stdlib.h>
#include "cJSON.h" #include "json.h"
#include "bcrypt.h" #include "bcrypt.h"
#include "common.h"
#include "logger.h" #include "logger.h"
#include "adguard.h" #include "sundry.h"
#include "system.h" #include "system.h"
#include "json.h" #include "adguard.h"
#include "constant.h"
void adguard_dump(adguard *info); void adguard_dump(adguard *info);
char *adguard_config(adguard *info, const char *raw_config); char *adguard_config(adguard *info, const char *raw_config);

5
src/applet/dnsproxy.c

@ -1,9 +1,10 @@
#include <stdlib.h> #include <stdlib.h>
#include "cJSON.h" #include "cJSON.h"
#include "common.h"
#include "logger.h" #include "logger.h"
#include "dnsproxy.h" #include "sundry.h"
#include "system.h" #include "system.h"
#include "constant.h"
#include "dnsproxy.h"
#include "structure.h" #include "structure.h"
char* dnsproxy_config(dnsproxy *info); char* dnsproxy_config(dnsproxy *info);

5
src/applet/overture.c

@ -1,9 +1,10 @@
#include <stdlib.h> #include <stdlib.h>
#include "cJSON.h" #include "cJSON.h"
#include "common.h"
#include "logger.h" #include "logger.h"
#include "overture.h" #include "sundry.h"
#include "system.h" #include "system.h"
#include "overture.h"
#include "constant.h"
#include "structure.h" #include "structure.h"
void overture_dump(overture *info); void overture_dump(overture *info);

13
src/cleardns.c

@ -1,13 +1,15 @@
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <time.h> #include <time.h>
#include <unistd.h>
#include "loader.h" #include "loader.h"
#include "logger.h" #include "logger.h"
#include "common.h" #include "constant.h"
#include "dnsproxy.h" #include "dnsproxy.h"
#include "overture.h" #include "overture.h"
#include "structure.h" #include "structure.h"
#include "adguard.h" #include "adguard.h"
#include "system.h"
//#include <stdio.h> //#include <stdio.h>
//#include <string.h> //#include <string.h>
@ -40,6 +42,15 @@ 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);
// if (is_file_exist("/tmp/test")) {
// log_info("File exist");
// }
// create_folder("/tmp/test");
// run_command("exit 3");
run_command("exit");
return 0;
// char **temp = string_list_init(); // char **temp = string_list_init();
// //

2
src/common/CMakeLists.txt

@ -1,3 +1,3 @@
cmake_minimum_required(VERSION 2.8.12) cmake_minimum_required(VERSION 2.8.12)
add_library(common common.c json.c system.c structure.c) add_library(common json.c sundry.c system.c structure.c)

102
src/common/json.c

@ -3,38 +3,63 @@
#include <string.h> #include <string.h>
#include "cJSON.h" #include "cJSON.h"
#include "logger.h" #include "logger.h"
#include "common.h" #include "sundry.h"
#include "system.h" #include "system.h"
#include "constant.h"
#include "structure.h" #include "structure.h"
char* to_json(const char *file) { uint8_t is_json_suffix(const char *file_name) { // whether file name end with `.json` suffix
if (strlen(file_name) <= 5) { // file name shorter than `.json`
return FALSE;
}
if (!strcmp(file_name + strlen(file_name) - 5, ".json")) { // xxx.json
return TRUE;
}
return FALSE;
}
char* to_json(const char *file) { // convert JSON / TOML / YAML to json format (if failed -> return NULL)
char flag[9]; char flag[9];
for (int i = 0; i < 8; ++i) { // generate 8-bits flag for (int i = 0; i < 8; ++i) { // generate 8-bits flag
flag[i] = (char)gen_rand_num(10); flag[i] = (char)gen_rand_num(10);
flag[i] += 48; flag[i] += 48;
} }
flag[8] = '\0'; flag[8] = '\0';
char *output_file = string_join("/tmp/tojson-", flag); char *output_file = string_join("/tmp/tojson-", flag);
char *to_json_cmd = (char *)malloc(strlen(file) + strlen(output_file) + 11); char *to_json_cmd = (char *)malloc(strlen(file) + strlen(output_file) + 11);
sprintf(to_json_cmd, "toJSON %s > %s", file, output_file); sprintf(to_json_cmd, "toJSON %s > %s", file, output_file);
// log_debug("JSON format command -> `%s`", to_json_cmd); int to_json_ret = run_command(to_json_cmd);
if (run_command(to_json_cmd)) { // toJSON return non-zero code
// TODO: try remove output file
return NULL; // convert failed
}
free(to_json_cmd); free(to_json_cmd);
char *remove_cmd = string_join("rm -f ", output_file);
run_command(remove_cmd);
free(remove_cmd);
if (to_json_ret) { // toJSON return non-zero code
free(output_file);
return NULL; // convert failed
}
char *json_content = read_file(output_file); char *json_content = read_file(output_file);
char *rm_cmd = string_join("rm -f ", output_file);
run_command(rm_cmd);
free(output_file); free(output_file);
free(rm_cmd);
return json_content; return json_content;
} }
void json_field_replace(cJSON *entry, const char *field, cJSON *content) { cJSON* json_field_get(cJSON *entry, const char *key) { // fetch key from json map (create when key not exist)
if (!cJSON_ReplaceItemInObject(entry, field, content)) { // field not exist cJSON *sub = entry->child;
cJSON_AddItemToObject(entry, field, content); // add new field while (sub != NULL) { // traverse all keys
if (!strcmp(sub->string, key)) { // target key found
return sub;
}
sub = sub->next;
}
cJSON *new = cJSON_CreateObject(); // create new json key
cJSON_AddItemToObject(entry, key, new);
return new;
}
void json_field_replace(cJSON *entry, const char *key, cJSON *content) {
if (!cJSON_ReplaceItemInObject(entry, key, content)) { // key not exist
cJSON_AddItemToObject(entry, key, content); // add new json key
} }
} }
@ -54,14 +79,21 @@ int json_int_value(char *key, cJSON *json) { // json int or string value -> int
return 0; // never reach return 0; // never reach
} }
char* json_string_value(char* key, cJSON *json) { uint8_t json_bool_value(char *key, cJSON *json) { // json bool value -> bool
if (!cJSON_IsBool(json)) {
log_fatal("`%s` must be boolean", key);
}
return json->valueint;
}
char* json_string_value(char* key, cJSON *json) { // json string value -> string
if (!cJSON_IsString(json)) { if (!cJSON_IsString(json)) {
log_fatal("`%s` must be string", key); log_fatal("`%s` must be string", key);
} }
return string_init(json->valuestring); return string_init(json->valuestring);
} }
char** json_string_list_value(char *key, cJSON *json, char **string_list) { char** json_string_list_value(char *key, cJSON *json, char **string_list) { // json string array -> string list
if (cJSON_IsString(json)) { if (cJSON_IsString(json)) {
string_list = string_list_append(string_list, json->valuestring); string_list = string_list_append(string_list, json->valuestring);
} else if (cJSON_IsArray(json)) { } else if (cJSON_IsArray(json)) {
@ -71,15 +103,15 @@ char** json_string_list_value(char *key, cJSON *json, char **string_list) {
log_fatal("`%s` must be string array", key); log_fatal("`%s` must be string array", key);
} }
string_list = string_list_append(string_list, json->valuestring); string_list = string_list_append(string_list, json->valuestring);
json = json->next; // next field json = json->next; // next key
} }
} else if (!cJSON_IsNull(json)) { } else if (!cJSON_IsNull(json)) { // allow null -> empty string list
log_fatal("`%s` must be array or string", key); log_fatal("`%s` must be array or string", key);
} }
return string_list; return string_list;
} }
uint32_t** json_uint32_list_value(char *key, cJSON *json, uint32_t **uint32_list) { uint32_t** json_uint32_list_value(char *key, cJSON *json, uint32_t **uint32_list) { // json uint32 array -> uint32 list
if (cJSON_IsNumber(json)) { if (cJSON_IsNumber(json)) {
uint32_list = uint32_list_append(uint32_list, json->valueint); uint32_list = uint32_list_append(uint32_list, json->valueint);
} else if (cJSON_IsArray(json)) { } else if (cJSON_IsArray(json)) {
@ -89,40 +121,10 @@ uint32_t** json_uint32_list_value(char *key, cJSON *json, uint32_t **uint32_list
log_fatal("`%s` must be number array", key); log_fatal("`%s` must be number array", key);
} }
uint32_list = uint32_list_append(uint32_list, json->valueint); uint32_list = uint32_list_append(uint32_list, json->valueint);
json = json->next; // next field json = json->next; // next key
} }
} else if (!cJSON_IsNull(json)) { } else if (!cJSON_IsNull(json)) { // allow null -> empty uint32 list
log_fatal("`%s` must be array or number", key); log_fatal("`%s` must be array or number", key);
} }
return uint32_list; return uint32_list;
} }
uint8_t json_bool_value(char *key, cJSON *json) { // json bool value -> bool
if (!cJSON_IsBool(json)) {
log_fatal("`%s` must be boolean", key);
}
return json->valueint;
}
cJSON* json_field_get(cJSON *entry, const char *field) {
cJSON *sub = entry->child;
while (sub != NULL) {
if (!strcmp(sub->string, field)) {
return sub;
}
sub = sub->next; // next field
}
cJSON *new = cJSON_CreateObject();
cJSON_AddItemToObject(entry, field, new);
return new;
}
uint8_t is_json_suffix(const char *file_name) {
if (strlen(file_name) <= 5) { // `.json`
return FALSE;
}
if (!strcmp(file_name + strlen(file_name) - 5, ".json")) {
return TRUE;
}
return FALSE;
}

32
src/common/structure.c

@ -1,14 +1,9 @@
#include <stdio.h> #include <stdio.h>
#include <string.h> #include <stdint.h>
#include <stdlib.h> #include <stdlib.h>
#include "common.h" #include <string.h>
#include "structure.h" #include "sundry.h"
#include "constant.h"
uint32_t* uint32_init(uint32_t number) { // new uint32 (by malloc)
uint32_t *data = (uint32_t *)malloc(sizeof(uint32_t));
*data = number;
return data;
}
char** string_list_init() { // init string list char** string_list_init() { // init string list
char **string_list = (char **)malloc(sizeof(char *)); char **string_list = (char **)malloc(sizeof(char *));
@ -30,20 +25,13 @@ char** string_list_append(char **string_list, const char *string) { // add new s
return string_list; return string_list;
} }
char** string_list_update(char **base_list, char **update_list) { char** string_list_update(char **base_list, char **update_list) { // combine two string list
for (char **string = update_list; *string != NULL; ++string) { for (char **string = update_list; *string != NULL; ++string) {
base_list = string_list_append(base_list, *string); base_list = string_list_append(base_list, *string);
} }
return base_list; return base_list;
} }
uint32_t** uint32_list_update(uint32_t **base_list, uint32_t **update_list) {
for (uint32_t **number = update_list; *number != NULL; ++number) {
base_list = uint32_list_append(base_list, **number);
}
return base_list;
}
void string_list_free(char **string_list) { // free string list void string_list_free(char **string_list) { // free string list
for (char **string = string_list; *string != NULL; ++string) { for (char **string = string_list; *string != NULL; ++string) {
free(*string); free(*string);
@ -80,11 +68,19 @@ uint32_t uint32_list_len(uint32_t **uint32_list) { // get len of uint32 list
uint32_t** uint32_list_append(uint32_t **uint32_list, uint32_t number) { // add new uint32 at the end of list uint32_t** uint32_list_append(uint32_t **uint32_list, uint32_t number) { // add new uint32 at the end of list
uint32_t len = uint32_list_len(uint32_list); uint32_t len = uint32_list_len(uint32_list);
uint32_list = (uint32_t **)realloc(uint32_list, sizeof(uint32_t *) * (len + 2)); uint32_list = (uint32_t **)realloc(uint32_list, sizeof(uint32_t *) * (len + 2));
uint32_list[len] = uint32_init(number); uint32_list[len] = (uint32_t *)malloc(sizeof(uint32_t));
*uint32_list[len] = number;
uint32_list[len + 1] = NULL; // list end sign uint32_list[len + 1] = NULL; // list end sign
return uint32_list; return uint32_list;
} }
uint32_t** uint32_list_update(uint32_t **base_list, uint32_t **update_list) { // combine two uint32 list
for (uint32_t **number = update_list; *number != NULL; ++number) {
base_list = uint32_list_append(base_list, **number);
}
return base_list;
}
void uint32_list_free(uint32_t **uint32_list) { // free uint32 list void uint32_list_free(uint32_t **uint32_list) { // free uint32 list
for (uint32_t **number = uint32_list; *number != NULL; ++number) { for (uint32_t **number = uint32_list; *number != NULL; ++number) {
free(*number); free(*number);

24
src/common/common.c → src/common/sundry.c

@ -2,17 +2,15 @@
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#include <sys/time.h> #include <sys/time.h>
#include "common.h"
#include "logger.h" #include "logger.h"
#include "constant.h"
#include "structure.h" #include "structure.h"
#include "bcrypt.h"
char* show_bool(uint8_t value) { // return `true` or `false` char* show_bool(uint8_t value) { // return `true` or `false`
if (value) { if (value) {
return "true"; return "true";
} else {
return "false";
} }
return "false";
} }
char* string_init(const char *str) { // new string char* string_init(const char *str) { // new string
@ -24,31 +22,31 @@ char* string_join(const char *base, const char *add) { // combine string
return strcat(strcpy(ret, base), add); return strcat(strcpy(ret, base), add);
} }
void string_list_debug(char *describe, char **string_list) { char* uint32_to_string(uint32_t number) { // convert uint32 -> string
char to_str[11]; // MAX_LEN(uint32) -> 4294967296(10-bytes)
sprintf(to_str, "%u", number);
return string_init(to_str);
}
void string_list_debug(char *describe, char **string_list) { // show string list in debug log
char *string_ret = string_list_dump(string_list); char *string_ret = string_list_dump(string_list);
log_debug("%s -> %s", describe, string_ret); log_debug("%s -> %s", describe, string_ret);
free(string_ret); free(string_ret);
} }
void uint32_list_debug(char *describe, uint32_t **uint32_list) { void uint32_list_debug(char *describe, uint32_t **uint32_list) { // show uint32 list in debug log
char *string_ret = uint32_list_dump(uint32_list); char *string_ret = uint32_list_dump(uint32_list);
log_debug("%s -> %s", describe, string_ret); log_debug("%s -> %s", describe, string_ret);
free(string_ret); free(string_ret);
} }
uint8_t check_port(uint16_t port) { uint8_t check_port(uint16_t port) { // whether port is valid
if (port > 0 && port <= 65535) { // 1 ~ 65535 if (port > 0 && port <= 65535) { // 1 ~ 65535
return TRUE; return TRUE;
} }
return FALSE; 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) uint16_t gen_rand_num(uint16_t limit) { // 0 ~ (limit - 1)
struct timeval tv; struct timeval tv;
gettimeofday(&tv, NULL); gettimeofday(&tv, NULL);

41
src/common/system.c

@ -1,25 +1,48 @@
#include <stdlib.h>
#include <stdio.h> #include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/stat.h>
#include "logger.h" #include "logger.h"
#include "common.h" #include "sundry.h"
#include "constant.h"
int run_command(const char *command) { // running command with system shell int run_command(const char *command) { // running command with system shell
log_debug("Run command -> `%s`", command); log_debug("Run command -> `%s`", command);
int ret = system(command) / 256;
// TODO: add non-zero return code warning if (ret != 0) {
log_warn("Command `%s` return non-zero code -> %d", command, ret);
return system(command) / 256; }
return ret;
} }
void create_folder(const char *folder) { void create_folder(const char *folder) { // create folder
if (!access(folder, 0)) { // target is file or folder
struct stat buf;
stat(folder, &buf);
if (!(S_IFDIR & buf.st_mode)) { // target is not folder
log_error("Create folder failed -> target is file");
}
return;
}
log_debug("Create folder -> %s", folder); log_debug("Create folder -> %s", folder);
char *command = string_join("mkdir -p ", folder); char *command = string_join("mkdir -p ", folder);
system(command); // TODO: check if folder exist -> skip or continue system(command);
free(command); free(command);
} }
uint8_t is_file_exist(const char *file) { // whether file exist
if (!access(file, 0)) { // target is file or folder
struct stat buf;
stat(file, &buf);
if (S_IFREG & buf.st_mode) { // target is file
return TRUE;
}
}
return FALSE;
}
void save_file(const char *file, const char *content) { // save content into file void save_file(const char *file, const char *content) { // save content into file
log_debug("Write into `%s` -> \n%s", file, content); log_debug("Write into `%s` ->\n%s", file, content);
FILE* fp = fopen(file , "w"); FILE* fp = fopen(file , "w");
if (fp == NULL) { if (fp == NULL) {
log_fatal("Fail to open file -> %s", file); log_fatal("Fail to open file -> %s", file);

3
src/loader/config.c

@ -1,7 +1,8 @@
#include <stdlib.h> #include <stdlib.h>
#include "config.h" #include "config.h"
#include "common.h"
#include "logger.h" #include "logger.h"
#include "sundry.h"
#include "constant.h"
#include "structure.h" #include "structure.h"
cleardns_config* config_init() { // init config struct of cleardns cleardns_config* config_init() { // init config struct of cleardns

7
src/loader/loader.c

@ -1,11 +1,12 @@
#include <stdlib.h> #include <stdlib.h>
#include "loader.h"
#include "config.h" #include "config.h"
#include "loader.h"
#include "parser.h" #include "parser.h"
#include "sundry.h"
#include "system.h"
#include "dnsproxy.h" #include "dnsproxy.h"
#include "constant.h"
#include "structure.h" #include "structure.h"
#include "system.h"
#include "common.h"
struct cleardns loader; struct cleardns loader;

7
src/loader/parser.c

@ -1,11 +1,10 @@
#include <string.h> #include <string.h>
#include <stdlib.h> #include <stdlib.h>
#include "cJSON.h" #include "json.h"
#include "common.h"
#include "logger.h"
#include "config.h" #include "config.h"
#include "logger.h"
#include "sundry.h"
#include "system.h" #include "system.h"
#include "json.h"
void cache_parser(cache_config *config, cJSON *json) { // cache options parser void cache_parser(cache_config *config, cJSON *json) { // cache options parser
if (!cJSON_IsObject(json)) { if (!cJSON_IsObject(json)) {

2
src/process_legacy.c

@ -6,7 +6,7 @@
#include <unistd.h> #include <unistd.h>
#include <sys/wait.h> #include <sys/wait.h>
#include <sys/prctl.h> #include <sys/prctl.h>
#include "common.h" #include "constant.h"
#include "flag.h" #include "flag.h"
int exiting = 0; int exiting = 0;

3
src/utils/process.c

@ -1,7 +1,8 @@
#include <stdlib.h> #include <stdlib.h>
#include "sundry.h"
#include "process.h" #include "process.h"
#include "constant.h"
#include "structure.h" #include "structure.h"
#include "common.h"
process* process_init(const char *caption, const char *bin) { // init process struct process* process_init(const char *caption, const char *bin) { // init process struct
process *proc = (process *)malloc(sizeof(process)); process *proc = (process *)malloc(sizeof(process));

Loading…
Cancel
Save