前言
最近我的 swag 服务突然证书 renew 失败
诊断了一下发现原来是无法解析 acme-v02.api.letsencrypt.org
域名
换了几个 DNS 都不行,应该是 DNS 被污染或者劫持了
这时我才意识到不上 DoH/DoT 怕是没办法了🤣
本文记录一下用一种简单的方法在服务器上实现 DoH/DoT
DoH/DoT
简单科普一下,DNS 是用来把网站解析到IP地址的协议
正常的 DNS 是明文传输,很容易被污染或者劫持
DoH 是 DNS over HTTPS,走加密的 HTTPS 流量(443 端口),看起来就像访问网页一样,不容易被污染或者劫持
除此之外还有 DoT(DNS over TLS)、ODoH(Oblivious DoH,隐私更强),都是更加安全的域名解析方式
cloudflared
https://github.com/cloudflare/cloudflared
这是 Cloudflare 官方开源的一个 Cloudflare Tunnel 客户端,用 go 语言开发的,非常容易安装部署。
简介
这个客户端不仅可以接入 Tunnel 实现内网穿透,还可以实现 DoH 代理
本文使用这个工具来实现 DoH 配置
安装
Ubuntu Server 的官方软件源没有这个工具
需要添加 Cloudflare 官方 APT 源
bash
# 1. 添加 GPG key
curl -fsSL https://pkg.cloudflare.com/cloudflare-main.gpg | sudo tee /usr/share/keyrings/cloudflare-main.gpg > /dev/null
# 2. 添加软件源
echo "deb [signed-by=/usr/share/keyrings/cloudflare-main.gpg] https://pkg.cloudflare.com/cloudflared jammy main" | sudo tee /etc/apt/sources.list.d/cloudflared.list
# 3. 更新并安装
sudo apt update
sudo apt install cloudflared
也可以直接下载 DEB 包安装
bash
# 1. 下载 cloudflared 最新 deb 包
wget https://github.com/cloudflare/cloudflared/releases/latest/download/cloudflared-linux-amd64.deb
# 2. 安装
sudo dpkg -i cloudflared-linux-amd64.deb
# 3. 验证
cloudflared --version
测试
安装好以后,运行:
bash
sudo cloudflared proxy-dns --address 127.0.0.1 --port 5053
测试一下:
bash
dig @127.0.0.1 -p 5053 acme-v02.api.letsencrypt.org
如果能返回解析结果,就说明成功了
这里默认使用的是 Cloudflare 官方的 DoH
如果不行的话,可以换成国内的 DoH 服务
比如阿里:
比如腾讯:
示例
bash
sudo cloudflared proxy-dns --address 127.0.0.1 --port 5053 \
--upstream https://223.5.5.5/dns-query \
--upstream https://223.6.6.6/dns-query
用 dig 或者 nslookup 之类的工具测试没问题的话,可以进入下一步。
添加服务
创建 systemd service,让 cloudflared 常驻运行
bash
sudo nano /etc/systemd/system/cloudflared-dns.service
内容:
ini
[Unit]
Description=Cloudflared DNS over HTTPS proxy
After=network.target
[Service]
ExecStart=/usr/local/bin/cloudflared proxy-dns --address 127.0.0.1 --port 5053 \
--upstream https://223.5.5.5/dns-query \
--upstream https://223.6.6.6/dns-query \
--upstream https://doh.pub/dns-query \
--upstream https://dns.pub/dns-query
Restart=always
User=nobody
AmbientCapabilities=CAP_NET_BIND_SERVICE
[Install]
WantedBy=multi-user.target
应用 & 启动
bash
sudo systemctl daemon-reexec
sudo systemctl enable --now cloudflared-dns
配置DNS
使用 drop-in 配置的方式来设置 DNS
不要直接改 /etc/systemd/resolved.conf
bash
sudo mkdir -p /etc/systemd/resolved.conf.d
sudo nano /etc/systemd/resolved.conf.d/dns.conf
内容:
ini
[Resolve]
DNS=127.0.0.1:5053
FallbackDNS=223.5.5.5 223.6.6.6
DNSSEC=no
重启服务
bash
sudo systemctl restart systemd-resolved
检查生效情况
bash
resolvectl status
可以看到以下输出
bash
$ resolvectl status
Global
Protocols: -LLMNR -mDNS -DNSOverTLS DNSSEC=no/unsupported
resolv.conf mode: stub
DNS Servers: 127.0.0.1:5053
Fallback DNS Servers: 223.5.5.5 223.6.6.6
这时候就搞定了,docker容器也会默认使用系统的这个 DNS
此时再去 swag renew 证书,就成功了✌️
一键脚本
老规矩,我让大模型爷爷帮忙写了一个一键配置DoH的脚本
默认使用从官方 GitHub 仓库下载 deb 包安装的方式
可以直接执行
bash
bash -c "$(curl -fsSL https://gist.github.com/Deali-Axy/8b2ad8e5a601f2c43f6e7debdfb0aa29/raw/3c57dbce7dc0e224ad4ad35606f15cbc34d4810e/install-doh.sh)"
脚本源码
bash
#!/usr/bin/env bash
set -e
echo "[1/6] 安装 cloudflared..."
if ! command -v cloudflared >/dev/null 2>&1; then
wget -q https://github.com/cloudflare/cloudflared/releases/latest/download/cloudflared-linux-amd64.deb -O /tmp/cloudflared.deb
sudo dpkg -i /tmp/cloudflared.deb || sudo apt-get install -f -y
rm -f /tmp/cloudflared.deb
else
echo "cloudflared 已安装,跳过。"
fi
CLOUDFLARED_BIN=$(command -v cloudflared)
echo "cloudflared 路径: $CLOUDFLARED_BIN"
echo "[2/6] 创建 systemd service..."
sudo tee /etc/systemd/system/cloudflared-dns.service >/dev/null <<EOF
[Unit]
Description=Cloudflared DNS over HTTPS proxy
After=network.target
[Service]
ExecStart=${CLOUDFLARED_BIN} proxy-dns --address 127.0.0.1 --port 5053 \
--upstream https://223.5.5.5/dns-query \
--upstream https://223.6.6.6/dns-query \
--upstream https://doh.pub/dns-query \
--upstream https://dns.pub/dns-query
Restart=always
User=nobody
AmbientCapabilities=CAP_NET_BIND_SERVICE
[Install]
WantedBy=multi-user.target
EOF
echo "[3/6] 重新加载 systemd 并启用服务..."
sudo systemctl daemon-reexec
sudo systemctl enable --now cloudflared-dns
echo "[4/6] 配置 systemd-resolved..."
sudo mkdir -p /etc/systemd/resolved.conf.d
sudo tee /etc/systemd/resolved.conf.d/dns.conf >/dev/null <<EOF
[Resolve]
DNS=127.0.0.1:5053
FallbackDNS=223.5.5.5 223.6.6.6
DNSSEC=no
EOF
echo "[5/6] 重启 systemd-resolved..."
sudo systemctl restart systemd-resolved
echo "[6/6] 检查 DNS 配置..."
resolvectl status | grep "DNS Servers"
echo "✅ 安装完成!现在系统 DNS 已经走 DoH。"
将以上代码保存为 install-doh.sh
运行
bash
chmod +x install-doh.sh
./install-doh.sh
脚本会自动:
- 下载并安装
cloudflared
- 写入
systemd
服务文件并启动 - 配置
systemd-resolved
- 自动重启相关服务并检查生效