本文主要是介绍golang使用DoH解析域名,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
按照RFC 8484 规范,DoH服务器支持GET或POST两种方式。
当使用GET方法,唯一的变量"dns"被赋值为base64url编码的DNS请求内容。
These examples use a DoH service with a URI Template of"https://dnsserver.example.net/dns-query{?dns}" to resolve IN Arecords.The requests are represented as bodies with media type "application/dns-message".The first example request uses GET to request "www.example.com".:method = GET:scheme = https:authority = dnsserver.example.net:path = /dns-query?dns=AAABAAABAAAAAAAAA3d3dwdleGFtcGxlA2NvbQAAAQABaccept = application/dns-message
当使用POST方法,DNS查询消息被包含在HTTP 请求的body中,
header需要包含 Content-Type application/dns-message。
The same DNS query for "www.example.com", using the POST method wouldbe::method = POST:scheme = https:authority = dnsserver.example.net:path = /dns-queryaccept = application/dns-messagecontent-type = application/dns-messagecontent-length = 33<33 bytes represented by the following hex encoding>00 00 01 00 00 01 00 00 00 00 00 00 03 77 77 7707 65 78 61 6d 70 6c 65 03 63 6f 6d 00 00 01 0001
下面使用golang实例演示使用opendns DoH解析域名
package main
import ("bytes""encoding/base64""fmt""github.com/miekg/dns""io""net/http"
)
// https://datatracker.ietf.org/doc/html/rfc8484#section-4.1.1
func main() {rfc8484Get()rfc8484Post()}
func rfc8484Get() {query := dns.Msg{}query.SetQuestion("www.github.com.", dns.TypeA)dsnReq, _ := query.Pack()base64Query := base64.RawURLEncoding.EncodeToString(dsnReq)client := &http.Client{}//GET请求必须具有 ?dns=查询参数,该参数带有采用 Base64Url编码的DNS消息req, err := http.NewRequest("GET", "https://doh.opendns.com/dns-query?dns="+base64Query, nil)if err != nil {fmt.Printf("Send query error, err:%v\n", err)return}req.Header.Add("Accept", "application/dns-message")resp, err := client.Do(req)if err != nil {fmt.Println(err)return}defer resp.Body.Close()bodyBytes, _ := io.ReadAll(resp.Body)response := dns.Msg{}response.Unpack(bodyBytes)fmt.Printf("Dns answer is :%v\n", response.String())
}func rfc8484Post() {query := dns.Msg{}query.SetQuestion("www.github.com.", dns.TypeA)dsnReq, _ := query.Pack()client := &http.Client{}//POST请求正文为二进制DNS消息req, err := http.NewRequest("POST", "https://doh.opendns.com/dns-query", bytes.NewBuffer(dsnReq))if err != nil {fmt.Printf("Send query error, err:%v\n", err)}req.Header.Add("Accept", "application/dns-message")//POST头需要包含 Content-Type application/dns-messagereq.Header.Add("Content-Type", "application/dns-message")resp, err := client.Do(req)if err != nil {fmt.Println(err)return}defer resp.Body.Close()bodyBytes, _ := io.ReadAll(resp.Body)response := dns.Msg{}response.Unpack(bodyBytes)fmt.Printf("Dns answer is :%v\n", response.String())}
这篇关于golang使用DoH解析域名的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!