抱歉,您的瀏覽器無法訪問本站
本頁面需要瀏覽器支持(啟用)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

本站總訪問量 總訪客數 🌎