From 42921ce8c35c61890f6727bc47a3acbf0bfe2a2f Mon Sep 17 00:00:00 2001 From: Nick Peng Date: Fri, 26 Jan 2024 21:45:59 +0800 Subject: [PATCH] smartdns: some bug fixes --- ReadMe.md | 1 + src/dns_conf.c | 11 ++++++++++- src/dns_conf.h | 1 + src/dns_server.c | 2 +- src/http_parse.c | 4 ++++ src/smartdns.c | 25 +++++++++++++++++++++++++ src/tlog.c | 5 ++++- 7 files changed, 46 insertions(+), 3 deletions(-) diff --git a/ReadMe.md b/ReadMe.md index 89ab865431..c0f1126fef 100644 --- a/ReadMe.md +++ b/ReadMe.md @@ -75,6 +75,7 @@ rtt min/avg/max/mdev = 5.954/6.133/6.313/0.195 ms 从对比看出,SmartDNS 找到了访问 www.baidu.com 最快的 IP 地址,比阿里 DNS 速度快了 5 倍。 ## 特性 + 1. **多虚拟DNS服务器** 支持多个虚拟DNS服务器,不同虚拟DNS服务器不同的端口,规则,客户端。 diff --git a/src/dns_conf.c b/src/dns_conf.c index 638c7feae7..481bdf4034 100644 --- a/src/dns_conf.c +++ b/src/dns_conf.c @@ -163,6 +163,7 @@ char dns_save_fail_packet_dir[DNS_MAX_PATH]; char dns_resolv_file[DNS_MAX_PATH]; int dns_no_pidfile; int dns_no_daemon; +int dns_restart_on_crash; size_t dns_socket_buff_size; struct hash_table conf_file_table; @@ -696,6 +697,10 @@ static int _config_current_group_pop_all(void) _config_current_group_pop(); } + if (dns_conf_default_group_info == NULL) { + return 0; + } + list_del(&dns_conf_default_group_info->list); free(dns_conf_default_group_info); dns_conf_default_group_info = NULL; @@ -5792,6 +5797,7 @@ static struct config_item _config_item[] = { CONF_YESNO("debug-save-fail-packet", &dns_save_fail_packet), CONF_YESNO("no-pidfile", &dns_no_pidfile), CONF_YESNO("no-daemon", &dns_no_daemon), + CONF_YESNO("restart-on-crash", &dns_restart_on_crash), CONF_SIZE("socket-buff-size", &dns_socket_buff_size, 0, 1024 * 1024 * 8), CONF_CUSTOM("plugin", _config_plugin, NULL), CONF_STRING("resolv-file", (char *)&dns_resolv_file, sizeof(dns_resolv_file)), @@ -5978,7 +5984,10 @@ static int _dns_server_load_conf_init(void) hash_init(dns_conf_srv_record_table.srv); hash_init(dns_conf_plugin_table.plugins); - _config_current_group_push_default(); + if (_config_current_group_push_default() != 0) { + tlog(TLOG_ERROR, "init default group failed."); + return -1; + } return 0; } diff --git a/src/dns_conf.h b/src/dns_conf.h index 463b7720de..7b8355ed66 100644 --- a/src/dns_conf.h +++ b/src/dns_conf.h @@ -697,6 +697,7 @@ extern char dns_resolv_file[DNS_MAX_PATH]; extern int dns_no_pidfile; extern int dns_no_daemon; +extern int dns_restart_on_crash; extern size_t dns_socket_buff_size; void dns_server_load_exit(void); diff --git a/src/dns_server.c b/src/dns_server.c index 13a46d5df0..b53b3d4483 100644 --- a/src/dns_server.c +++ b/src/dns_server.c @@ -6604,7 +6604,7 @@ static int _dns_server_parser_request(struct dns_request *request, struct dns_pa case DNS_OPT_T_TCP_KEEPALIVE: { unsigned short idle_timeout = 0; ret = dns_get_OPT_TCP_KEEPALIVE(rrs, &idle_timeout); - if (idle_timeout == 0) { + if (idle_timeout == 0 || ret != 0) { continue; } diff --git a/src/http_parse.c b/src/http_parse.c index 88a491e9c6..9f9a8ceae5 100644 --- a/src/http_parse.c +++ b/src/http_parse.c @@ -328,6 +328,10 @@ static int _http_head_parse_params(struct http_head *http_head, char *url, int u char *field = NULL; char *value = NULL; + if (url == NULL) { + return -1; + } + param_start = strstr(url, "?"); if (param_start == NULL) { return 0; diff --git a/src/smartdns.c b/src/smartdns.c index 9a973f26ba..494358cc2d 100644 --- a/src/smartdns.c +++ b/src/smartdns.c @@ -987,6 +987,17 @@ void smartdns_restart(void) exit_restart = 1; } +static int smartdns_enter_monitor_mode(int argc, char *argv[], int no_deamon) +{ + setenv("SMARTDNS_RESTART_ON_CRASH", "1", 1); + if (no_deamon == 1) { + setenv("SMARTDNS_NO_DAEMON", "1", 1); + } + execv(argv[0], argv); + tlog(TLOG_ERROR, "execv failed, %s", strerror(errno)); + return -1; +} + #ifdef TEST static smartdns_post_func _smartdns_post = NULL; @@ -1096,6 +1107,16 @@ int main(int argc, char *argv[]) } } + if (getenv("SMARTDNS_RESTART_ON_CRASH") != NULL) { + restart_when_crash = 1; + unsetenv("SMARTDNS_RESTART_ON_CRASH"); + } + + if (getenv("SMARTDNS_NO_DAEMON") != NULL) { + is_run_as_daemon = 0; + unsetenv("SMARTDNS_NO_DAEMON"); + } + smartdns_run_monitor_ret init_ret = _smartdns_run_monitor(restart_when_crash, is_run_as_daemon); if (init_ret != SMARTDNS_RUN_MONITOR_OK) { if (init_ret == SMARTDNS_RUN_MONITOR_EXIT) { @@ -1115,6 +1136,10 @@ int main(int argc, char *argv[]) goto errout; } + if (dns_restart_on_crash && restart_when_crash == 0) { + return smartdns_enter_monitor_mode(argc, argv, dns_no_daemon || !is_run_as_daemon); + } + if (dns_no_daemon || restart_when_crash) { is_run_as_daemon = 0; } diff --git a/src/tlog.c b/src/tlog.c index b77cc439d1..2f008852d4 100644 --- a/src/tlog.c +++ b/src/tlog.c @@ -1574,8 +1574,11 @@ static void _tlog_work_write(struct tlog_log *log, int log_len, int log_extlen, msg += sizeof(struct tlog_segment_log_head); log_head->info.level = TLOG_WARN; } + int len = snprintf(msg, msg - dropmsg, "[Total Dropped %d Messages]\n", log_dropped); - log_head->len = len; + if (log_head) { + log_head->len = len; + } _tlog_write_output_func(log, dropmsg, strnlen(dropmsg, sizeof(dropmsg))); } }