RFC 8484 定义了 DNS over HTTPS (DoH) 协议,核心思想是将传统的 DNS 查询与响应封装在加密的 HTTPS 流量中进行传输,从而提升隐私性和安全性。

一些名词说明:

  1. DoH服务器:支持此协议的服务器称为“DoH服务器”,以区别于“DNS服务器”,支持此协议的客户端称为“DoH客户端”

DoH,通过名字可以看得出来,这个【客户端】请求 - 【服务器】响应 的过程,是通过HTTP进行的,自然地,这里就会有我们熟知的 GET, POST 等请求响应方式。

请求 (4.1. The HTTP Request)

  1. GET 方式

These examples use a DoH service with a URI Template of “https://dnsserver.example.net/dns-query{?dns}” to resolve IN A records.

可知,使用GET方式的时候,URI 模板为: https://dnsserver.example.net/dns-query{?dns}.DNS 查询消息本身是二进制,不能直接塞进 URL,所以要先做 base64url 编码,然后作为 URL 参数 dns 的值。

:method = GET
:scheme = https
:authority = dnsserver.example.net
:path = /dns-query?dns=AAABAAABAAAAAAAAA3d3dwdleGFtcGxlA2NvbQAAAQAB
accept = application/dns-message

值得注意的是: 请求的 Accept 必须为: application/dns-message

  1. POST 方式

POST 不把 DNS 查询放 URL,而是直接放在 HTTP 请求体里:

POST /dns-query HTTP/1.1
Host: dns.example.net
Content-Type: application/dns-message
Accept: application/dns-message

<这里是原始 DNS 查询二进制>
   <33 bytes represented by the following hex encoding>
   00 00 01 00 00 01 00 00  00 00 00 00 03 77 77 77
   07 65 78 61 6d 70 6c 65  03 63 6f 6d 00 00 01 00
   01

Content-Type: application/dns-message 告诉服务器:请求体里装的是 DNS 消息。

用一下文档中的例子:

基于GET的查询“a.62characterlabel-makes-base64url-distinct-from-standard-base64.example.com”为例,强调base64url的编码字母表不同于常规base64,并且省略了填充: DNS查询以DNS wire格式表示,为94字节,由以下内容表示:

00 00 01 00 00 01 00 00 00 00 00 00 01 61 3e 36 32 63 68 61 72 61 63 74 65 72 6c 61 62 65 6c 2d 6d 61 6b 65 73 2d 62 61 73 65 36 34 75 72 6c 2d 64 69 73 74 69 6e 63 74 2d 66 72 6f 6d 2d 73 74 61 6e 64 61 72 64 2d 62 61 73 65 36 34 07 65 78 61 6d 70 6c 65 03 63 6f 6d 00 00 01 00 01

那么请求就长这样:

:method = GET
  :scheme = https
  :authority = dnsserver.example.net
  :path = /dns-query? (no space or Carriage Return (CR))
         dns=AAABAAABAAAAAAAAAWE-NjJjaGFyYWN0ZXJsYWJl (no space or CR)
          bC1tYWtlcy1iYXNlNjR1cmwtZGlzdGluY3QtZnJvbS1z (no space or CR)
          dGFuZGFyZC1iYXNlNjQHZXhhbXBsZQNjb20AAAEAAQ
 accept = application/dns-message

响应 (4.2 The HTTP Response)

客户端有了请求,服务器必有响应

The only response type defined in this document is application/dns-message

响应类型:目前只定义 application/dns-message

也就是:

content-type: application/dns-message

响应体里装的是 DNS wire format 的 DNS 响应报文。

Each DNS request-response pair is mapped to one HTTP exchange. The responses may be processed and transported in any order using HTTP’s multi-streaming functionality (see Section 5 of [RFC7540]).

也就是 一个DNS查询请求+响应 = 一次完整的HTTP交换

DNS和HTTP错误处理

DNS 查询失败,HTTP 仍然可以是 2xx

DNS response codes indicate either success or failure for the DNS query. A successful HTTP response with a 2xx status code (see Section 6.3 of [RFC7231]) is used for any valid DNS response, regardless of the DNS response code. For example, a successful 2xx HTTP status code is used even with a DNS message whose DNS response code indicates failure, such as SERVFAIL or NXDOMAIN.

也就是说:

  • DNS 返回 NOERROR:HTTP 用 200;
  • DNS 返回 NXDOMAIN:HTTP 仍然可以用 200;
  • DNS 返回 SERVFAIL:HTTP 仍然可以用 200。

因为从 HTTP 角度看,DoH 服务器成功处理了这个 HTTP 请求,并且返回了一个合法的 DNS 响应报文。DNS 层面的失败信息在 DNS 响应报文的 RCODE 里,不在 HTTP 状态码里。

所以不能看到 HTTP 200 就以为域名一定解析成功;还要看 DNS 响应里的 RCODE。

非 2xx HTTP 响应不包含 DNS 答复

HTTP responses with non-successful HTTP status codes do not contain replies to the original DNS question.

意思是:

如果 HTTP 状态码不是 2xx,比如 401、415、406;

那么这个 HTTP 响应体里不包含对原 DNS 查询的 DNS 答复。

这时 DoH 客户端要像普通 HTTP 客户端一样处理 HTTP 错误。

原文举的例子:

  • 401 授权失败:可能用同一个 DoH 服务器重试;
  • 415 不支持的媒体类型:可能换一个 DoH 服务器重试;
  • 406 服务器无法生成适合客户端的表示:也可能换服务器或调整请求。

注意:这里说的是“可能”,不是强制重试策略。

响应示例

This is an example response for a query for the IN AAAA records for “www.example.com” with recursion turned on. The response bears one answer record with an address of 2001:db8:abcd:12:1:2:3:4 and a TTL of 3709 seconds.

HTTP/2 响应头是:

:status = 200
content-type = application/dns-message
content-length = 61
cache-control = max-age=3709

含义:

:status = 200:HTTP 成功;
content-type = application/dns-message:响应体是 DNS wire 格式;
content-length = 61:DNS 响应报文 61 字节;
cache-control = max-age=3709:HTTP 缓存最多存 3709 秒,对应 DNS 记录 TTL。

响应体十六进制:

// 关于DNS报文 我想后面再看看 不然容易混的感觉

00 00 81 80 00 01 00 01 00 00 00 00
03 77 77 77 07 65 78 61 6d 70 6c 65 03 63 6f 6d 00
00 1c 00 01
c0 0c 00 1c 00 01 00 00 0e 7d 00 10
20 01 0d b8 ab cd 00 12 00 01 00 02 00 03 00 04

总结的来说 :

  1. DoH 响应目前标准格式是 application/dns-message,响应体是 DNS wire 报文。
  2. 一个 DNS 查询-响应对应一次 HTTP 交换。
  3. DNS 的失败不通过 HTTP 状态码表达:
    • NXDOMAIN、SERVFAIL 等仍然可以返回 HTTP 200;
    • 客户端必须解析 DNS 响应里的 RCODE。
  4. HTTP 非 2xx 响应不包含 DNS 答复,客户端按 HTTP 语义处理,可能重试或换服务器。
  5. 示例中 cache-control: max-age=3709 和 DNS 答案 TTL 3709 对应,说明 HTTP 缓存生命周期要和 DNS TTL 保持一致。

必须使用https,纯http 不行

HTTP/2[RFC7540]是用于DoH的HTTP的最低推荐版本。

The messages in classic UDP-based DNS [RFC1035] are inherently unordered and have low overhead. A competitive HTTP transport needs to support reordering, parallelism, priority, and header compression to achieve similar performance. Those features were introduced to HTTP in HTTP/2 [RFC7540]. Earlier versions of HTTP are capable of conveying the semantic requirements of DoH but may result in very poor performance.

经典的基于UDP的DNS[RFC1035]中的消息本质上是无序的,开销很低。有竞争力的HTTP传输需要支持重新排序、并行性、优先级和头压缩,以实现类似的性能。这些特性是在HTTP/2[RFC7540]中引入HTTP的。早期版本的HTTP能够传达DoH的语义需求,但可能会导致非常差的性能。


至此,我对于DoH协议的实现有了基本的认识。

链接:

  1. DNS Queries over HTTPS (DoH)
  2. RFC 8484: DNS Queries over HTTPS (DoH)

注:本文未使用Ai生成。