dnscrypt-proxy
dnscrypt-proxy 是一个 DNS 代理客户端,支持加密 DNS 协议 DNS over HTTPS 和 DNSCrypt,可用于防止中间人攻击和窃听。dnscrypt-proxy 也兼容 DNSSEC。
安装
安装 dnscrypt-proxy 包。
配置
默认配置文件位于 /etc/dnscrypt-proxy/dnscrypt-proxy.toml。
启动
可以通过两种互斥的方式启动服务(即只能 启用 其中一种):
- 使用 systemd 服务
dnscrypt-proxy.service。- 在使用服务时,必须在配置文件中配置
listen_addresses选项(例如listen_addresses = ['127.0.0.1:53', '[::1]:53'])。
- 在使用服务时,必须在配置文件中配置
- 通过套接字激活(socket activation)使用
dnscrypt-proxy.socket。- 在配置文件中,
listen_addresses选项必须设置为空(即listen_addresses = [ ]),因为 systemd 会负责套接字配置。
- 在配置文件中,
选择解析器
通过在配置文件中将 server_names 注释掉,dnscrypt-proxy 将从 [sources] 下已配置的源中选择最快的服务器。[3]。列表将被下载、验证并自动更新。[4] 因此,配置特定的服务器集是可选的。
要手动设置使用的服务器,请取消注释配置文件中的 server_names 变量并选择一个或多个服务器。例如,要使用 Cloudflare 的服务器:
server_names = ['cloudflare', 'cloudflare-ipv6']
解析器列表的完整列表可在 上游页面 或 Github 上找到。如果 dnscrypt-proxy 之前在该系统上成功运行过,/var/cache/dnscrypt-proxy/public-resolvers.md 中也会包含一个列表。查看描述以了解哪些服务器支持 DNSSEC 验证、不记录日志且未被审查。这些要求可以通过 require_dnssec、require_nolog、require_nofilter 选项进行全局配置。
修改 resolv.conf 并与其他本地域名解析器协调
修改 resolv.conf 文件,并将当前的解析器地址集替换为环回 IP 地址和选项。[5]
/etc/resolv.conf
nameserver ::1 nameserver 127.0.0.1 options edns0
接下来,必须配置现有的域名解析服务,使其不覆盖这些设置。有关详细信息,请参阅 resolv.conf#Overwriting of /etc/resolv.conf。如果它们配置为绑定到不同于 localhost 的地址,并将 DNS 请求转发到 localhost 的 53 端口,则可以使用它们。例如,systemd-resolved 默认使用地址 127.0.0.53。
禁用绑定到 53 端口的任何服务
要查看是否有程序在使用端口 53,请运行:
$ ss -lp 'sport = :domain'
如果输出的内容多于第一行列名,您就需要禁用正在使用端口 53 的任何服务。例如,NetworkManager 可能会尝试自动激活一个解析器,但其他网络管理器也可能有类似的组件。当上述命令的输出只剩下标题行时,您就可以继续了。
Netid State Recv-Q Send-Q Local Address:Port Peer Address:Port Process
启动 systemd 服务
最后,根据您上面选择的方法,启动/启用 dnscrypt-proxy.service unit 或 dnscrypt-proxy.socket。
检查 dnscrypt-proxy 是否正常工作
打开浏览器并访问 DnsLeakTest 并进行扩展测试,如果结果显示您在配置文件中设置的服务器,则表示 dnscrypt-proxy 工作正常,否则就存在问题。
技巧与提示
启用、下载和自动更新过滤列表 / 阻止列表
在 /usr/share/dnscrypt-proxy/utils/generate-domains-blocklist/domains-blocklist.conf 中配置过滤列表源。例如:
# NextDNS CNAME cloaking list https://raw.githubusercontent.com/nextdns/cname-cloaking-blocklist/master/domains # AdGuard Simplified Domain Names filter https://adguardteam.github.io/AdGuardSDNSFilter/Filters/filter.txt # OISD.NL Big: https://big.oisd.nl/domainswild # HaGeZi Multi Pro https://raw.githubusercontent.com/hagezi/dns-blocklists/main/wildcard/pro-onlydomains.txt # HaGeZi Thread Intelligence Feeds https://raw.githubusercontent.com/hagezi/dns-blocklists/main/wildcard/tif-onlydomains.txt
创建一个服务来下载和合并过滤列表。/etc/systemd/system/dnscrypt-filterlist-update.service
[Unit] Description=DNSCrypt Filterlist Update [Service] Type=oneshot User=root WorkingDirectory=/usr/share/dnscrypt-proxy/utils/generate-domains-blocklist/ ExecStart=generate-domains-blocklist -a domains-allowlist.txt -o blocklist.txt ; sleep 2 ; systemctl restart dnscrypt-proxy.service [Install] WantedBy=multi-user.target
创建一个定时器,在启动时运行,每 5 小时运行一次。/etc/systemd/system/dnscrypt-filterlist-update.timer
[Unit] Description=Run 15min after boot and every 5 hours (DNSCrypt Filterlist Update) [Timer] OnBootSec=15min OnUnitActiveSec=5h [Install] WantedBy=timers.target
启用定时器
systemctl daemon-reload systemctl enable dnscrypt-filterlist-update.timer
配置 DNSCrypt 应用创建的过滤规则。/etc/dnscrypt-proxy/dnscrypt-proxy.toml
blocked_names_file = '/usr/share/dnscrypt-proxy/utils/generate-domains-blocklist/blocklist.txt' log_file = '/var/log/dnscrypt-proxy/blocked-names.log'
本地 DNS 缓存配置
cache = true 行即表示启用。建议将 dnscrypt-proxy 用作本地 DNS 缓存的转发器,如果未使用 dnscrypt-proxy 的缓存功能;否则,每个查询都将需要往返一次到上游解析器。任何本地 DNS 缓存程序都可以工作。除了设置 dnscrypt-proxy,您还必须设置您的本地 DNS 缓存程序。
更改端口
为了从本地 DNS 缓存转发查询,dnscrypt-proxy 应该监听一个不同于默认 53 端口的端口,因为 DNS 缓存本身需要监听 53 端口并查询 dnscrypt-proxy 的另一个端口。本节中使用端口号 54 作为示例。
有两种方法可以更改默认端口:
套接字方法
使用以下内容 编辑 dnscrypt-proxy.socket:
[Socket] ListenStream= ListenDatagram= ListenStream=127.0.0.1:54 ListenStream=[::1]:54 ListenDatagram=127.0.0.1:54 ListenDatagram=[::1]:54
当从本地 DNS 缓存转发查询到 54 端口时,dnscrypt-proxy.socket 将启动 dnscrypt-proxy.service。
服务方法
在 /etc/dnscrypt-proxy/dnscrypt-proxy.toml 中编辑 listen_addresses 选项,如下所示:
listen_addresses = ['127.0.0.1:54', '[::1]:54']
示例本地 DNS 缓存配置
以下配置应能与 dnscrypt-proxy 配合使用,并假定它正在监听端口 54。
Unbound
根据您的喜好配置 Unbound (特别是,请参阅 Unbound#Local DNS server),并将以下行添加到 /etc/unbound/unbound.conf 的 server 部分末尾:
do-not-query-localhost: no forward-zone: name: "." forward-addr: ::1@54 forward-addr: 127.0.0.1@54
server: 部分添加 interface: 0.0.0.0@53 和 access-control: your-network/subnet-mask allow,以便其他计算机可以连接到服务器。客户端必须在 /etc/resolv.conf 中配置 nameserver address-of-your-server。为应用更改,请 重新启动 unbound.service。
dnsmasq
将 dnsmasq 配置为 本地 DNS 缓存。与 dnscrypt-proxy 配合使用的基本配置:
/etc/dnsmasq.conf
no-resolv server=::1#54 server=127.0.0.1#54 listen-address=::1,127.0.0.1
如果您将 dnscrypt-proxy 配置为使用启用了 DNSSEC 验证的解析器,请确保也在 dnsmasq 中启用它:
/etc/dnsmasq.conf
conf-file=/usr/share/dnsmasq/trust-anchors.conf dnssec
为应用更改,请 重新启动 dnsmasq.service。
pdnsd
安装 pdnsd。与 dnscrypt-proxy 配合使用的基本配置如下:
/etc/pdnsd.conf
global {
perm_cache = 1024;
cache_dir = "/var/cache/pdnsd";
run_as = "pdnsd";
server_ip = 127.0.0.1;
status_ctl = on;
query_method = udp_tcp;
min_ttl = 15m; # Retain cached entries at least 15 minutes.
max_ttl = 1w; # One week.
timeout = 10; # Global timeout option (10 seconds).
neg_domain_pol = on;
udpbufsize = 1024; # Upper limit on the size of UDP messages.
}
server {
label = "dnscrypt-proxy";
ip = 127.0.0.1;
port = 54;
timeout = 4;
proxy_only = on;
}
source {
owner = localhost;
file = "/etc/hosts";
}
为应用更改,请 重新启动 pdnsd.service。
启用 EDNS0
Extension Mechanisms for DNS (DNS 扩展机制),它允许客户端指定 UDP 回复的最大大小等。
在您的 /etc/resolv.conf 中添加以下行:
options edns0
测试 EDNS0
使用 DNS Reply Size Test Server,并使用 drill 命令行工具为名称 rs.dns-oarc.net 发出 TXT 查询。
$ drill rs.dns-oarc.net TXT
在支持 EDNS0 的情况下,输出的“回答部分”应类似于:
rst.x3827.rs.dns-oarc.net. rst.x4049.x3827.rs.dns-oarc.net. rst.x4055.x4049.x3827.rs.dns-oarc.net. "2a00:d880:3:1::a6c1:2e89 DNS reply size limit is at least 4055 bytes" "2a00:d880:3:1::a6c1:2e89 sent EDNS buffer size 4096"
故障排除
缓存文件时出现“只读文件系统”或“没有此类文件或目录”
设置 /var/cache/dnscrypt-proxy 的正确 所有权,通常是 dnscrypt-proxy:dnscrypt-proxy。