/
doh.go
97 lines (85 loc) · 2.1 KB
/
doh.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
package dnsclient
import (
"context"
"crypto/tls"
"encoding/base64"
"io/ioutil"
"net"
"time"
"net/http"
"net/url"
"github.com/miekg/dns"
"golang.org/x/net/proxy"
)
// DoHClient encapsulates all functions and attributes for a DoH client
type DoHClient struct {
URL url.URL
proxy string
dialer proxy.Dialer
isSkipVerify bool
req *http.Request
httpclient *http.Client
}
// NewDoHClient creates a new DoH client
func NewDoHClient(server url.URL, SkipVerify bool, proxy string) (Client, error) {
// Select TLS protocols for DoH
c := DoHClient{
URL: server,
proxy: proxy,
isSkipVerify: SkipVerify,
}
// get the proxy dialer
var err error
c.dialer, err = GetDialer(proxy)
if err != nil {
return nil, err
}
c.httpclient = &http.Client{
Transport: &http.Transport{
DialContext: func(ctx context.Context, network string, addr string) (net.Conn, error) {
return c.dialer.Dial(network, addr)
},
TLSClientConfig: &tls.Config{
InsecureSkipVerify: SkipVerify,
},
},
}
c.req, err = http.NewRequest(http.MethodGet, c.URL.String(), nil)
return &c, err // nil error
}
// Query performs a DoH query
func (c DoHClient) Query(ctx context.Context, msg *dns.Msg) ([]dns.RR, time.Duration, error) {
// get the time
start := time.Now()
dohbytes, err := msg.Pack()
if err != nil {
return []dns.RR{}, time.Since(start), err
}
// convert to base64
dohbase64 := base64.RawURLEncoding.EncodeToString(dohbytes)
q := c.req.URL.Query()
q.Set("dns", dohbase64)
c.req.URL.RawQuery = q.Encode()
c.req = c.req.WithContext(ctx)
res, err := c.httpclient.Do(c.req)
if err != nil {
return []dns.RR{}, time.Since(start), err
}
body, _ := ioutil.ReadAll(res.Body)
res.Body.Close()
// read the body
// parse body as a dns message
var msg2 dns.Msg
err = msg2.Unpack(body)
return msg2.Answer, time.Since(start), err
}
func (c *DoHClient) Close() error {
c.req.Close = true
return nil
}
func (c *DoHClient) Reconnect() error {
newClient, err := NewDoHClient(c.URL, c.isSkipVerify, c.proxy)
c2 := newClient.(*DoHClient)
c.req = c2.req
return err
}