Browse Source

feat: assets options parser

dev
Dnomd343 2 years ago
parent
commit
1451a330da
  1. 11
      include/common/json.h
  2. 4
      include/common/structure.h
  3. 49
      src/cleardns.c
  4. 41
      src/common/json.c
  5. 23
      src/common/structure.c
  6. 2
      src/common/sundry.c
  7. 5
      src/loader/config.c
  8. 14
      src/loader/loader.c
  9. 4
      src/loader/parser.c

11
include/common/json.h

@ -9,10 +9,11 @@ uint8_t is_json_suffix(const char *file_name);
cJSON* json_field_get(cJSON *entry, const char *key); cJSON* json_field_get(cJSON *entry, const char *key);
void json_field_replace(cJSON *entry, const char *key, cJSON *content); void json_field_replace(cJSON *entry, const char *key, cJSON *content);
int json_int_value(char *key, cJSON *json); int json_int_value(char *caption, cJSON *json);
uint8_t json_bool_value(char *key, cJSON *json); uint8_t json_bool_value(char *caption, cJSON *json);
char* json_string_value(char* key, cJSON *json); char* json_string_value(char* caption, cJSON *json);
char** json_string_list_value(char *key, cJSON *json, char **string_list); char** json_string_list_value(char *caption, cJSON *json, char **string_list);
uint32_t** json_uint32_list_value(char *key, cJSON *json, uint32_t **uint32_list); uint32_t** json_uint32_list_value(char *caption, cJSON *json, uint32_t **uint32_list);
void json_string_map_value(char *caption, cJSON *json, char ***key_list, char ***value_list);
#endif #endif

4
include/common/structure.h

@ -7,8 +7,6 @@ char** string_list_init();
void string_list_free(char **string_list); void string_list_free(char **string_list);
char* string_list_dump(char **string_list); char* string_list_dump(char **string_list);
uint32_t string_list_len(char **string_list); 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);
void string_list_update(char ***base_list, char **update_list); void string_list_update(char ***base_list, char **update_list);
void string_list_append(char ***string_list, const char *string); void string_list_append(char ***string_list, const char *string);
@ -17,8 +15,6 @@ char* uint32_list_dump(uint32_t **int_list);
void uint32_list_free(uint32_t **uint32_list); void uint32_list_free(uint32_t **uint32_list);
uint32_t uint32_list_len(uint32_t **int_list); uint32_t uint32_list_len(uint32_t **int_list);
void uint32_list_append(uint32_t ***uint32_list, uint32_t number); void uint32_list_append(uint32_t ***uint32_list, uint32_t number);
//uint32_t** uint32_list_append(uint32_t **int_list, uint32_t number);
void uint32_list_update(uint32_t ***base_list, uint32_t **update_list); void uint32_list_update(uint32_t ***base_list, uint32_t **update_list);
//uint32_t** uint32_list_update(uint32_t **base_list, uint32_t **update_list);
#endif #endif

49
src/cleardns.c

@ -46,49 +46,6 @@ int main(int argc, char *argv[]) { // ClearDNS service
LOG_LEVEL = LOG_DEBUG; LOG_LEVEL = LOG_DEBUG;
log_info("ClearDNS server start (%s)", VERSION); log_info("ClearDNS server start (%s)", VERSION);
// char **test = string_list_init();
// log_info("len -> %u", string_list_len(test));
// string_list_append(&test, "test1");
// string_list_append(&test, "test2");
// string_list_append(&test, "test3");
// log_info("len -> %u", string_list_len(test));
//
// char *temp = string_list_dump(test);
// log_info("content -> %s", temp);
// free(temp);
//
// char **add = string_list_init();
// string_list_append(&add, "test4");
// string_list_append(&add, "test5");
//
// string_list_update(&test, add);
// log_info("len -> %d", string_list_len(test));
// temp = string_list_dump(test);
// log_info("content -> %s", temp);
// free(temp);
uint32_t **test = uint32_list_init();
log_info("len -> %u", uint32_list_len(test));
uint32_list_append(&test, 1);
uint32_list_append(&test, 2);
uint32_list_append(&test, 3);
log_info("len -> %u", uint32_list_len(test));
char *temp = uint32_list_dump(test);
log_info("content -> %s", temp);
free(temp);
uint32_t **add = uint32_list_init();
uint32_list_append(&add, 4);
uint32_list_append(&add, 5);
uint32_list_update(&test, add);
log_info("len -> %d", uint32_list_len(test));
temp = uint32_list_dump(test);
log_info("content -> %s", temp);
free(temp);
// process *test = process_init("TEST", "lls"); // process *test = process_init("TEST", "lls");
// process *test = process_init("TEST", "ls"); // process *test = process_init("TEST", "ls");
@ -101,11 +58,11 @@ int main(int argc, char *argv[]) { // ClearDNS service
// wait(&status); // wait(&status);
// return 0; // return 0;
// create_folder(WORK_DIR); create_folder(WORK_DIR);
// TODO: load assets first // TODO: load assets first
// load_config(config_file); load_config(config_file);
// free(config_file); free(config_file);
// process_list_init(); // process_list_init();
// process_list_append(dnsproxy_load("Domestic", loader.domestic, "domestic.json")); // process_list_append(dnsproxy_load("Domestic", loader.domestic, "domestic.json"));

41
src/common/json.c

@ -60,68 +60,83 @@ void json_field_replace(cJSON *entry, const char *key, cJSON *content) {
} }
} }
int json_int_value(char *key, cJSON *json) { // json int or string value -> int int json_int_value(char *caption, cJSON *json) { // json int or string value -> int
if (cJSON_IsNumber(json)) { if (cJSON_IsNumber(json)) {
return json->valueint; return json->valueint;
} else if (cJSON_IsString(json)) { } else if (cJSON_IsString(json)) {
char *p; char *p;
int int_ret = (int)strtol(json->valuestring, &p, 10); int int_ret = (int)strtol(json->valuestring, &p, 10);
if (int_ret == 0 && strcmp(json->valuestring, "0") != 0) { // invalid number in string if (int_ret == 0 && strcmp(json->valuestring, "0") != 0) { // invalid number in string
log_fatal("`%s` not a valid number", key); log_fatal("`%s` not a valid number", caption);
} }
return int_ret; return int_ret;
} else { } else {
log_fatal("`%s` must be number or string", key); log_fatal("`%s` must be number or string", caption);
} }
return 0; // never reach return 0; // never reach
} }
uint8_t json_bool_value(char *key, cJSON *json) { // json bool value -> bool uint8_t json_bool_value(char *caption, cJSON *json) { // json bool value -> bool
if (!cJSON_IsBool(json)) { if (!cJSON_IsBool(json)) {
log_fatal("`%s` must be boolean", key); log_fatal("`%s` must be boolean", caption);
} }
return json->valueint; return json->valueint;
} }
char* json_string_value(char* key, cJSON *json) { // json string value -> string char* json_string_value(char* caption, 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", caption);
} }
return string_init(json->valuestring); return string_init(json->valuestring);
} }
char** json_string_list_value(char *key, cJSON *json, char **string_list) { // json string array -> string list char** json_string_list_value(char *caption, cJSON *json, char **string_list) { // json string array
if (cJSON_IsString(json)) { if (cJSON_IsString(json)) {
string_list_append(&string_list, json->valuestring); string_list_append(&string_list, json->valuestring);
} else if (cJSON_IsArray(json)) { } else if (cJSON_IsArray(json)) {
json = json->child; json = json->child;
while (json != NULL) { while (json != NULL) {
if (!cJSON_IsString(json)) { if (!cJSON_IsString(json)) {
log_fatal("`%s` must be string array", key); log_fatal("`%s` must be string array", caption);
} }
string_list_append(&string_list, json->valuestring); string_list_append(&string_list, json->valuestring);
json = json->next; // next key json = json->next; // next key
} }
} else if (!cJSON_IsNull(json)) { // allow null -> empty string list } 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", caption);
} }
return string_list; return string_list;
} }
uint32_t** json_uint32_list_value(char *key, cJSON *json, uint32_t **uint32_list) { // json uint32 array -> uint32 list uint32_t** json_uint32_list_value(char *caption, cJSON *json, uint32_t **uint32_list) { // json uint32 array
if (cJSON_IsNumber(json)) { if (cJSON_IsNumber(json)) {
uint32_list_append(&uint32_list, json->valueint); uint32_list_append(&uint32_list, json->valueint);
} else if (cJSON_IsArray(json)) { } else if (cJSON_IsArray(json)) {
json = json->child; json = json->child;
while (json != NULL) { while (json != NULL) {
if (!cJSON_IsNumber(json)) { if (!cJSON_IsNumber(json)) {
log_fatal("`%s` must be number array", key); log_fatal("`%s` must be number array", caption);
} }
uint32_list_append(&uint32_list, json->valueint); uint32_list_append(&uint32_list, json->valueint);
json = json->next; // next key json = json->next; // next key
} }
} else if (!cJSON_IsNull(json)) { // allow null -> empty uint32 list } 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", caption);
} }
return uint32_list; return uint32_list;
} }
void json_string_map_value(char *caption, cJSON *json, char ***key_list, char ***value_list) { // json string map
if (!cJSON_IsObject(json)) {
log_fatal("`%s` must be map", caption);
}
json = json->child;
while (json != NULL) { // traverse all json field
if (!cJSON_IsString(json)) {
log_fatal("`%s` must be string-string map", caption);
}
string_list_append(key_list, json->string);
string_list_append(value_list, json->valuestring);
json = json->next;
}
}

23
src/common/structure.c

@ -3,7 +3,6 @@
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#include "sundry.h" #include "sundry.h"
#include "constant.h"
#include "structure.h" #include "structure.h"
char** string_list_init() { // init string list char** string_list_init() { // init string list
@ -18,37 +17,17 @@ uint32_t string_list_len(char **string_list) { // get len of string list
return num - 1; return num - 1;
} }
// TODO: use char *** with void return
//char** string_list_append(char **string_list, const char *string) { // add new string at the end of list
// uint32_t len = string_list_len(string_list);
// string_list = (char **)realloc(string_list, sizeof(char *) * (len + 2)); // extend string list
// string_list[len] = string_init(string);
// string_list[len + 1] = NULL; // list end sign
// return string_list;
//}
void string_list_append(char ***string_list, const char *string) { void string_list_append(char ***string_list, const char *string) {
uint32_t len = string_list_len(*string_list); uint32_t len = string_list_len(*string_list);
*string_list = (char **)realloc(*string_list, sizeof(char *) * (len + 2)); // extend string list *string_list = (char **)realloc(*string_list, sizeof(char *) * (len + 2)); // extend string list
(*string_list)[len] = string_init(string); (*string_list)[len] = string_init(string);
(*string_list)[len + 1] = NULL; // list end sign (*string_list)[len + 1] = NULL; // list end sign
// return string_list;
} }
//char** string_list_update(char **base_list, char **update_list) { // combine two string list
// for (char **string = update_list; *string != NULL; ++string) {
//// base_list = string_list_append(base_list, *string);
// string_list_append(&base_list, *string);
// }
// return base_list;
//}
void string_list_update(char ***base_list, char **update_list) { // combine two string list void 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);
string_list_append(base_list, *string); string_list_append(base_list, *string);
} }
// return base_list;
} }
void string_list_free(char **string_list) { // free string list void string_list_free(char **string_list) { // free string list
@ -90,14 +69,12 @@ void uint32_list_append(uint32_t ***uint32_list, uint32_t number) { // add new u
(*uint32_list)[len] = (uint32_t *)malloc(sizeof(uint32_t)); (*uint32_list)[len] = (uint32_t *)malloc(sizeof(uint32_t));
*(*uint32_list)[len] = number; *(*uint32_list)[len] = number;
(*uint32_list)[len + 1] = NULL; // list end sign (*uint32_list)[len + 1] = NULL; // list end sign
// return uint32_list;
} }
void uint32_list_update(uint32_t ***base_list, uint32_t **update_list) { // combine two uint32 list void uint32_list_update(uint32_t ***base_list, uint32_t **update_list) { // combine two uint32 list
for (uint32_t **number = update_list; *number != NULL; ++number) { for (uint32_t **number = update_list; *number != NULL; ++number) {
uint32_list_append(base_list, **number); 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

2
src/common/sundry.c

@ -2,6 +2,7 @@
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#include <sys/time.h> #include <sys/time.h>
#include <unistd.h>
#include "logger.h" #include "logger.h"
#include "constant.h" #include "constant.h"
#include "structure.h" #include "structure.h"
@ -51,5 +52,6 @@ uint16_t gen_rand_num(uint16_t limit) { // 0 ~ (limit - 1)
struct timeval tv; struct timeval tv;
gettimeofday(&tv, NULL); gettimeofday(&tv, NULL);
srand(tv.tv_usec); srand(tv.tv_usec);
usleep(1); // sleep 1us (different time seed)
return rand() % limit; // NOLINT return rand() % limit; // NOLINT
} }

5
src/loader/config.c

@ -102,12 +102,15 @@ void config_free(cleardns_config *config) { // free config struct of cleardns
string_list_free(config->diverter.china_ip); string_list_free(config->diverter.china_ip);
string_list_free(config->diverter.chinalist); string_list_free(config->diverter.chinalist);
string_list_free(config->assets.update_file);
string_list_free(config->assets.update_url);
uint32_list_free(config->reject); uint32_list_free(config->reject);
string_list_free(config->hosts); string_list_free(config->hosts);
string_list_free(config->ttl); string_list_free(config->ttl);
string_list_free(config->script); string_list_free(config->script);
free(config->adguard.username); free(config->adguard.username);
free(config->adguard.password); free(config->adguard.password);
free(config->assets.cron);
free(config); free(config);
} }

14
src/loader/loader.c

@ -113,14 +113,15 @@ adguard* load_filter(cleardns_config *config) {
return filter; return filter;
} }
void load_config(const char *config_file) { assets_config* load_assets(cleardns_config *config) {
// TODO: load assets
return NULL;
}
void load_config(const char *config_file) { // parser and load cleardns configure
cleardns_config *config = config_init(); cleardns_config *config = config_init();
load_default_config(config_file); // load default configure load_default_config(config_file); // load default configure
config_parser(config, config_file); // configure parser config_parser(config, config_file); // configure parser
// insert code (remove after test)
string_list_append(&config->assets.update_file, "geoip.dat");
string_list_append(&config->assets.update_url, "https://test.net/geoip.dat");
config_dump(config); config_dump(config);
log_info("Loading configure options"); log_info("Loading configure options");
@ -135,7 +136,10 @@ void load_config(const char *config_file) {
log_debug("Diverter options parser success"); log_debug("Diverter options parser success");
loader.filter = load_filter(config); loader.filter = load_filter(config);
log_debug("Filter options parser success"); log_debug("Filter options parser success");
loader.assets = load_assets(config);
log_debug("Assets options parser success");
loader.script = string_list_init(); loader.script = string_list_init();
string_list_update(&loader.script, config->script); string_list_update(&loader.script, config->script);
log_debug("Custom script parser success");
config_free(config); config_free(config);
} }

4
src/loader/parser.c

@ -113,7 +113,6 @@ void adguard_parser(adguard_config *config, cJSON *json) { // adguard options pa
} }
void assets_parser(assets_config *config, cJSON *json) { // assets options parser void assets_parser(assets_config *config, cJSON *json) { // assets options parser
if (!cJSON_IsObject(json)) { if (!cJSON_IsObject(json)) {
log_fatal("`assets` must be array"); log_fatal("`assets` must be array");
} }
@ -123,8 +122,7 @@ void assets_parser(assets_config *config, cJSON *json) { // assets options parse
config->cron = json_string_value("assets.cron", json); config->cron = json_string_value("assets.cron", json);
} }
if (!strcmp(json->string, "update")) { if (!strcmp(json->string, "update")) {
log_warn("GET UPDATE KEY"); json_string_map_value("assets.update", json, &config->update_file, &config->update_url);
// TODO: get string map
} }
json = json->next; // next field json = json->next; // next field
} }

Loading…
Cancel
Save