--- aliases: atlas: "[[Atlas/Card|Card]]" created: 2024-03-12 02:26:18 modified: 2024-03-12 23:45:34 tags: - DNS title: 自建 DoH 服务器 --- 为了在网上冲浪时 DNS 不被污染,自建一个干净的 DoH 服务器很有必要,考虑到 DNS 查询的响应时间,我选择在一台位于香港且经过多线 BGP 优化的服务器上部署 # 安装 ## 一键脚本 我采用的软件是 AdGuardHome,由于是直接在服务器上安装而不是使用 Docker 安装,因此直接执行官方在 GitHub 上提供的一键脚本即可 `curl -s -S -L https://raw.githubusercontent.com/AdguardTeam/AdGuardHome/master/scripts/install.sh | sh -s -- -v` ## 解决端口占用 由于是直接部署在本机上,因此默认的 53 端口会被系统自带的 DNS 服务占用,因此需要将系统的停用 ### 停用系统自带 DNS 服务 `systemctl stop systemd-resolved` ### 创建文件夹 `mkdir /etc/systemd/resolved.conf.d/` ### 创建并编辑配置文件 `vim /etc/systemd/resolved.conf.d/adguardhome.conf` ``` [Resolve] DNS=127.0.0.1 DNSStubListener=no ``` ### 重命名原本的配置文件 `mv /etc/resolv.conf /etc/resolv.conf.backup` ### 将 /etc/resolv.conf 链接至 /run/systemd/resolve/resolv.conf `ln -s /run/systemd/resolve/resolv.conf /etc/resolv.conf` ### 重启 DNSStubListener `systemctl restart systemd-resolved` # 设置 ## 常规设置 启用日志和统计数据,并设置保留时间为 30 天 ## DNS 设置 ### 上游 DNS 服务器 - https://cloudflare-dns.com/dns-query - https://dns.google/dns-query - https://dns.quad9.net/dns-query - https://dns.adguard.com/dns-query - https://doh.opendns.com/dns-query - https://hk-hkg.doh.sb/dns-query 使用并行请求以同时查询所有上游服务器来加快解析速度,其余配置保持默认即可 ### DNS 服务配置 将速度限制设为 0 以不限制客户端的每秒查询请求,并启用 EDNS 客户端子网和 DNSSEC,其余配置保持默认即可 ### DNS 缓存配置 缓存大小设置为 104857600 并启用乐观缓存 ## 加密设置 由于是直接部署在服务器上的,而 80 和 443 端口已经被 NPM 给接管了,此时就需要将 HTTPS 端口改为其他端口,我这里设置为 444,同时要记得在防火墙中放行 444 端口,具体指令为 `sudo ufw allow 444` ;并且要为 DoH 服务器设置证书和私钥,虽然我已经用 NPM 反代了,不过这里手动再设置一下也无法,从 NPM 管理面板将泛域名证书下载下来,并将 fullchain 和 privkey 放到任意位置并指定路径即可 ## DNS 黑名单 - [AdGuard DNS filter](https://adguardteam.github.io/HostlistsRegistry/assets/filter_1.txt) - [CHN: AdRules DNS List](https://adguardteam.github.io/HostlistsRegistry/assets/filter_29.txt) - [CHN: anti-AD](https://adguardteam.github.io/HostlistsRegistry/assets/filter_21.txt) - [Anti-AD](https://anti-ad.net/easylist.txt) - [EasyPrivacy](https://easylist-downloads.adblockplus.org/easyprivacy.txt) - [I don’t care about cookies](https://www.i-dont-care-about-cookies.eu/abp/) - [EasyList](https://easylist-downloads.adblockplus.org/easylist.txt) - [EasyList China](https://easylist-downloads.adblockplus.org/easylistchina.txt) - [CJX’s Annoyance List](https://raw.githubusercontent.com/cjx82630/cjxlist/master/cjx-annoyance.txt) - [Cats-Team](https://gitea.com/Cats-Team/AdRules/raw/branch/main/dns.txt) ## 自定义过滤规则 `@@||ad.域名^$important` # 反向代理 为了避免主动探测导致域名和服务器被屏蔽,将默认的 `/dns-query` 进行隐藏是很有必要的,除了正常的 NPM 反代外还要在 Advanced 中加入下列代码 ``` location /任意字符 { proxy_pass https://服务器IP:444/dns-query; proxy_set_header Host $http_host; proxy_redirect http:// https://; proxy_set_header X-Forwarded-Host $http_host; proxy_set_header X-Forwarded-Port $server_port; proxy_set_header X-Forwarded-Proto $scheme; proxy_set_header X-Forwarded-Scheme $scheme; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $remote_addr; proxy_set_header Upgrade $http_upgrade; proxy_set_header Connection $http_connection; proxy_http_version 1.1; } ``` 然后就可以在软路由中通过 `域名/任意字符` 的网址进行 DNS 查询了