DoH简介及CentOS 7 下DoH中转服务器搭建
1、DoH 简介
DoH全称为DNS-over-HTTP,顾名思义,其主要的使用是使用https协议来进行DNS查询请求。
正常情况下,DNS的请求过程是通过计算机上的DNS客户端程序来完成DNS的查询请求,可以参考《dnsmasq简介及基于域名的流量管理》这篇文章的第一节。
默认情况下DNS查询使用的协议是UDP协议,而UDP协议存在安全性上的缺陷,基于UDP协议发起的DNS查询请求有可能会遭到DNS劫持,攻击者会将用户想要访问的域名解析到别的IP地址,或者在用户用浏览器访问网站时,会被插入一些广告。而DoH就是为了解决这个问题而产生的。
DoH 利用HTTP协议的GET命令发出经由JSON等编码的DNS解析请求。请求到达DoH服务器后,由DoH服务器解码HTTPS并发送DNS请求,DNS请求结果返回到DoH服务器上后,再由将其打包成HTTPS返回给客户端,这就保证了客户端发起的dns请求不会被攻击者拿到。
较于传统的DNS协议,此处的HTTP协议通信处于具有加密作用的SSL/TLS协议(两者统称作HTTPS)的保护之下。但是,由于其基于HTTPS,而HTTPS本身需要经由多次数据来回传递才能完成协议初始化,其域名解析耗时较原标准DNS协议会显著增加。
2、DoH 中转服务器搭建
2.1 DoH 中转服务器的查询流程
本地PC -> DoH中转服务器 -> DNS服务器 -> DoH中转服务器 -> 本地PC
以上流程中的一个箭头代表一个HTTPS加密链接,使得该过程中的所有流量皆受到强加密。
DoH 中转服务器与DNS服务器之间的通信,有两种方式:
(1)普通DNS请求,这种情况下的DNS服务器可以是任意一台普通DNS服务器。
(2)加密DNS请求,这种情况下需要在DoH服务器上安装dnscrypt-proxy,向支持DoH请求的DNS服务器发送加密DNS请求。
搭建DoH 中转服务器需要Nginx配合使用,其中,不使用dnscrypt-proxy的流程为:
使用dnscrypt-proxy的流程为:
2.2 安装并配置 dnscrypt-proxy
2.2.1 安装编译环境
由于 dnscrypt-proxy 与 dns-over-https 是基于go语言编写的,因此在编译前需要先安装golang编译环境,可以通过以下命令直接安装:
1 | $ sudo yum install golang |
也可以参考官网 https://golang.org 通过源码安装。
2.2.2 安装 dnscrypt-proxy
在CentOS 7 下面,该程序可以直接通过yum命令安装:
1 | $ sudo yum install dnscrypt-proxy |
但用该方法安装的版本为较旧的版本,推荐下面的安装方法:
访问 https://github.com/DNSCrypt/dnscrypt-proxy/releases/latest,选择并下载最新的版本并解压,得到如下内容:
1 2 3 4 | $ ls dnscrypt-proxy example-dnscrypt-proxy.toml example-whitelist.txt example-blacklist.txt example-forwarding-rules.txt LICENSE example-cloaking-rules.txt example-ip-blacklist.txt localhost.pem |
将解压出的二进制程序 dnscrypt-proxy 拷贝到 /usr/sbin/ 目录。将 example-dnscrypt-proxy.toml 改名为dnscrypt-proxy.toml并拷贝到/etc/目录,并修改文件中的server_name 字段为如下内容:
1 | server_names = ['cloudflare'] |
或
1 | server_names = ['google'] |
2.2.3 配置 dnscrypt-proxy 服务
创建 dnscrypt-proxy.service 文件,输入如下内容:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | [Unit] Description=DNSCrypt-Proxy Server Documentation=https://github.com/DNSCrypt/dnscrypt-proxy After=network.target [Service] AmbientCapabilities=CAP_NET_BIND_SERVICE ExecStart=/usr/sbin/dnscrypt-proxy -config /etc/dnscrypt-proxy.toml LimitNOFILE=1048576 Restart=always RestartSec=3 Type=simple User=nobody [Install] WantedBy=multi-user.target |
之后执行以下命令以安装dnscrypt-proxy 服务:
1 2 | sudo install -Dm0644 dnscrypt-proxy.service /usr/lib/systemd/system/dnscrypt-proxy.service sudo systemctl daemon-reload || true |
2.2.4 配置 dnscrypt-proxy对应的NetworkManager 服务
创建文件 dnscrypt-proxy-nm 文件,并输入以下内容:
1 2 3 4 5 6 7 8 9 10 11 12 13 | #!/bin/bash case "$2" in up) /usr/bin/systemctl is-active dnscrypt-proxy.service > /dev/null && /usr/bin/systemctl restart dnscrypt-proxy.service ;; down) /usr/bin/systemctl is-active dnscrypt-proxy.service > /dev/null && /usr/bin/systemctl restart dnscrypt-proxy.service ;; *) exit 0 ;; esac |
之后为其添加可执行权限并安装:
1 2 | $ chmod +x dnscrypt-proxy-nm $ sudo install -Dm0755 dnscrypt-proxy-nm /etc/NetworkManager/dispatcher.d/dnscrypt-proxy |
2.2 安装 dns-over-https 程序
为解决传统DNS的各种弊端,先后诞生过多种解决方案,如DNSSEC、DNSCrypt、DNS over TLS、DNS over HTTPS等。其中DNS over HTTPS是比较新的一种解决方案,可参考RFC8484标准。
目前比较常用的DoH 源码是https://github.com/m13253/dns-over-https,该源码由go语言实现,遵循谷歌DoH协议(https://developers.google.com/speed/public-dns/docs/doh)和RFC8484(https://tools.ietf.org/html/rfc8484)标准。
之后,编译并安装 dns-over-https
1 2 3 4 | $ git clone https://github.com/m13253/dns-over-https.git $ cd dns-over-https $ make PREFIX=/usr $ sudo make install |
设置开机启动
1 2 | $ sudo systemctl start doh-server.service $ sudo systemctl enable doh-server.service |
默认情况下,dns-over-https 的监听地址为 127.0.0.1:8053。
如果使用dnscrypt-proxy,需要对dns-over-https进行配置。打开 /etc/dns-over-https/doh-server.conf文件,修改upstream 字段,将监听地址修改为dnscrypt-proxy的监听地址,默认为127.0.0.53:53
2.3 配置Nginx
打开网站对应的配置文件 /etc/nginx/conf.d/www.example.com.conf,添加如下内容:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | upstream dns-backend { server 127.0.0.1:8053; } server { …… location /dns-query { proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_set_header Host $http_host; proxy_set_header X-NginX-Proxy true; proxy_http_version 1.1; proxy_set_header Upgrade $http_upgrade; proxy_redirect off; proxy_set_header X-Forwarded-Proto $scheme; proxy_read_timeout 86400; proxy_pass http://dns-backend/dns-query ; } ...... } |
其中location字段是主要的配置内容,该字段的最后一行的proxy_pass字段就设置为doh-server的监听地址。
之后,先检查一下配置是否正确,如果没问题则重启nginx服务:
1 2 | $ sudo nginx -t $ sudo systemctl restart nginx |
3、提供DoH功能的公共DNS
https://cloudflare-dns.com/dns-query
https://1.1.1.1/dns-query
https://1.0.0.1/dns-query
https://dns.google/dns-query
https://dns.quad9.net/dns-query
4、在Firefox中试用DoH
可参考 https://support.mozilla.org/zh-CN/kb/%E7%81%AB%E7%8B%90%E5%9F%BA%E4%BA%8Ehttps%20%E7%9A%84%20dns
5、参考
https://zh.wikipedia.org/wiki/DNS_over_HTTPS
https://mesalab.cn/download?id=888&url=f%2Farticle%2Fdetail%3Fid%3D186
https://github.com/curl/curl/wiki/DNS-over-HTTPS
https://github.com/DNSCrypt/dnscrypt-proxy
https://github.com/m13253/dns-over-https
https://blog.csdn.net/qq_39378221/article/details/103245108
https://blog.csdn.net/p5deyt322jacs/article/details/79892869
https://kb.adguard.com/en/general/dns-providers
————————————————————
原创文章,转载请注明: 转载自孙希栋的博客
本文链接地址: 《DoH简介及CentOS 7 下DoH中转服务器搭建》