琐碎笔记

用 AdGuard Home 自建 DNS over HTTPS 服务

佩佩 · 4月19日 · 2020年 · · · · · 10173次查看

说明

DoH 其实就是给 DNS 加了 HTTPS 而已,而且速度更慢了,但是能防止运营商劫持,会更安全了。而且自己有更大的可操作空间。

安装

# 安装相关组件
yum install bind-utils nano bind9-host -y

# 获取最新编译好的包,并解压
wget https://static.adguard.com/adguardhome/release/AdGuardHome_linux_amd64.tar.gz
tar xvf AdGuardHome_linux_amd64.tar.gz

# 运行
cd AdGuardHome
./AdGuardHome

# 指定端口运行
./AdGuardHome -p 1111 //默认端口为 3000 ,运行前请记得开放相关端口

此刻,在浏览器输入服务器公网 IP 和端口号(如:http://127.0.0.1:3000) 访问就可以进行安装了。

配置

常规设置

按需打钩

DNS 设置

  1. 速度限制,不作限制,设为 0
  2. 使用客户端的子网地址(开启 EDNS),建议勾选;
  3. 因为我的服务器没有 IPv6 地址,不支持 IPv6,所以禁用掉 IPv6 解析;
  4. 拦截模式,默认。

上游 DNS 服务器设置

加密设置

  1. 勾选“启用加密”;
  2. 写绑定的域名;“HTTPS 自动重定向”暂不作勾选,稍后通过 Nginx 反向代理配置;
  3. HTTPS端口只要不是 443 端口和其它已经存在的端口就可以;
  4. DoT 端口一般 853,无需变更。

证书配置,正确填写所绑定域名的SSL私钥和证书内容,或填写证书所在服务器的绝对路径。

过滤器

我自己在用的规则

Nginx 配置

# DoH 对应的 Nginx 所有配置
server
{
    listen 80;
    #QUIC 支持,如果你的 nginx 不支持,请务必禁用此项。
    listen 443 quic; 
    listen 443 ssl http2;
    server_name doh.dns.com;
    index index.php index.html index.htm default.php default.htm default.html;
    root /home/doh.dns.com;
    
    #SSL-START SSL相关配置
    ssl_certificate    /home/cert/ecc.dns.com.crt;
    ssl_certificate_key    /home/cert/ecc.dns.com.key;
    ssl_protocols TLSv1.2 TLSv1.3;
    ssl_ciphers [TLS13+AESGCM+AES128|TLS13+CHACHA20]:TLS13+AESGCM+AES256:[EECDH+ECDSA+AESGCM+AES128|EECDH+ECDSA+CHACHA20]:EECDH+ECDSA+AESGCM+AES256:EECDH+ECDSA+AES128+SHA:EECDH+ECDSA+AES256+SHA:[EECDH+aRSA+AESGCM+AES128|EECDH+aRSA+CHACHA20]:EECDH+aRSA+AESGCM+AES256:EECDH+aRSA+AES128+SHA:EECDH+aRSA+AES256+SHA;
    ssl_prefer_server_ciphers on;
    ssl_early_data on;
    ssl_ecdh_curve X25519:P-256:P-384;
    ssl_session_cache shared:SSL:10m;
    ssl_session_timeout 10m;
    error_page 497  https://$host$request_uri;

    # OCSP Stapling
    ssl_stapling on;
    ssl_stapling_verify on;
    ssl_stapling_file /home/others/ocsp/dns.com.ocsp.resp;

    #SSL-END
    
    # Add Alt-Svc header to negotiate HTTP/3.
    add_header alt-svc 'h3-27=":443"; ma=86400, h3-25=":443"; ma=86400, h3-24=":443"; ma=86400, h3-23=":443"; ma=86400';
    
   location / {
        proxy_redirect off;
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection "upgrade";
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_pass http://127.0.0.1:3000; # 此处为程序运行时指定的端口
    }

    location ~ .*.(gif|jpg|jpeg|png|bmp|swf|css|js)$ {
        proxy_pass http://127.0.0.1:3000;
        proxy_set_header Host  $host;
        proxy_set_header X-Forwarded-For $remote_addr;
    }

    location /dns-query { # 这里的 Path 可以是任意
        proxy_http_version 1.1;
        proxy_set_header Host $http_host;
        proxy_buffering off;
        proxy_redirect off;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
	    # 此处为 https 而不是 http; 端口为上述填写的 HTTPS 端口
        proxy_pass https://doh.dns.com:2345/dns-query; # 这里的 Path 必须为 dns-query
    }
  
    #REWRITE-START URL重写规则引用,修改后将导致面板设置的伪静态规则失效
    include /www/server/panel/vhost/rewrite/doh.dns.com.conf;
    #REWRITE-END
    
}

重启 Nginx

systemctl restart nginx

配置为系统启动项

Ctrl+C 先结束进程,将 AdGuardHome 整个目录移动到 /etc

cp -r /root/AdGuardHome /etc/AdGuardHome

创建配置文件

vim /etc/systemd/system/AdGuard.service

填写以下内容

[Unit]
Description=AdGuard Home
After=network.target
Wants=network.target

[Service]
User=root
Group=root
WorkingDirectory=/etc/AdGuardHome
# 此处指定配置文件和工作目录
ExecStart=/etc/AdGuardHome/AdGuardHome -c /etc/AdGuardHome/AdGuardHome.yaml -w /etc/AdGuardHome
Restart=on-failure
RestartSec=30s
LimitCORE=infinity
LimitNOFILE=1000000
LimitNPROC=1000000

[Install]
WantedBy=multi-user.target

启动参数

# 开启开机自启
systemctl enable AdGuardHome
# 启动服务
systemctl start AdGuardHome
# 重启服务
systemctl restart AdGuardHome
# 停止服务
systemctl stop AdGuardHome
# 查看服务状态,查看长日志请在命令后加参数 -l
systemctl status AdGuardHome

测试

浏览器访问 https://doh.dns.com/ 为 AdGuard Home Web 界面且正常登陆状态即为正常。

验证

需要用到另一个工具

# 下载解压
wget https://github.com/ameshkov/dnslookup/releases/download/v1.3.0/dnslookup-linux-amd64-v1.3.0.tar.gz
tar zxf dnslookup-linux-amd64-v1.3.0.tar.gz && cd linux-amd64

DNS over TLS

./dnslookup www.baidu.com tls://doh.dns.com
dnslookup v1.3.0
dnslookup result:
;; opcode: QUERY, status: NOERROR, id: 49410
;; flags: qr rd ra; QUERY: 1, ANSWER: 3, AUTHORITY: 0, ADDITIONAL: 0

;; QUESTION SECTION:
;www.baidu.com.	IN	 A

;; ANSWER SECTION:
www.baidu.com.	959	IN	CNAME	www.a.shifen.com.
www.a.shifen.com.	59	IN	CNAME	www.wshifen.com.
www.wshifen.com.	59	IN	A	103.235.46.39

DNS over HTTPS

./dnslookup www.google.com https://doh.dns.com/dns-query
dnslookup v1.3.0
dnslookup result:
;; opcode: QUERY, status: NOERROR, id: 19994
;; flags: qr rd ra; QUERY: 1, ANSWER: 1, AUTHORITY: 0, ADDITIONAL: 0

;; QUESTION SECTION:
;www.google.com.	IN	 A

;; ANSWER SECTION:
www.google.com.	241	IN	A	216.58.197.100


本文作者:佩佩
原文链接:https://www.nange.cn/dns-over-https-for-adguard.html
版权声明:若无特殊注明,文章均为本站【楠格】原创,并以《知识共享署名-相同方式共享 4.0 国际 (CC BY-NC-SA 4.0)》协议进行许可,转载时请以超链接形式标明文章原始出处和作者信息,否则谢绝转载!

3 条回应
  1. WM Chrome 97.0.4692.71 Windows 102022-1-15 · 16:01

    按照教程 已经搭建。 现在adguardhome升级到v0.107.2后 ad页面客服端只显示VPS的IP 没有显示实际的客服端IP。google了几天 不知道怎么处理。希望博主能给个解决的办法 谢谢

  2. 忘我思考 Chrome 91.0.4472.88 Android 10 | DT2002C 2021-6-20 · 23:03

    简单一点他不香吗

  3. aliquanquan Chrome 84.0.4147.105 Windows 102020-8-3 · 20:30

    教程还行,emmm,有关nginx的配置能不能说再清楚点,nginx需要另外申请ssl证书并且加入配置文件吗?