自建无污染DNS

自建无污染DNS

Posted by BY on February 23, 2024

准备
1. 一台自己家里的虚拟机
2. 一台海外 VPS
3. 基本的 Linux 使用能力

具体实现原理

这里,我在海外 VPS 和国内 VPS 各搭建了一个 AdguardHome,国内 VPS 使用 overture 进行国内外分流,海外 VPS 搭建的 AdguardHome 作为国内 VPS 的 overture 的上游,overture 作为国内 VPS 的 AdguardHome 的上游,客户端连接国内 vps 的 AdguardHome
之所以使用海外 VPS 搭建的 AdguardHome 作为 overture 的上游,是因为 GFW 对海外 DNS 存在屏蔽,使用海外 VPS 搭建的 AdguardHome 作为上游可以起到代理的作用

海外 DNS 的准备
进海外 VPS 搭建 AdguardHome

curl -sSL https://raw.githubusercontent.com/AdguardTeam/AdGuardHome/master/scripts/install.sh | sh

搭建完成后访问 http://IP:3000 就可以来到主界面了
在【设置】-【DNS】中填入海外 DNS
这里我用的是谷歌和 CF 的 DNS
之后点击【设置】-【加密】,并启用加密
在服务器名称中填入解析到这个 VPS 的域名
HTTPS 端口可以保留默认,但如果端口被 nginx 占用的话可以修改成其他的。
在证书中填入你的 SSL 证书,当然也可以去腾讯云,阿里云等申请免费亚洲诚信证书
文章配图
至此,海外 DNS 的准备已经完成

来到国内端,安装 overture

mkdir /opt/overture
cd /opt/overture
wget https://github.com/shawn1m/overture/releases/download/v1.8/overture-linux-amd64.zip
unzip overture-linux-amd64.zip

然后添加一下进程守护
vi /etc/systemd/system/overture.service

[Unit]
Description=overture
After=network.target
[Service]
ExecStart=/opt/overture/overture-linux-amd64 -c /opt/overture/config.yml
Restart=always
[Install]
WantedBy=multi-user.target

下载国内 IP 库

curl -s https://raw.githubusercontent.com/17mon/china_ip_list/master/china_ip_list.txt > china_ip_list.txt

下载 GFW 库

curl https://raw.githubusercontent.com/Loukky/gfwlist-by-loukky/master/gfwlist.txt | base64 -d | sort -u | sed ‘/^$\|@@/d’| sed ‘s#!.\+##; s#|##g; s#@##g; s#http:\/\/##; s#https:\/\/##;’ | sed ‘/\*/d; /apple\.com/d; /sina\.cn/d; /sina\.com\.cn/d; /baidu\.com/d; /qq\.com/d’ | sed ‘/^[0-9]\+\.[0-9]\+\.[0-9]\+\.[0-9]\+$/d’ | grep ‘^[0-9a-zA-Z\.-]\+
| grep ‘\.’ | sed ‘s#^\.\+##’ | sort -u > gfwlist.txt
curl https://raw.githubusercontent.com/hq450/fancyss/master/rules/gfwlist.conf | sed ‘s/ipset=\/\.//g; s/\/gfwlist//g; /^server/d’ > koolshare.txt
cat gfwlist.txt koolshare.txt | sort -u > gfw_list.txt
rm gfwlist.txt koolshare.txt

修改配置文件
vi config.yml

修改为以下内容(记得将 https://example.com/dns-query 改成你自己的 doh 地址)

bindAddress: :5353
debugHTTPAddress: 127.0.0.1:5555
dohEnabled: false
primaryDNS:
– name: Ali DNS
address: https://dns.alidns.com/dns-query
protocol: https
socks5Address:
timeout: 6
ednsClientSubnet:
policy: auto
externalIP:
noCookie: true
– name: Dnspod
address: https://doh.pub/dns-query
protocol: https
socks5Address:
timeout: 6
ednsClientSubnet:
policy: auto
externalIP:
noCookie: true
alternativeDNS:
– name: AdGuardHome DNS
address: https://example.com/dns-query
protocol: https
socks5Address:
timeout: 6
ednsClientSubnet:
policy: auto
externalIP:
noCookie: true
onlyPrimaryDNS: false
ipv6UseAlternativeDNS: false
alternativeDNSConcurrent: false
whenPrimaryDNSAnswerNoneUse: primaryDNS
ipNetworkFile:
primary: /opt/overture/china_ip_list.txt
alternative: ./ip_network_alternative_sample
domainFile:
primary: ./domain_primary_sample
alternative: /opt/overture/gfw_list.txt
matcher: suffix-tree
hostsFile:
hostsFile: ./hosts_sample
finder: full-map
minimumTTL: 0
domainTTLFile: ./domain_ttl_sample
cacheSize: 0
cacheRedisUrl:
cacheRedisConnectionPoolSize:
rejectQType:
– 255

之后启动 overture 即可

systemctl start overture
systemctl enable overture

国内 DNS 搭建
搭建完 overture 后,就可以继续在国内 VPS 搭建 AdguardHome 了

curl -sSL https://raw.githubusercontent.com/AdguardTeam/AdGuardHome/master/scripts/install.sh | sh

搭建完后在 DNS 配置里设置好 DNS
务必将 dns 设置为 127.0.0.1:5353,这样才能使用上边搭建的 overture 作为上游
至此,无污染 DNS 就搭建完成了。