自建DoH服务

文章目录

1. 前言

DoH(DNS over HTTPS),顾名思义,使用HTTPS协议执行DNS查询,除了最常用的UDP外,还有DoT(DNS over TLS),DNS over HTTP(服务提供商自定义)等方案,对比如下:

协议 标准 描述
DNS over HTTPS RFC8484 使用TLS加密的HTTP/2执行DNS查询
DNS over TLS RFC7858 使用TLS加密的TCP执行DNS查询
DNS over HTTP 服务提供商自定义 使用自定义加密的HTTP/1.1执行DNS查询

移动端的DNS优化已经有很多实践,最常见的是DNS over HTTP,通过加密的HTTP请求规避运营商对DNS的UDP包劫持,从而优化App访问服务器的延迟。但这个方案并没有形成统一的标准,通常需要内嵌DNS服务提供商的SDK,通过访问固定的BGP或任播IP获取DNS响应。

大概是意识到DNS在移动互联网中的扮演越来越重要的角色,在DoT和DoH的规范相继推出后,许多DNS服务提供商都跟进了部署,国内的阿里云、DNSPod,国外的谷歌、Cloudflare等目前已经推出了免费的DoT和DoH服务。

客户端方面,常用的Chrome、FireFox已经支持了自定义DoH服务器,macOS、iOS也可通过配置文件设置系统范围的默认DoH服务器。

笔者也正好有一个自定义DNS的需求:

  1. 需要针对一些域名的DNS查询仅返回IPv4记录
  2. 使用的某某路由器系统的自定义DNS服务仅支持设置UDP和DoH
  3. UDP模式默认使用53端口,不可修改,UDP包容易遭受干扰
  4. DoH可自定义域名、端口且使用HTTP2作为传输协议,稳定性更强

综上,只有自建DoH服务了,于是就有了下面的折腾,最后测试时发现这个傻瓜路由器系统只支持一些特定的DoH服务商如阿里云DNS、DNSPod等,不支持自建的DoH服务。

2. 部署方案

DoH本质上就是一个HTTP请求,只是目前协议定义要求启用TLS与HTTP/2。最初没有跑通coredns的DoH时,使用了nginx作为前端转发DoH请求到doh-server,然后doh-server使用本地的coredns服务作为上游。

最近再仔细研究了下文档,发现coredns已经支持了DoH服务,可直接对外暴露服务,或者通过nginx转发来复用已经部署好的web服务。

2.1 nginx + doh-server + coredns

https://github.com/m13253/dns-over-https 是一个提供DNS over HTTP的服务,需要一个web前端和一个DNS后端,可用的docker镜像地址为:satishweb/doh-server,使用doh-server时,DNS请求流转如下:

1HTTP Service -> doh-server -> DNS Server

RFC8484中指定使用/dns-query路径作为默认查询路径,因此只需要将该路径前缀的请求转发到doh-server即可,如下:

nginx配置(已配置好TLS与HTTP2)

 1server {
 2    listen 443 ssl http2 fastopen=256 reuseport;
 3    listen [::]:443 ssl http2 fastopen=256 reuseport;
 4    server_name doh.wbuntu.com
 5    ...
 6    location /dns-query {
 7    proxy_redirect off;
 8    proxy_http_version 1.1;
 9    proxy_set_header Host $http_host;
10    # show real IP
11    proxy_set_header X-Real-IP $remote_addr;
12    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
13    proxy_pass http://127.0.0.1:8053;
14  }
15}

doh-server

使用hostNetwork模式启动服务,监听8053端口

1docker run -d --restart unless-stopped --network host --name doh-server \
2  -e UPSTREAM_DNS_SERVER="udp:127.0.0.1:53" \
3  -e DOH_HTTP_PREFIX="/dns-query" \
4  -e DOH_SERVER_LISTEN="127.0.0.1:8053" \
5  -e DOH_SERVER_TIMEOUT="10" \
6  -e DOH_SERVER_TRIES="3" \
7  -e DOH_SERVER_VERBOSE="true" \
8  satishweb/doh-server

coredns

coredns配置文件如下

 1➜  ~ tree /etc/coredns/
 2/etc/coredns/
 3└── Corefile
 4
 50 directories, 1 files
 6➜  cat /etc/coredns/Corefile
 7.:53 {
 8    bind 127.0.0.1
 9    forward . 1.1.1.1 1.0.0.1
10    log
11    errors
12    cache
13}

使用hostNetwork模式启动服务,监听53端口

1docker run -d --restart unless-stopped --network host --name coredns \
2  -v /etc/coredns:/etc/coredns \
3  coredns/coredns \
4  -conf /etc/coredns/Corefile

服务启动后,我们可以得到一个自定义的DoH服务:https://doh.wbuntu.com/dns-query

2.2 coredns

目前coredns支持作为DoH服务端,不支持连接上游DoH服务器,上游服务器可使用UDP和DoT。

直接对外暴露服务需要使用有效的TLS证书,coredns配置文件及证书位置如下:

 1➜  ~ tree /etc/coredns/
 2/etc/coredns/
 3├── Corefile
 4├── tls.crt
 5└── tls.key
 6
 70 directories, 3 files
 8➜  cat /etc/coredns/Corefile
 9https://.:443 {
10    tls /etc/coredns/tls.crt /etc/coredns/tls.key
11    bind 0.0.0.0
12    forward . 1.1.1.1 1.0.0.1
13    log
14    errors
15    cache
16}

使用hostNetwork模式启动服务,监听443端口

1docker run -d --restart unless-stopped --network host --name coredns \
2  -v /etc/coredns:/etc/coredns \
3  coredns/coredns \
4  -conf /etc/coredns/Corefile

服务启动后,我们可以得到一个自定义的DoH服务:https://doh.wbuntu.com/dns-query

2.3 nginx + coredns

直接暴露coredns服务到公网需要占用端口,coredns在未配置TLS证书时,可使用nginx作为前端来复用web服务,如下:

nginx配置(已配置好TLS与HTTP2)

 1server {
 2    listen 443 ssl http2 fastopen=256 reuseport;
 3    listen [::]:443 ssl http2 fastopen=256 reuseport;
 4    server_name doh.wbuntu.com
 5    ...
 6    location /dns-query {
 7    proxy_redirect off;
 8    proxy_http_version 1.1;
 9    proxy_set_header Host $http_host;
10    # show real IP
11    proxy_set_header X-Real-IP $remote_addr;
12    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
13    proxy_pass http://127.0.0.1:8053;
14  }
15}

coredns

coredns配置文件如下

 1➜  ~ tree /etc/coredns/
 2/etc/coredns/
 3└── Corefile
 4
 50 directories, 1 files
 6➜  cat /etc/coredns/Corefile
 7https://.:8053 {
 8    bind 127.0.0.1
 9    forward . 1.1.1.1 1.0.0.1
10    log
11    errors
12    cache
13}

使用hostNetwork模式启动服务,监听8053端口

1docker run -d --restart unless-stopped --network host --name coredns \
2  -v /etc/coredns:/etc/coredns \
3  coredns/coredns \
4  -conf /etc/coredns/Corefile

服务启动后,我们可以得到一个自定义的DoH服务:https://doh.wbuntu.com/dns-query

3. 测试

使用谷歌浏览器配置DoH服务:Settings -> Secutiry and Privacy -> Secutiry -> Advanced -> Use secure DNS

使用Go代码测试:github.com/wbuntu/example/blob/main/2021/doh/main.go

 1package main
 2
 3import (
 4        "encoding/base64"
 5        "fmt"
 6        "github.com/miekg/dns"
 7        "io/ioutil"
 8        "net/http"
 9        "os"
10)
11
12func main() {
13       query := dns.Msg{}
14       query.SetQuestion("www.google.com.", dns.TypeA)
15       msg, _ := query.Pack()
16       b64 := base64.RawURLEncoding.EncodeToString(msg)
17       resp, err := http.Get("https://doh.wbuntu.com/dns-query?dns=" + b64)
18       if err != nil {
19            fmt.Printf("Send query error, err:%v\n", err)
20            os.Exit(1)
21       }
22       defer resp.Body.Close()
23       bodyBytes, _ := ioutil.ReadAll(resp.Body)
24       response := dns.Msg{}
25       response.Unpack(bodyBytes)
26       fmt.Printf("Dns answer is :%v\n", response.String())
27}