Cloudflare 遵循 RFC 1035 ↗ 中定义的 DNS 线路格式。
要使用 DNS 线路格式发送查询,请设置 accept: application/dns-message 标头,如果使用 POST 发起请求以指示查询的媒体类型,则设置 content-type: application/dns-message 标头。
使用 DNS 线路格式的查询可以通过 POST 或 GET 发送。
使用 POST 发起请求时,DNS 查询包含在 HTTP 请求的消息体中,且 MIME 类型(见下文)包含在 Content-Type 请求标头中。Cloudflare 将使用客户端发送的 HTTP 请求消息体,因此消息体不应进行编码。
以下是一个请求示例。针对 www.example.com 的同一个 DNS 查询,使用 POST 方法将如下所示:
:method = POST
:scheme = https
:authority = cloudflare-dns.com
:path = /dns-query
accept = application/dns-message
content-type = application/dns-message
content-length = 33
<以下十六进制编码表示的 33 字节>
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并以线路格式返回答案:
:status = 200
content-type = application/dns-message
content-length = 64
cache-control = max-age=128
<以下十六进制编码表示的 64 字节>
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 01 00
01 03 77 77 77 07 65 78 61 6d 70 6c 65 03 63 6f
6d 00 00 01 00 01 00 00 00 80 00 04 C0 00 02 01要使用 cURL 尝试此操作,请编写:
echo -n 'q80BAAABAAAAAAAAA3d3dwdleGFtcGxlA2NvbQAAAQAB' | base64 --decode | curl --header 'content-type: application/dns-message' --data-binary @- https://cloudflare-dns.com/dns-query --output - | hexdump使用 GET 发起请求时,DNS 查询将被编码到 URL 中。accept 标头可用于指示 MIME 类型(默认为:application/dns-message)。
请求示例:
curl --header 'accept: application/dns-message' --verbose 'https://cloudflare-dns.com/dns-query?dns=q80BAAABAAAAAAAAA3d3dwdleGFtcGxlA2NvbQAAAQAB' | hexdump* Using HTTP2, server supports multi-use
* Connection state changed (HTTP/2 confirmed)
* Copying HTTP/2 data in stream buffer to connection buffer after upgrade: len=0
* Using Stream ID: 1 (easy handle 0x7f968700a400)
GET /dns-query?dns=q80BAAABAAAAAAAAA3d3dwdleGFtcGxlA2NvbQAAAQAB HTTP/2
Host: cloudflare-dns.com
User-Agent: curl/7.54.0
accept: application/dns-message
* Connection state changed (MAX_CONCURRENT_STREAMS updated)!
HTTP/2 200
date: Fri, 23 Mar 2018 05:14:02 GMT
content-type: application/dns-message
content-length: 49
cache-control: max-age=0
set-cookie: \__cfduid=dd1fb65f0185fadf50bbb6cd14ecbc5b01521782042; expires=Sat, 23-Mar-19 05:14:02 GMT; path=/; domain=.cloudflare.com; HttpOnly
server: cloudflare-nginx
cf-ray: 3ffe69838a418c4c-SFO-DOG
{ [49 bytes data]
100 49 100 49 0 0 493 0 --:--:-- --:--:-- --:--:-- 494
* Connection #0 to host cloudflare-dns.com left intact
0000000 ab cd 81 80 00 01 00 01 00 00 00 00 03 77 77 77
0000010 07 65 78 61 6d 70 6c 65 03 63 6f 6d 00 00 01 00
0000020 01 c0 0c 00 01 00 01 00 00 0a 8b 00 04 5d b8 d8
0000030 22
0000031