diff --git a/ReadMe.md b/ReadMe.md index d04cbc5..397885f 100755 --- a/ReadMe.md +++ b/ReadMe.md @@ -300,6 +300,7 @@ vi /opt/etc/smartdns/smartdns.conf |log-file|日志文件路径|/var/log/smartdns.log|路径|log-file /var/log/smartdns.log |log-size|日志大小|128K|数字+K,M,G|log-size 128K |log-num|日志归档个数|2|数字|log-num 2 +|conf-file|附加日志文件|无|文件路径|conf-file /etc/smartdns/smartdns.more.conf |server|上游UDP DNS|114.114.114.114|[ip][:port],可重复| server 8.8.8.8:53 |server-tcp|上游TCP DNS|无|[IP][:port],可重复| server-tcp 8.8.8.8:53 |address|指定域名IP地址|无|address /domain/ip| address /www.example.com/1.2.3.4 diff --git a/package/luci/files/luci/controller/smartdns.lua b/package/luci/files/luci/controller/smartdns.lua index 281fdcc..c69b422 100644 --- a/package/luci/files/luci/controller/smartdns.lua +++ b/package/luci/files/luci/controller/smartdns.lua @@ -27,8 +27,8 @@ function act_status() e.local_port = smartdns.get_config_option("smartdns", "smartdns", "port", nil); if e.redirect == "1" then if e.local_port ~= nil and e.local_port ~= "53" then - e.ipv4_works = luci.sys.call("iptables -t nat -nL PREROUTING | grep REDIRECT | grep dpt:53 | grep %q >/dev/null 2>&1" % e.local_port) == 0 - e.ipv6_works = luci.sys.call("ip6tables -t nat -nL PREROUTING | grep REDIRECT | grep dpt:53 | grep %q >/dev/null 2>&1" % e.local_port) == 0 + e.ipv4_works = luci.sys.call("iptables -t nat -nL PREROUTING 2>/dev/null | grep REDIRECT | grep dpt:53 | grep %q >/dev/null 2>&1" % e.local_port) == 0 + e.ipv6_works = luci.sys.call("ip6tables -t nat -nL PREROUTING 2>/dev/null| grep REDIRECT | grep dpt:53 | grep %q >/dev/null 2>&1" % e.local_port) == 0 else e.redirect = 0 end diff --git a/package/luci/files/luci/i18n/smartdns.zh-cn.po b/package/luci/files/luci/i18n/smartdns.zh-cn.po index 4eaee73..c894668 100644 --- a/package/luci/files/luci/i18n/smartdns.zh-cn.po +++ b/package/luci/files/luci/i18n/smartdns.zh-cn.po @@ -13,6 +13,9 @@ msgstr "SmartDNS 服务器" msgid "SmartDNS is a local high-performance DNS server, supports finding fastest IP, supports ad filtering, and supports avoiding DNS poisoning." msgstr "SmartDNS是一个本地高性能DNS服务器,支持避免域名污染,支持返回最快IP,支持广告过滤。" +msgid "Custom Settings" +msgstr "自定义设置" + msgid "Server Name" msgstr "服务器名称" @@ -67,6 +70,9 @@ msgstr "域名TTL最大值" msgid "Maximum TTL for all domain result." msgstr "设置所有域名的TTL最大值" +msgid "smartdns custom settings" +msgstr "smartdns 自定义设置,具体配置参数参考指导" + msgid "Upstream Servers" msgstr "上游服务器" diff --git a/package/luci/files/luci/model/cbi/smartdns.lua b/package/luci/files/luci/model/cbi/smartdns.lua index 5ddb9be..75db311 100644 --- a/package/luci/files/luci/model/cbi/smartdns.lua +++ b/package/luci/files/luci/model/cbi/smartdns.lua @@ -15,25 +15,28 @@ m:section(SimpleSection).template = "smartdns/smartdns_status" s = m:section(TypedSection, "smartdns", translate("Settings"), translate("General Settings")) s.anonymous = true +s:tab("settings", translate("General Settings")) +s:tab("custom", translate("Custom Settings")) + ---- Eanble -o = s:option(Flag, "enabled", translate("Enable"), translate("Enable or disable smartdns server")) +o = s:taboption("settings", Flag, "enabled", translate("Enable"), translate("Enable or disable smartdns server")) o.default = o.disabled o.rempty = false ---- server name -o = s:option(Value, "server_name", translate("Server Name"), translate("Smartdns server name")) +o = s:taboption("settings", Value, "server_name", translate("Server Name"), translate("Smartdns server name")) o.default = "smartdns" o.datatype = "hostname" o.rempty = false ---- Port -o = s:option(Value, "port", translate("Local Port"), translate("Smartdns local server port")) +o = s:taboption("settings", Value, "port", translate("Local Port"), translate("Smartdns local server port")) o.placeholder = 5353 o.default = 5353 o.datatype = "port" o.rempty = false -o = s:option(Flag, "redirect", translate("Redirect"), translate("Redirect standard dns query from 53 to smartdns, as default DNS server")) +o = s:taboption("settings", Flag, "redirect", translate("Redirect"), translate("Redirect standard dns query from 53 to smartdns, as default DNS server")) o.rmempty = false o.default = o.enabled o.cfgvalue = function(...) @@ -41,21 +44,40 @@ o.cfgvalue = function(...) end ---- cache-size -o = s:option(Value, "cache_size", translate("Cache Size"), translate("DNS domain result cache size")) +o = s:taboption("settings", Value, "cache_size", translate("Cache Size"), translate("DNS domain result cache size")) o.rempty = true ---- rr-ttl -o = s:option(Value, "rr_ttl", translate("Domain TTL"), translate("TTL for all domain result.")) +o = s:taboption("settings", Value, "rr_ttl", translate("Domain TTL"), translate("TTL for all domain result.")) o.rempty = true ---- rr-ttl-min -o = s:option(Value, "rr_ttl_min", translate("Domain TTL Min"), translate("Minimum TTL for all domain result.")) +o = s:taboption("settings", Value, "rr_ttl_min", translate("Domain TTL Min"), translate("Minimum TTL for all domain result.")) o.rempty = true ---- rr-ttl-max -o = s:option(Value, "rr_ttl_max", translate("Domain TTL Max"), translate("Maximum TTL for all domain result.")) +o = s:taboption("settings", Value, "rr_ttl_max", translate("Domain TTL Max"), translate("Maximum TTL for all domain result.")) o.rempty = true + +----- custom settings +custom = s:taboption("custom", Value, "Custom Settings", + translate(""), + translate("smartdns custom settings")) + +custom.template = "cbi/tvalue" +custom.rows = 20 + +function custom.cfgvalue(self, section) + return nixio.fs.readfile("/etc/smartdns/custom.conf") +end + +function custom.write(self, section, value) + value = value:gsub("\r\n?", "\n") + nixio.fs.writefile("/etc/smartdns/custom.conf", value) +end + + -- Upstream servers s = m:section(TypedSection, "server", translate("Upstream Servers"), translate("Upstream Servers, support UDP, TCP protocol. " .. "Please configure multiple DNS servers, including multiple foreign DNS servers.")) diff --git a/package/openwrt/control/conffiles b/package/openwrt/control/conffiles index b8dc0cf..0fa2363 100644 --- a/package/openwrt/control/conffiles +++ b/package/openwrt/control/conffiles @@ -1 +1,3 @@ /etc/config/smartdns +/etc/smartdns/address.conf +/etc/smartdns/custom.conf diff --git a/package/openwrt/custom.conf b/package/openwrt/custom.conf new file mode 100644 index 0000000..fd552bc --- /dev/null +++ b/package/openwrt/custom.conf @@ -0,0 +1,11 @@ +# Add custom settings here. + +# set log level +# log-level [level], level=error, warn, info, debug +# log-level error + +# log-size k,m,g +# log-size 128k + +# log-file /var/log/smartdns.log +# log-num 2 \ No newline at end of file diff --git a/package/openwrt/files/etc/init.d/smartdns b/package/openwrt/files/etc/init.d/smartdns index 4eca30f..9f82bb7 100644 --- a/package/openwrt/files/etc/init.d/smartdns +++ b/package/openwrt/files/etc/init.d/smartdns @@ -8,6 +8,7 @@ SERVICE_DAEMONIZE=1 BASECONFIGFILE="/etc/smartdns/smartdns.conf" SMARTDNS_CONF="/var/etc/smartdns.conf" ADDRESS_CONF="/etc/smartdns/address.conf" +CUSTOM_CONF="/etc/smartdns/custom.conf" SMARTDNS_CONF_TMP="${SMARTDNS_CONF}.tmp" set_iptable() @@ -141,8 +142,8 @@ start_service() { config_foreach load_server "server" - echo "# address" >> $SMARTDNS_CONF_TMP - grep "^ *address " $ADDRESS_CONF >> $SMARTDNS_CONF_TMP + echo "conf-file $ADDRESS_CONF" >> $SMARTDNS_CONF_TMP + echo "conf-file $CUSTOM_CONF" >> $SMARTDNS_CONF_TMP config_get_bool "enabled" "$section" "enabled" '0' mv $SMARTDNS_CONF_TMP $SMARTDNS_CONF diff --git a/package/openwrt/make.sh b/package/openwrt/make.sh index 40a0353..485a69f 100644 --- a/package/openwrt/make.sh +++ b/package/openwrt/make.sh @@ -7,6 +7,7 @@ SMARTDNS_DIR=$CURR_DIR/../../ SMARTDNS_BIN=$SMARTDNS_DIR/src/smartdns SMARTDNS_CONF=$SMARTDNS_DIR/etc/smartdns/smartdns.conf ADDRESS_CONF=$CURR_DIR/address.conf +CUSTOM_CONF=$CURR_DIR/custom.conf showhelp() { @@ -32,6 +33,7 @@ build() cp $SMARTDNS_CONF $ROOT/root/etc/smartdns/ cp $ADDRESS_CONF $ROOT/root/etc/smartdns/ + cp $CUSTOM_CONF $ROOT/root/etc/smartdns/ cp $CURR_DIR/files/etc $ROOT/root/ -af cp $SMARTDNS_BIN $ROOT/root/usr/sbin diff --git a/src/conf.c b/src/conf.c index e206d36..9cd900d 100644 --- a/src/conf.c +++ b/src/conf.c @@ -28,6 +28,8 @@ int dns_conf_rr_ttl; int dns_conf_rr_ttl_min; int dns_conf_rr_ttl_max; +int load_conf_file(const char *file); + int config_bind(char *value) { /* server bind address */ @@ -116,7 +118,12 @@ int config_address(char *value) memset(address, 0, sizeof(*address)); len = end - begin; - memcpy(domain, begin, len); + memcpy(domain + 1, begin, len); + + /* add dot for subdomain */ + domain[0] = '.'; + len++; + domain[len] = 0; reverse_string(domain_key + 1, domain, len); @@ -209,6 +216,8 @@ int config_log_level(char *value) dns_conf_log_level = TLOG_WARN; } else if (strncmp("error", value, MAX_LINE_LEN) == 0) { dns_conf_log_level = TLOG_ERROR; + } else { + return -1; } return 0; @@ -297,6 +306,18 @@ int config_rr_ttl_max(char *value) return 0; } +int config_addtional_file(char *value) +{ + char *file_path = value; + + if (access(file_path, R_OK) != 0) { + tlog(TLOG_WARN, "conf file %s is not readable.", file_path); + return 0; + } + + return load_conf_file(file_path); +} + struct config_item { const char *item; @@ -318,6 +339,7 @@ struct config_item config_item[] = { {"rr-ttl", config_rr_ttl}, {"rr-ttl-min", config_rr_ttl_min}, {"rr-ttl-max", config_rr_ttl_max}, + {"conf-file", config_addtional_file}, }; int config_item_num = sizeof(config_item) / sizeof(struct config_item); @@ -333,7 +355,7 @@ void load_exit(void) config_address_destroy(); } -int load_conf(const char *file) +int load_conf_file(const char *file) { FILE *fp = NULL; char line[MAX_LINE_LEN]; @@ -343,8 +365,6 @@ int load_conf(const char *file) int line_num = 0; int i; - load_conf_init(); - fp = fopen(file, "r"); if (fp == NULL) { tlog(TLOG_ERROR, "config file %s not exist.", file); @@ -386,9 +406,17 @@ int load_conf(const char *file) return 0; errout: - printf("invalid config at line %d: %s", line_num, line); + printf("invalid config at file %s:%d, %s", file, line_num, line); + tlog(TLOG_ERROR, "invalid config at file %s:%d, %s", file, line_num, line); if (fp) { fclose(fp); } return -1; } + +int load_conf(const char *file) +{ + load_conf_init(); + + return load_conf_file(file); +} diff --git a/src/dns_client.c b/src/dns_client.c index ece404b..9ccc9eb 100644 --- a/src/dns_client.c +++ b/src/dns_client.c @@ -870,7 +870,7 @@ static void *_dns_client_work(void *arg) unsigned long now = {0}; unsigned int sleep = 100; int sleep_time; - unsigned int expect_time = 0; + unsigned long expect_time = 0; sleep_time = sleep; now = get_tick_count() - sleep; diff --git a/src/dns_server.c b/src/dns_server.c index 90274ad..a1b0ac6 100644 --- a/src/dns_server.c +++ b/src/dns_server.c @@ -773,6 +773,9 @@ static struct dns_address *_dns_server_get_address_by_domain(char *domain, int q reverse_string(domain_key + 1, domain, domain_len); domain_key[0] = type; domain_len++; + domain_key[domain_len] = '.'; + domain_len++; + domain_key[domain_len] = 0; if (likely(dns_conf_log_level > TLOG_INFO)) { return art_substring(&dns_conf_address, (unsigned char *)domain_key, domain_len, NULL, NULL); diff --git a/src/smartdns.c b/src/smartdns.c index 6c2f39b..4f3ece5 100644 --- a/src/smartdns.c +++ b/src/smartdns.c @@ -182,11 +182,11 @@ int smartdns_init(void) char logname[DNS_MAX_PATH]; if (dns_conf_log_file[0] != 0) { - strncpy(logdir, dns_conf_log_file, DNS_MAX_PATH); - strncpy(logname, dns_conf_log_file, DNS_MAX_PATH); - - dirname(logdir); - basename(logname); + char dns_log_file[DNS_MAX_PATH]; + strncpy(dns_log_file, dns_conf_log_file, DNS_MAX_PATH); + strncpy(logdir, dirname(dns_log_file), DNS_MAX_PATH); + strncpy(dns_log_file, dns_conf_log_file, DNS_MAX_PATH); + strncpy(logname, basename(dns_log_file), DNS_MAX_PATH); } else { strncpy(logdir, SMARTDNS_LOG_PATH, DNS_MAX_PATH); strncpy(logname, SMARTDNS_LOG_FILE, DNS_MAX_PATH);