Browse Source

update: loader of diverter and filter

dev
dnomd343 2 years ago
parent
commit
ae89e93bc4
  1. 8
      include/common.h
  2. 6
      include/loader/loader.h
  3. 1
      include/utils/structure.h
  4. 7
      src/cleardns.c
  5. 2
      src/common.c
  6. 68
      src/loader/loader.c
  7. 2
      src/utils/json.c
  8. 7
      src/utils/structure.c

8
include/common.h

@ -31,6 +31,14 @@
#define ADGUARD_BIN "AdGuardHome"
#define WORK_DIR "/etc/cleardns/"
#define EXPOSE_DIR "/cleardns/"
#define ADGUARD_DIR "/cleardns/adguard/"
#define ASSET_TTL "ttl.txt"
#define ASSET_HOSTS "hosts.txt"
#define ASSET_GFW_LIST "gfwlist.txt"
#define ASSET_CHINA_IP "china-ip.txt"
#define ASSET_CHINA_LIST "chinalist.txt"
//extern char **adguard_command;
//extern char **overture_command;

6
include/loader/loader.h

@ -5,12 +5,14 @@
#include "dnsproxy.h"
#include "overture.h"
extern struct {
struct cleardns {
dnsproxy *domestic;
dnsproxy *foreign;
overture *diverter;
adguard *filter;
} loader;
};
extern struct cleardns loader;
void load_config(const char *config_file);

1
include/utils/structure.h

@ -8,6 +8,7 @@ char* uint32_list_dump(uint32_t **int_list);
void uint32_list_free(uint32_t **uint32_list);
uint32_t uint32_list_len(uint32_t **int_list);
uint32_t** uint32_list_append(uint32_t **int_list, uint32_t number);
uint32_t** uint32_list_update(uint32_t **base_list, uint32_t **update_list);
char** string_list_init();
void string_list_free(char **string_list);

7
src/cleardns.c

@ -43,8 +43,11 @@ int main(int argc, char *argv[]) { // ClearDNS server
load_config("test.json");
// dnsproxy_load("Domestic", loader.domestic, "domestic.json");
// dnsproxy_load("Foreign", loader.foreign, "foreign.json");
dnsproxy_load("Domestic", loader.domestic, "domestic.json");
dnsproxy_load("Foreign", loader.foreign, "foreign.json");
overture_load(loader.diverter, "overture.json");
adguard_load(loader.filter, ADGUARD_DIR);
// char **temp = string_list_init();
// temp = string_list_append(temp, "123");

2
src/common.c

@ -1,7 +1,6 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <sys/time.h>
#include "common.h"
#include "logger.h"
@ -58,6 +57,7 @@ uint16_t gen_rand_num(uint16_t limit) { // 0 ~ (limit - 1)
}
int run_command(const char *command) {
log_debug("Run command -> `%s`", command);
return system(command) / 256;
}

68
src/loader/loader.c

@ -1,8 +1,12 @@
#include <stdlib.h>
#include "loader.h"
#include "config.h"
#include "parser.h"
#include "dnsproxy.h"
#include "structure.h"
#include "common.h"
struct cleardns loader;
dnsproxy* load_domestic(cleardns_config *config) {
dnsproxy *domestic = dnsproxy_init(config->domestic.port);
@ -34,35 +38,45 @@ dnsproxy* load_foreign(cleardns_config *config) {
overture* load_diverter(cleardns_config *config) {
overture *diverter = overture_init();
// ttl_file
// host_file
// foreign_port
// domestic_port
// reject_type
// foreign_ip_file
// domestic_ip_file
// foreign_domain_file
// domestic_domain_file
// TODO: load assets file
diverter->port = config->diverter.port;
diverter->foreign_port = config->foreign.port;
diverter->domestic_port = config->domestic.port;
free(diverter->ttl_file);
free(diverter->host_file);
// TODO: load into ASSET_TTL
// TODO: load into ASSET_HOSTS
diverter->ttl_file = string_init(ASSET_TTL);
diverter->host_file = string_init(ASSET_HOSTS);
free(diverter->domestic_ip_file);
free(diverter->foreign_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->foreign_domain_file = string_init(ASSET_GFW_LIST);
diverter->domestic_domain_file = string_init(ASSET_CHINA_LIST);
// TODO: assets file append
diverter->reject_type = uint32_list_update(diverter->reject_type, config->reject);
return diverter;
}
adguard* load_filter(cleardns_config *config) {
if (!config->adguard.enable) {
return NULL; // disable adguard
}
adguard *filter = adguard_init();
// dns_port
// web_port
// upstream
// username
// password
filter->dns_port = config->port;
filter->web_port = config->adguard.port;
filter->username = config->adguard.username;
filter->password = config->adguard.password;
char *diverter_port = uint32_to_string(config->diverter.port);
filter->upstream = string_join("127.0.0.1:", diverter_port);
free(diverter_port);
return filter;
}
@ -70,9 +84,9 @@ void load_config(const char *config_file) {
cleardns_config *config = config_init();
config_parser(config, config_file);
config_dump(config);
// TODO: use dns port as diverter when adguard disabled
if (!config->adguard.enable) {
config->diverter.port = config->port; // override diverter port by dns port
}
loader.domestic = load_domestic(config);
loader.foreign = load_foreign(config);
loader.diverter = load_diverter(config);

2
src/utils/json.c

@ -16,7 +16,7 @@ char* to_json(const char *file) {
char *output_file = string_join("/tmp/tojson-", flag);
char *to_json_cmd = (char *)malloc(strlen(file) + strlen(output_file) + 11);
sprintf(to_json_cmd, "toJSON %s > %s", file, output_file);
log_debug("JSON format command -> `%s`", to_json_cmd);
// log_debug("JSON format command -> `%s`", to_json_cmd);
if (run_command(to_json_cmd)) { // toJSON return non-zero code
// TODO: try remove output file
return NULL; // convert failed

7
src/utils/structure.c

@ -37,6 +37,13 @@ char** string_list_update(char **base_list, char **update_list) {
return base_list;
}
uint32_t** uint32_list_update(uint32_t **base_list, uint32_t **update_list) {
for (uint32_t **number = update_list; *number != NULL; ++number) {
base_list = uint32_list_append(base_list, **number);
}
return base_list;
}
void string_list_free(char **string_list) { // free string list
for (char **string = string_list; *string != NULL; ++string) {
free(*string);

Loading…
Cancel
Save