Преглед изворни кода

support set smartdns as dnsmasq upstream server

Nick Peng пре 7 година
родитељ
комит
ad6741e4da

+ 26 - 20
ReadMe.md

@@ -181,16 +181,16 @@ Download the matching version of the SmartDNS installation package. The correspo
 
 1. Check if the service is configured successfully
 
-    Query domain name with `nslookup -querytype=ptr 127.0.0.1`  
+    Query domain name with `nslookup -querytype=ptr 0.0.0.0`  
     Check if the `name` item in the command result is displayed as `smartdns` or `hostname`, such as `smartdns`
 
     ```shell
-    pi@raspberrypi:~/code/smartdns_build $ nslookup -querytype=ptr 127.0.0.1
+    pi@raspberrypi:~/code/smartdns_build $ nslookup -querytype=ptr 0.0.0.0
     Server:         192.168.1.1
     Address:        192.168.1.1#53
 
     Non-authoritative answer:
-    1.0.0.127.in-addr.arpa  name = smartdns.
+    0.0.0.0.in-addr.arpa  name = smartdns.
     ```
 
 ### openwrt/LEDE
@@ -221,20 +221,20 @@ Download the matching version of the SmartDNS installation package. The correspo
 
     * **Enable SmartDNS port 53 port redirection**
 
-        Log in to the router, click on `Services`->`SmartDNS`, check the `Redirect` option to enable port 53 forwarding.
+        Log in to the router, click on `Services`->`SmartDNS`->`redirect`, select `Redirect 53 port to SmartDNS` option to enable port 53 forwarding.
 
     * **Check if the service is configured successfully**
 
-        Query domain name with `nslookup -querytype=ptr 127.0.0.1`
+        Query domain name with `nslookup -querytype=ptr 0.0.0.0`
         See if the `name` item in the command result is displayed as `smartdns` or `hostname`, such as `smartdns`
 
         ```shell
-        pi@raspberrypi:~/code/smartdns_build $ nslookup -querytype=ptr 127.0.0.1
+        pi@raspberrypi:~/code/smartdns_build $ nslookup -querytype=ptr 0.0.0.0
         Server:         192.168.1.1
         Address:        192.168.1.1#53
 
         Non-authoritative answer:
-        1.0.0.127.in-addr.arpa  name = smartdns.
+        0.0.0.0.in-addr.arpa  name = smartdns.
         ```
 
     * **The interface prompts that the redirect failed**
@@ -251,23 +251,25 @@ Download the matching version of the SmartDNS installation package. The correspo
 
 1. Method 2: SmartDNS as upstream DNS Server of DNSMASQ
 
-    * **Disable SmartDNS port 53 port redirection**
+    * **Forward dnsmasq's request to SmartDNS**
 
-        Log in to the router, click on `Services`->`SmartDNS`, uncheck the `Redirect` option to disable port 53 forwarding.
+        Log in to the router, click on `Services`->`SmartDNS`->`redirect`, select `Run as dnsmasq upstream server` option to forwarding dnsmasq request to Smartdns.
 
-    * **Forward dnsmasq's request to SmartDNS**
+    * **Check if the service is configured successfully**
 
-        Log in to the router, click `Network`->`DHCP and DNS`, and modify `DNS forwardings` to:
+        * Method 1: Query domain name with `nslookup -querytype=ptr 0.0.0.1`
+        See if the `name` item in the command result is displayed as `smartdns` or `hostname`, such as `smartdns`
 
         ```shell
-        /#/127.0.0.1#5053
-        ```
-
-        Where `#5053` is the service port number of smartdns. If it is not modified, the default is 5053.
+        pi@raspberrypi:~/code/smartdns_build $ nslookup -querytype=ptr 0.0.0.0
+        Server:         192.168.1.1
+        Address:        192.168.1.1#53
 
-    * **Check if the service is configured successfully**
+        Non-authoritative answer:
+        0.0.0.0.in-addr.arpa  name = smartdns.
+        ```
 
-        Use `nslookup` to query the `www.baidu.com` domain name to see if the IP address of Baidu in the result is `only one. If there are multiple IP addresses returned, it means that it is not valid. Please try to check several domain names.
+        * Method 2: Use `nslookup` to query the `www.baidu.com` domain name to see if the IP address of Baidu in the result is `only one. If there are multiple IP addresses returned, it means that it is not valid. Please try to check several domain names.
 
         ```shell
         pi@raspberrypi:~ $ nslookup www.baidu.com 192.168.1.1
@@ -319,16 +321,16 @@ Note: Merlin firmware is derived from ASUS firmware and can theoretically be use
 
 1. Restart router
 
-    After the router is started, use `nslookup -querytype=ptr 127.0.0.1` to query the domain name.  
+    After the router is started, use `nslookup -querytype=ptr 0.0.0.0` to query the domain name.  
     See if the `name` item in the command result is displayed as `smartdns` or `hostname`, such as `smartdns`
 
     ```shell
-    pi@raspberrypi:~/code/smartdns_build $ nslookup -querytype=ptr 127.0.0.1
+    pi@raspberrypi:~/code/smartdns_build $ nslookup -querytype=ptr 0.0.0.0
     Server:         192.168.1.1
     Address:        192.168.1.1#53
 
     Non-authoritative answer:
-    1.0.0.127.in-addr.arpa  name = smartdns.
+    0.0.0.0.in-addr.arpa  name = smartdns.
     ```
 
 1. Note
@@ -381,6 +383,10 @@ Note: Merlin firmware is derived from ASUS firmware and can theoretically be use
 |log-file|log path|/var/log/smartdns.log|File Pah|log-file /var/log/smartdns.log
 |log-size|log size|128K|number+K,M,G|log-size 128K
 |log-num|archived log number|2|Integer|log-num 2
+|audit-enable|audit log enable|no|[yes\|no]|audit-enable yes
+|audit-file|audit log file|/var/log/smartdns-audit.log|File Path|audit-file /var/log/smartdns-audit.log
+|audit-size|audit log size|128K|number+K,M,G|audit-size 128K
+|audit-num|archived audit log number|2|Integer|audit-num 2
 |conf-file|additional conf file|None|File path|conf-file /etc/smartdns/smartdns.more.conf
 |server|Upstream UDP DNS server|None|[ip][:port], Repeatable| server 8.8.8.8:53
 |server-tcp|Upstream TCP DNS server|None|[IP][:port], Repeatable| server-tcp 8.8.8.8:53

+ 26 - 20
ReadMe_zh-CN.md

@@ -181,16 +181,16 @@ rtt min/avg/max/mdev = 5.954/6.133/6.313/0.195 ms
 
 1. 检测服务是否配置成功。
 
-    使用`nslookup -querytype=ptr 127.0.0.1`查询域名  
+    使用`nslookup -querytype=ptr 0.0.0.0`查询域名  
     看命令结果中的`name`项目是否显示为`smartdns`或`主机名`,如`smartdns`则表示生效  
 
     ```shell
-    pi@raspberrypi:~/code/smartdns_build $ nslookup -querytype=ptr 127.0.0.1
+    pi@raspberrypi:~/code/smartdns_build $ nslookup -querytype=ptr 0.0.0.0
     Server:         192.168.1.1
     Address:        192.168.1.1#53
 
     Non-authoritative answer:
-    1.0.0.127.in-addr.arpa  name = smartdns.
+    0.0.0.0.in-addr.arpa  name = smartdns.
     ```
 
 ### openwrt/LEDE
@@ -221,20 +221,20 @@ rtt min/avg/max/mdev = 5.954/6.133/6.313/0.195 ms
 
     * **启用smartdns的53端口重定向**
 
-        登录路由器,点击`Services`->`SmartDNS`,勾选`Redirect`选项,启用53端口转发。
+        登录路由器,点击`Services`->`SmartDNS`->`redirect`,选择`重定向53端口到SmartDNS`启用53端口转发。
 
     * **检测转发服务是否配置成功**
 
-        使用`nslookup -querytype=ptr 127.0.0.1`查询域名  
+        使用`nslookup -querytype=ptr 0.0.0.0`查询域名  
         看命令结果中的`name`项目是否显示为`smartdns`或`主机名`,如`smartdns`则表示生效  
 
         ```shell
-        pi@raspberrypi:~/code/smartdns_build $ nslookup -querytype=ptr 127.0.0.1
+        pi@raspberrypi:~/code/smartdns_build $ nslookup -querytype=ptr 0.0.0.0
         Server:         192.168.1.1
         Address:        192.168.1.1#53
 
         Non-authoritative answer:
-        1.0.0.127.in-addr.arpa  name = smartdns.
+        0.0.0.0.in-addr.arpa  name = smartdns.
         ```
 
     * **界面提示重定向失败**
@@ -251,23 +251,25 @@ rtt min/avg/max/mdev = 5.954/6.133/6.313/0.195 ms
 
 1. 方法二:作为DNSMASQ的上游
 
-    * **停用smartdns的53端口重定向**
+    * **将dnsmasq的请求发送到smartdns**
 
-        登录路由器,点击`Services`->`SmartDNS`,去勾选`Redirect`选项,停用53端口转发
+        登录路由器,点击`Services`->`SmartDNS`->`redirect`,选择`作为dnsmasq的上游服务器`设置dnsmasq的上游服务器为smartdns
 
-    * **将dnsmasq的请求发送到smartdns**
+    * **检测上游服务是否配置成功**
 
-        登录路由器,点击`Network`->`DHCP and DNS`,修改`DNS forwardings(DNS转发)`为:
+        * 方法一:使用`nslookup -querytype=ptr 0.0.0.0`查询域名  
+        看命令结果中的`name`项目是否显示为`smartdns`或`主机名`,如`smartdns`则表示生效  
 
         ```shell
-        /#/127.0.0.1#5053
-        ```
-
-        其中`#5053`为smartdns的服务端口号,未修改的情况下,默认为5053。
+        pi@raspberrypi:~/code/smartdns_build $ nslookup -querytype=ptr 0.0.0.0
+        Server:         192.168.1.1
+        Address:        192.168.1.1#53
 
-    * **检测上游服务是否配置成功**
+        Non-authoritative answer:
+        0.0.0.0.in-addr.arpa  name = smartdns.
+        ```
 
-        使用`nslookup`查询`www.baidu.com`域名,查看结果中百度的IP地址是否`只有一个`,如有多个IP地址返回,则表示未生效,请多尝试几个域名检查。
+        * 方法二:使用`nslookup`查询`www.baidu.com`域名,查看结果中百度的IP地址是否`只有一个`,如有多个IP地址返回,则表示未生效,请多尝试几个域名检查。
 
         ```shell
         pi@raspberrypi:~ $ nslookup www.baidu.com 192.168.1.1
@@ -319,16 +321,16 @@ rtt min/avg/max/mdev = 5.954/6.133/6.313/0.195 ms
 
 1. 重启路由器生效服务
 
-    待路由器启动后,使用`nslookup -querytype=ptr 127.0.0.1`查询域名  
+    待路由器启动后,使用`nslookup -querytype=ptr 0.0.0.0`查询域名  
     看命令结果中的`name`项目是否显示为`smartdns`或`主机名`,如`smartdns`则表示生效  
 
     ```shell
-    pi@raspberrypi:~/code/smartdns_build $ nslookup -querytype=ptr 127.0.0.1
+    pi@raspberrypi:~/code/smartdns_build $ nslookup -querytype=ptr 0.0.0.0
     Server:         192.168.1.1
     Address:        192.168.1.1#53
 
     Non-authoritative answer:
-    1.0.0.127.in-addr.arpa  name = smartdns.
+    0.0.0.0.in-addr.arpa  name = smartdns.
     ```
 
 1. 额外说明
@@ -381,6 +383,10 @@ rtt min/avg/max/mdev = 5.954/6.133/6.313/0.195 ms
 |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
+|audit-enable|设置审计启用|no|[yes\|no]|audit-enable yes
+|audit-file|审计文件路径|/var/log/smartdns-audit.log|路径|audit-file /var/log/smartdns-audit.log
+|audit-size|审计大小|128K|数字+K,M,G|audit-size 128K
+|audit-num|审计归档个数|2|数字|audit-num 2
 |conf-file|附加配置文件|无|文件路径|conf-file /etc/smartdns/smartdns.more.conf
 |server|上游UDP DNS|无|[ip][:port],可重复| server 8.8.8.8:53
 |server-tcp|上游TCP DNS|无|[IP][:port],可重复| server-tcp 8.8.8.8:53

+ 22 - 2
package/luci/files/luci/controller/smartdns.lua

@@ -22,13 +22,24 @@ end
 function act_status()
 	local e={}
 	local ipv6_server;
+	local redirect_mode="none";
+
 	e.ipv6_works = 2;
 	e.ipv4_works = 2;
 	e.ipv6_server = 1;
-	e.redirect = smartdns.get_config_option("smartdns", "smartdns", "redirect", nil);
+	e.dnsmasq_forward = 0;
+	redirect_mode = smartdns.get_config_option("smartdns", "smartdns", "redirect", nil);
+	if redirect_mode == "redirect" then
+		e.redirect = 1
+	elseif redirect_mode == "dnsmasq-upstream" then
+		e.redirect = 2
+	else
+		e.redirect = 0
+	end
+
 	e.local_port = smartdns.get_config_option("smartdns", "smartdns", "port", nil);
 	ipv6_server = smartdns.get_config_option("smartdns", "smartdns", "ipv6_server", nil);
-	if e.redirect == "1" then 
+	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 2>/dev/null | grep REDIRECT | grep dpt:53 | grep %q >/dev/null 2>&1" % e.local_port) == 0
 			if ipv6_server == "1" then
@@ -39,6 +50,15 @@ function act_status()
 		else
 			e.redirect = 0
 		end
+	elseif e.redirect == 2 then
+		local str;
+		local dnsmasq_server = luci.sys.exec("uci get dhcp.@dnsmasq[0].server")
+		if e.local_port ~= nil then
+			str = "127.0.0.1#" .. e.local_port 
+			if  string.sub(dnsmasq_server,1,string.len(str)) == str then 
+				e.dnsmasq_forward = 1
+			end
+		end
 	end
 	e.running = is_running()
 

+ 11 - 2
package/luci/files/luci/i18n/smartdns.zh-cn.po

@@ -40,6 +40,9 @@ msgstr "IPV4 53端口重定向失败"
 msgid "IPV6 53 Port Redirect Failure"
 msgstr "IPV6 53端口重定向失败"
 
+msgid "Dnsmasq Forwared To Smartdns Failure"
+msgstr "重定向dnsmasq到smartdns失败"
+
 msgid "TCP Server"
 msgstr "TCP服务器"
 
@@ -55,8 +58,14 @@ msgstr "启用IPV6服务器"
 msgid "Redirect"
 msgstr "重定向"
 
-msgid "Redirect standard dns query from 53 to smartdns, as default DNS server"
-msgstr "将53端口的DNS请求重定向到SmartDNS, 替换默认DNS服务"
+msgid "SmartDNS redirect mode"
+msgstr "SmartDNS 重定向模式"
+
+msgid "Run as dnsmasq upstream server"
+msgstr "作为dnsmasq的上游服务器"
+
+msgid "Redirect 53 port to SmartDNS"
+msgstr "重定向53端口到SmartDNS"
 
 msgid "Cache Size"
 msgstr "缓存大小"

+ 10 - 9
package/luci/files/luci/model/cbi/smartdns.lua

@@ -31,8 +31,8 @@ o.rempty      = false
 
 ---- Port
 o = s:taboption("settings", Value, "port", translate("Local Port"), translate("Smartdns local server port"))
-o.placeholder = 5053
-o.default     = 5053
+o.placeholder = 6053
+o.default     = 6053
 o.datatype    = "port"
 o.rempty      = false
 
@@ -52,12 +52,14 @@ o.cfgvalue    = function(...)
     return Flag.cfgvalue(...) or "1"
 end
 
-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(...)
-    return Flag.cfgvalue(...) or "1"
-end
+---- Redirect
+o = s:taboption("settings", ListValue, "redirect", translate("Redirect"), translate("SmartDNS redirect mode"))
+o.placeholder = "none"
+o:value("none", translate("none"))
+o:value("dnsmasq-upstream", translate("Run as dnsmasq upstream server"))
+o:value("redirect", translate("Redirect 53 port to SmartDNS"))
+o.default     = "none"
+o.rempty      = false
 
 ---- cache-size
 o = s:taboption("settings", Value, "cache_size", translate("Cache Size"), translate("DNS domain result cache size"))
@@ -93,7 +95,6 @@ function custom.write(self, section, value)
 	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."))

+ 14 - 8
package/luci/files/luci/view/smartdns/smartdns_status.htm

@@ -6,14 +6,20 @@ XHR.poll(3, '<%=luci.dispatcher.build_url("admin", "services", "smartdns", "stat
 			var links = "";
 			if (data.running) {
 				links = '<b><font color=green>SmartDNS - <%:RUNNING%></font></b></em>';
-				if (data.redirect && data.redirect == 1) {
-					if (data.ipv4_works == 0) {
-						links += "<br></br><b><font color=red><%:IPV4 53 Port Redirect Failure%></font></b>"
-					}
-					
-					if (data.ipv6_works != 2) {
-						if (data.ipv6_works == 0) {
-							links += "<br></br><b><font color=red><%:IPV6 53 Port Redirect Failure%></font></b>"
+				if (data.redirect) {
+					if (data.redirect == 1) {
+						if (data.ipv4_works == 0) {
+							links += "<br></br><b><font color=red><%:IPV4 53 Port Redirect Failure%></font></b>"
+						}
+						
+						if (data.ipv6_works != 2) {
+							if (data.ipv6_works == 0) {
+								links += "<br></br><b><font color=red><%:IPV6 53 Port Redirect Failure%></font></b>"
+							}
+						}
+					} else if (data.redirect == 2) {
+						if (data.dnsmasq_forward == 0) {
+							links += "<br></br><b><font color=red><%:Dnsmasq Forwared To Smartdns Failure%></font></b>"
 						}
 					}
 				}

+ 1 - 0
package/openwrt/control/control

@@ -3,6 +3,7 @@ Architecture:
 Priority: optional
 Section: net
 Version: 
+Depends: libc, libopenssl, libpthread
 Maintainer: pymumu
 Source: http://127.0.0.1/
 Description: A smart dns server

+ 31 - 3
package/openwrt/files/etc/init.d/smartdns

@@ -2,15 +2,39 @@
 # Copyright (C) 2018 Nick Peng ([email protected])
 
 START=99
+NAME=smartdns
 SERVICE_USE_PID=1
 SERVICE_WRITE_PID=1
 SERVICE_DAEMONIZE=1
+SERVICE_PID_FILE="/var/run/smartdns.pid"
 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_forward_dnsmasq()
+{
+	SMARTDNS_PORT="$1"
+	addr="127.0.0.1#$SMARTDNS_PORT"
+	uci add_list dhcp.@dnsmasq[0].server=$addr
+	uci delete dhcp.@dnsmasq[0].resolvfile
+	uci set dhcp.@dnsmasq[0].noresolv=1
+	uci commit dhcp 
+	/etc/init.d/dnsmasq restart
+}
+
+stop_forward_dnsmasq()
+{
+	SMARTDNS_PORT="$1"
+	addr="127.0.0.1#$SMARTDNS_PORT"
+	uci del_list dhcp.@dnsmasq[0].server=$addr 2>/dev/null
+	uci set dhcp.@dnsmasq[0].resolvfile=/tmp/resolv.conf.auto 2>/dev/null
+	uci delete dhcp.@dnsmasq[0].noresolv 2>/dev/null
+	uci commit dhcp
+	/etc/init.d/dnsmasq restart
+}
+
 set_iptable()
 {
 	local ipv6_server=$1
@@ -100,7 +124,7 @@ start_service() {
 		conf_append "server-name" "$server_name"	
 	fi
 
-	config_get "port" "$section" "port" "5053"
+	config_get "port" "$section" "port" "6053"
 	config_get "ipv6_server" "$section" "ipv6_server" "1"
 	config_get "tcp_server" "$section" "tcp_server" "1"
 	if [ "$ipv6_server" = "1" ]; then
@@ -160,10 +184,14 @@ start_service() {
 		conf_append "log-file" "$log_file"
 	fi
 
+	config_get "redirect" "$section" "redirect" "none"
+
 	clear_iptable
-	config_get_bool "redirect" "$section" "redirect" '0'
-	if [ "$redirect" -eq 1 ]; then
+	stop_forward_dnsmasq "$SMARTDNS_PORT"
+	if [ "$redirect" = "redirect" ]; then
 		set_iptable $ipv6_server
+	elif [ "$redirect" = "dnsmasq-upstream" ]; then
+		set_forward_dnsmasq "$SMARTDNS_PORT"
 	fi
 
 	config_foreach load_server "server"

+ 1 - 1
package/openwrt/make.sh

@@ -42,7 +42,7 @@ build()
 
     sed -i "s/^Architecture.*/Architecture: $ARCH/g" $ROOT/control/control
     sed -i "s/Version:.*/Version: $VER/" $ROOT/control/control
-    sed -i "s/^\(bind .*\):53/\1:5053/g" $ROOT/root/etc/smartdns/smartdns.conf
+    sed -i "s/^\(bind .*\):53/\1:6053/g" $ROOT/root/etc/smartdns/smartdns.conf
     if [ ! -z "$INST_SIZE" ]; then
         echo "Installed-Size: $INST_SIZE" >> $ROOT/control/control
     fi

+ 1 - 1
src/conf.c

@@ -26,7 +26,7 @@ int dns_conf_log_level = TLOG_ERROR;
 char dns_conf_log_file[DNS_MAX_PATH];
 int dns_conf_log_size = 1024 * 1024;
 int dns_conf_log_num = 8;
-int dns_conf_audit_enable;
+int dns_conf_audit_enable = 0;
 char dns_conf_audit_file[DNS_MAX_PATH];
 int dns_conf_audit_size = 1024 * 1024;
 int dns_conf_audit_num = 2;

+ 4 - 0
src/dns_server.c

@@ -929,6 +929,10 @@ static int _dns_server_process_ptr(struct dns_request *request, struct dns_packe
 		}
 	}
 
+	if (strstr(request->domain, "0.0.0.0") != NULL) {
+		found = 1;
+	}
+
 	if (found == 0) {
 		goto errout;
 	}