dnsdist 是一款高度 DNS 、DoS 和滥用感知的负载均衡器。其目标是将流量路由到最佳服务器,为合法用户提供最佳性能,同时分流或阻止滥用流量 文档: https://dnsdist.org/
addLocal("127.0.0.1:5200")newServer{address="127.0.0.1:5373", useClientSubnet=true,pool="china"}newServer({address="127.0.0.1:443", tls="openssl", subjectName="xxxx", dohPath="/dns-query", validateCertificates=true,pool="default",useClientSubnet=true})local shuntset = newDNSNameSet()for line in io.lines("/etc/dnsdist/domains.txt") do local trimmed_line = line:match("^%s*(.-)%s*$") if trimmed_line ~= "" then shuntset:add(newDNSName(line)) endendfunction matchChinaDomain(qname,shuntset) local primary_domain = qname:toStringNoDot():match("([^.]+%.[^.]+)$") domain = newDNSName(qname:toStringNoDot()) -- infolog("Query domain: " .. qname:toStringNoDot()) -- infolog("Query domain: " .. primary_domain) if shuntset:check(domain) then return true else primarydomain = newDNSName(primary_domain) -- infolog("Exact match: " .. tostring(shuntset:check(primarydomain))) return shuntset:check(primarydomain) endendaddAction(LuaRule( function(dq) return matchChinaDomain(dq.qname,shuntset) end), PoolAction("china"))addAction(AllRule(), PoolAction("default"))
这是来自 /etc/dnsdist/dnsdist.conf dnsdist 的配置文件下面简单解释一下
- addLocal 监听地址就是你要把 dnsdist 服务运行在哪个 ip:端口上newServer 把 dns 转发到哪个递归服务器 useClientSubnet 就是是否转发 edns pool 可以理解为一个标签这里有两个第一个是转发常规 udp 第二个是转发到 doh 服务器上去addAction 这个就是控制发送到哪个 newServer 的规则了 AllRule()表示默认也就是所有流量都转发到标签为 default 的递归服务器上去 LuaRule 表示通过 lua 代码处理后的规则然后发送到标签为 china 的递归服务器上去剩下的就是 dnsdist 内置的 lua 函数和 lua 原生的一些东西了这里就不过多解释了
下面是 shell 脚本用于生成 domains.txt 文件
#!/bin/bashcd /root/app/chinarm -f i*.txtrm -f tmp*.txtwget -O i1.txt https://raw.githubusercontent.com/felixonmars/dnsmasq-china-list/master/accelerated-domains.china.confwget -O i2.txt https://raw.githubusercontent.com/Loyalsoldier/v2ray-rules-dat/release/apple-cn.txtcat i1.txt | grep -E -v "^#" > tmp1.txtsed -i s'/server=\//\//g' tmp1.txtsed -i s'/\/114.114.114.114//g' tmp1.txtsed -i s'/full:/\//g' i2.txtcat zdy.dd i2.txt tmp1.txt > tump.txtsed -i s'/^\///g' tump.txtrm -f i*.txtmv tump.txt /etc/dnsdist/domains.txt
zdy.dd 这个文件就是自定义的域名了虽然来自 github 的中国大陆的域名列表挺完善的但也有覆盖不到的
zdy.dd 里面的格式“/xxx.xx”,一行一个
当然 dnsdist 不仅仅只能实现基于域名分流还可以实现更多的功能(基于 ip 分流、基于 ip 和域名分流、基于恶意域名列表拦截、基于威胁情报 ip 源进行拦截、正则匹配拦截,改写 edns 、改写 dns 回复数据等等)
这个方案我用了很长一段时间和其它的有什么区别我不知道但是对比 adguardhome 的分流 dnsdist 无论是占用还是响应都比 adguardhome 快一些
最后我想知道如果做公益 dns 是否要加上广告拦截呢,我一直在纠结这个问题我目前做的是加上广告拦截的但是老是一堆反映这个怎么拦截不了这个被误杀了挺烦的毕竟每个人对这个东西需求不一样希望各位大佬给点建议