Files
smartdns/ReadMe.md
2018-07-30 23:37:11 +08:00

316 lines
12 KiB
Markdown
Executable File
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
SmartDNS
==============
SmartDNS是一个运行在本地的DNS服务器SmartDNS接受本地客户端的DNS查询请求从多个上游DNS服务器获取DNS查询结果并将访问速度最快的结果返回个客户端避免DNS污染提高网络访问速度。
同时支持指定特定域名IP地址并高性匹配达到过滤广告的效果。
与dnsmasq的all-servers不同smartdns返回的是访问速度最快的解析结果。
支持树莓派openwrt华硕路由器等设备。
**阿里DNS**
```shell
pi@raspberrypi:~/code/smartdns_build $ nslookup www.baidu.com 223.5.5.5
Server: 223.5.5.5
Address: 223.5.5.5#53
Non-authoritative answer:
www.baidu.com canonical name = www.a.shifen.com.
Name: www.a.shifen.com
Address: 180.97.33.108
Name: www.a.shifen.com
Address: 180.97.33.107
pi@raspberrypi:~/code/smartdns_build $ ping 180.97.33.107 -c 2
PING 180.97.33.107 (180.97.33.107) 56(84) bytes of data.
64 bytes from 180.97.33.107: icmp_seq=1 ttl=55 time=24.3 ms
64 bytes from 180.97.33.107: icmp_seq=2 ttl=55 time=24.2 ms
--- 180.97.33.107 ping statistics ---
2 packets transmitted, 2 received, 0% packet loss, time 1001ms
rtt min/avg/max/mdev = 24.275/24.327/24.380/0.164 ms
pi@raspberrypi:~/code/smartdns_build $ ping 180.97.33.108 -c 2
PING 180.97.33.108 (180.97.33.108) 56(84) bytes of data.
64 bytes from 180.97.33.108: icmp_seq=1 ttl=55 time=31.1 ms
64 bytes from 180.97.33.108: icmp_seq=2 ttl=55 time=31.0 ms
--- 180.97.33.108 ping statistics ---
2 packets transmitted, 2 received, 0% packet loss, time 1001ms
rtt min/avg/max/mdev = 31.014/31.094/31.175/0.193 ms
```
**smartdns**
```shell
pi@raspberrypi:~/code/smartdns_build $ nslookup www.baidu.com
Server: 192.168.1.1
Address: 192.168.1.1#53
Non-authoritative answer:
www.baidu.com canonical name = www.a.shifen.com.
Name: www.a.shifen.com
Address: 14.215.177.39
pi@raspberrypi:~/code/smartdns_build $ ping 14.215.177.39 -c 2
PING 14.215.177.39 (14.215.177.39) 56(84) bytes of data.
64 bytes from 14.215.177.39: icmp_seq=1 ttl=56 time=6.31 ms
64 bytes from 14.215.177.39: icmp_seq=2 ttl=56 time=5.95 ms
--- 14.215.177.39 ping statistics ---
2 packets transmitted, 2 received, 0% packet loss, time 1001ms
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服务器异常也不会影响查询。
1. **返回最快IP地址**
支持从域名所属IP地址列表中查找到访问速度最快的IP地址并返回给客户端避免DNS污染提高网络访问速度。
1. **支持非标准端口**
支持非53端口查询支持TCP查询有效避免DNS污染。
1. **特定域名IP地址指定**
支持指定域名的IP地址达到广告过滤效果避免恶意网站的效果。
1. **域名高性能后缀匹配**
支持域名后缀匹配模式简化过滤配置过滤20万条记录时间<1ms
1. **Linux多平台支持**
支持标准Linux系统树莓派openwrt系统各种固件华硕路由器原生固件。
1. **支持IPV4, IPV6双栈**
支持IPV4IPV6网络支持查询A, AAAA记录。
1. **高性能,占用资源少**
多线程异步IO模式cache缓存查询结果。
架构
-------------
![Architecture](doc/architecture.png)
1. SmartDNS接收本地网络设备的DNS查询请求如PC手机的查询请求。
2. SmartDNS将查询请求发送到多个上游DNS服务器可采用标准UDP查询非标准端口UDP查询及TCP查询。
3. 上游DNS服务器返回域名对应的Server IP地址列表。SmartDNS检测与本地网络访问速度最快的Server IP。
4. 将访问速度最快的Server IP返回给本地客户端。
使用
==============
下载配套安装包
--------------
下载配套版本的SmartDNS安装包对应安装包配套关系如下。
|系统 |安装包|说明
|-----|-----|-----
|标准Linux系统(树莓派)| smartdns.xxxxxxxx.armhf.deb|支持树莓派Raspbian stretchDebian 9系统。
|标准Linux系统(x86_64)| smartdns.xxxxxxxx.x86_64..tar.gz|支持x86_64系统。
|华硕原生固件(optware)|smartdns.xxxxxxx.mipsbig.ipk|支持MIPS大端架构的系统如RT-AC55U, RT-AC66U.
|华硕原生固件(optware)|smartdns.xxxxxxx.mipsel.ipk|支持MIPS小端架构的系统如RT-AC68U。
|openwrt 15.01|smartdns.xxxxxxxx.ar71xx.ipk|支持AR71XX MIPS系统。
|openwrt 15.01|smartdns.xxxxxxxx.ramips_24kec.ipk|支持MT762X等小端路由器
|openwrt 15.01(潘多拉)|smartdns.xxxxxxxx.mipsel_24kec_dsp.ipk|支持MT7620系列的潘多拉固件
|openwrt LEDE|smartdns.xxxxxxxx.mips_24kc.ipk|支持AR71XX MIPS系统。
|openwrt LEDE|smartdns.xxxxxxxx.mipsel_24kc.ipk|支持MT726X等小端路由器
|openwrt LEDE|smartdns.xxxxxxxx.x86_64.ipk|支持x86_64路由器
|openwrt LEDE|smartdns.xxxxxxxxxxx.arm_cortex-a9.ipk|支持arm A9核心CPU的路由器
|openwrt LEDE|smartdns.xxxxxxxxx.arm_cortex-a7_neon-vfpv4.ipk|支持arm A7核心CPU的路由器
|openwrt LUCI|luci-app-smartdns.xxxxxxxxx.xxxx.all.ipk|openwrt管理统一界面
openwrt系统CPU架构比较多请查看CPU架构后下载CPU架构可在路由器管理界面找到查看方法
* 登录路由器,点击`System`->`Software`,点击`Configuration` Tab页面在opkg安装源中可找到对应软件架构下载路径中可找到如下架构为ar71xx
```
src/gz chaos_calmer_base http://downloads.openwrt.org/chaos_calmer/15.05/ar71xx/generic/packages/base
```
[此处下载](https://github.com/pymumu/smartdns/releases)
标准Linux系统安装树莓派
--------------
1. 安装
下载配套安装包`smartdns.xxxxxxxx.armhf.deb`并上传到Linux系统中。 执行如下命令安装
```
dpkg -i smartdns.xxxxxxxx.armhf.deb
```
2. 修改配置
安装完成后可配置smartdns的上游服务器信息。具体配置参数参考`配置参数`说明。
一般情况下,只需要增加`server [IP]:port`, `server-tcp [IP]:port`配置项
```
vi /etc/smartdns/smartdns.conf
```
3. 启动服务
```
systemctl enable smartdns
systemctl start smartdns
```
4. 将DNS请求转发的SmartDNS解析。
修改本地路由器的DNS服务器将DNS服务器配置为SmartDNS。
* 登录到本地网络的路由器中配置树莓派分配静态IP地址。
* 修改WAN口或者DHCP DNS为树莓派IP地址。
注意:
I. 每款路由器配置方法不尽相同,请百度搜索相关的配置方法。
II. 华为等路由器可能不支持配置DNS为本地IP请修改PC端手机端DNS服务器为树莓派IP。
5. 检测服务是否配置成功。
使用nslookup查询域名看命令结果中的`服务器`项目是否显示为`Linux主机名`如raspberry则表示生效
```
C:\Users\meikechong>nslookup www.baidu.com
服务器: raspberry
Address: 192.168.1.1
非权威应答:
名称: www.a.shifen.com
Address: 14.215.177.39
Aliases: www.baidu.com
```
openwrt/LEDE
--------------
1. 安装
将软件使用winscp上传到路由器的/root目录执行如下命令安装
```
opkg install smartdns.xxxxxxxx.xxxx.ipk
opkg install luci-app-smartdns.xxxxxxxx.xxxx.all.ipk
```
2. 修改配置
登陆openwrt管理页面打开Services->SmartDNS进行配置。
* 在Upstream Servers增加上游DNS服务器配置将以配置多个国内外DNS服务器。
* 在Domain Address指定特定域名的IP地址可用于广告屏蔽。
3. 启动服务
勾选配置页面中的`Enable(启用)`来启动SmartDNS
4. 检测服务是否配置成功。
使用nslookup查询域名看命令结果中的`服务器`项目是否显示为`Linux主机名`,如`smartdns`则表示生效
```
C:\Users\meikechong>nslookup www.baidu.com
服务器: smartdns
Address: 192.168.1.1
非权威应答:
名称: www.a.shifen.com
Address: 14.215.177.39
Aliases: www.baidu.com
```
5. 注意:
* 如已经安装chinaDNS建议将chinaDNS的上游配置为SmartDNS。
* SmartDNS默认情况将53端口的请求转发到SmartDNS的本地端口`Redirect`配置选项控制。
华硕路由器原生固件
--------------
1. 准备
在使用此软件时需要确认路由器是否支持U盘并准备好U盘一个。
1. 启用SSH登录
登录管理界面,点击`系统管理`->点击`系统设置`,配置`Enable SSH``Lan Only`
SSH登录用户名密码与管理界面相同。
2. 下载`Download Master`
在管理界面点击`USB相关应用`->点击`Download Master`下载。
下载完成后,启用`Download Master`,如果不需要下载功能,此处可以卸载`Download Master`但要保证卸载前Download Master是启用的。
3. 安装SmartDNS
将软件使用winscp上传到路由器的`/tmp/mnt/sda1`目录。或网上邻居复制到sda1共享目录
```
ipkg install smartdns.xxxxxxx.mipsbig.ipk
```
4. 重启路由器生效服务
待路由器启动后使用nslookup查询域名看命令结果中的`服务器`项目是否显示为`smartdns`如显示smartdns则表示生效
```
C:\Users\meikechong>nslookup www.baidu.com
服务器: smartdns
Address: 192.168.1.1
非权威应答:
名称: www.a.shifen.com
Address: 14.215.177.39
Aliases: www.baidu.com
```
5. 额外说明
上述过程smartdns将安装到U盘根目录采用optware的模式运行。
其目录结构如下: 此处仅列出smartdns相关文件
```
U盘
└── asusware.mipsbig
├── bin
├── etc
| ├── smartdns
| | └── smartdns.conf
| └── init.d
| └── S50smartdns
├── lib
├── sbin
├── usr
| └── sbin
| └── smartdns
....
```
如要修改配置可以ssh登录路由器使用vi命令修改
```
vi /opt/etc/smartdns/smartdns.conf
```
也可以通过网上邻居修改,网上邻居共享目录`sda1`看不到`asusware.mipsbig`目录,但可以直接在`文件管理器`中输入`asusware.mipsbig\etc\init.d`访问。
```
\\192.168.1.1\sda1\asusware.mipsbig\etc\init.d
```
配置参数
==============
|参数|功能|默认值|配置值|例子|
|--|--|--|--|--|
|server-name|DNS服务器名称|操作系统主机名/smartdns|符合主机名规格的字符串|server-name smartdns
|bind|DNS监听端口号|[::]:53|IP:PORT|bind 192.168.1.1:53
|cache-size|域名结果缓存个数|512|数字|cache-size 512
|rr-ttl|域名结果TTL|远程查询结果|大于0的数字|rr-ttl 600
|rr-ttl-min|允许的最小TTL值|远程查询结果|大于0的数字|rr-ttl-min 60
|rr-ttl-max|允许的最大TTL值|远程查询结果|大于0的数组|rr-ttl-max 600
|log-level|设置日志级别|error|error,warn,info,debug|log-level error
|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
|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
[Donate](#Donate)
==============
如果你觉得此项目对你有帮助,请捐助我们,以使项目能持续发展,更加完善。
* PayPal
[![Support via PayPal](https://cdn.rawgit.com/twolfson/paypal-github-button/1.0.0/dist/button.svg)](https://paypal.me/PengNick/)
* Alipay 支付宝
![alipay](doc/alipay_donate.jpg)
* Wechat 微信
![wechat](doc/wechat_donate.jpg)
说明
==============
目前代码未开源,后续根据情况开源。