Browse Source

Merge branch 'dev'

dev
Dnomd343 2 years ago
parent
commit
2bfed836fc
  1. 4
      include/applet/dnsproxy.h
  2. 2
      include/common/sundry.h
  3. 4
      src/applet/adguard.c
  4. 3
      src/applet/crontab.c
  5. 12
      src/applet/dnsproxy.c
  6. 9
      src/applet/overture.c
  7. 6
      src/cleardns.c
  8. 6
      src/common/json.c
  9. 7
      src/common/structure.c
  10. 21
      src/common/sundry.c
  11. 6
      src/common/system.c
  12. 7
      src/loader/config.c
  13. 4
      src/loader/default.c
  14. 18
      src/loader/loader.c
  15. 4
      src/utils/assets.c
  16. 5
      src/utils/process.c

4
include/applet/dnsproxy.h

@ -20,8 +20,4 @@ void dnsproxy_free(dnsproxy *info);
dnsproxy* dnsproxy_init(uint16_t port); dnsproxy* dnsproxy_init(uint16_t port);
process* dnsproxy_load(const char *caption, dnsproxy *info, const char *file); process* dnsproxy_load(const char *caption, dnsproxy *info, const char *file);
void dnsproxy_add_primary(dnsproxy *info, const char *server);
void dnsproxy_add_fallback(dnsproxy *info, const char *server);
void dnsproxy_add_bootstrap(dnsproxy *info, const char *server);
#endif #endif

2
include/common/sundry.h

@ -4,7 +4,7 @@
#include <stdint.h> #include <stdint.h>
char* show_bool(uint8_t value); char* show_bool(uint8_t value);
char* string_init(const char *str); char* string_load(const char *fmt, ...);
char* uint32_to_string(uint32_t number); char* uint32_to_string(uint32_t number);
char* string_join(const char *base, const char *add); char* string_join(const char *base, const char *add);
void string_list_debug(char *describe, char **string_list); void string_list_debug(char *describe, char **string_list);

4
src/applet/adguard.c

@ -25,8 +25,8 @@ adguard* adguard_init() { // init adguard options
info->dns_port = DNS_PORT; info->dns_port = DNS_PORT;
info->web_port = ADGUARD_PORT; info->web_port = ADGUARD_PORT;
info->upstream = string_join("127.0.0.1:", port_str); // default upstream info->upstream = string_join("127.0.0.1:", port_str); // default upstream
info->username = string_init(ADGUARD_USER); info->username = strdup(ADGUARD_USER);
info->password = string_init(ADGUARD_PASSWD); info->password = strdup(ADGUARD_PASSWD);
free(port_str); free(port_str);
return info; return info;
} }

3
src/applet/crontab.c

@ -1,4 +1,5 @@
#include <stdlib.h> #include <stdlib.h>
#include <string.h>
#include <unistd.h> #include <unistd.h>
#include "logger.h" #include "logger.h"
#include "sundry.h" #include "sundry.h"
@ -17,7 +18,7 @@ void crontab_free(crontab *info) { // free crontab options
crontab* crontab_init() { // init crontab options crontab* crontab_init() { // init crontab options
crontab *info = (crontab *)malloc(sizeof(crontab)); crontab *info = (crontab *)malloc(sizeof(crontab));
info->debug = FALSE; info->debug = FALSE;
info->cron = string_init(UPDATE_CRON); info->cron = strdup(UPDATE_CRON);
return info; return info;
} }

12
src/applet/dnsproxy.c

@ -10,18 +10,6 @@
char* dnsproxy_config(dnsproxy *info); char* dnsproxy_config(dnsproxy *info);
void dnsproxy_dump(const char *caption, dnsproxy *info); void dnsproxy_dump(const char *caption, dnsproxy *info);
void dnsproxy_add_primary(dnsproxy *info, const char *server) { // add primary dns server
string_list_append(&info->primary, server);
}
void dnsproxy_add_fallback(dnsproxy *info, const char *server) { // add fallback dns server
string_list_append(&info->fallback, server);
}
void dnsproxy_add_bootstrap(dnsproxy *info, const char *server) { // add bootstrap dns server
string_list_append(&info->bootstrap, server);
}
void dnsproxy_free(dnsproxy *info) { // free dnsproxy options void dnsproxy_free(dnsproxy *info) { // free dnsproxy options
string_list_free(info->bootstrap); string_list_free(info->bootstrap);
string_list_free(info->fallback); string_list_free(info->fallback);

9
src/applet/overture.c

@ -1,4 +1,5 @@
#include <stdlib.h> #include <stdlib.h>
#include <string.h>
#include "cJSON.h" #include "cJSON.h"
#include "logger.h" #include "logger.h"
#include "sundry.h" #include "sundry.h"
@ -30,10 +31,10 @@ overture* overture_init() { // init overture options
info->foreign_port = FOREIGN_PORT; info->foreign_port = FOREIGN_PORT;
info->domestic_port = DOMESTIC_PORT; info->domestic_port = DOMESTIC_PORT;
info->reject_type = uint32_list_init(); info->reject_type = uint32_list_init();
info->foreign_ip_file = string_init("/dev/null"); info->foreign_ip_file = strdup("/dev/null");
info->domestic_ip_file = string_init("/dev/null"); info->domestic_ip_file = strdup("/dev/null");
info->foreign_domain_file = string_init("/dev/null"); info->foreign_domain_file = strdup("/dev/null");
info->domestic_domain_file = string_init("/dev/null"); info->domestic_domain_file = strdup("/dev/null");
return info; return info;
} }

6
src/cleardns.c

@ -22,13 +22,13 @@ struct {
} settings; } settings;
void init(int argc, char *argv[]) { // return config file void init(int argc, char *argv[]) { // return config file
settings.config = string_init(CONFIG_FILE); settings.config = strdup(CONFIG_FILE);
settings.debug = FALSE; settings.debug = FALSE;
settings.verbose = FALSE; settings.verbose = FALSE;
if (getenv("CONFIG") != NULL) { if (getenv("CONFIG") != NULL) {
free(settings.config); free(settings.config);
settings.config = string_init(getenv("CONFIG")); settings.config = strdup(getenv("CONFIG"));
} }
if (getenv("DEBUG") != NULL && !strcmp(getenv("DEBUG"), "TRUE")) { if (getenv("DEBUG") != NULL && !strcmp(getenv("DEBUG"), "TRUE")) {
settings.debug = TRUE; settings.debug = TRUE;
@ -55,7 +55,7 @@ void init(int argc, char *argv[]) { // return config file
exit(1); exit(1);
} }
free(settings.config); free(settings.config);
settings.config = string_init(argv[++i]); // use custom config file settings.config = strdup(argv[++i]); // use custom config file
} }
} }
log_debug("Config file -> %s", settings.config); log_debug("Config file -> %s", settings.config);

6
src/common/json.c

@ -1,4 +1,3 @@
#include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#include "cJSON.h" #include "cJSON.h"
@ -27,8 +26,7 @@ char* to_json(const char *file) { // convert JSON / TOML / YAML to json format (
flag[8] = '\0'; flag[8] = '\0';
char *output_file = string_join("/tmp/to-json-", flag); char *output_file = string_join("/tmp/to-json-", flag);
char *to_json_cmd = (char *)malloc(strlen(file) + strlen(output_file) + 11); char *to_json_cmd = string_load("toJSON %s > %s", file, output_file);
sprintf(to_json_cmd, "toJSON %s > %s", file, output_file);
int to_json_ret = run_command(to_json_cmd); int to_json_ret = run_command(to_json_cmd);
free(to_json_cmd); free(to_json_cmd);
@ -87,7 +85,7 @@ char* json_string_value(char* caption, cJSON *json) { // json string value -> st
if (!cJSON_IsString(json)) { if (!cJSON_IsString(json)) {
log_fatal("`%s` must be string", caption); log_fatal("`%s` must be string", caption);
} }
return string_init(json->valuestring); return strdup(json->valuestring);
} }
char** json_string_list_value(char *caption, cJSON *json, char **string_list) { // json string array char** json_string_list_value(char *caption, cJSON *json, char **string_list) { // json string array

7
src/common/structure.c

@ -2,7 +2,6 @@
#include <stdint.h> #include <stdint.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#include "sundry.h"
#include "structure.h" #include "structure.h"
char** string_list_init() { // init string list char** string_list_init() { // init string list
@ -20,7 +19,7 @@ uint32_t string_list_len(char **string_list) { // get len of string list
void string_list_append(char ***string_list, const char *string) { void string_list_append(char ***string_list, const char *string) {
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
(*string_list)[len] = string_init(string); (*string_list)[len] = strdup(string);
(*string_list)[len + 1] = NULL; // list end sign (*string_list)[len + 1] = NULL; // list end sign
} }
@ -39,7 +38,7 @@ void string_list_free(char **string_list) { // free string list
char* string_list_dump(char **string_list) { // ['a', 'b', 'c', ...] char* string_list_dump(char **string_list) { // ['a', 'b', 'c', ...]
if (string_list_len(string_list) == 0) { if (string_list_len(string_list) == 0) {
return string_init("[]"); // empty string list return strdup("[]"); // empty string list
} }
char *string_ret = (char *)malloc(2); char *string_ret = (char *)malloc(2);
strcpy(string_ret, "["); strcpy(string_ret, "[");
@ -86,7 +85,7 @@ void uint32_list_free(uint32_t **uint32_list) { // free uint32 list
char* uint32_list_dump(uint32_t **uint32_list) { // [1, 2, 3, ...] char* uint32_list_dump(uint32_t **uint32_list) { // [1, 2, 3, ...]
if (uint32_list_len(uint32_list) == 0) { if (uint32_list_len(uint32_list) == 0) {
return string_init("[]"); // empty int list return strdup("[]"); // empty int list
} }
char uint32_str[12]; char uint32_str[12];
char *string_ret = (char *)malloc(2); char *string_ret = (char *)malloc(2);

21
src/common/sundry.c

@ -1,8 +1,13 @@
#ifndef _GNU_SOURCE
#define _GNU_SOURCE // NOLINT
#endif
#include <stdio.h> #include <stdio.h>
#include <stdarg.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#include <sys/time.h>
#include <unistd.h> #include <unistd.h>
#include <sys/time.h>
#include "logger.h" #include "logger.h"
#include "constant.h" #include "constant.h"
#include "structure.h" #include "structure.h"
@ -14,19 +19,23 @@ char* show_bool(uint8_t value) { // return `true` or `false`
return "false"; return "false";
} }
char* string_init(const char *str) { // new string
return strcpy((char *)malloc(strlen(str) + 1), str);
}
char* string_join(const char *base, const char *add) { // combine string char* string_join(const char *base, const char *add) { // combine string
char *ret = (char *)malloc(strlen(base) + strlen(add) + 1); char *ret = (char *)malloc(strlen(base) + strlen(add) + 1);
return strcat(strcpy(ret, base), add); return strcat(strcpy(ret, base), add);
} }
char* string_load(const char *fmt, ...) {
va_list ap;
va_start(ap, fmt);
char *buf = NULL;
vasprintf(&buf, fmt, ap);
return buf;
}
char* uint32_to_string(uint32_t number) { // convert uint32 -> string char* uint32_to_string(uint32_t number) { // convert uint32 -> string
char to_str[11]; // MAX_LEN(uint32) -> 4294967296(10-bytes) char to_str[11]; // MAX_LEN(uint32) -> 4294967296(10-bytes)
sprintf(to_str, "%u", number); sprintf(to_str, "%u", number);
return string_init(to_str); return strdup(to_str);
} }
void string_list_debug(char *describe, char **string_list) { // show string list in debug log void string_list_debug(char *describe, char **string_list) { // show string list in debug log

6
src/common/system.c

@ -66,8 +66,7 @@ void save_file(const char *file, const char *content) { // save content into fil
} }
void file_append(const char *base_file, const char *append_file) { // append_file >> base_file void file_append(const char *base_file, const char *append_file) { // append_file >> base_file
char *append_cmd = (char *)malloc(strlen(base_file) + strlen(append_file) + 9); char *append_cmd = string_load("cat %s >> %s", append_file, base_file);
sprintf(append_cmd, "cat %s >> %s", append_file, base_file);
run_command(append_cmd); run_command(append_cmd);
free(append_cmd); free(append_cmd);
} }
@ -109,8 +108,7 @@ void save_string_list(const char *file, char **string_list) { // save string lis
void download_file(const char *file, const char *url) { // download file void download_file(const char *file, const char *url) { // download file
log_debug("Download file `%s` -> %s", file, url); log_debug("Download file `%s` -> %s", file, url);
char *download_cmd = (char *)malloc(strlen(file) + strlen(url) + 15); char *download_cmd = string_load("wget -T 8 -O %s %s", file, url);
sprintf(download_cmd, "wget -T 8 -O %s %s", file, url);
if (run_command(download_cmd)) { if (run_command(download_cmd)) {
log_warn("File `%s` download failed", url); log_warn("File `%s` download failed", url);
} }

7
src/loader/config.c

@ -1,4 +1,5 @@
#include <stdlib.h> #include <stdlib.h>
#include <string.h>
#include "config.h" #include "config.h"
#include "logger.h" #include "logger.h"
#include "sundry.h" #include "sundry.h"
@ -33,11 +34,11 @@ cleardns_config* config_init() { // init config struct of cleardns
config->adguard.port = ADGUARD_PORT; config->adguard.port = ADGUARD_PORT;
config->adguard.enable = TRUE; config->adguard.enable = TRUE;
config->adguard.username = string_init(ADGUARD_USER); config->adguard.username = strdup(ADGUARD_USER);
config->adguard.password = string_init(ADGUARD_PASSWD); config->adguard.password = strdup(ADGUARD_PASSWD);
config->assets.disable = FALSE; config->assets.disable = FALSE;
config->assets.cron = string_init(UPDATE_CRON); config->assets.cron = strdup(UPDATE_CRON);
config->assets.update_file = string_list_init(); config->assets.update_file = string_list_init();
config->assets.update_url = string_list_init(); config->assets.update_url = string_list_init();

4
src/loader/default.c

@ -1,7 +1,7 @@
#include <stdlib.h> #include <stdlib.h>
#include <string.h>
#include "json.h" #include "json.h"
#include "logger.h" #include "logger.h"
#include "sundry.h"
#include "system.h" #include "system.h"
#define DEFAULT_CONFIG "\ #define DEFAULT_CONFIG "\
@ -63,7 +63,7 @@ void load_default_config(const char *config_file) {
remove_file(temp_file); remove_file(temp_file);
} }
if (config_content == NULL) { if (config_content == NULL) {
config_content = string_init(DEFAULT_CONFIG); config_content = strdup(DEFAULT_CONFIG);
} }
save_file(config_file, config_content); save_file(config_file, config_content);
free(config_content); free(config_content);

18
src/loader/loader.c

@ -1,5 +1,5 @@
#include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h>
#include "config.h" #include "config.h"
#include "loader.h" #include "loader.h"
#include "logger.h" #include "logger.h"
@ -63,14 +63,14 @@ overture* load_diverter(cleardns_config *config) {
if (string_list_len(config->ttl)) { if (string_list_len(config->ttl)) {
free(diverter->ttl_file); free(diverter->ttl_file);
diverter->ttl_file = string_init(ASSET_TTL); diverter->ttl_file = strdup(ASSET_TTL);
char *ttl_file = string_join(WORK_DIR, ASSET_TTL); char *ttl_file = string_join(WORK_DIR, ASSET_TTL);
save_string_list(ttl_file, config->ttl); save_string_list(ttl_file, config->ttl);
free(ttl_file); free(ttl_file);
} }
if (string_list_len(config->hosts)) { if (string_list_len(config->hosts)) {
free(diverter->host_file); free(diverter->host_file);
diverter->host_file = string_init(ASSET_HOSTS); diverter->host_file = strdup(ASSET_HOSTS);
char *hosts_file = string_join(WORK_DIR, ASSET_HOSTS); char *hosts_file = string_join(WORK_DIR, ASSET_HOSTS);
save_string_list(hosts_file, config->hosts); save_string_list(hosts_file, config->hosts);
free(hosts_file); free(hosts_file);
@ -79,9 +79,9 @@ overture* load_diverter(cleardns_config *config) {
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);
diverter->domestic_ip_file = string_init(ASSET_CHINA_IP); diverter->domestic_ip_file = strdup(ASSET_CHINA_IP);
diverter->foreign_domain_file = string_init(ASSET_GFW_LIST); diverter->foreign_domain_file = strdup(ASSET_GFW_LIST);
diverter->domestic_domain_file = string_init(ASSET_CHINA_LIST); diverter->domestic_domain_file = strdup(ASSET_CHINA_LIST);
char *gfwlist = string_join(WORK_DIR, ASSET_GFW_LIST); char *gfwlist = string_join(WORK_DIR, ASSET_GFW_LIST);
char *china_ip = string_join(WORK_DIR, ASSET_CHINA_IP); char *china_ip = string_join(WORK_DIR, ASSET_CHINA_IP);
@ -108,8 +108,8 @@ adguard* load_filter(cleardns_config *config) {
adguard *filter = adguard_init(); adguard *filter = adguard_init();
filter->dns_port = config->port; filter->dns_port = config->port;
filter->web_port = config->adguard.port; filter->web_port = config->adguard.port;
filter->username = string_init(config->adguard.username); filter->username = strdup(config->adguard.username);
filter->password = string_init(config->adguard.password); filter->password = strdup(config->adguard.password);
char *diverter_port = uint32_to_string(config->diverter.port); char *diverter_port = uint32_to_string(config->diverter.port);
filter->upstream = string_join("127.0.0.1:", diverter_port); filter->upstream = string_join("127.0.0.1:", diverter_port);
free(diverter_port); free(diverter_port);
@ -121,7 +121,7 @@ crontab* load_crond(cleardns_config *config) {
return NULL; // disable crond return NULL; // disable crond
} }
crontab *crond = crontab_init(); crontab *crond = crontab_init();
crond->cron = string_init(config->assets.cron); crond->cron = strdup(config->assets.cron);
return crond; return crond;
} }

4
src/utils/assets.c

@ -1,4 +1,3 @@
#include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#include <signal.h> #include <signal.h>
@ -81,8 +80,7 @@ void extract(const char *file) { // extract one asset file from `.tar.xz` file
} }
free(output_file); free(output_file);
char *extract_cmd = (char *)malloc(strlen(ASSETS_PKG) + strlen(file) + strlen(ASSETS_DIR) + 13); char *extract_cmd = string_load("tar xf %s %s -C %s", ASSETS_PKG, file, ASSETS_DIR);
sprintf(extract_cmd, "tar xf %s %s -C %s", ASSETS_PKG, file, ASSETS_DIR);
if (run_command(extract_cmd)) { if (run_command(extract_cmd)) {
log_warn("Extract asset `%s` failed", file); log_warn("Extract asset `%s` failed", file);
} else { } else {

5
src/utils/process.c

@ -3,6 +3,7 @@
#endif #endif
#include <stdlib.h> #include <stdlib.h>
#include <string.h>
#include <unistd.h> #include <unistd.h>
#include <sys/wait.h> #include <sys/wait.h>
#include <sys/prctl.h> #include <sys/prctl.h>
@ -28,7 +29,7 @@ void process_exec(process *proc);
process* process_init(const char *caption, const char *bin) { // init process struct process* process_init(const char *caption, const char *bin) { // init process struct
process *proc = (process *)malloc(sizeof(process)); process *proc = (process *)malloc(sizeof(process));
proc->pid = 0; // process not running proc->pid = 0; // process not running
proc->name = string_init(caption); // process caption proc->name = strdup(caption); // process caption
proc->cmd = string_list_init(); proc->cmd = string_list_init();
string_list_append(&proc->cmd, bin); // argv[0] normally be process file name string_list_append(&proc->cmd, bin); // argv[0] normally be process file name
proc->env = string_list_init(); // empty environment variable proc->env = string_list_init(); // empty environment variable
@ -116,7 +117,7 @@ char* get_exit_msg(int status) { // get why the child process death
free(exit_sig); free(exit_sig);
return exit_msg; return exit_msg;
} }
return string_init("Unknown reason"); return strdup("Unknown reason");
} }
void server_exit(int exit_code) { // kill sub process and exit void server_exit(int exit_code) { // kill sub process and exit

Loading…
Cancel
Save