support set smartdns as dnsmasq upstream server

This commit is contained in:
Nick Peng
2018-12-01 20:04:16 +08:00
parent 9eb8aa1571
commit ad6741e4da
11 changed files with 151 additions and 70 deletions

View File

@@ -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**
Log in to the router, click on `Services`->`SmartDNS`, uncheck the `Redirect` option to disable port 53 forwarding.
* **Forward dnsmasq's request to SmartDNS**
Log in to the router, click `Network`->`DHCP and DNS`, and modify `DNS forwardings` to:
```shell
/#/127.0.0.1#5053
```
Where `#5053` is the service port number of smartdns. If it is not modified, the default is 5053.
Log in to the router, click on `Services`->`SmartDNS`->`redirect`, select `Run as dnsmasq upstream server` option to forwarding dnsmasq request to Smartdns.
* **Check if the service is configured successfully**
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 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
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.
```
* 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

View File

@@ -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端口重定向**
登录路由器,点击`Services`->`SmartDNS`,去勾选`Redirect`选项停用53端口转发。
* **将dnsmasq的请求发送到smartdns**
登录路由器,点击`Network`->`DHCP and DNS`,修改`DNS forwardings(DNS转发)`为:
```shell
/#/127.0.0.1#5053
```
其中`#5053`为smartdns的服务端口号未修改的情况下默认为5053。
登录路由器,点击`Services`->`SmartDNS`->`redirect`,选择`作为dnsmasq的上游服务器`设置dnsmasq的上游服务器为smartdns。
* **检测上游服务是否配置成功**
使用`nslookup`查询`www.baidu.com`域名查看结果中百度的IP地址是否`只有一个`如有多个IP地址返回则表示未生效请多尝试几个域名检查。
* 方法一:使用`nslookup -querytype=ptr 0.0.0.0`查询域名
看命令结果中的`name`项目是否显示为`smartdns`或`主机名`,如`smartdns`则表示生效
```shell
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地址返回则表示未生效请多尝试几个域名检查。
```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

View File

@@ -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()

View File

@@ -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 "缓存大小"

View File

@@ -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."))

View File

@@ -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>"
}
}
}

View File

@@ -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

View File

@@ -2,15 +2,39 @@
# Copyright (C) 2018 Nick Peng (pymumu@gmail.com)
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"

View File

@@ -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

View File

@@ -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;

View File

@@ -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;
}