-
Notifications
You must be signed in to change notification settings - Fork 251
/
proxy.go
661 lines (520 loc) · 15.8 KB
/
proxy.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
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
// Package proxy implements a DNS proxy that supports all known DNS
// encryption protocols.
package proxy
import (
"context"
"fmt"
"io"
"net"
"net/http"
"net/netip"
"sync"
"sync/atomic"
"time"
"github.com/AdguardTeam/dnsproxy/fastip"
proxynetutil "github.com/AdguardTeam/dnsproxy/internal/netutil"
"github.com/AdguardTeam/dnsproxy/upstream"
"github.com/AdguardTeam/golibs/errors"
"github.com/AdguardTeam/golibs/log"
"github.com/AdguardTeam/golibs/netutil"
"github.com/ameshkov/dnscrypt/v2"
"github.com/miekg/dns"
gocache "github.com/patrickmn/go-cache"
"github.com/quic-go/quic-go"
"github.com/quic-go/quic-go/http3"
)
const (
defaultTimeout = 10 * time.Second
minDNSPacketSize = 12 + 5
)
// Proto is the DNS protocol.
type Proto string
// Proto values.
const (
// ProtoUDP is the plain DNS-over-UDP protocol.
ProtoUDP Proto = "udp"
// ProtoTCP is the plain DNS-over-TCP protocol.
ProtoTCP Proto = "tcp"
// ProtoTLS is the DNS-over-TLS (DoT) protocol.
ProtoTLS Proto = "tls"
// ProtoHTTPS is the DNS-over-HTTPS (DoH) protocol.
ProtoHTTPS Proto = "https"
// ProtoQUIC is the DNS-over-QUIC (DoQ) protocol.
ProtoQUIC Proto = "quic"
// ProtoDNSCrypt is the DNSCrypt protocol.
ProtoDNSCrypt Proto = "dnscrypt"
)
const (
// UnqualifiedNames is reserved name for "unqualified names only", ie names without dots
UnqualifiedNames = "unqualified_names"
)
// Proxy combines the proxy server state and configuration
type Proxy struct {
// counter is the counter of messages. It must only be incremented
// atomically, so it must be the first member of the struct to make sure
// that it has a 64-bit alignment.
//
// See https://golang.org/pkg/sync/atomic/#pkg-note-BUG.
counter uint64
// started indicates if the proxy has been started.
started bool
// Listeners
// --
// udpListen are the listened UDP connections.
udpListen []*net.UDPConn
// tcpListen are the listened TCP connections.
tcpListen []net.Listener
// tlsListen are the listened TCP connections with TLS.
tlsListen []net.Listener
// quicListen are the listened QUIC connections.
quicListen []*quic.EarlyListener
// httpsListen are the listened HTTPS connections.
httpsListen []net.Listener
// h3Listen are the listened HTTP/3 connections.
h3Listen []*quic.EarlyListener
// httpsServer serves queries received over HTTPS.
httpsServer *http.Server
// h3Server serves queries received over HTTP/3.
h3Server *http3.Server
// dnsCryptUDPListen are the listened UDP connections for DNSCrypt.
dnsCryptUDPListen []*net.UDPConn
// dnsCryptTCPListen are the listened TCP connections for DNSCrypt.
dnsCryptTCPListen []net.Listener
// dnsCryptServer serves DNSCrypt queries.
dnsCryptServer *dnscrypt.Server
// Upstream
// --
// upstreamRttStats is a map of upstream addresses and their rtt. Used to
// sort upstreams by their latency.
upstreamRttStats map[string]int
// rttLock protects upstreamRttStats.
rttLock sync.Mutex
// DNS64 (in case dnsproxy works in a NAT64/DNS64 network)
// --
// dns64Prefs is a set of NAT64 prefixes that are used to detect and
// construct DNS64 responses. The DNS64 function is disabled if it is
// empty.
dns64Prefs []netip.Prefix
// Ratelimit
// --
// ratelimitBuckets is a storage for ratelimiters for individual IPs.
ratelimitBuckets *gocache.Cache
// ratelimitLock protects ratelimitBuckets.
ratelimitLock sync.Mutex
// proxyVerifier checks if the proxy is in the trusted list.
proxyVerifier netutil.SubnetSet
// DNS cache
// --
// cache is used to cache requests. It is disabled if nil.
cache *cache
// shortFlighter is used to resolve the expired cached requests without
// repetitions.
shortFlighter *optimisticResolver
// FastestAddr module
// --
// fastestAddr finds the fastest IP address for the resolved domain.
fastestAddr *fastip.FastestAddr
// Other
// --
// bytesPool is a pool of byte slices used to read DNS packets.
bytesPool *sync.Pool
// udpOOBSize is the size of the out-of-band data for UDP connections.
udpOOBSize int
// RWMutex protects the whole proxy.
sync.RWMutex
// requestGoroutinesSema limits the number of simultaneous requests.
//
// TODO(a.garipov): Currently we have to pass this exact semaphore to
// the workers, to prevent races on restart. In the future we will need
// a better restarting mechanism that completely prevents such invalid
// states.
//
// See also: https://github.com/AdguardTeam/AdGuardHome/issues/2242.
requestGoroutinesSema semaphore
// Config is the proxy configuration.
Config
}
// Init populates fields of p but does not start listeners.
func (p *Proxy) Init() (err error) {
p.initCache()
if p.MaxGoroutines > 0 {
log.Info("dnsproxy: max goroutines is set to %d", p.MaxGoroutines)
p.requestGoroutinesSema, err = newChanSemaphore(p.MaxGoroutines)
if err != nil {
return fmt.Errorf("can't init semaphore: %w", err)
}
} else {
p.requestGoroutinesSema = newNoopSemaphore()
}
p.udpOOBSize = proxynetutil.UDPGetOOBSize()
p.bytesPool = &sync.Pool{
New: func() interface{} {
// 2 bytes may be used to store packet length (see TCP/TLS)
b := make([]byte, 2+dns.MaxMsgSize)
return &b
},
}
if p.UpstreamMode == UModeFastestAddr {
log.Info("dnsproxy: fastest ip is enabled")
p.fastestAddr = fastip.NewFastestAddr()
if timeout := p.FastestPingTimeout; timeout > 0 {
p.fastestAddr.PingWaitTimeout = timeout
}
}
var trusted []*net.IPNet
trusted, err = netutil.ParseSubnets(p.TrustedProxies...)
if err != nil {
return fmt.Errorf("initializing subnet detector for proxies verifying: %w", err)
}
p.proxyVerifier = netutil.SliceSubnetSet(trusted)
err = p.setupDNS64()
if err != nil {
return fmt.Errorf("setting up DNS64: %w", err)
}
return nil
}
// Start initializes the proxy server and starts listening
func (p *Proxy) Start() (err error) {
log.Info("dnsproxy: starting dns proxy server")
p.Lock()
defer p.Unlock()
err = p.validateConfig()
if err != nil {
return err
}
err = p.Init()
if err != nil {
return err
}
// TODO(a.garipov): Accept a context into this method.
ctx := context.Background()
err = p.startListeners(ctx)
if err != nil {
return fmt.Errorf("starting listeners: %w", err)
}
p.started = true
return nil
}
// closeAll closes all closers and appends the occurred errors to errs.
func closeAll[C io.Closer](errs []error, closers ...C) (appended []error) {
for _, c := range closers {
err := c.Close()
if err != nil {
errs = append(errs, err)
}
}
return errs
}
// Stop stops the proxy server including all its listeners
func (p *Proxy) Stop() error {
log.Info("dnsproxy: stopping dns proxy server")
p.Lock()
defer p.Unlock()
if !p.started {
log.Info("dnsproxy: dns proxy server is not started")
return nil
}
errs := closeAll(nil, p.tcpListen...)
p.tcpListen = nil
errs = closeAll(errs, p.udpListen...)
p.udpListen = nil
errs = closeAll(errs, p.tlsListen...)
p.tlsListen = nil
if p.httpsServer != nil {
errs = closeAll(errs, p.httpsServer)
p.httpsServer = nil
// No need to close these since they're closed by httpsServer.Close().
p.httpsListen = nil
}
if p.h3Server != nil {
errs = closeAll(errs, p.h3Server)
p.h3Server = nil
}
errs = closeAll(errs, p.h3Listen...)
p.h3Listen = nil
errs = closeAll(errs, p.quicListen...)
p.quicListen = nil
errs = closeAll(errs, p.dnsCryptUDPListen...)
p.dnsCryptUDPListen = nil
errs = closeAll(errs, p.dnsCryptTCPListen...)
p.dnsCryptTCPListen = nil
if p.UpstreamConfig != nil {
errs = closeAll(errs, p.UpstreamConfig)
}
p.started = false
log.Println("dnsproxy: stopped dns proxy server")
if len(errs) > 0 {
return errors.List("stopping dns proxy server", errs...)
}
return nil
}
// Addrs returns all listen addresses for the specified proto or nil if the proxy does not listen to it.
// proto must be "tcp", "tls", "https", "quic", or "udp"
func (p *Proxy) Addrs(proto Proto) []net.Addr {
p.RLock()
defer p.RUnlock()
var addrs []net.Addr
switch proto {
case ProtoTCP:
for _, l := range p.tcpListen {
addrs = append(addrs, l.Addr())
}
case ProtoTLS:
for _, l := range p.tlsListen {
addrs = append(addrs, l.Addr())
}
case ProtoHTTPS:
for _, l := range p.httpsListen {
addrs = append(addrs, l.Addr())
}
case ProtoUDP:
for _, l := range p.udpListen {
addrs = append(addrs, l.LocalAddr())
}
case ProtoQUIC:
for _, l := range p.quicListen {
addrs = append(addrs, l.Addr())
}
case ProtoDNSCrypt:
// Using only UDP addrs here
// TODO: to do it better we should either do ProtoDNSCryptTCP/ProtoDNSCryptUDP
// or we should change the configuration so that it was not possible to
// set different ports for TCP/UDP listeners.
for _, l := range p.dnsCryptUDPListen {
addrs = append(addrs, l.LocalAddr())
}
default:
panic("proto must be 'tcp', 'tls', 'https', 'quic', 'dnscrypt' or 'udp'")
}
return addrs
}
// Addr returns the first listen address for the specified proto or null if the proxy does not listen to it
// proto must be "tcp", "tls", "https", "quic", or "udp"
func (p *Proxy) Addr(proto Proto) net.Addr {
p.RLock()
defer p.RUnlock()
switch proto {
case ProtoTCP:
if len(p.tcpListen) == 0 {
return nil
}
return p.tcpListen[0].Addr()
case ProtoTLS:
if len(p.tlsListen) == 0 {
return nil
}
return p.tlsListen[0].Addr()
case ProtoHTTPS:
if len(p.httpsListen) == 0 {
return nil
}
return p.httpsListen[0].Addr()
case ProtoUDP:
if len(p.udpListen) == 0 {
return nil
}
return p.udpListen[0].LocalAddr()
case ProtoQUIC:
if len(p.quicListen) == 0 {
return nil
}
return p.quicListen[0].Addr()
case ProtoDNSCrypt:
if len(p.dnsCryptUDPListen) == 0 {
return nil
}
return p.dnsCryptUDPListen[0].LocalAddr()
default:
panic("proto must be 'tcp', 'tls', 'https', 'quic', 'dnscrypt' or 'udp'")
}
}
// needsLocalUpstream returns true if the request should be handled by a private
// upstream servers.
func (p *Proxy) needsLocalUpstream(req *dns.Msg) (ok bool) {
if req.Question[0].Qtype != dns.TypePTR {
return false
}
host := req.Question[0].Name
ip, err := netutil.IPFromReversedAddr(host)
if err != nil {
log.Debug("dnsproxy: failed to parse ip from ptr request: %s", err)
return false
}
return p.shouldStripDNS64(ip)
}
// selectUpstreams returns the upstreams to use for the specified host. It
// firstly considers custom upstreams if those aren't empty and then the
// configured ones. It returns false, if no upstreams are available for current
// request.
func (p *Proxy) selectUpstreams(d *DNSContext) (upstreams []upstream.Upstream, ok bool) {
host := d.Req.Question[0].Name
if p.needsLocalUpstream(d.Req) {
if p.PrivateRDNSUpstreamConfig == nil {
return nil, false
}
ip, _ := netutil.IPAndPortFromAddr(d.Addr)
// TODO(e.burkov): Detect against the actual configured subnet set.
// Perhaps, even much earlier.
if !netutil.IsLocallyServed(ip) {
return nil, false
}
return p.PrivateRDNSUpstreamConfig.getUpstreamsForDomain(host), true
}
if d.CustomUpstreamConfig != nil {
upstreams = d.CustomUpstreamConfig.getUpstreamsForDomain(host)
}
if upstreams != nil {
return upstreams, true
}
return p.UpstreamConfig.getUpstreamsForDomain(host), true
}
// replyFromUpstream tries to resolve the request.
func (p *Proxy) replyFromUpstream(d *DNSContext) (ok bool, err error) {
req := d.Req
upstreams, ok := p.selectUpstreams(d)
if !ok {
return false, upstream.ErrNoUpstreams
}
start := time.Now()
// Perform the DNS request.
var reply *dns.Msg
var u upstream.Upstream
reply, u, err = p.exchange(req, upstreams)
if dns64Ups := p.performDNS64(req, reply, upstreams); dns64Ups != nil {
u = dns64Ups
} else if p.isBogusNXDomain(reply) {
log.Tracef("response ip is contained in bogus-nxdomain list")
reply = p.genWithRCode(req, dns.RcodeNameError)
}
log.Tracef("RTT: %s", time.Since(start))
if err != nil && p.Fallbacks != nil {
log.Tracef("using the fallback upstream due to %s", err)
reply, u, err = upstream.ExchangeParallel(p.Fallbacks, req)
}
if ok = reply != nil; ok {
// This branch handles the successfully exchanged response.
// Set upstream that have resolved the request to DNSContext.
d.Upstream = u
p.setMinMaxTTL(reply)
// Explicitly construct the question section since some upstreams may
// respond with invalidly constructed messages which cause out-of-range
// panics afterwards.
//
// See https://github.com/AdguardTeam/AdGuardHome/issues/3551.
if len(req.Question) > 0 && len(reply.Question) == 0 {
reply.Question = []dns.Question{req.Question[0]}
}
} else {
reply = p.genServerFailure(req)
d.hasEDNS0 = false
}
d.Res = reply
return ok, err
}
// addDO adds EDNS0 RR if needed and sets DO bit of msg to true.
func addDO(msg *dns.Msg) {
if o := msg.IsEdns0(); o != nil {
if !o.Do() {
o.SetDo()
}
return
}
msg.SetEdns0(defaultUDPBufSize, true)
}
// defaultUDPBufSize defines the default size of UDP buffer for EDNS0 RRs.
const defaultUDPBufSize = 2048
// Resolve is the default resolving method used by the DNS proxy to query
// upstream servers.
func (p *Proxy) Resolve(dctx *DNSContext) (err error) {
if p.EnableEDNSClientSubnet {
dctx.processECS(p.EDNSAddr)
}
dctx.calcFlagsAndSize()
// Use cache only if it's enabled and the query doesn't use custom upstream.
// Also don't lookup the cache for responses with DNSSEC checking disabled
// since only validated responses are cached and those may be not the
// desired result for user specifying CD flag.
cacheWorks := p.cacheWorks(dctx)
if cacheWorks {
if p.replyFromCache(dctx) {
// Complete the response from cache.
dctx.scrub()
return nil
}
// On cache miss request for DNSSEC from the upstream to cache it
// afterwards.
addDO(dctx.Req)
}
var ok bool
ok, err = p.replyFromUpstream(dctx)
// Don't cache the responses having CD flag, just like Dnsmasq does. It
// prevents the cache from being poisoned with unvalidated answers which may
// differ from validated ones.
//
// See https://github.com/imp/dnsmasq/blob/770bce967cfc9967273d0acfb3ea018fb7b17522/src/forward.c#L1169-L1172.
if cacheWorks && ok && !dctx.Res.CheckingDisabled {
// Cache the response with DNSSEC RRs.
p.cacheResp(dctx)
}
// It is possible that the response is nil if the upstream hasn't been
// chosen.
if dctx.Res != nil {
filterMsg(dctx.Res, dctx.Res, dctx.adBit, dctx.doBit, 0)
}
// Complete the response.
dctx.scrub()
if p.ResponseHandler != nil {
p.ResponseHandler(dctx, err)
}
return err
}
// cacheWorks returns true if the cache works for the given context. If not, it
// returns false and logs the reason why.
func (p *Proxy) cacheWorks(dctx *DNSContext) (ok bool) {
var reason string
switch {
case p.cache == nil:
reason = "disabled"
case dctx.CustomUpstreamConfig != nil:
// See https://github.com/AdguardTeam/dnsproxy/issues/169.
reason = "custom upstreams used"
case dctx.Req.CheckingDisabled:
reason = "dnssec check disabled"
default:
return true
}
log.Debug("dnsproxy: cache: %s; not caching", reason)
return false
}
// processECS adds EDNS Client Subnet data into the request from d.
func (dctx *DNSContext) processECS(cliIP net.IP) {
if ecs, _ := ecsFromMsg(dctx.Req); ecs != nil {
if ones, _ := ecs.Mask.Size(); ones != 0 {
dctx.ReqECS = ecs
log.Debug("dnsproxy: passing through ecs: %s", dctx.ReqECS)
return
}
}
// Set ECS.
if cliIP == nil {
cliIP, _ = netutil.IPAndPortFromAddr(dctx.Addr)
if cliIP == nil {
return
}
}
if !netutil.IsSpecialPurpose(cliIP) {
// A Stub Resolver MUST set SCOPE PREFIX-LENGTH to 0. See RFC 7871
// Section 6.
dctx.ReqECS = setECS(dctx.Req, cliIP, 0)
log.Debug("dnsproxy: setting ecs: %s", dctx.ReqECS)
}
}
// newDNSContext returns a new properly initialized *DNSContext.
func (p *Proxy) newDNSContext(proto Proto, req *dns.Msg) (d *DNSContext) {
return &DNSContext{
Proto: proto,
Req: req,
StartTime: time.Now(),
RequestID: atomic.AddUint64(&p.counter, 1),
}
}