Browse Source

feat: loader interface

dev
dnomd343 2 years ago
parent
commit
76155b916f
  1. 10
      include/loader/loader.h
  2. 1
      include/utils/structure.h
  3. 3
      src/cleardns.c
  4. 4
      src/dnsproxy.c
  5. 54
      src/loader/loader.c
  6. 5
      src/overture.c
  7. 7
      src/utils/structure.c

10
include/loader/loader.h

@ -1,6 +1,16 @@
#ifndef _LOADER_H_ #ifndef _LOADER_H_
#define _LOADER_H_ #define _LOADER_H_
#include "dnsproxy.h"
#include "overture.h"
struct {
dnsproxy *domestic;
dnsproxy *foreign;
overture *diverter;
// adguard
} loader;
void load_config(const char *config_file); void load_config(const char *config_file);
#endif #endif

1
include/utils/structure.h

@ -13,6 +13,7 @@ char** string_list_init();
void string_list_free(char **string_list); void string_list_free(char **string_list);
char* string_list_dump(char **string_list); char* string_list_dump(char **string_list);
uint32_t string_list_len(char **string_list); uint32_t string_list_len(char **string_list);
char** string_list_update(char **base_list, char **update_list);
char** string_list_append(char **string_list, const char *string); char** string_list_append(char **string_list, const char *string);
#endif #endif

3
src/cleardns.c

@ -38,6 +38,9 @@ int main(int argc, char *argv[]) { // ClearDNS server
load_config("test.json"); load_config("test.json");
dnsproxy_load("Domestic", loader.domestic, "domestic.json");
dnsproxy_load("Foreign", loader.foreign, "foreign.json");
// char **temp = string_list_init(); // char **temp = string_list_init();
// temp = string_list_append(temp, "123"); // temp = string_list_append(temp, "123");
// temp = string_list_append(temp, "abc"); // temp = string_list_append(temp, "abc");

4
src/dnsproxy.c

@ -58,6 +58,10 @@ void dnsproxy_dump(const char *caption, dnsproxy *info) { // show dnsproxy info
} }
process* dnsproxy_load(const char *caption, dnsproxy *info, const char *file) { process* dnsproxy_load(const char *caption, dnsproxy *info, const char *file) {
// TODO: check primary server number (non-zero)
// TODO: check port (1 ~ 65535)
dnsproxy_dump(caption, info); dnsproxy_dump(caption, info);
char *config = dnsproxy_config(info); // string config (JSON format) char *config = dnsproxy_config(info); // string config (JSON format)
char *config_file = string_join(WORK_DIR, file); char *config_file = string_join(WORK_DIR, file);

54
src/loader/loader.c

@ -1,19 +1,65 @@
#include "loader.h" #include "loader.h"
#include "logger.h"
#include "config.h" #include "config.h"
#include "parser.h" #include "parser.h"
#include "dnsproxy.h"
#include "structure.h"
void load_dnsproxy() { dnsproxy* load_domestic(cleardns_config *config) {
dnsproxy *domestic = dnsproxy_init(config->domestic.port);
domestic->verify = config->domestic.verify;
domestic->parallel = config->domestic.parallel;
if (config->cache.enable) {
domestic->cache = config->cache.size;
domestic->optimistic = config->cache.optimistic;
}
domestic->bootstrap = string_list_update(domestic->bootstrap, config->domestic.bootstrap);
domestic->fallback = string_list_update(domestic->fallback, config->domestic.fallback);
domestic->primary = string_list_update(domestic->primary, config->domestic.primary);
return domestic;
}
dnsproxy* load_foreign(cleardns_config *config) {
dnsproxy *foreign = dnsproxy_init(config->foreign.port);
foreign->verify = config->foreign.verify;
foreign->parallel = config->foreign.parallel;
if (config->cache.enable) {
foreign->cache = config->cache.size;
foreign->optimistic = config->cache.optimistic;
}
foreign->bootstrap = string_list_update(foreign->bootstrap, config->foreign.bootstrap);
foreign->fallback = string_list_update(foreign->fallback, config->foreign.fallback);
foreign->primary = string_list_update(foreign->primary, config->foreign.primary);
return foreign;
}
overture* load_diverter(cleardns_config *config) {
overture *diverter = overture_init(config->diverter.port);
// timeout
// ttl_file
// host_file
// foreign_port
// domestic_port
// reject_type
// foreign_ip_file
// domestic_ip_file
// foreign_domain_file
// domestic_domain_file
return diverter;
} }
void load_config(const char *config_file) { 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: load into process // TODO: use dns port as diverter when adguard disabled
loader.domestic = load_domestic(config);
loader.foreign = load_foreign(config);
loader.diverter = load_diverter(config);
// load adguard
config_free(config); config_free(config);
} }

5
src/overture.c

@ -45,6 +45,11 @@ void overture_dump(overture *info) { // show overture info in debug log
} }
process* overture_load(overture *info, const char *file) { process* overture_load(overture *info, const char *file) {
// TODO: check port (1 ~ 65535)
// TODO: check timeout not zero
// TODO: check whether file exist
overture_dump(info); overture_dump(info);
char *config = overture_config(info); // string config (JSON format) char *config = overture_config(info); // string config (JSON format)
char *config_file = string_join(WORK_DIR, file); char *config_file = string_join(WORK_DIR, file);

7
src/utils/structure.c

@ -29,6 +29,13 @@ char** string_list_append(char **string_list, const char *string) { // add new s
return string_list; return string_list;
} }
char** string_list_update(char **base_list, char **update_list) {
for (char **string = update_list; *string != NULL; ++string) {
base_list = string_list_append(base_list, *string);
}
return base_list;
}
void string_list_free(char **string_list) { // free string list void string_list_free(char **string_list) { // free string list
for (char **string = string_list; *string != NULL; ++string) { for (char **string = string_list; *string != NULL; ++string) {
free(*string); free(*string);

Loading…
Cancel
Save