-
Notifications
You must be signed in to change notification settings - Fork 507
/
server.go
589 lines (527 loc) · 17 KB
/
server.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
package dns
import (
"context"
"errors"
"fmt"
"net"
"strings"
"sync"
"sync/atomic"
"time"
"github.com/miekg/dns"
"google.golang.org/protobuf/types/known/durationpb"
"github.com/datawire/dlib/dcontext"
"github.com/datawire/dlib/dgroup"
"github.com/datawire/dlib/dlog"
"github.com/datawire/dlib/dtime"
rpc "github.com/telepresenceio/telepresence/rpc/v2/daemon"
"github.com/telepresenceio/telepresence/v2/pkg/client"
"github.com/telepresenceio/telepresence/v2/pkg/iputil"
"github.com/telepresenceio/telepresence/v2/pkg/vif"
)
type Resolver func(ctx context.Context, domain string) ([]net.IP, error)
// recursionCheck is a special host name in a well known namespace that isn't expected to exist. It
// is used once for determining if the cluster's DNS resolver will call the Telepresence DNS resolver
// recursively. This is common when the cluster is running on the local host (k3s in docker for instance).
const recursionCheck = "tel2-recursion-check.kube-system."
// defaultClusterDomain used unless traffic-manager reports otherwise
const defaultClusterDomain = "cluster.local."
type FallbackPool interface {
Exchange(context.Context, *dns.Client, *dns.Msg) (*dns.Msg, time.Duration, error)
RemoteAddr() string
LocalAddrs() []*net.UDPAddr
}
const (
recursionUnknown = int32(iota)
recursionNotDetected
recursionDetected
recursionTestInProgress
)
// Server is a DNS server which implements the github.com/miekg/dns Handler interface
type Server struct {
ctx context.Context // necessary to make logging work in ServeDNS function
fallbackPool FallbackPool
resolve Resolver
requestCount int64
cache sync.Map
recursive int32 // one of the recursionXXX constants declared above (unique type avoided because it just gets messy with the atomic calls)
cacheResolve func(*dns.Question) ([]dns.RR, error)
// Namespaces, accessible using <service-name>.<namespace-name>
namespaces map[string]struct{}
domains map[string]struct{}
search []string
// The domainsLock locks usage of namespaces, domains, and search
domainsLock sync.RWMutex
// searchPathCh receives requests to change the search path.
searchPathCh chan []string
config *rpc.DNSConfig
// clusterDomain reported by the traffic-manager
clusterDomain string
// Function that sends a lookup requrest to the traffic-manager
clusterLookup func(context.Context, string) ([][]byte, error)
}
type cacheEntry struct {
created time.Time
currentQType int32 // will be set to the current qType during call to cluster
answer []dns.RR
wait chan struct{}
}
// cacheTTL is the time to live for an entry in the local DNS cache.
const cacheTTL = 60 * time.Second
func (dv *cacheEntry) expired() bool {
return time.Since(dv.created) > cacheTTL
}
// NewServer returns a new dns.Server
func NewServer(config *rpc.DNSConfig, clusterLookup func(context.Context, string) ([][]byte, error)) *Server {
if config == nil {
config = &rpc.DNSConfig{}
}
if len(config.ExcludeSuffixes) == 0 {
config.ExcludeSuffixes = []string{
".arpa",
".com",
".io",
".net",
".org",
".ru",
}
}
if config.LookupTimeout.AsDuration() <= 0 {
config.LookupTimeout = durationpb.New(8 * time.Second)
}
s := &Server{
config: config,
namespaces: make(map[string]struct{}),
domains: make(map[string]struct{}),
search: []string{""},
searchPathCh: make(chan []string, 5),
clusterDomain: defaultClusterDomain,
clusterLookup: clusterLookup,
}
s.cacheResolve = s.resolveWithRecursionCheck
return s
}
// tel2SubDomain aims to fix a search-path problem when using Docker on non-linux systems where
// Docker uses its own search-path for single label names. This means that the search path that
// is declared in the macOS resolver is ignored although the rest of the DNS-resolution works OK.
// Since the search-path is likely to change during a session, a stable fake domain is needed to
// emulate the search-path. That fake-domain can then be used in the search path declared in the
// Docker config.
//
// The "tel2-search" domain fills this purpose and a request for "<single label name>.tel2-search."
// will be resolved as "<single label name>." using the search path of this resolver.
const tel2SubDomain = "tel2-search"
const tel2SubDomainDot = tel2SubDomain + "."
var localhostIPs = []net.IP{{127, 0, 0, 1}, {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1}}
func (s *Server) shouldDoClusterLookup(query string) bool {
if strings.HasSuffix(query, "."+s.clusterDomain) && strings.Count(query, ".") < 4 {
return false
}
query = query[:len(query)-1] // skip last dot
// Always include configured includeSuffixes
for _, sfx := range s.config.IncludeSuffixes {
if strings.HasSuffix(query, sfx) {
return true
}
}
// Skip configured excludeSuffixes
for _, sfx := range s.config.ExcludeSuffixes {
if strings.HasSuffix(query, sfx) {
return false
}
}
return true
}
func (s *Server) resolveInCluster(c context.Context, query string) (results []net.IP, err error) {
query = strings.ToLower(query)
query = strings.TrimSuffix(query, tel2SubDomainDot)
if query == "localhost." {
// BUG(lukeshu): I have no idea why a lookup
// for localhost even makes it to here on my
// home WiFi when connecting to a k3sctl
// cluster (but not a kubernaut.io cluster).
// But it does, so I need this in order to be
// productive at home. We should really
// root-cause this, because it's weird.
return localhostIPs, nil
}
if !s.shouldDoClusterLookup(query) {
return nil, nil
}
// Give the cluster lookup a reasonable timeout.
c, cancel := context.WithTimeout(c, s.config.LookupTimeout.AsDuration())
defer cancel()
result, err := s.clusterLookup(c, query[:len(query)-1])
if err != nil {
return nil, client.CheckTimeout(c, err)
}
if len(result) == 0 {
return nil, nil
}
ips := make(iputil.IPs, len(result))
for i, ip := range result {
ips[i] = ip
}
return ips, nil
}
func (s *Server) GetConfig() *rpc.DNSConfig {
dnsConfig := &rpc.DNSConfig{}
if s.config != nil {
dnsConfig.LocalIp = s.config.LocalIp
dnsConfig.ExcludeSuffixes = s.config.ExcludeSuffixes
dnsConfig.IncludeSuffixes = s.config.IncludeSuffixes
dnsConfig.LookupTimeout = s.config.LookupTimeout
}
return dnsConfig
}
func (s *Server) SetClusterDomainAndDNS(domain string, dnsIP net.IP) {
s.clusterDomain = domain
if s.config == nil {
s.config = &rpc.DNSConfig{}
}
if s.config.RemoteIp == nil {
s.config.RemoteIp = dnsIP
}
}
// SetSearchPath updates the DNS search path used by the resolver
func (s *Server) SetSearchPath(ctx context.Context, paths, namespaces []string) {
// Provide direct access to intercepted namespaces
for _, ns := range namespaces {
paths = append(paths, ns+".svc."+s.clusterDomain)
}
select {
case <-ctx.Done():
case s.searchPathCh <- paths:
}
}
func newLocalUDPListener(c context.Context) (net.PacketConn, error) {
lc := &net.ListenConfig{}
return lc.ListenPacket(c, "udp", "127.0.0.1:0")
}
func (s *Server) processSearchPaths(g *dgroup.Group, processor func(context.Context, []string, *vif.Device) error, dev *vif.Device) {
g.Go("SearchPaths", func(c context.Context) error {
var prevPaths []string
unchanged := func(paths []string) bool {
if len(paths) != len(prevPaths) {
return false
}
for i, path := range paths {
if path != prevPaths[i] {
return false
}
}
return true
}
for {
select {
case <-c.Done():
return nil
case paths := <-s.searchPathCh:
if len(s.searchPathCh) > 0 {
// Only interested in the last one
continue
}
if !unchanged(paths) {
dlog.Debugf(c, "%v -> %v", prevPaths, paths)
prevPaths = make([]string, len(paths))
copy(prevPaths, paths)
if err := processor(c, paths, dev); err != nil {
return err
}
if atomic.LoadInt32(&s.recursive) == recursionUnknown {
for _, p := range prevPaths {
if p == "kube-system" {
if atomic.CompareAndSwapInt32(&s.recursive, recursionUnknown, recursionTestInProgress) {
go s.performRecursionCheck(c)
}
break
}
}
}
}
}
}
})
}
func (s *Server) flushDNS() {
s.cache.Range(func(key, _ interface{}) bool {
s.cache.Delete(key)
return true
})
}
// splitToUDPAddr splits the given address into an UDPAddr. It's
// an error if the address is based on a hostname rather than an IP.
func splitToUDPAddr(netAddr net.Addr) (*net.UDPAddr, error) {
ip, port, err := iputil.SplitToIPPort(netAddr)
if err != nil {
return nil, err
}
return &net.UDPAddr{IP: ip, Port: int(port)}, nil
}
// RequestCount returns the number of requests that this server has received.
func (s *Server) RequestCount() int {
return int(atomic.LoadInt64(&s.requestCount))
}
func copyRRs(rrs []dns.RR, qType uint16) []dns.RR {
if len(rrs) == 0 {
return rrs
}
cp := make([]dns.RR, 0, len(rrs))
for _, rr := range rrs {
if rr.Header().Rrtype == qType {
cp = append(cp, dns.Copy(rr))
}
}
return cp
}
// resolveThruCache resolves the given query by first performing a cache lookup. If a cached
// entry is found that hasn't expired, it's returned. If not, this function will call
// resolveQuery() to resolve and store in the case.
func (s *Server) resolveThruCache(q *dns.Question) ([]dns.RR, error) {
newDv := &cacheEntry{wait: make(chan struct{}), created: time.Now()}
if v, loaded := s.cache.LoadOrStore(q.Name, newDv); loaded {
oldDv := v.(*cacheEntry)
if atomic.LoadInt32(&s.recursive) == recursionDetected && atomic.LoadInt32(&oldDv.currentQType) == int32(q.Qtype) {
// We have to assume that this is a recursion from the cluster.
return nil, nil
}
<-oldDv.wait
if !oldDv.expired() {
return copyRRs(oldDv.answer, q.Qtype), nil
}
s.cache.Store(q.Name, newDv)
}
return s.resolveQuery(q, newDv)
}
// resolveWithRecursionCheck is a special version of resolveThruCache which is only used until the
// recursionCheck query has completed, and it has been determined whether a query that is propagated
// to the cluster will recurse back to this resolver or not.
func (s *Server) resolveWithRecursionCheck(q *dns.Question) ([]dns.RR, error) {
newDv := &cacheEntry{wait: make(chan struct{}), created: time.Now()}
if v, loaded := s.cache.LoadOrStore(q.Name, newDv); loaded {
oldDv := v.(*cacheEntry)
if atomic.LoadInt32(&oldDv.currentQType) == int32(q.Qtype) {
if q.Name == recursionCheck {
atomic.StoreInt32(&s.recursive, recursionDetected)
}
if atomic.LoadInt32(&s.recursive) == recursionDetected {
return nil, nil
}
}
<-oldDv.wait
if !oldDv.expired() {
return copyRRs(oldDv.answer, q.Qtype), nil
}
s.cache.Store(q.Name, newDv)
}
answer, err := s.resolveQuery(q, newDv)
if q.Name == recursionCheck {
if atomic.LoadInt32(&s.recursive) == recursionDetected {
dlog.Debug(s.ctx, "DNS resolver is recursive")
} else {
atomic.StoreInt32(&s.recursive, recursionNotDetected)
dlog.Debug(s.ctx, "DNS resolver is not recursive")
}
s.cacheResolve = s.resolveThruCache
}
return answer, err
}
// dfs is a func that implements the fmt.Stringer interface. Used in log statements to ensure
// that the function isn't evaluated until the log output is formatted (which will happen only
// if the given loglevel is enabled).
type dfs func() string
func (d dfs) String() string {
return d()
}
func (s *Server) performRecursionCheck(c context.Context) {
const maxRetry = 10
defer dlog.Debug(c, "Recursion check finished")
rc := strings.TrimSuffix(recursionCheck, ".")
dlog.Debugf(c, "Performing initial recursion check with %s", rc)
i := 0
for ; i < maxRetry; i++ {
_, err := net.DefaultResolver.LookupIP(c, "ip4", rc)
if err != nil {
if derr, ok := err.(*net.DNSError); ok && derr.IsNotFound {
err = nil
}
}
if err != nil {
dlog.Errorf(c, "recursion check ended with %v", err)
}
if atomic.LoadInt32(&s.recursive) != recursionTestInProgress {
return
}
// Check didn't hit our resolver. Try again after a second
dtime.SleepWithContext(c, time.Second)
// Check that the resolver didn't get hit during our wait. We don't want
// to retry if it did, because that will give the false impression that
// the resolver is recursive.
if atomic.LoadInt32(&s.recursive) != recursionTestInProgress {
return
}
dlog.Debug(c, "retrying recursion check")
}
if i == maxRetry {
dlog.Errorf(c, "recursion check failed. The DNS isn't working properly")
}
}
// ServeDNS is an implementation of github.com/miekg/dns Handler.ServeDNS.
func (s *Server) ServeDNS(w dns.ResponseWriter, r *dns.Msg) {
c := s.ctx
defer func() {
// Closing the response tells the DNS service to terminate
if c.Err() != nil {
_ = w.Close()
}
}()
q := &r.Question[0]
atomic.AddInt64(&s.requestCount, 1)
answerString := func(a []dns.RR) string {
if a == nil {
return ""
}
switch len(a) {
case 0:
return "EMPTY"
case 1:
return a[0].String()
default:
return fmt.Sprintf("%v", a)
}
}
qts := dns.TypeToString[q.Qtype]
answer, err := s.cacheResolve(q)
var rc int
var pfx dfs = func() string { return "" }
var txt dfs = func() string { return "" }
var rct dfs = func() string { return dns.RcodeToString[rc] }
var msg *dns.Msg
defer func() {
dlog.Debugf(c, "%s%-6s %s -> %s %s", pfx, qts, q.Name, rct, txt)
_ = w.WriteMsg(msg)
}()
if err == nil && answer != nil {
rc = dns.RcodeSuccess
msg = new(dns.Msg)
msg.SetReply(r)
msg.Answer = answer
msg.Authoritative = true
// mac dns seems to fallback if you don't
// support recursion, if you have more than a
// single dns server, this will prevent us
// from intercepting all queries
msg.RecursionAvailable = true
txt = func() string { return answerString(msg.Answer) }
return
}
// The recursion check query, or queries that end with the cluster domain name, are not dispatched to the
// fallback DNS-server.
if s.fallbackPool == nil || strings.HasPrefix(q.Name, recursionCheck) || strings.HasSuffix(q.Name, s.clusterDomain) {
if err == nil {
rc = dns.RcodeNameError
} else {
rc = dns.RcodeServerFailure
if errors.Is(err, context.DeadlineExceeded) {
txt = func() string { return "timeout" }
} else {
txt = err.Error
}
}
msg = new(dns.Msg)
msg.SetRcode(r, rc)
return
}
pfx = func() string { return fmt.Sprintf("(%s) ", s.fallbackPool.RemoteAddr()) }
dc := &dns.Client{Net: "udp", Timeout: s.config.LookupTimeout.AsDuration()}
msg, _, err = s.fallbackPool.Exchange(c, dc, r)
if err != nil {
msg = new(dns.Msg)
rc = dns.RcodeServerFailure
txt = err.Error
if err, ok := err.(net.Error); ok {
switch {
case err.Timeout():
txt = func() string { return "timeout" }
case err.Temporary(): //nolint:staticcheck // err.Temporary is deprecated
rc = dns.RcodeRefused
default:
}
}
msg.SetRcode(r, rc)
} else {
rc = msg.Rcode
txt = func() string { return answerString(msg.Answer) }
}
}
// dnsTTL is the number of seconds that a found DNS record should be allowed to live in the callers cache. We
// keep this low to avoid such caching.
const dnsTTL = 4
func (s *Server) resolveQuery(q *dns.Question, dv *cacheEntry) ([]dns.RR, error) {
atomic.StoreInt32(&dv.currentQType, int32(q.Qtype))
defer func() {
atomic.StoreInt32(&dv.currentQType, int32(dns.TypeNone))
close(dv.wait)
}()
var err error
switch q.Qtype {
case dns.TypeA, dns.TypeAAAA:
var ips []net.IP
if ips, err = s.resolve(s.ctx, q.Name); err != nil || len(ips) == 0 {
break
}
answer := make([]dns.RR, 0, len(ips))
for _, ip := range ips {
var rr dns.RR
if ip4 := ip.To4(); ip4 != nil {
rr = &dns.A{
Hdr: dns.RR_Header{Name: q.Name, Rrtype: dns.TypeA, Class: dns.ClassINET, Ttl: dnsTTL},
A: ip4,
}
} else {
rr = &dns.AAAA{
Hdr: dns.RR_Header{Name: q.Name, Rrtype: dns.TypeAAAA, Class: dns.ClassINET, Ttl: dnsTTL},
AAAA: ip,
}
}
answer = append(answer, rr)
}
dv.answer = answer
default:
var ips []net.IP
if ips, err = s.resolve(s.ctx, q.Name); err != nil {
break
}
if len(ips) > 0 {
// a reply exists, but for another type, so our reply here is EMPTY
dv.answer = []dns.RR{}
}
}
if err != nil || len(dv.answer) == 0 {
s.cache.Delete(q.Name) // Don't cache unless the entry is found.
}
// Return a result for the correct query type. The result will be nil (nxdomain) if nothing was found. It might
// also be empty if no RRs were found for the given query type and that is OK.
// See https://datatracker.ietf.org/doc/html/rfc4074#section-3
return copyRRs(dv.answer, q.Qtype), err
}
// Run starts the DNS server(s) and waits for them to end
func (s *Server) Run(c context.Context, initDone chan<- struct{}, listeners []net.PacketConn, fallbackPool FallbackPool, resolve Resolver) error {
s.ctx = c
s.fallbackPool = fallbackPool
s.resolve = resolve
g := dgroup.NewGroup(c, dgroup.GroupConfig{})
for _, listener := range listeners {
srv := &dns.Server{PacketConn: listener, Handler: s, ReadTimeout: time.Second}
g.Go(listener.LocalAddr().String(), func(c context.Context) error {
go func() {
<-c.Done()
dlog.Debugf(c, "Shutting down DNS server")
_ = srv.ShutdownContext(dcontext.HardContext(c))
}()
return srv.ActivateAndServe()
})
}
close(initDone)
return g.Wait()
}