From 1f40577ef0d67308ea42f7150990ae611c12d4d4 Mon Sep 17 00:00:00 2001 From: Nick Peng Date: Mon, 16 Jan 2023 20:29:56 +0800 Subject: [PATCH] openwrt: save cache to disk when reboot system --- .../files/luci/i18n/smartdns.zh-cn.po | 6 ++++++ .../files/luci/model/cbi/smartdns/smartdns.lua | 8 ++++++++ package/luci/files/luci/i18n/smartdns.zh-cn.po | 6 ++++++ .../resources/view/smartdns/smartdns.js | 5 +++++ package/openwrt/Makefile | 3 +++ package/openwrt/control/conffiles | 2 ++ package/openwrt/control/prerm | 2 ++ package/openwrt/files/etc/init.d/smartdns | 14 ++++++++++++++ src/dns_server.c | 4 ++-- src/smartdns.c | 17 +++++++++++++---- 10 files changed, 61 insertions(+), 6 deletions(-) diff --git a/package/luci-compat/files/luci/i18n/smartdns.zh-cn.po b/package/luci-compat/files/luci/i18n/smartdns.zh-cn.po index 22ec41d..7d8ce77 100644 --- a/package/luci-compat/files/luci/i18n/smartdns.zh-cn.po +++ b/package/luci-compat/files/luci/i18n/smartdns.zh-cn.po @@ -31,6 +31,9 @@ msgstr "绑定到设备" msgid "Bind Device Name" msgstr "绑定的设备名称" +msgid "Cache Persist" +msgstr "持久化缓存" + msgid "Cache Size" msgstr "缓存大小" @@ -502,6 +505,9 @@ msgstr "" "用于校验 TLS 服务器的有效性,数值为 Base64 编码的 SPKI 指纹,留空表示不验证 " "TLS 的合法性。" +msgid "Write cache to disk on exit and load on startup." +msgstr "退出时保存cache到磁盘,启动时加载。" + msgid "domain list (/etc/smartdns/domain-set)" msgstr "域名列表(/etc/smartdns/domain-set)" diff --git a/package/luci-compat/files/luci/model/cbi/smartdns/smartdns.lua b/package/luci-compat/files/luci/model/cbi/smartdns/smartdns.lua index 24c7ee1..90795a9 100644 --- a/package/luci-compat/files/luci/model/cbi/smartdns/smartdns.lua +++ b/package/luci-compat/files/luci/model/cbi/smartdns/smartdns.lua @@ -161,6 +161,14 @@ end o = s:taboption("advanced", Value, "cache_size", translate("Cache Size"), translate("DNS domain result cache size")) o.rempty = true +---- cache-persist; +o = s:taboption("advanced", Flag, "cache_persist", translate("Cache Persist"), translate("Write cache to disk on exit and load on startup.")) +o.rmempty = false; +o.default = o.enabled; +o.cfgvalue = function(...) + return Flag.cfgvalue(...) or "1" +end + -- cache-size o = s:taboption("advanced", Flag, "resolve_local_hostnames", translate("Resolve Local Hostnames"), translate("Resolve local hostnames by reading Dnsmasq lease file.")) o.rmempty = false diff --git a/package/luci/files/luci/i18n/smartdns.zh-cn.po b/package/luci/files/luci/i18n/smartdns.zh-cn.po index 0c51a14..92f46b6 100644 --- a/package/luci/files/luci/i18n/smartdns.zh-cn.po +++ b/package/luci/files/luci/i18n/smartdns.zh-cn.po @@ -37,6 +37,9 @@ msgstr "屏蔽域名" msgid "Block domain." msgstr "屏蔽域名。" +msgid "Cache Persist" +msgstr "持久化缓存" + msgid "Cache Size" msgstr "缓存大小" @@ -537,6 +540,9 @@ msgstr "" "用于校验 TLS 服务器的有效性,数值为 Base64 编码的 SPKI 指纹,留空表示不验证 " "TLS 的合法性。" +msgid "Write cache to disk on exit and load on startup." +msgstr "退出时保存cache到磁盘,启动时加载。" + msgid "domain list (/etc/smartdns/domain-set)" msgstr "域名列表(/etc/smartdns/domain-set)" diff --git a/package/luci/files/root/www/luci-static/resources/view/smartdns/smartdns.js b/package/luci/files/root/www/luci-static/resources/view/smartdns/smartdns.js index b5edc8c..11b34c9 100644 --- a/package/luci/files/root/www/luci-static/resources/view/smartdns/smartdns.js +++ b/package/luci/files/root/www/luci-static/resources/view/smartdns/smartdns.js @@ -242,6 +242,11 @@ return view.extend({ o = s.taboption("advanced", form.Value, "cache_size", _("Cache Size"), _("DNS domain result cache size")); o.rempty = true; + // cache-persist; + o = s.taboption("advanced", form.Flag, "cache_persist", _("Cache Persist"), _("Write cache to disk on exit and load on startup.")); + o.rmempty = false; + o.default = o.enabled; + // cache-size; o = s.taboption("advanced", form.Flag, "resolve_local_hostnames", _("Resolve Local Hostnames"), _("Resolve local hostnames by reading Dnsmasq lease file.")); o.rmempty = false; diff --git a/package/openwrt/Makefile b/package/openwrt/Makefile index 60b1681..aa65d23 100644 --- a/package/openwrt/Makefile +++ b/package/openwrt/Makefile @@ -47,10 +47,13 @@ define Package/smartdns/conffiles /etc/smartdns/address.conf /etc/smartdns/blacklist-ip.conf /etc/smartdns/custom.conf +/etc/smartdns/domain-block.list +/etc/smartdns/domain-forwarding.list endef define Package/smartdns/install $(INSTALL_DIR) $(1)/usr/sbin $(1)/etc/config $(1)/etc/init.d $(1)/etc/smartdns + $(INSTALL_DIR) $(1)/etc/smartdns/conf.d $(1)/etc/smartdns/domain-set $(INSTALL_BIN) $(PKG_BUILD_DIR)/src/smartdns $(1)/usr/sbin/smartdns $(INSTALL_BIN) $(PKG_BUILD_DIR)/package/openwrt/files/etc/init.d/smartdns $(1)/etc/init.d/smartdns $(INSTALL_CONF) $(PKG_BUILD_DIR)/package/openwrt/address.conf $(1)/etc/smartdns/address.conf diff --git a/package/openwrt/control/conffiles b/package/openwrt/control/conffiles index 748e075..ddb991b 100644 --- a/package/openwrt/control/conffiles +++ b/package/openwrt/control/conffiles @@ -2,3 +2,5 @@ /etc/smartdns/address.conf /etc/smartdns/blacklist-ip.conf /etc/smartdns/custom.conf +/etc/smartdns/domain-block.list +/etc/smartdns/domain-forwarding.list diff --git a/package/openwrt/control/prerm b/package/openwrt/control/prerm index b615728..c9f1b31 100644 --- a/package/openwrt/control/prerm +++ b/package/openwrt/control/prerm @@ -19,4 +19,6 @@ default_prerm $0 $@ rm /var/etc/smartdns.conf -f rm /var/etc/smartdns/smartdns.conf -f +rm /var/log/smartdns/ -fr +rm /etc/smartdns/smartdns.cache -f exit 0 diff --git a/package/openwrt/files/etc/init.d/smartdns b/package/openwrt/files/etc/init.d/smartdns index f5062f3..18e0230 100644 --- a/package/openwrt/files/etc/init.d/smartdns +++ b/package/openwrt/files/etc/init.d/smartdns @@ -16,6 +16,7 @@ # along with this program. If not, see . START=19 +STOP=82 NAME=smartdns USE_PROCD=1 SERVICE_USE_PID=1 @@ -536,6 +537,19 @@ load_service() config_get bind_device_name "$section" "bind_device_name" "${lan_device}" [ ! -z "$bind_device_name" ] && [ "$bind_device" = "1" ] && device="${bind_device_name}" + config_get cache_file "$section" "cache_file" "$SMARTDNS_CONF_DIR/smartdns.cache" + + config_get_bool cache_persist "$section" "cache_persist" "0" + [ "$cache_persist" = "1" ] && { + conf_append "cache-persist" "yes" + conf_append "cache-file" "$cache_file" + } + + [ "$cache_persist" = "0" ] && { + conf_append "cache-persist" "no" + [ -f "$cache_file" ] && rm -f "$cache_file" + } + config_get proxy_server "$section" "proxy_server" "" [ -z "$proxy_server" ] || conf_append "proxy-server" "$proxy_server -name default-proxy" diff --git a/src/dns_server.c b/src/dns_server.c index fcde3b2..67e9add 100644 --- a/src/dns_server.c +++ b/src/dns_server.c @@ -4811,7 +4811,7 @@ static int _dns_server_tcp_process_one_request(struct dns_server_conn_tcp_client request_len = ntohs(*((unsigned short *)(request_data))); if (request_len >= sizeof(tcpclient->recvbuff.buf)) { - tlog(TLOG_ERROR, "request length is invalid."); + tlog(TLOG_DEBUG, "request length is invalid."); return RECV_ERROR_FAIL; } @@ -4864,7 +4864,7 @@ static int _dns_server_tcp_process_requests(struct dns_server_conn_tcp_client *t request_ret = _dns_server_tcp_process_one_request(tcpclient); if (request_ret < 0) { /* failed */ - tlog(TLOG_ERROR, "process one request failed."); + tlog(TLOG_DEBUG, "process one request failed."); return RECV_ERROR_FAIL; } diff --git a/src/smartdns.c b/src/smartdns.c index 95e6182..ade94e6 100644 --- a/src/smartdns.c +++ b/src/smartdns.c @@ -380,6 +380,7 @@ static int _smartdns_init(void) { int ret = 0; const char *logfile = _smartdns_log_path(); + int i = 0; ret = tlog_init(logfile, dns_conf_log_size, dns_conf_log_num, 0, 0); if (ret != 0) { @@ -401,11 +402,19 @@ static int _smartdns_init(void) goto errout; } - if (dns_conf_server_num <= 0) { - if (_smartdns_load_from_resolv() != 0) { - tlog(TLOG_ERROR, "load dns from resolv failed."); - goto errout; + for (i = 0; i < 60 && dns_conf_server_num <= 0; i++) { + ret = _smartdns_load_from_resolv(); + if (ret == 0) { + continue; } + + tlog(TLOG_DEBUG, "load dns from resolv failed, retry after 1s, retry times %d.", i + 1); + sleep(1); + } + + if (dns_conf_server_num <= 0) { + tlog(TLOG_ERROR, "no dns server found, exit..."); + goto errout; } ret = fast_ping_init();