forked from folbricht/routedns
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dohclient.go
346 lines (311 loc) · 9.03 KB
/
dohclient.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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
package rdns
import (
"bytes"
"context"
"crypto/tls"
"encoding/base64"
"errors"
"fmt"
"io/ioutil"
"net"
"net/http"
"sync"
"time"
"github.com/jtacoma/uritemplates"
"github.com/lucas-clemente/quic-go"
"github.com/lucas-clemente/quic-go/http3"
"github.com/miekg/dns"
"github.com/sirupsen/logrus"
"golang.org/x/net/http2"
)
// DoHClientOptions contains options used by the DNS-over-HTTP resolver.
type DoHClientOptions struct {
// Query method, either GET or POST. If empty, POST is used.
Method string
// Bootstrap address - IP to use for the service instead of looking up
// the service's hostname with potentially plain DNS.
BootstrapAddr string
// Transport protocol to run HTTPS over. "quic" or "tcp", defaults to "tcp".
Transport string
// Local IP to use for outbound connections. If nil, a local address is chosen.
LocalAddr net.IP
TLSConfig *tls.Config
}
// DoHClient is a DNS-over-HTTP resolver with support fot HTTP/2.
type DoHClient struct {
id string
endpoint string
template *uritemplates.UriTemplate
client *http.Client
opt DoHClientOptions
metrics *ListenerMetrics
}
var _ Resolver = &DoHClient{}
func NewDoHClient(id, endpoint string, opt DoHClientOptions) (*DoHClient, error) {
// Parse the URL template
template, err := uritemplates.Parse(endpoint)
if err != nil {
return nil, err
}
var tr http.RoundTripper
switch opt.Transport {
case "tcp", "":
tr, err = dohTcpTransport(opt)
case "quic":
tr, err = dohQuicTransport(opt)
default:
err = fmt.Errorf("unknown protocol: '%s'", opt.Transport)
}
if err != nil {
return nil, err
}
client := &http.Client{
Transport: tr,
}
if opt.Method == "" {
opt.Method = "POST"
}
if opt.Method != "POST" && opt.Method != "GET" {
return nil, fmt.Errorf("unsupported method '%s'", opt.Method)
}
return &DoHClient{
id: id,
endpoint: endpoint,
template: template,
client: client,
opt: opt,
metrics: NewListenerMetrics("client", id),
}, nil
}
// Resolve a DNS query.
func (d *DoHClient) Resolve(q *dns.Msg, ci ClientInfo) (*dns.Msg, error) {
logger(d.id, q, ci).WithFields(logrus.Fields{
"resolver": d.endpoint,
"protocol": "doh",
"method": d.opt.Method,
}).Debug("querying upstream resolver")
// Add padding before sending the query over HTTPS
padQuery(q)
d.metrics.query.Add(1)
switch d.opt.Method {
case "POST":
return d.ResolvePOST(q)
case "GET":
return d.ResolveGET(q)
}
return nil, errors.New("unsupported method")
}
// ResolvePOST resolves a DNS query via DNS-over-HTTP using the POST method.
func (d *DoHClient) ResolvePOST(q *dns.Msg) (*dns.Msg, error) {
// Pack the DNS query into wire format
b, err := q.Pack()
if err != nil {
d.metrics.err.Add("pack", 1)
return nil, err
}
// The URL could be a template. Process it without values since POST doesn't use variables in the URL.
u, err := d.template.Expand(map[string]interface{}{})
if err != nil {
d.metrics.err.Add("template", 1)
return nil, err
}
req, err := http.NewRequest("POST", u, bytes.NewReader(b))
if err != nil {
d.metrics.err.Add("http", 1)
return nil, err
}
req.Header.Add("accept", "application/dns-message")
req.Header.Add("content-type", "application/dns-message")
resp, err := d.client.Do(req)
if err != nil {
d.metrics.err.Add("post", 1)
return nil, err
}
defer resp.Body.Close()
return d.responseFromHTTP(resp)
}
// ResolveGET resolves a DNS query via DNS-over-HTTP using the GET method.
func (d *DoHClient) ResolveGET(q *dns.Msg) (*dns.Msg, error) {
// Pack the DNS query into wire format
b, err := q.Pack()
if err != nil {
d.metrics.err.Add("pack", 1)
return nil, err
}
// Encode the query as base64url without padding
b64 := base64.RawURLEncoding.EncodeToString(b)
// The URL must be a template. Process it with the "dns" param containing the encoded query.
u, err := d.template.Expand(map[string]interface{}{"dns": b64})
if err != nil {
d.metrics.err.Add("template", 1)
return nil, err
}
req, err := http.NewRequest("GET", u, nil)
if err != nil {
d.metrics.err.Add("http", 1)
return nil, err
}
req.Header.Add("accept", "application/dns-message")
resp, err := d.client.Do(req)
if err != nil {
d.metrics.err.Add("get", 1)
return nil, err
}
defer resp.Body.Close()
return d.responseFromHTTP(resp)
}
func (d *DoHClient) String() string {
return d.id
}
// Check the HTTP response status code and parse out the response DNS message.
func (d *DoHClient) responseFromHTTP(resp *http.Response) (*dns.Msg, error) {
if resp.StatusCode < 200 || resp.StatusCode > 299 {
d.metrics.err.Add(fmt.Sprintf("http%d", resp.StatusCode), 1)
return nil, fmt.Errorf("unexpected status code %d", resp.StatusCode)
}
rb, err := ioutil.ReadAll(resp.Body)
if err != nil {
d.metrics.err.Add("read", 1)
return nil, err
}
a := new(dns.Msg)
err = a.Unpack(rb)
if err != nil {
d.metrics.err.Add("unpack", 1)
} else {
d.metrics.response.Add(rCode(a), 1)
}
return a, err
}
func dohTcpTransport(opt DoHClientOptions) (http.RoundTripper, error) {
tr := &http.Transport{
Proxy: http.ProxyFromEnvironment,
TLSClientConfig: opt.TLSConfig,
DisableCompression: true,
ResponseHeaderTimeout: 10 * time.Second,
IdleConnTimeout: 30 * time.Second,
}
// If we're using a custom tls.Config, HTTP2 isn't enabled by default in
// the HTTP library. Turn it on for this transport.
if tr.TLSClientConfig != nil {
if err := http2.ConfigureTransport(tr); err != nil {
return nil, err
}
}
// Use a custom dialer if a bootstrap address or local address was provided
if opt.BootstrapAddr != "" || opt.LocalAddr != nil {
d := net.Dialer{LocalAddr: &net.TCPAddr{IP: opt.LocalAddr}}
tr.DialContext = func(ctx context.Context, network, addr string) (net.Conn, error) {
if opt.BootstrapAddr != "" {
_, port, err := net.SplitHostPort(addr)
if err != nil {
return nil, err
}
addr = net.JoinHostPort(opt.BootstrapAddr, port)
}
return d.DialContext(ctx, network, addr)
}
}
return tr, nil
}
func dohQuicTransport(opt DoHClientOptions) (http.RoundTripper, error) {
tr := &http3.RoundTripper{
TLSClientConfig: opt.TLSConfig,
QuicConfig: &quic.Config{
TokenStore: quic.NewLRUTokenStore(10, 10),
},
Dial: func(network, addr string, tlsConfig *tls.Config, config *quic.Config) (quic.EarlySession, error) {
hostname, port, err := net.SplitHostPort(addr)
if err != nil {
return nil, err
}
if opt.BootstrapAddr != "" {
tlsConfig = tlsConfig.Clone()
tlsConfig.ServerName = hostname
addr = net.JoinHostPort(opt.BootstrapAddr, port)
}
return newQuicSession(hostname, addr, opt.LocalAddr, tlsConfig, config)
},
}
return tr, nil
}
// QUIC session that automatically restarts when it's used after having timed out. Needed
// since the quic-go RoundTripper doesn't have any session management and timed out
// sessions aren't restarted. This one doesn't support Early sessions, and instead just
// uses a regular session.
type quicSession struct {
quic.Session
hostname string
rAddr string
lAddr net.IP
tlsConfig *tls.Config
config *quic.Config
mu sync.Mutex
expiredContext context.Context
}
func newQuicSession(hostname, rAddr string, lAddr net.IP, tlsConfig *tls.Config, config *quic.Config) (quic.EarlySession, error) {
session, err := quicDial(hostname, rAddr, lAddr, tlsConfig, config)
if err != nil {
return nil, err
}
expired, cancel := context.WithCancel(context.Background())
cancel()
return &quicSession{
hostname: hostname,
rAddr: rAddr,
lAddr: lAddr,
tlsConfig: tlsConfig,
config: config,
Session: session,
expiredContext: expired,
}, nil
}
func (s *quicSession) HandshakeComplete() context.Context {
return s.expiredContext
}
func (s *quicSession) OpenStreamSync(ctx context.Context) (quic.Stream, error) {
s.mu.Lock()
defer s.mu.Unlock()
stream, err := s.Session.OpenStreamSync(ctx)
if err != nil {
_ = s.Session.CloseWithError(quic.ErrorCode(DOQNoError), "")
var session quic.Session
session, err = quicDial(s.hostname, s.rAddr, s.lAddr, s.tlsConfig, s.config)
if err != nil {
return nil, err
}
s.Session = session
stream, err = s.Session.OpenStreamSync(ctx)
}
return stream, err
}
func (s *quicSession) OpenStream() (quic.Stream, error) {
s.mu.Lock()
defer s.mu.Unlock()
stream, err := s.Session.OpenStream()
if err != nil {
_ = s.Session.CloseWithError(quic.ErrorCode(DOQNoError), "")
var session quic.Session
session, err = quicDial(s.hostname, s.rAddr, s.lAddr, s.tlsConfig, s.config)
if err != nil {
return nil, err
}
s.Session = session
stream, err = s.Session.OpenStream()
}
return stream, err
}
func (s *quicSession) NextSession() quic.Session {
return nil
}
func quicDial(hostname, rAddr string, lAddr net.IP, tlsConfig *tls.Config, config *quic.Config) (quic.Session, error) {
udpAddr, err := net.ResolveUDPAddr("udp", rAddr)
if err != nil {
return nil, err
}
udpConn, err := net.ListenUDP("udp", &net.UDPAddr{IP: lAddr, Port: 0})
if err != nil {
return nil, err
}
return quic.Dial(udpConn, udpAddr, hostname, tlsConfig, config)
}