-
Notifications
You must be signed in to change notification settings - Fork 244
/
upstream_doh.go
159 lines (134 loc) · 4.46 KB
/
upstream_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
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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
package upstream
import (
"encoding/base64"
"fmt"
"io/ioutil"
"net/http"
"os"
"sync"
"time"
"github.com/joomcode/errorx"
"github.com/miekg/dns"
"golang.org/x/net/http2"
)
// dnsOverHTTPS represents DNS-over-HTTPS upstream.
type dnsOverHTTPS struct {
boot *bootstrapper
// The Client's Transport typically has internal state (cached TCP
// connections), so Clients should be reused instead of created as
// needed. Clients are safe for concurrent use by multiple goroutines.
client *http.Client
clientGuard sync.Mutex
}
// type check
var _ Upstream = &dnsOverHTTPS{}
func (p *dnsOverHTTPS) Address() string { return p.boot.URL.String() }
func (p *dnsOverHTTPS) Exchange(m *dns.Msg) (*dns.Msg, error) {
client, err := p.getClient()
if err != nil {
return nil, errorx.Decorate(err, "couldn't initialize HTTP client or transport")
}
logBegin(p.Address(), m)
r, err := p.exchangeHTTPSClient(m, client)
logFinish(p.Address(), err)
return r, err
}
// exchangeHTTPSClient sends the DNS query to a DOH resolver using the specified
// http.Client instance.
func (p *dnsOverHTTPS) exchangeHTTPSClient(m *dns.Msg, client *http.Client) (*dns.Msg, error) {
buf, err := m.Pack()
if err != nil {
return nil, errorx.Decorate(err, "couldn't pack request msg")
}
// It appears, that GET requests are more memory-efficient with Golang
// implementation of HTTP/2.
requestURL := p.Address() + "?dns=" + base64.RawURLEncoding.EncodeToString(buf)
req, err := http.NewRequest("GET", requestURL, nil)
if err != nil {
return nil, errorx.Decorate(err, "couldn't create a HTTP request to %s", p.boot.URL)
}
req.Header.Set("Accept", "application/dns-message")
resp, err := client.Do(req)
if resp != nil && resp.Body != nil {
defer resp.Body.Close()
}
if err != nil {
// TODO: consider using errors.As
if os.IsTimeout(err) {
// If this is a timeout error, trying to forcibly re-create the HTTP client instance
// See https://github.com/AdguardTeam/AdGuardHome/issues/3217 for more details on this
p.clientGuard.Lock()
p.client = nil
p.clientGuard.Unlock()
}
return nil, errorx.Decorate(err, "couldn't do a GET request to '%s'", p.boot.URL)
}
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, errorx.Decorate(err, "couldn't read body contents for '%s'", p.boot.URL)
}
if resp.StatusCode != http.StatusOK {
return nil, fmt.Errorf("got an unexpected HTTP status code %d from '%s'", resp.StatusCode, p.boot.URL)
}
response := dns.Msg{}
err = response.Unpack(body)
if err != nil {
return nil, errorx.Decorate(err, "couldn't unpack DNS response from '%s': body is %s", p.boot.URL, string(body))
}
if response.Id != m.Id {
err = dns.ErrId
}
return &response, err
}
// getClient gets or lazily initializes an HTTP client (and transport) that will
// be used for this DOH resolver.
func (p *dnsOverHTTPS) getClient() (c *http.Client, err error) {
startTime := time.Now()
p.clientGuard.Lock()
defer p.clientGuard.Unlock()
if p.client != nil {
return p.client, nil
}
// Timeout can be exceeded while waiting for the lock
// This happens quite often on mobile devices
elapsed := time.Since(startTime)
if p.boot.options.Timeout > 0 && elapsed > p.boot.options.Timeout {
return nil, fmt.Errorf("timeout exceeded: %s", elapsed)
}
p.client, err = p.createClient()
return p.client, err
}
func (p *dnsOverHTTPS) createClient() (*http.Client, error) {
transport, err := p.createTransport()
if err != nil {
return nil, errorx.Decorate(err, "couldn't initialize HTTP transport")
}
client := &http.Client{
Transport: transport,
Timeout: p.boot.options.Timeout,
Jar: nil,
}
p.client = client
return p.client, nil
}
// createTransport initializes an HTTP transport that will be used specifically
// for this DOH resolver. This HTTP transport ensures that the HTTP requests
// will be sent exactly to the IP address got from the bootstrap resolver.
func (p *dnsOverHTTPS) createTransport() (*http.Transport, error) {
tlsConfig, dialContext, err := p.boot.get()
if err != nil {
return nil, errorx.Decorate(err, "couldn't bootstrap %s", p.boot.URL)
}
transport := &http.Transport{
TLSClientConfig: tlsConfig,
DisableCompression: true,
DialContext: dialContext,
}
// It appears that this is important to explicitly configure transport to use HTTP2
// Relevant issue: https://github.com/AdguardTeam/dnsproxy/issues/11
_, err = http2.ConfigureTransports(transport)
if err != nil {
return nil, err
}
return transport, nil
}