Browse Source

update: load diverter assets

dev
dnomd343 2 years ago
parent
commit
8d31a49cdc
  1. 3
      include/common.h
  2. 9
      src/cleardns.c
  3. 20
      src/common.c
  4. 44
      src/loader/loader.c

3
include/common.h

@ -32,6 +32,7 @@
#define WORK_DIR "/etc/cleardns/" #define WORK_DIR "/etc/cleardns/"
#define EXPOSE_DIR "/cleardns/" #define EXPOSE_DIR "/cleardns/"
#define ASSETS_DIR "/cleardns/assets/"
#define ADGUARD_DIR "/cleardns/adguard/" #define ADGUARD_DIR "/cleardns/adguard/"
#define ASSET_TTL "ttl.txt" #define ASSET_TTL "ttl.txt"
@ -57,6 +58,8 @@ uint8_t check_port(uint16_t port);
uint16_t gen_rand_num(uint16_t limit); uint16_t gen_rand_num(uint16_t limit);
char* uint32_to_string(uint32_t number); char* uint32_to_string(uint32_t number);
void save_string_list(const char *file, char **string_list);
char* gen_bcrypt(const char *data); char* gen_bcrypt(const char *data);
int run_command(const char *command); int run_command(const char *command);
void create_folder(const char *folder); void create_folder(const char *folder);

9
src/cleardns.c

@ -41,6 +41,15 @@ int main(int argc, char *argv[]) { // ClearDNS server
log_info("ClearDNS server start (%s)", VERSION); log_info("ClearDNS server start (%s)", VERSION);
// char **temp = string_list_init();
//
// temp = string_list_append(temp, "a");
// temp = string_list_append(temp, "b");
// temp = string_list_append(temp, "c");
//
// save_string_list("test.txt", temp);
load_config("test.json"); load_config("test.json");
dnsproxy_load("Domestic", loader.domestic, "domestic.json"); dnsproxy_load("Domestic", loader.domestic, "domestic.json");

20
src/common.c

@ -56,8 +56,11 @@ uint16_t gen_rand_num(uint16_t limit) { // 0 ~ (limit - 1)
return rand() % limit; // NOLINT return rand() % limit; // NOLINT
} }
int run_command(const char *command) { int run_command(const char *command) { // running command with system shell
log_debug("Run command -> `%s`", command); log_debug("Run command -> `%s`", command);
// TODO: add non-zero return code warning
return system(command) / 256; return system(command) / 256;
} }
@ -114,3 +117,18 @@ char* read_file(const char *file) { // read file content
log_debug("File `%s` read success ->\n%s", file, content); log_debug("File `%s` read success ->\n%s", file, content);
return content; return content;
} }
void save_string_list(const char *file, char **string_list) { // save string list into file
log_debug("Write string list into `%s`", file);
FILE* fp = fopen(file , "w");
if (fp == NULL) {
log_fatal("Fail to open file -> %s", file);
}
for (char **string = string_list; *string != NULL; ++string) {
log_debug("File append -> `%s`", *string);
fputs(*string, fp);
fputs("\n", fp);
}
fclose(fp);
log_debug("Save `%s` success", file);
}

44
src/loader/loader.c

@ -8,6 +8,12 @@
struct cleardns loader; struct cleardns loader;
void load_diverter_assets() {
// ${ASSETS_DIR}${ASSET_GFW_LIST} >> ${WORK_DIR}${ASSET_GFW_LIST}
// ${ASSETS_DIR}${ASSET_CHINA_IP} >> ${WORK_DIR}${ASSET_CHINA_IP}
// ${ASSETS_DIR}${ASSET_CHINA_LIST} >> ${WORK_DIR}${ASSET_CHINA_LIST}
}
dnsproxy* load_domestic(cleardns_config *config) { dnsproxy* load_domestic(cleardns_config *config) {
dnsproxy *domestic = dnsproxy_init(config->domestic.port); dnsproxy *domestic = dnsproxy_init(config->domestic.port);
domestic->verify = config->domestic.verify; domestic->verify = config->domestic.verify;
@ -42,26 +48,40 @@ overture* load_diverter(cleardns_config *config) {
diverter->foreign_port = config->foreign.port; diverter->foreign_port = config->foreign.port;
diverter->domestic_port = config->domestic.port; diverter->domestic_port = config->domestic.port;
free(diverter->ttl_file); if (string_list_len(config->ttl)) {
free(diverter->host_file); free(diverter->ttl_file);
// TODO: load into ASSET_TTL diverter->ttl_file = string_init(ASSET_TTL);
// TODO: load into ASSET_HOSTS char *ttl_file = string_join(WORK_DIR, ASSET_TTL);
diverter->ttl_file = string_init(ASSET_TTL); save_string_list(ttl_file, config->ttl);
diverter->host_file = string_init(ASSET_HOSTS); free(ttl_file);
}
if (string_list_len(config->hosts)) {
free(diverter->host_file);
diverter->host_file = string_init(ASSET_HOSTS);
char *hosts_file = string_join(WORK_DIR, ASSET_HOSTS);
save_string_list(hosts_file, config->hosts);
free(hosts_file);
}
free(diverter->domestic_ip_file); free(diverter->domestic_ip_file);
free(diverter->foreign_domain_file); free(diverter->foreign_domain_file);
free(diverter->domestic_domain_file); free(diverter->domestic_domain_file);
// TODO: load into ASSET_CHINA_IP
// TODO: load into ASSET_GFW_LIST
// TODO: load into ASSET_CHINA_LIST
diverter->domestic_ip_file = string_init(ASSET_CHINA_IP); diverter->domestic_ip_file = string_init(ASSET_CHINA_IP);
diverter->foreign_domain_file = string_init(ASSET_GFW_LIST); diverter->foreign_domain_file = string_init(ASSET_GFW_LIST);
diverter->domestic_domain_file = string_init(ASSET_CHINA_LIST); diverter->domestic_domain_file = string_init(ASSET_CHINA_LIST);
// TODO: assets file append char *gfwlist = string_join(WORK_DIR, ASSET_GFW_LIST);
char *china_ip = string_join(WORK_DIR, ASSET_CHINA_IP);
char *chinalist = string_join(WORK_DIR, ASSET_CHINA_LIST);
save_string_list(gfwlist, config->diverter.gfwlist);
save_string_list(china_ip, config->diverter.china_ip);
save_string_list(chinalist, config->diverter.chinalist);
free(chinalist);
free(china_ip);
free(gfwlist);
diverter->reject_type = uint32_list_update(diverter->reject_type, config->reject); diverter->reject_type = uint32_list_update(diverter->reject_type, config->reject);
load_diverter_assets();
return diverter; return diverter;
} }
@ -84,6 +104,10 @@ void load_config(const char *config_file) {
cleardns_config *config = config_init(); cleardns_config *config = config_init();
config_parser(config, config_file); config_parser(config, config_file);
config_dump(config); config_dump(config);
// TODO: extract assets file
// TODO: mkdir -p ${WORK_DIR}
if (!config->adguard.enable) { if (!config->adguard.enable) {
config->diverter.port = config->port; // override diverter port by dns port config->diverter.port = config->port; // override diverter port by dns port
} }

Loading…
Cancel
Save