Browse Source

feat: assets support multiple sources, ref #3

dev
Dnomd343 2 years ago
parent
commit
2411fe494c
  1. 3
      include/loader/loader.h
  2. 30
      include/utils/assets.h
  3. 70
      src/cleardns.c
  4. 16
      src/loader/loader.c
  5. 127
      src/utils/assets.c

3
include/loader/loader.h

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

30
include/utils/assets.h

@ -2,13 +2,31 @@
#define ASSETS_H_ #define ASSETS_H_
typedef struct { typedef struct {
char **update_file; char *file; // string
char **update_url; char **sources; // string list
} assets; } asset;
assets* assets_init(); asset* asset_init(const char *name);
void assets_free(assets *info); //void asset_add_src(asset *res, const char *src);
void assets_load(assets *info);
asset** assets_init();
void assets_append(asset ***asset_list, asset *res);
void assets_free(asset **asset_list);
//void asset_free(asset *res);
uint32_t assets_size(asset **asset_list);
void assets_dump(asset **asset_list);
//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();

70
src/cleardns.c

@ -87,14 +87,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
} }
@ -119,23 +119,69 @@ void cleardns() { // cleardns service
int main(int argc, char *argv[]) { int main(int argc, char *argv[]) {
char **demo = string_list_init(); LOG_LEVEL = LOG_DEBUG;
string_list_append(&demo, "item 1"); asset *res_1 = asset_init("demo 1");
string_list_append(&demo, "item 2"); asset *res_2 = asset_init("demo 2");
string_list_append(&demo, "item 3"); asset *res_3 = asset_init("demo 3");
string_list_append(&demo, "item 4");
string_list_append(&demo, "item 5");
char *tmp = string_list_dump(demo); string_list_append(&res_1->sources, "item a1");
log_warn("dump -> %s", tmp); 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(TRUE);
assets_log_init(FALSE); assets_log_init(FALSE);
rust_assets_update("test.txt", demo, ASSETS_DIR); // rust_assets_update("test.txt", demo, ASSETS_DIR);
string_list_free(demo); // string_list_free(demo);
log_warn("test end"); log_warn("test end");
exit(0); exit(0);

16
src/loader/loader.c

@ -127,12 +127,13 @@ crontab* load_crond(cleardns_config *config) {
return crond; return crond;
} }
assets* load_assets(cleardns_config *config) { // TODO: new assets manager
assets *resource = assets_init(); //assets* load_assets(cleardns_config *config) {
string_list_update(&resource->update_file, config->assets.update_file); // assets *resource = assets_init();
string_list_update(&resource->update_url, config->assets.update_url); // string_list_update(&resource->update_file, config->assets.update_file);
return resource; // string_list_update(&resource->update_url, config->assets.update_url);
} // return resource;
//}
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();
@ -161,7 +162,8 @@ 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");
loader.resource = load_assets(config); // TODO: update assets loader
// 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();

127
src/utils/assets.c

@ -8,59 +8,112 @@
#include "structure.h" #include "structure.h"
#include "assets.h" #include "assets.h"
assets update; //assets update;
void assets_update(); //void assets_update();
void assets_dump(assets *info); //void assets_dump(assets *info);
void extract(const char *file); void extract(const char *file);
void assets_free(assets *info) { // free assets mapping //void assets_free(assets *info) { // free assets mapping
string_list_free(info->update_file); // string_list_free(info->update_file);
string_list_free(info->update_url); // string_list_free(info->update_url);
free(info); // 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 *res = (asset *)malloc(sizeof(asset));
res->file = strdup(name);
res->sources = string_list_init(); // with multi sources
return res;
} }
assets* assets_init() { // init assets mapping //void asset_add_src(asset *res, const char *src) { // append source for asset item
assets *info = (assets *)malloc(sizeof(assets)); // string_list_append(&res->sources, src);
info->update_file = string_list_init(); //}
info->update_url = string_list_init(); //
return info; //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 **asset_list = (asset **)malloc(sizeof(asset *));
*asset_list = NULL; // list end sign
return asset_list;
} }
void assets_dump(assets *info) { // show assets mapping in debug log void assets_free(asset **asset_list) {
for (char **file = info->update_file; *file != NULL; ++file) { for (asset **res = asset_list; *res != NULL; ++res) {
char **url = file - info->update_file + info->update_url; string_list_free((*res)->sources);
log_info("Asset `%s` -> %s", *file, *url); free((*res)->file);
free(*res);
} }
free(asset_list);
} }
void assets_load(assets *info) { // load assets mapping uint32_t assets_size(asset **asset_list) { // get size of asset list
update.update_file = string_list_init(); uint32_t num = 0;
update.update_url = string_list_init(); while(asset_list[num++] != NULL); // get list size
string_list_update(&update.update_file, info->update_file); return num - 1;
string_list_update(&update.update_url, info->update_url);
signal(SIGALRM, assets_update); // receive SIGALRM signal
assets_dump(&update);
} }
void assets_update() { // update all assets void assets_dump(asset **asset_list) {
if (!string_list_len(update.update_file)) { // empty assets mapping for (asset **res = asset_list; *res != NULL; ++res) { // iterate over each item
log_info("Skip update assets"); char *sources = string_list_dump((*res)->sources);
return; log_info("Asset item `%s` -> %s", (*res)->file, sources);
free(sources);
} }
log_info("Start assets update");
for (char **file = update.update_file; *file != NULL; ++file) {
char **url = file - update.update_file + update.update_url;
char *asset_file = string_join(ASSETS_DIR, *file);
log_info("Update asset `%s` -> %s", asset_file, *url);
download_file(asset_file, *url); // download asset from url
free(asset_file);
} }
log_info("Restart overture");
run_command("pgrep overture | xargs kill"); // restart overture void assets_append(asset ***asset_list, asset *res) { // append asset item for asset list
log_info("Assets update complete"); uint32_t len = assets_size(*asset_list);
*asset_list = (asset **)realloc(*asset_list, sizeof(asset *) * (len + 2)); // extend asset list
(*asset_list)[len] = res;
(*asset_list)[len + 1] = NULL; // list end sign
} }
//void assets_dump(assets *info) { // show assets mapping in debug log
// for (char **file = info->update_file; *file != NULL; ++file) {
// char **url = file - info->update_file + info->update_url;
// log_info("Asset `%s` -> %s", *file, *url);
// }
//}
//void assets_load(assets *info) { // load assets mapping
// update.update_file = string_list_init();
// update.update_url = string_list_init();
// string_list_update(&update.update_file, info->update_file);
// string_list_update(&update.update_url, info->update_url);
// signal(SIGALRM, assets_update); // receive SIGALRM signal
// assets_dump(&update);
//}
//void assets_update() { // update all assets
// 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) {
// char **url = file - update.update_file + update.update_url;
// char *asset_file = string_join(ASSETS_DIR, *file);
// log_info("Update asset `%s` -> %s", asset_file, *url);
// download_file(asset_file, *url); // download asset from url
// free(asset_file);
// }
// log_info("Restart overture");
// 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");
create_folder(ASSETS_DIR); create_folder(ASSETS_DIR);

Loading…
Cancel
Save