Browse Source

feat: loading assets

dev
dnomd343 2 years ago
parent
commit
96724894bb
  1. 3
      include/constant.h
  2. 6
      include/utils/assets.h
  3. 94
      src/cleardns.c
  4. 4
      src/loader/loader.c
  5. 3
      src/loader/parser.c
  6. 2
      src/utils/CMakeLists.txt
  7. 40
      src/utils/assets.c

3
include/common/constant.h → include/constant.h

@ -20,9 +20,10 @@
#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 WORK_DIR "/etc/cleardns/"
#define EXPOSE_DIR "/cleardns/" #define EXPOSE_DIR "/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/"

6
include/utils/assets.h

@ -0,0 +1,6 @@
#ifndef ASSETS_H_
#define ASSETS_H_
void load_assets();
#endif

94
src/cleardns.c

@ -10,6 +10,7 @@
#include "structure.h" #include "structure.h"
#include "adguard.h" #include "adguard.h"
#include "system.h" #include "system.h"
#include "assets.h"
//#include <stdio.h> //#include <stdio.h>
//#include <string.h> //#include <string.h>
@ -35,7 +36,7 @@
// fprintf(stderr, "\"\n"); // fprintf(stderr, "\"\n");
//} //}
// TODO: load `--debug`, `--config ...`, `--version`
int main(int argc, char *argv[]) { // ClearDNS server int main(int argc, char *argv[]) { // ClearDNS server
@ -43,13 +44,10 @@ 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(); // TODO: load assets -> extract `/assets.tar.xz` -> ${ASSETS_DIR}*.txt
//
// temp = string_list_append(temp, "a"); load_assets();
// temp = string_list_append(temp, "b"); return 0;
// temp = string_list_append(temp, "c");
//
// save_string_list("test.txt", temp);
load_config("test.json"); load_config("test.json");
@ -60,86 +58,6 @@ int main(int argc, char *argv[]) { // ClearDNS server
adguard_load(loader.filter, ADGUARD_DIR); adguard_load(loader.filter, ADGUARD_DIR);
// char **temp = string_list_init();
// temp = string_list_append(temp, "123");
// temp = string_list_append(temp, "abc");
// temp = string_list_append(temp, "ok");
//
// char *str = string_list_dump(temp);
// log_info("`%s`", str);
// free(str);
// string_list_free(temp);
// uint32_t **temp = uint32_list_init();
// temp = uint32_list_append(temp, 123);
// temp = uint32_list_append(temp, 456);
// temp = uint32_list_append(temp, 789);
//
// char *str = uint32_list_dump(temp);
// log_info("`%s`\n", str);
// free(str);
// uint32_list_free(temp);
// dnsproxy *domestic = dnsproxy_init(DOMESTIC_PORT);
//
// dnsproxy_add_bootstrap(domestic, "1.1.1.1");
// dnsproxy_add_bootstrap(domestic, "8.8.8.8");
//
// dnsproxy_add_primary(domestic, "223.5.5.5");
// dnsproxy_add_primary(domestic, "tls://dns.pub");
//
// dnsproxy_add_fallback(domestic, "tls://223.6.6.6");
// dnsproxy_add_fallback(domestic, "tls://120.53.53.53");
//
// domestic->verify = FALSE;
// domestic->parallel = FALSE;
// domestic->optimistic = TRUE;
// domestic->debug = TRUE;
// domestic->cache = 4194304; // 4MiB
//
// process *p = dnsproxy_load("Domestic", domestic, "domestic.json");
// log_info("cmd -> %s", string_list_dump(p->cmd));
// log_info("env -> %s", string_list_dump(p->env));
// log_info("cwd -> %s", p->cwd);
// overture *diverter = overture_init();
//
// diverter->port = 5454;
// diverter->timeout = 8;
// diverter->domestic_ip_file = "china-ip.txt";
// diverter->domestic_domain_file = "chinalist.txt";
// diverter->foreign_domain_file = "gfwlist.txt";
//
// diverter->debug = TRUE;
// diverter->ttl_file = "domain_ttl.txt";
// diverter->host_file = "hosts.txt";
// diverter->reject_type = uint32_list_append(diverter->reject_type, 255);
// diverter->reject_type = uint32_list_append(diverter->reject_type, 254);
//
// process *p = overture_load(diverter, "overture.json");
// log_info("cmd -> %s", string_list_dump(p->cmd));
// log_info("env -> %s", string_list_dump(p->env));
// log_info("cwd -> %s", p->cwd);
// adguard *filter = adguard_init();
//
// filter->debug = TRUE;
// filter->dns_port = 54;
// filter->web_port = 8080;
// filter->upstream = "127.0.0.1:5454";
//
// filter->username = "dnomd343";
// filter->password = "password";
//
// process *p = adguard_load(filter, "/cleardns/adguard/");
// log_info("cmd -> %s", string_list_dump(p->cmd));
// log_info("env -> %s", string_list_dump(p->env));
// log_info("cwd -> %s", p->cwd);
// int debug_mode = 0; // int debug_mode = 0;
// fprintf(stderr, "[ClearDNS] Server start.\n"); // fprintf(stderr, "[ClearDNS] Server start.\n");
// for (char **p = argv; p < argv + argc; ++p) { // for (char **p = argv; p < argv + argc; ++p) {

4
src/loader/loader.c

@ -104,10 +104,12 @@ adguard* load_filter(cleardns_config *config) {
void load_config(const char *config_file) { void load_config(const char *config_file) {
cleardns_config *config = config_init(); cleardns_config *config = config_init();
// TODO: load default configure
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} // TODO: mkdir -p ${WORK_DIR}
if (!config->adguard.enable) { if (!config->adguard.enable) {

3
src/loader/parser.c

@ -152,6 +152,9 @@ void cleardns_parser(cleardns_config *config, const char *config_content) { // J
} }
void config_parser(cleardns_config *config, const char *config_file) { void config_parser(cleardns_config *config, const char *config_file) {
// TODO: load custom script
char *config_content = read_file(config_file); char *config_content = read_file(config_file);
if (is_json_suffix(config_file)) { // JSON format if (is_json_suffix(config_file)) { // JSON format
log_info("Start JSON configure parser"); log_info("Start JSON configure parser");

2
src/utils/CMakeLists.txt

@ -1,3 +1,3 @@
cmake_minimum_required(VERSION 2.8.12) cmake_minimum_required(VERSION 2.8.12)
add_library(utils cJSON.c logger.c process.c) add_library(utils cJSON.c assets.c logger.c process.c)

40
src/utils/assets.c

@ -0,0 +1,40 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "logger.h"
#include "sundry.h"
#include "system.h"
#include "constant.h"
void load_asset(const char *file);
// TODO: assets update -> crontab task
void load_assets() {
log_info("Start loading assets");
create_folder(ASSETS_DIR);
load_asset(ASSET_GFW_LIST);
load_asset(ASSET_CHINA_IP);
load_asset(ASSET_CHINA_LIST);
log_info("Assets loaded complete");
}
void load_asset(const char *file) {
log_debug("Start extract `%s`", file);
char *output_file = string_join(ASSETS_DIR, file);
if (is_file_exist(output_file)) {
log_debug("Assets `%s` exist -> skip extract", file);
free(output_file);
return;
}
free(output_file);
char *extract_cmd = (char *)malloc(strlen(ASSETS_PKG) + strlen(file) + strlen(ASSETS_DIR) + 15);
sprintf(extract_cmd, "tar xf %s ./%s -C %s", ASSETS_PKG, file, ASSETS_DIR);
if (run_command(extract_cmd)) {
log_warn("Extract asset `%s` failed", file);
} else {
log_info("Extract asset `%s` success", file);
}
free(extract_cmd);
}
Loading…
Cancel
Save