Browse Source

feat: add assets options

dev
Dnomd343 2 years ago
parent
commit
36909feb70
  1. 18
      include/constant.h
  2. 7
      include/loader/config.h
  3. 4
      include/loader/loader.h
  4. 40
      src/cleardns.c
  5. 1
      src/common/structure.c
  6. 2
      src/loader/CMakeLists.txt
  7. 10
      src/loader/config.c
  8. 7
      src/loader/default.c
  9. 4
      src/loader/loader.c
  10. 21
      src/loader/parser.c

18
include/constant.h

@ -1,11 +1,12 @@
#ifndef CONSTANT_H_ #ifndef CONSTANT_H_
#define CONSTANT_H_ #define CONSTANT_H_
#define VERSION "1.3.0-dev"
#define TRUE 1 #define TRUE 1
#define FALSE 0 #define FALSE 0
#define VERSION "1.3.0-dev"
#define CONFIG_FILE "test.yml"
#define DNS_PORT 53 #define DNS_PORT 53
#define ADGUARD_PORT 80 #define ADGUARD_PORT 80
#define DIVERTER_PORT 5353 #define DIVERTER_PORT 5353
@ -15,22 +16,31 @@
#define DIVERTER_TIMEOUT 6 #define DIVERTER_TIMEOUT 6
#define ADGUARD_USER "admin" #define ADGUARD_USER "admin"
#define ADGUARD_PASSWD "adguard" #define ADGUARD_PASSWD "cleardns"
#define DNSPROXY_BIN "dnsproxy" #define DNSPROXY_BIN "dnsproxy"
#define OVERTURE_BIN "overture" #define OVERTURE_BIN "overture"
#define ADGUARD_BIN "AdGuardHome" #define ADGUARD_BIN "AdGuardHome"
#define ASSETS_PKG "/assets.tar.xz"
#define EXPOSE_DIR "/cleardns/" #define EXPOSE_DIR "/cleardns/"
#define WORK_DIR "/etc/cleardns/" #define WORK_DIR "/etc/cleardns/"
#define ASSETS_DIR "/cleardns/assets/" #define ASSETS_DIR "/cleardns/assets/"
#define ADGUARD_DIR "/cleardns/adguard/" #define ADGUARD_DIR "/cleardns/adguard/"
#define UPDATE_CRON "0 4 * * *"
#define ASSETS_PKG "/assets.tar.xz"
#define ASSET_TTL "ttl.txt" #define ASSET_TTL "ttl.txt"
#define ASSET_HOSTS "hosts.txt" #define ASSET_HOSTS "hosts.txt"
#define ASSET_GFW_LIST "gfwlist.txt" #define ASSET_GFW_LIST "gfwlist.txt"
#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"
#define HELP_MSG "\
ClearDNS usage \n\
--debug ...\n\
--config ...\n\
--help ...\n\
"
#endif #endif

7
include/loader/config.h

@ -32,6 +32,12 @@ typedef struct {
char *password; char *password;
} adguard_config; } adguard_config;
typedef struct {
char *cron;
char **update_file;
char **update_url;
} assets_config;
typedef struct { typedef struct {
uint16_t port; uint16_t port;
cache_config cache; cache_config cache;
@ -39,6 +45,7 @@ typedef struct {
upstream_config foreign; upstream_config foreign;
diverter_config diverter; diverter_config diverter;
adguard_config adguard; adguard_config adguard;
assets_config assets;
uint32_t **reject; uint32_t **reject;
char **hosts; char **hosts;
char **ttl; char **ttl;

4
include/loader/loader.h

@ -1,16 +1,18 @@
#ifndef LOADER_H_ #ifndef LOADER_H_
#define LOADER_H_ #define LOADER_H_
#include "config.h"
#include "adguard.h" #include "adguard.h"
#include "dnsproxy.h" #include "dnsproxy.h"
#include "overture.h" #include "overture.h"
struct cleardns { struct cleardns {
char **script;
dnsproxy *domestic; dnsproxy *domestic;
dnsproxy *foreign; dnsproxy *foreign;
overture *diverter; overture *diverter;
adguard *filter; adguard *filter;
char **script; assets_config *assets;
}; };
extern struct cleardns loader; extern struct cleardns loader;

40
src/cleardns.c

@ -11,15 +11,6 @@
#include "system.h" #include "system.h"
#include "sundry.h" #include "sundry.h"
#define HELP_MSG "\
ClearDNS usage \n\
--debug ...\n\
--config ...\n\
--help ...\n\
"
#define CONFIG_FILE "test.json"
char* init(int argc, char *argv[]) { // return config file char* init(int argc, char *argv[]) { // return config file
char *config = string_init(CONFIG_FILE); char *config = string_init(CONFIG_FILE);
for (int i = 0; i < argc; ++i) { for (int i = 0; i < argc; ++i) {
@ -47,8 +38,7 @@ char* init(int argc, char *argv[]) { // return config file
return config; return config;
} }
int main(int argc, char *argv[]) { // ClearDNS service
int main(int argc, char *argv[]) { // ClearDNS server
char *config_file = init(argc, argv); char *config_file = init(argc, argv);
@ -56,26 +46,22 @@ int main(int argc, char *argv[]) { // ClearDNS server
log_info("ClearDNS server start (%s)", VERSION); log_info("ClearDNS server start (%s)", VERSION);
// process *test = process_init("TEST", "lls"); // process *test = process_init("TEST", "lls");
process *test = process_init("TEST", "ls"); // process *test = process_init("TEST", "ls");
// process_add_arg(test, "-al");
process_add_arg(test, "-al");
// test->cwd = "/etc/cleardns/fuck"; // test->cwd = "/etc/cleardns/fuck";
// int pid = process_exec(test);
// log_info("PID = %d", pid);
int pid = process_exec(test); // int status;
// wait(&status);
log_info("PID = %d", pid); // return 0;
int status;
wait(&status);
return 0;
create_folder(WORK_DIR); create_folder(WORK_DIR);
// load_config(config_file); // TODO: load assets first
// free(config_file); load_config(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"));
// process_list_append(dnsproxy_load("Foreign", loader.foreign, "foreign.json")); // process_list_append(dnsproxy_load("Foreign", loader.foreign, "foreign.json"));
@ -86,7 +72,7 @@ int main(int argc, char *argv[]) { // ClearDNS server
// TODO: running custom script // TODO: running custom script
process_list_run(); // process_list_run();
// init_server(init_script, custom_script); // run init script and custom script // init_server(init_script, custom_script); // run init script and custom script

1
src/common/structure.c

@ -17,6 +17,7 @@ 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 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); 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

2
src/loader/CMakeLists.txt

@ -1,4 +1,4 @@
cmake_minimum_required(VERSION 2.8.12) cmake_minimum_required(VERSION 2.8.12)
add_library(loader config.c default.c loader.c parser.c) add_library(loader config.c default.c loader.c parser.c)
target_link_libraries(loader applet) target_link_libraries(loader applet common)

10
src/loader/config.c

@ -36,6 +36,10 @@ cleardns_config* config_init() { // init config struct of cleardns
config->adguard.username = string_init(ADGUARD_USER); config->adguard.username = string_init(ADGUARD_USER);
config->adguard.password = string_init(ADGUARD_PASSWD); config->adguard.password = string_init(ADGUARD_PASSWD);
config->assets.cron = string_init(UPDATE_CRON);
config->assets.update_file = string_list_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();
config->ttl = string_list_init(); config->ttl = string_list_init();
@ -73,6 +77,12 @@ void config_dump(cleardns_config *config) { // dump config info of cleardns
log_debug("AdGuardHome username -> %s", config->adguard.username); log_debug("AdGuardHome username -> %s", config->adguard.username);
log_debug("AdGuardHome password -> %s", config->adguard.password); log_debug("AdGuardHome password -> %s", config->adguard.password);
log_debug("Assets update cron -> `%s`", config->assets.cron);
for (char **file = config->assets.update_file; *file != NULL; ++file) { // show string mapping
char **url = file - config->assets.update_file + config->assets.update_url;
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);
string_list_debug("Hosts", config->hosts); string_list_debug("Hosts", config->hosts);

7
src/loader/default.c

@ -40,6 +40,13 @@ foreign:\n\
fallback:\n\ fallback:\n\
- 1.1.1.1\n\ - 1.1.1.1\n\
- 8.8.4.4\n\ - 8.8.4.4\n\
\n\
assets:\n\
cron: \"0 4 * * *\"\n\
update:\n\
gfwlist.txt: https://res.dnomd343.top/Share/gfwlist/gfwlist.txt\n\
china-ip.txt: https://res.dnomd343.top/Share/chinalist/china-ip.txt\n\
chinalist.txt: https://res.dnomd343.top/Share/chinalist/chinalist.txt\n\
" "
void load_default_config(const char *config_file) { void load_default_config(const char *config_file) {

4
src/loader/loader.c

@ -117,6 +117,10 @@ void load_config(const char *config_file) {
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)
config->assets.update_file = string_list_append(config->assets.update_file, "geoip.dat");
config->assets.update_url = 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");

21
src/loader/parser.c

@ -112,6 +112,24 @@ void adguard_parser(adguard_config *config, cJSON *json) { // adguard options pa
} }
} }
void assets_parser(assets_config *config, cJSON *json) { // assets options parser
if (!cJSON_IsObject(json)) {
log_fatal("`assets` must be array");
}
json = json->child;
while (json != NULL) {
if (!strcmp(json->string, "cron")) {
config->cron = json_string_value("assets.cron", json);
}
if (!strcmp(json->string, "update")) {
log_warn("GET UPDATE KEY");
// TODO: get string map
}
json = json->next; // next field
}
}
void cleardns_parser(cleardns_config *config, const char *config_content) { // JSON format configure void cleardns_parser(cleardns_config *config, const char *config_content) { // JSON format configure
cJSON *json = cJSON_Parse(config_content); cJSON *json = cJSON_Parse(config_content);
if (json == NULL) { if (json == NULL) {
@ -137,6 +155,9 @@ void cleardns_parser(cleardns_config *config, const char *config_content) { // J
if (!strcmp(json->string, "adguard")) { if (!strcmp(json->string, "adguard")) {
adguard_parser(&config->adguard, json); adguard_parser(&config->adguard, json);
} }
if (!strcmp(json->string, "assets")) {
assets_parser(&config->assets, json);
}
if (!strcmp(json->string, "reject")) { if (!strcmp(json->string, "reject")) {
config->reject = json_uint32_list_value("reject", json, config->reject); config->reject = json_uint32_list_value("reject", json, config->reject);
} }

Loading…
Cancel
Save