/
doh_client.go
130 lines (110 loc) · 2.98 KB
/
doh_client.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
package doh
import (
"bytes"
"encoding/base64"
"encoding/json"
"errors"
"io/ioutil"
"net/http"
"github.com/miekg/dns"
"github.com/projectdiscovery/retryablehttp-go"
)
type Client struct {
DefaultResolver Resolver
httpClient *retryablehttp.Client
}
func NewWithOptions(options Options) *Client {
return &Client{DefaultResolver: options.DefaultResolver, httpClient: options.HttpClient}
}
func New() *Client {
return NewWithOptions(Options{DefaultResolver: Cloudflare, HttpClient: retryablehttp.NewClient(retryablehttp.DefaultOptionsSingle)})
}
func (c *Client) Query(name string, question QuestionType) (*Response, error) {
return c.QueryWithResolver(c.DefaultResolver, name, question)
}
func (c *Client) QueryWithResolver(r Resolver, name string, question QuestionType) (*Response, error) {
return c.QueryWithJsonAPI(r, name, question)
}
func (c *Client) QueryWithJsonAPI(r Resolver, name string, question QuestionType) (*Response, error) {
req, err := retryablehttp.NewRequest(http.MethodGet, r.URL, nil)
if err != nil {
return nil, err
}
req.Header.Set("Accept", "application/dns-json")
q := req.URL.Query()
q.Add("name", name)
q.Add("type", question.ToString())
req.URL.RawQuery = q.Encode()
resp, err := c.httpClient.Do(req)
if err != nil {
return nil, err
}
defer resp.Body.Close()
if resp.Body == nil {
return nil, errors.New("empty response body")
}
var response Response
err = json.NewDecoder(resp.Body).Decode(&response)
if err != nil {
return nil, err
}
return &response, nil
}
func (c *Client) QueryWithDOH(method Method, r Resolver, name string, question uint16) (*dns.Msg, error) {
msg := &dns.Msg{}
msg.Id = 0
msg.Question = make([]dns.Question, 1)
msg.Question[0] = dns.Question{
Name: dns.Fqdn(name),
Qtype: question,
Qclass: dns.ClassINET,
}
return c.QueryWithDOHMsg(method, r, msg)
}
func (c *Client) QueryWithDOHMsg(method Method, r Resolver, msg *dns.Msg) (*dns.Msg, error) {
packedMsg, err := msg.Pack()
if err != nil {
return nil, err
}
var body []byte
var dnsParam string
switch method {
case MethodPost:
dnsParam = ""
body = packedMsg
case MethodGet:
dnsParam = base64.RawURLEncoding.EncodeToString(packedMsg)
body = nil
default:
return nil, errors.New("unsupported method")
}
req, err := retryablehttp.NewRequest(string(method), r.URL, bytes.NewReader(body))
if err != nil {
return nil, err
}
req.Header.Set("Accept", "application/dns-message")
if dnsParam != "" {
q := req.URL.Query()
q.Add("dns", dnsParam)
req.URL.RawQuery = q.Encode()
} else if len(body) > 0 {
req.Header.Set("Content-Type", "application/dns-message")
}
resp, err := c.httpClient.Do(req)
if err != nil {
return nil, err
}
defer resp.Body.Close()
if resp.Body == nil {
return nil, errors.New("empty response body")
}
respBodyBytes, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, err
}
respMsg := &dns.Msg{}
if err := respMsg.Unpack(respBodyBytes); err != nil {
return nil, err
}
return respMsg, nil
}