最新基于gfwlist+dnsmasq+unbound的dns防污染
尝试了多种方案后,我还是决定用域名黑名单的思路,它的适用性比较强,自动化比较靠谱。
因为我也写了好多篇关于科学上网方案的博客了,所以我就不重复说明原理了,仅仅介绍一下我做了什么。
架构
开源项目gfwlist持续维护了一份污染域名黑名单,里面的域名都需要翻墙解析才行,而其他域名则国内解析即可。
通过自建dnsmasq引入gfwlist黑名单,对这些污染域名转发给unbound走TCP翻墙到8.8.8.8解析,其他的直接走国内的114.114.114.114解析即可。
至于目标IP是否走翻墙,则是iptables中的ipset做实现,大陆IP网段名单来自ipdeny网站。
总结一下,DNS智能解析依赖gfwlist,TCP智能翻墙依赖ipset。
实现
gfwlist提供的域名黑名单是一种叫做adblock Plus的特殊文件格式,所以我写了一套py脚本做解析。
我的自动化更新脚本在这里:https://github.com/owenliang/ilovegfw,它做了2件事情:
- 下载gfwlist,跑py脚本解析domain列表,加工成dnsmasq的配置格式,并重启dnsmasq。
- 下载ipdeny大陆IP网段文件,处理后添加到叫做china的ipset中,并持久化规则。
最终dnsmasq的关键配置是这样的:
1 2 3 4 5 6 7 8 9 10 |
no-resolv # If you don't want dnsmasq to poll /etc/resolv.conf or other resolv # files for changes and re-read them then uncomment this. no-poll # Add other name servers here, with domain specs if they are for # non-public domains. #server=/localnet/192.168.0.1 server=114.114.114.114#53 |
默认全走114国内解析,而对于污染域名则走/etc/dnsmasq.d/gfwlist.conf规则转给发给unbound:
1 2 3 4 5 6 7 |
server=/empfil.com/127.0.0.1#531 server=/isc.sans.edu/127.0.0.1#531 server=/lotuslight.org.tw/127.0.0.1#531 server=/martsangkagyuofficial.org/127.0.0.1#531 server=/chinasucks.net/127.0.0.1#531 server=/aomiwang.com/127.0.0.1#531 server=/casinoking.com/127.0.0.1#531 |
而unbound是TCP upstream到8.8.8.8的,根据ipset大陆网段规则走TCP智能翻墙。
如果文章帮助您解决了工作难题,您可以帮我点击屏幕上的任意广告,或者赞助少量费用来支持我的持续创作,谢谢~
