Browse Source

feat: multiple sources configure parser, ref #3

dev
Dnomd343 2 years ago
parent
commit
9bf1763dd8
  1. 6
      include/loader/config.h
  2. 3
      include/loader/loader.h
  3. 24
      include/utils/assets.h
  4. 73
      src/cleardns.c
  5. 15
      src/common/json.c
  6. 12
      src/loader/config.c
  7. 2
      src/loader/default.c
  8. 18
      src/loader/loader.c
  9. 13
      src/loader/parser.c
  10. 84
      src/utils/assets.c

6
include/loader/config.h

@ -2,6 +2,7 @@
#define CONFIG_H_ #define CONFIG_H_
#include <stdint.h> #include <stdint.h>
#include "assets.h"
typedef struct { typedef struct {
uint16_t port; uint16_t port;
@ -34,10 +35,9 @@ typedef struct {
} adguard_config; } adguard_config;
typedef struct { typedef struct {
uint8_t disable; // bool value
char *cron; char *cron;
char **update_file; uint8_t disable; // bool value
char **update_url; asset **resources;
} assets_config; } assets_config;
typedef struct { typedef struct {

3
include/loader/loader.h

@ -15,8 +15,7 @@ struct cleardns {
overture *diverter; overture *diverter;
adguard *filter; adguard *filter;
crontab *crond; crontab *crond;
// TODO: update assets ptr asset **resource;
// assets *resource;
}; };
extern struct cleardns loader; extern struct cleardns loader;

24
include/utils/assets.h

@ -7,31 +7,19 @@ typedef struct {
} asset; } asset;
asset* asset_init(const char *name); asset* asset_init(const char *name);
//void asset_add_src(asset *res, const char *src);
asset** assets_init(); asset** assets_init();
void assets_append(asset ***asset_list, asset *res); void assets_dump(asset **asset_list);
void assets_free(asset **asset_list); void assets_free(asset **asset_list);
//void asset_free(asset *res);
uint32_t assets_size(asset **asset_list); uint32_t assets_size(asset **asset_list);
void assets_append(asset ***asset_list, asset *res);
void assets_dump(asset **asset_list); void assets_load(asset **info);
//typedef struct {
// char **update_file;
// char **update_url;
//} assets;
//assets* assets_init();
//void assets_free(assets *info);
//void assets_load(assets *info);
void assets_extract(); void assets_extract();
void assets_log_init(uint8_t verbose); //void assets_log_init(uint8_t verbose);
//
uint8_t rust_assets_update(const char *file, char *const *sources, const char *assets_dir); //uint8_t rust_assets_update(const char *file, char *const *sources, const char *assets_dir);
#endif #endif

73
src/cleardns.c

@ -66,6 +66,7 @@ void init(int argc, char *argv[]) { // return config file
void cleardns() { // cleardns service void cleardns() { // cleardns service
if (settings.verbose || settings.debug) { if (settings.verbose || settings.debug) {
// TODO: rust log module settings
LOG_LEVEL = LOG_DEBUG; // enable debug log level LOG_LEVEL = LOG_DEBUG; // enable debug log level
} }
create_folder(EXPOSE_DIR); create_folder(EXPOSE_DIR);
@ -87,14 +88,14 @@ void cleardns() { // cleardns service
log_info("Start loading process"); log_info("Start loading process");
process_list_init(); process_list_init();
// assets_load(loader.resource); assets_load(loader.resource);
process_list_append(dnsproxy_load("Domestic", loader.domestic, "domestic.json")); process_list_append(dnsproxy_load("Domestic", loader.domestic, "domestic.json"));
process_list_append(dnsproxy_load("Foreign", loader.foreign, "foreign.json")); process_list_append(dnsproxy_load("Foreign", loader.foreign, "foreign.json"));
process_list_append(overture_load(loader.diverter, "overture.json")); process_list_append(overture_load(loader.diverter, "overture.json"));
overture_free(loader.diverter); overture_free(loader.diverter);
dnsproxy_free(loader.domestic); dnsproxy_free(loader.domestic);
dnsproxy_free(loader.foreign); dnsproxy_free(loader.foreign);
// assets_free(loader.resource); assets_free(loader.resource);
if (loader.crond != NULL) { if (loader.crond != NULL) {
process_list_append(crontab_load(loader.crond)); // free crontab struct later process_list_append(crontab_load(loader.crond)); // free crontab struct later
} }
@ -118,74 +119,6 @@ void cleardns() { // cleardns service
} }
int main(int argc, char *argv[]) { int main(int argc, char *argv[]) {
LOG_LEVEL = LOG_DEBUG;
asset *res_1 = asset_init("demo 1");
asset *res_2 = asset_init("demo 2");
asset *res_3 = asset_init("demo 3");
string_list_append(&res_1->sources, "item a1");
string_list_append(&res_1->sources, "item a2");
string_list_append(&res_1->sources, "item a3");
// asset_add_src(res_1, "item a1");
// asset_add_src(res_1, "item a2");
// asset_add_src(res_1, "item a3");
string_list_append(&res_2->sources, "item b1");
string_list_append(&res_2->sources, "item b2");
// asset_add_src(res_2, "item b1");
// asset_add_src(res_2, "item b2");
string_list_append(&res_3->sources, "item c1");
// asset_add_src(res_3, "item c1");
asset **res = assets_init();
log_info("res init size: %d", assets_size(res));
assets_append(&res, res_1);
assets_append(&res, res_2);
assets_append(&res, res_3);
log_info("res size: %d", assets_size(res));
assets_dump(res);
// asset_free(res_1);
// asset_free(res_2);
// asset_free(res_3);
assets_free(res);
// char **res_1 = string_list_init();
// char **res_2 = string_list_init();
// char **res_3 = string_list_init();
//
// string_list_append(&res_1, "item a1");
// string_list_append(&res_1, "item a2");
// string_list_append(&res_1, "item a3");
//
// string_list_append(&res_2, "item b1");
// string_list_append(&res_2, "item b2");
//
// string_list_append(&res_3, "item c");
// char *tmp = string_list_dump(demo);
// log_warn("dump -> %s", tmp);
// assets_log_init(TRUE);
assets_log_init(FALSE);
// rust_assets_update("test.txt", demo, ASSETS_DIR);
// string_list_free(demo);
log_warn("test end");
exit(0);
init(argc, argv); init(argc, argv);
log_info("ClearDNS server start (%s)", VERSION); log_info("ClearDNS server start (%s)", VERSION);
cleardns(); cleardns();

15
src/common/json.c

@ -115,18 +115,3 @@ uint32_t** json_uint32_list_value(char *caption, cJSON *json, uint32_t **uint32_
} }
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;
}
}

12
src/loader/config.c

@ -41,8 +41,7 @@ cleardns_config* config_init() { // init config struct of cleardns
config->assets.disable = FALSE; config->assets.disable = FALSE;
config->assets.cron = strdup(UPDATE_CRON); config->assets.cron = strdup(UPDATE_CRON);
config->assets.update_file = string_list_init(); config->assets.resources = assets_init();
config->assets.update_url = string_list_init();
config->reject = uint32_list_init(); config->reject = uint32_list_init();
config->hosts = string_list_init(); config->hosts = string_list_init();
@ -85,10 +84,8 @@ void config_dump(cleardns_config *config) { // dump config info of cleardns
log_debug("Assets disable -> %s", show_bool(config->assets.disable)); log_debug("Assets disable -> %s", show_bool(config->assets.disable));
log_debug("Assets update cron -> `%s`", config->assets.cron); log_debug("Assets update cron -> `%s`", config->assets.cron);
for (char **file = config->assets.update_file; *file != NULL; ++file) { // show string mapping log_debug("Assets with %d resource items", assets_size(config->assets.resources));
char **url = file - config->assets.update_file + config->assets.update_url; assets_dump(config->assets.resources);
log_debug("Assets file `%s` -> %s", *file, *url);
}
uint32_list_debug("DNS reject type", config->reject); uint32_list_debug("DNS reject type", config->reject);
string_list_debug("Domain TTL", config->ttl); string_list_debug("Domain TTL", config->ttl);
@ -113,8 +110,7 @@ void config_free(cleardns_config *config) { // free config struct of cleardns
free(config->adguard.password); free(config->adguard.password);
free(config->assets.cron); free(config->assets.cron);
string_list_free(config->assets.update_file); assets_free(config->assets.resources);
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);

2
src/loader/default.c

@ -51,7 +51,7 @@ assets:\n\
void load_default_config(const char *config_file) { void load_default_config(const char *config_file) {
if (is_file_exist(config_file)) { if (is_file_exist(config_file)) {
log_debug("Configure file exist -> skip load default"); log_debug("Configure file exist -> skip loading default");
return; return;
} }
log_info("Loading default configure file"); log_info("Loading default configure file");

18
src/loader/loader.c

@ -127,13 +127,14 @@ crontab* load_crond(cleardns_config *config) {
return crond; return crond;
} }
// TODO: new assets manager asset** load_assets(cleardns_config *config) {
//assets* load_assets(cleardns_config *config) { asset **resources = assets_init();
// assets *resource = assets_init(); for (asset **res = config->assets.resources; *res != NULL; ++res) {
// string_list_update(&resource->update_file, config->assets.update_file); assets_append(&resources, *res); // pointer movement
// string_list_update(&resource->update_url, config->assets.update_url); }
// return resource; *(config->assets.resources) = NULL; // disable old assets list
//} return resources;
}
void load_config(const char *config_file) { // parser and load cleardns configure void load_config(const char *config_file) { // parser and load cleardns configure
cleardns_config *config = config_init(); cleardns_config *config = config_init();
@ -162,8 +163,7 @@ void load_config(const char *config_file) { // parser and load cleardns configur
loader.crond = load_crond(config); loader.crond = load_crond(config);
log_debug("Crond options parser success"); log_debug("Crond options parser success");
// TODO: update assets loader loader.resource = load_assets(config);
// loader.resource = load_assets(config);
log_debug("Assets options parser success"); log_debug("Assets options parser success");
loader.script = string_list_init(); loader.script = string_list_init();

13
src/loader/parser.c

@ -130,7 +130,18 @@ 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")) {
json_string_map_value("assets.update", json, &config->update_file, &config->update_url); if (!cJSON_IsObject(json)) {
log_fatal("`%s` must be map", "assets.update");
}
cJSON *asset_item = json->child;
while (asset_item != NULL) { // traverse all json field
asset *res = asset_init(asset_item->string);
char *caption = string_join("assets.update.", asset_item->string);
res->sources = json_string_list_value(caption, asset_item, res->sources);
free(caption);
assets_append(&config->resources, res);
asset_item = asset_item->next;
}
} }
json = json->next; // next field json = json->next; // next field
} }

84
src/utils/assets.c

@ -8,25 +8,11 @@
#include "structure.h" #include "structure.h"
#include "assets.h" #include "assets.h"
//assets update; asset **update_info;
//void assets_update(); void assets_update_entry();
//void assets_dump(assets *info);
void extract(const char *file); void extract(const char *file);
//void assets_free(assets *info) { // free assets mapping
// string_list_free(info->update_file);
// string_list_free(info->update_url);
// free(info);
//}
//assets* assets_init() { // init assets mapping
// assets *info = (assets *)malloc(sizeof(assets));
// info->update_file = string_list_init();
// info->update_url = string_list_init();
// return info;
//}
asset* asset_init(const char *name) { // init asset item asset* asset_init(const char *name) { // init asset item
asset *res = (asset *)malloc(sizeof(asset)); asset *res = (asset *)malloc(sizeof(asset));
res->file = strdup(name); res->file = strdup(name);
@ -34,23 +20,13 @@ asset* asset_init(const char *name) { // init asset item
return res; return res;
} }
//void asset_add_src(asset *res, const char *src) { // append source for asset item
// string_list_append(&res->sources, src);
//}
//
//void asset_free(asset *res) { // free asset item
// string_list_free(res->sources);
// free(res->file);
// free(res);
//}
asset** assets_init() { // init assets list asset** assets_init() { // init assets list
asset **asset_list = (asset **)malloc(sizeof(asset *)); asset **asset_list = (asset **)malloc(sizeof(asset *));
*asset_list = NULL; // list end sign *asset_list = NULL; // list end sign
return asset_list; return asset_list;
} }
void assets_free(asset **asset_list) { void assets_free(asset **asset_list) { // free assets list
for (asset **res = asset_list; *res != NULL; ++res) { for (asset **res = asset_list; *res != NULL; ++res) {
string_list_free((*res)->sources); string_list_free((*res)->sources);
free((*res)->file); free((*res)->file);
@ -59,49 +35,46 @@ void assets_free(asset **asset_list) {
free(asset_list); free(asset_list);
} }
uint32_t assets_size(asset **asset_list) { // get size of asset list uint32_t assets_size(asset **asset_list) { // get size of assets list
uint32_t num = 0; uint32_t num = 0;
while(asset_list[num++] != NULL); // get list size while(asset_list[num++] != NULL); // get list size
return num - 1; return num - 1;
} }
void assets_dump(asset **asset_list) { void assets_dump(asset **asset_list) { // dump assets list content into debug log
for (asset **res = asset_list; *res != NULL; ++res) { // iterate over each item for (asset **res = asset_list; *res != NULL; ++res) { // iterate over each item
char *sources = string_list_dump((*res)->sources); char *sources = string_list_dump((*res)->sources);
log_info("Asset item `%s` -> %s", (*res)->file, sources); log_debug("Asset item `%s` -> %s", (*res)->file, sources);
free(sources); free(sources);
} }
} }
void assets_append(asset ***asset_list, asset *res) { // append asset item for asset list void assets_append(asset ***asset_list, asset *res) { // push asset item for assets list
uint32_t len = assets_size(*asset_list); uint32_t len = assets_size(*asset_list);
*asset_list = (asset **)realloc(*asset_list, sizeof(asset *) * (len + 2)); // extend asset list *asset_list = (asset **)realloc(*asset_list, sizeof(asset *) * (len + 2)); // extend asset list
(*asset_list)[len] = res; (*asset_list)[len] = res;
(*asset_list)[len + 1] = NULL; // list end sign (*asset_list)[len + 1] = NULL; // list end sign
} }
//void assets_dump(assets *info) { // show assets mapping in debug log void assets_load(asset **info) { // load assets list
// for (char **file = info->update_file; *file != NULL; ++file) { update_info = assets_init();
// char **url = file - info->update_file + info->update_url; for (asset **res = info; *res != NULL; ++res) {
// log_info("Asset `%s` -> %s", *file, *url); assets_append(&update_info, *res); // pointer movement
// } }
//} *info = NULL; // disable old assets list
assets_dump(update_info);
log_info("Remote assets load success");
signal(SIGALRM, assets_update_entry); // receive SIGALRM signal
}
//void assets_load(assets *info) { // load assets mapping void assets_update_entry() { // receive SIGALRM for update all assets
// update.update_file = string_list_init(); if (assets_size(update_info) == 0) { // empty assets list
// update.update_url = string_list_init(); log_info("Skip update assets");
// string_list_update(&update.update_file, info->update_file); return;
// string_list_update(&update.update_url, info->update_url); }
// signal(SIGALRM, assets_update); // receive SIGALRM signal log_info("Start assets update");
// assets_dump(&update);
//}
//void assets_update() { // update all assets // TODO: call rust `assets_update` function
// if (!string_list_len(update.update_file)) { // empty assets mapping
// log_info("Skip update assets");
// return;
// }
// log_info("Start assets update");
// for (char **file = update.update_file; *file != NULL; ++file) { // for (char **file = update.update_file; *file != NULL; ++file) {
// char **url = file - update.update_file + update.update_url; // char **url = file - update.update_file + update.update_url;
// char *asset_file = string_join(ASSETS_DIR, *file); // char *asset_file = string_join(ASSETS_DIR, *file);
@ -109,10 +82,11 @@ void assets_append(asset ***asset_list, asset *res) { // append asset item for a
// download_file(asset_file, *url); // download asset from url // download_file(asset_file, *url); // download asset from url
// free(asset_file); // free(asset_file);
// } // }
// log_info("Restart overture");
// run_command("pgrep overture | xargs kill"); // restart overture log_info("Restart overture");
// log_info("Assets update complete"); run_command("pgrep overture | xargs kill"); // restart overture
//} log_info("Assets update complete");
}
void assets_extract() { // init assets and load update process void assets_extract() { // init assets and load update process
log_info("Start loading assets"); log_info("Start loading assets");

Loading…
Cancel
Save