抱歉,您的浏览器无法访问本站
本页面需要浏览器支持(启用)JavaScript
了解详情 >

最近我发现打开网页的速度变得很慢,测速显示是200M-300M宽带,默认DNS解析速度100ms,于是我测试了最佳DNS地址(推荐DnsTools),目前使用的是114.114.114.114和DNSPod DNS+的119.29.29.29,同时我也把ipv6的DNS修改成了阿里的2400:3200::1和2400:3200:baba::1。网页打开速度确实加快了不少。这期间产生了自建DNS服务器的想法。

前期准备:

拥有一个Azure账户,并且有公网IP(ipv4、ipv6)的服务器(当然其他服务器也可以,但是我的Azure的服务器基于日本IP,可以有效防止DNS泄露),服务器开启入站53、3128端口;出站80、443端口。本教程基于Debian,具体系统版本如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
       _,met$$$$$gg.           debian@virtual-machine
,g$$$$$$$$$$$$$$$P. OS: Debian 11 bullseye
,g$$P"" """Y$$.". Kernel: x86_64 Linux 5.10.0-34-cloud-amd64
,$$P' `$$$. Uptime: 6d 22h 48m
',$$P ,ggs. `$$b: Packages: 379
`d$$' ,$P"' . $$$ Shell: bash 5.1.4
$$P d$' , $$P Disk: 1.2G / 68G (2%)
$$: $$. - ,d$$' CPU: Intel Xeon E5-2673 v4 @ 2.295GHz
$$\; Y$b._ _,d$P' RAM: 326MiB / 913MiB
Y$$. `.`"Y$$$$P"'
`$$b "-.__
`Y$$
`Y$$.
`$$b.
`Y$$b.
`"Y$b._
`""""

软件:

  • dnsutils:使用dig命令检测DNS是否成功解析
  • squid:监听端口
  • cloudflared:代理DNS请求

在Debian下安装这几个软件:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
sudo apt install dnsutils squid


# cloudflared的安装参考官网:https://pkg.cloudflare.com/index.html#debian-any

#Any Debian Based Distribution (Recommended)
# Add cloudflare gpg key
sudo mkdir -p --mode=0755 /usr/share/keyrings
curl -fsSL https://pkg.cloudflare.com/cloudflare-main.gpg | sudo tee /usr/share/keyrings/cloudflare-main.gpg >/dev/null

# Add this repo to your apt repositories
echo 'deb [signed-by=/usr/share/keyrings/cloudflare-main.gpg] https://pkg.cloudflare.com/cloudflared any main' | sudo tee /etc/apt/sources.list.d/cloudflared.list

# install cloudflared
sudo apt-get update && sudo apt-get install cloudflared

设置监听端口:

编辑squid配置(/etc/squid/squid.conf),添加以下配置:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
# 监听 IPv6 的 3128 端口
http_port [::]:3128

# 定义允许的客户端 IPv6 网段
acl ipv6_clients src fd12:3456:789a::/64 # 这里可以不设置,功能只是允许某个网段的ipv6连接

# 定义 HTTPS 端口
acl SSL_ports port 443

# 允许来自 ipv6_clients 的 HTTPS 请求(CONNECT 方法)
http_access allow ipv6_clients SSL_ports

# 允许来自 ipv6_clients 的其他 HTTP 请求(可选,按需启用)
http_access allow ipv6_clients

# DNS 优先解析 IPv4
dns_v4_first on

# 默认拒绝其他所有请求
http_access deny all

这里的ipv6设置按需配置,如果不需要ipv6,可以删除ipv6_clients的设置。
然后直接启动squid即可:

1
2
3
4
5
6
7
8
9
10
11
12
sudo systemctl start squid

# 设置开机启动
sudo systemctl enable squid

# 查看squid状态
sudo systemctl status squid

# 查看是否正在监听3128
sudo ss -tulpn | grep 'squid'
# 输出类似的表示正在监听3128
tcp6 0 0 :::3128 :::* LISTEN 12646/(squid-1)

开启cloudflare代理流量

简单通过命令行方式:

1
2
3
4
5
6
7
cloudflared proxy-dns \
--address 0.0.0.0 \
--port 53 \
--upstream https://dns.google/dns-query \ # 这里是google公共ipv4的dns-over-https
--upstream https://dns.adguard.com/dns-query \
--upstream https://cloudflare-dns.com/dns-query \
--upstream https://[2001:4860:4860::8888]/dns-query # 这里是google公共ipv6的dns-over-https

这样就完成dns的设置,现在检查一下dns是否在服务器上成功解析:

1
dig @127.0.0.1 www.google.com

如果可以得到google.com的IP地址则表明现在dns服务器可以成功运行。

但是每次这样手动开启cloudflare太麻烦,现在把他设置为开机启动:

1
sudo vim /etc/systemd/system/cloudflared-dns.service

并填入一下内容:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
[Unit]
Description=Cloudflare DoH/DoT DNS Proxy
After=network.target

[Service]
User=root
Group=root
ExecStart=/usr/local/bin/cloudflared proxy-dns \
--address 0.0.0.0 \
--port 53 \
--upstream https://dns.google/dns-query \
--upstream https://dns.adguard.com/dns-query \
--upstream https://cloudflare-dns.com/dns-query \
--upstream https://[2001:4860:4860::8888]/dns-query
Restart=always
RestartSec=5

[Install]
WantedBy=multi-user.target

然后执行:

1
2
3
sudo systemctl daemon-reload
sudo systemctl start cloudflared-dns
sudo systemctl enable cloudflared-dns

这样就实现了cloudflare的开机启动。

电脑连接DNS代理

以win11为例,打开”网络和Internet” $\rightarrow$ “WLAN”,然后选择你连接的WiFi属性,把”DNS服务器分配”改为手动,并且添加你的服务器ipv4、ipv6地址即可。

检测是否生效,可以打开DNS泄露测试,等待检测完成后,查看检测到的DNS服务提供商,如果显示的是你设置的”Google”、”Adguard”或者”Cloudflare”任意一个,则表明成功使用服务器的dns解析。如图:
图片加载失败

如果没有配置ipv6的dns解析,只配置ipv4的dns,那么需要手动将ipv6关闭并重新进行测试,否则很有可能显示的还是国内dns解析地址。

另外需要注意这样搭建的DNS服务器使用google的dns进行解析,会导致网速变慢,但是这样的好处是可以解决有些VPN服务器或者VPN客户端产生dns泄露的问题(详见下一节),这里的dns服务器的使用主要场景是使用VPN期间。

评论



Powered by Hexo | Theme keep Volantis

本站总访问量 总访客数 🌎