forked from d2iq-archive/mesos-dns
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathresolver.go
660 lines (574 loc) · 16.7 KB
/
resolver.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
// Package resolver contains functions to handle resolving .mesos domains
package resolver
import (
"errors"
"fmt"
"math/rand"
"net"
"net/http"
"strconv"
"strings"
"sync"
"sync/atomic"
"time"
"github.com/emicklei/go-restful"
_ "github.com/mesos/mesos-go/detector/zoo" // Registers the ZK detector
"github.com/mesosphere/mesos-dns/exchanger"
"github.com/mesosphere/mesos-dns/logging"
"github.com/mesosphere/mesos-dns/records"
"github.com/mesosphere/mesos-dns/util"
"github.com/miekg/dns"
)
// Resolver holds configuration state and the resource records
type Resolver struct {
masters []string
version string
config records.Config
rs *records.RecordGenerator
rsLock sync.RWMutex
rng *rand.Rand
fwd exchanger.Forwarder
}
// New returns a Resolver with the given version and configuration.
func New(version string, config records.Config) *Resolver {
var recordGenerator *records.RecordGenerator
recordGenerator = records.NewRecordGenerator(time.Duration(config.StateTimeoutSeconds) * time.Second)
r := &Resolver{
version: version,
config: config,
rs: recordGenerator,
// rand.Sources aren't safe for concurrent use, except the global one.
// See: https://github.com/golang/go/issues/3611
rng: rand.New(&lockedSource{src: rand.NewSource(time.Now().UnixNano())}),
masters: append([]string{""}, config.Masters...),
}
timeout := 5 * time.Second
if config.Timeout != 0 {
timeout = time.Duration(config.Timeout) * time.Second
}
rs := config.Resolvers
if !config.ExternalOn {
rs = rs[:0]
}
r.fwd = exchanger.NewForwarder(rs, exchangers(timeout, "udp", "tcp"))
return r
}
func exchangers(timeout time.Duration, protos ...string) map[string]exchanger.Exchanger {
exs := make(map[string]exchanger.Exchanger, len(protos))
for _, proto := range protos {
exs[proto] = exchanger.Decorate(
&dns.Client{
Net: proto,
DialTimeout: timeout,
ReadTimeout: timeout,
WriteTimeout: timeout,
},
exchanger.ErrorLogging(logging.Error),
exchanger.Instrumentation(
logging.CurLog.NonMesosForwarded,
logging.CurLog.NonMesosSuccess,
logging.CurLog.NonMesosFailed,
),
)
}
return exs
}
// return the current (read-only) record set. attempts to write to the returned
// object will likely result in a data race.
func (res *Resolver) records() *records.RecordGenerator {
res.rsLock.RLock()
defer res.rsLock.RUnlock()
return res.rs
}
// LaunchDNS starts a (TCP and UDP) DNS server for the Resolver,
// returning a error channel to which errors are asynchronously sent.
func (res *Resolver) LaunchDNS() <-chan error {
// Handers for Mesos requests
dns.HandleFunc(res.config.Domain+".", panicRecover(res.HandleMesos))
// Handler for nonMesos requests
dns.HandleFunc(".", panicRecover(res.HandleNonMesos))
errCh := make(chan error, 2)
_, e1 := res.Serve("tcp")
go func() { errCh <- <-e1 }()
_, e2 := res.Serve("udp")
go func() { errCh <- <-e2 }()
return errCh
}
// Serve starts a DNS server for net protocol (tcp/udp), returns immediately.
// the returned signal chan is closed upon the server successfully entering the listening phase.
// if the server aborts then an error is sent on the error chan.
func (res *Resolver) Serve(proto string) (<-chan struct{}, <-chan error) {
defer util.HandleCrash()
ch := make(chan struct{})
server := &dns.Server{
Addr: net.JoinHostPort(res.config.Listener, strconv.Itoa(res.config.Port)),
Net: proto,
TsigSecret: nil,
NotifyStartedFunc: func() { close(ch) },
}
errCh := make(chan error, 1)
go func() {
defer close(errCh)
err := server.ListenAndServe()
if err != nil {
errCh <- fmt.Errorf("Failed to setup %q server: %v", proto, err)
} else {
logging.Error.Printf("Not listening/serving any more requests.")
}
}()
return ch, errCh
}
// SetMasters sets the given masters.
// This method is not goroutine-safe.
func (res *Resolver) SetMasters(masters []string) {
res.masters = masters
}
// Reload triggers a new state load from the configured mesos masters.
// This method is not goroutine-safe.
func (res *Resolver) Reload() {
t := records.NewRecordGenerator(time.Duration(res.config.StateTimeoutSeconds) * time.Second)
err := t.ParseState(res.config, res.masters...)
if err == nil {
timestamp := uint32(time.Now().Unix())
// may need to refactor for fairness
res.rsLock.Lock()
defer res.rsLock.Unlock()
atomic.StoreUint32(&res.config.SOASerial, timestamp)
res.rs = t
} else {
logging.Error.Printf("Warning: Error generating records: %v; keeping old DNS state", err)
}
logging.PrintCurLog()
}
// formatSRV returns the SRV resource record for target
func (res *Resolver) formatSRV(name string, target string) (*dns.SRV, error) {
ttl := uint32(res.config.TTL)
h, port, err := net.SplitHostPort(target)
if err != nil {
return nil, errors.New("invalid target")
}
p, _ := strconv.Atoi(port)
return &dns.SRV{
Hdr: dns.RR_Header{
Name: name,
Rrtype: dns.TypeSRV,
Class: dns.ClassINET,
Ttl: ttl,
},
Priority: 0,
Weight: 0,
Port: uint16(p),
Target: h,
}, nil
}
// returns the A resource record for target
// assumes target is a well formed IPv4 address
func (res *Resolver) formatA(dom string, target string) (*dns.A, error) {
ttl := uint32(res.config.TTL)
a := net.ParseIP(target)
if a == nil {
return nil, errors.New("invalid target")
}
return &dns.A{
Hdr: dns.RR_Header{
Name: dom,
Rrtype: dns.TypeA,
Class: dns.ClassINET,
Ttl: ttl},
A: a.To4(),
}, nil
}
// formatSOA returns the SOA resource record for the mesos domain
func (res *Resolver) formatSOA(dom string) *dns.SOA {
ttl := uint32(res.config.TTL)
return &dns.SOA{
Hdr: dns.RR_Header{
Name: dom,
Rrtype: dns.TypeSOA,
Class: dns.ClassINET,
Ttl: ttl,
},
Ns: res.config.SOAMname,
Mbox: res.config.SOARname,
Serial: atomic.LoadUint32(&res.config.SOASerial),
Refresh: res.config.SOARefresh,
Retry: res.config.SOARetry,
Expire: res.config.SOAExpire,
Minttl: ttl,
}
}
// formatNS returns the NS record for the mesos domain
func (res *Resolver) formatNS(dom string) *dns.NS {
ttl := uint32(res.config.TTL)
return &dns.NS{
Hdr: dns.RR_Header{
Name: dom,
Rrtype: dns.TypeNS,
Class: dns.ClassINET,
Ttl: ttl,
},
Ns: res.config.SOAMname,
}
}
// reorders answers for very basic load balancing
func shuffleAnswers(rng *rand.Rand, answers []dns.RR) []dns.RR {
n := len(answers)
for i := 0; i < n; i++ {
r := i + rng.Intn(n-i)
answers[r], answers[i] = answers[i], answers[r]
}
return answers
}
// HandleNonMesos handles non-mesos queries by forwarding to configured
// external DNS servers.
func (res *Resolver) HandleNonMesos(w dns.ResponseWriter, r *dns.Msg) {
logging.CurLog.NonMesosRequests.Inc()
m, err := res.fwd(r, w.RemoteAddr().Network())
if err != nil {
m = new(dns.Msg).SetRcode(r, rcode(err))
} else if len(m.Answer) == 0 {
logging.CurLog.NonMesosNXDomain.Inc()
}
reply(w, m)
}
func rcode(err error) int {
switch err.(type) {
case *exchanger.ForwardError:
return dns.RcodeRefused
default:
return dns.RcodeServerFailure
}
}
// HandleMesos is a resolver request handler that responds to a resource
// question with resource answer(s)
// it can handle {A, SRV, ANY}
func (res *Resolver) HandleMesos(w dns.ResponseWriter, r *dns.Msg) {
logging.CurLog.MesosRequests.Inc()
m := &dns.Msg{MsgHdr: dns.MsgHdr{
Authoritative: true,
RecursionAvailable: res.config.RecurseOn,
}}
m.SetReply(r)
var errs multiError
rs := res.records()
name := strings.ToLower(cleanWild(r.Question[0].Name))
switch r.Question[0].Qtype {
case dns.TypeSRV:
errs.Add(res.handleSRV(rs, name, m, r))
case dns.TypeA:
errs.Add(res.handleA(rs, name, m))
case dns.TypeSOA:
errs.Add(res.handleSOA(m, r))
case dns.TypeNS:
errs.Add(res.handleNS(m, r))
case dns.TypeANY:
errs.Add(
res.handleSRV(rs, name, m, r),
res.handleA(rs, name, m),
res.handleSOA(m, r),
res.handleNS(m, r),
)
}
if len(m.Answer) == 0 {
errs.Add(res.handleEmpty(rs, name, m, r))
} else {
shuffleAnswers(res.rng, m.Answer)
logging.CurLog.MesosSuccess.Inc()
}
if !errs.Nil() {
logging.Error.Println(errs.Error())
logging.CurLog.MesosFailed.Inc()
}
reply(w, m)
}
func (res *Resolver) handleSRV(rs *records.RecordGenerator, name string, m, r *dns.Msg) error {
var errs multiError
for _, srv := range rs.SRVs[name] {
srvRR, err := res.formatSRV(r.Question[0].Name, srv)
if err != nil {
errs.Add(err)
continue
}
m.Answer = append(m.Answer, srvRR)
host := strings.Split(srv, ":")[0]
if len(rs.As[host]) == 0 {
continue
}
aRR, err := res.formatA(host, rs.As[host][0])
if err != nil {
errs.Add(err)
continue
}
m.Extra = append(m.Extra, aRR)
}
return errs
}
func (res *Resolver) handleA(rs *records.RecordGenerator, name string, m *dns.Msg) error {
var errs multiError
for _, a := range rs.As[name] {
rr, err := res.formatA(name, a)
if err != nil {
errs.Add(err)
continue
}
m.Answer = append(m.Answer, rr)
}
return errs
}
func (res *Resolver) handleSOA(m, r *dns.Msg) error {
m.Ns = append(m.Ns, res.formatSOA(r.Question[0].Name))
return nil
}
func (res *Resolver) handleNS(m, r *dns.Msg) error {
m.Ns = append(m.Ns, res.formatNS(r.Question[0].Name))
return nil
}
func (res *Resolver) handleEmpty(rs *records.RecordGenerator, name string, m, r *dns.Msg) error {
qType := r.Question[0].Qtype
switch qType {
case dns.TypeSOA, dns.TypeNS, dns.TypeSRV:
logging.CurLog.MesosSuccess.Inc()
return nil
}
m.Rcode = dns.RcodeNameError
// Because we don't implement AAAA records, AAAA queries will always
// go via this path
// Unfortunately, we don't implement AAAA queries in Mesos-DNS,
// and although the 'Not Implemented' error code seems more suitable,
// RFCs do not recommend it: https://tools.ietf.org/html/rfc4074
// Therefore we always return success, which is synonymous with NODATA
// to get a positive cache on no records AAAA
// Further information:
// PR: https://github.com/mesosphere/mesos-dns/pull/366
// Issue: https://github.com/mesosphere/mesos-dns/issues/363
// The second component is just a matter of returning NODATA if we have
// SRV or A records for the given name, but no neccessarily the given query
if (qType == dns.TypeAAAA) || (len(rs.SRVs[name])+len(rs.As[name]) > 0) {
m.Rcode = dns.RcodeSuccess
}
logging.CurLog.MesosNXDomain.Inc()
logging.VeryVerbose.Println("total A rrs:\t" + strconv.Itoa(len(rs.As)))
logging.VeryVerbose.Println("failed looking for " + r.Question[0].String())
m.Ns = append(m.Ns, res.formatSOA(r.Question[0].Name))
return nil
}
// reply writes the given dns.Msg out to the given dns.ResponseWriter,
// compressing the message first and truncating it accordingly.
func reply(w dns.ResponseWriter, m *dns.Msg) {
m.Compress = true // https://github.com/mesosphere/mesos-dns/issues/{170,173,174}
if err := w.WriteMsg(truncate(m, isUDP(w))); err != nil {
logging.Error.Println(err)
}
}
// isUDP returns true if the transmission channel in use is UDP.
func isUDP(w dns.ResponseWriter) bool {
return strings.HasPrefix(w.RemoteAddr().Network(), "udp")
}
// truncate removes answers until the given dns.Msg fits the permitted
// length of the given transmission channel and sets the TC bit.
// See https://tools.ietf.org/html/rfc1035#section-4.2.1
func truncate(m *dns.Msg, udp bool) *dns.Msg {
max := dns.MinMsgSize
if !udp {
max = dns.MaxMsgSize
} else if opt := m.IsEdns0(); opt != nil {
max = int(opt.UDPSize())
}
m.Truncated = m.Len() > max
if !m.Truncated {
return m
}
m.Extra = nil // Drop all extra records first
if m.Len() < max {
return m
}
answers := m.Answer[:]
left, right := 0, len(m.Answer)
for {
if left == right {
break
}
mid := (left + right) / 2
m.Answer = answers[:mid]
if m.Len() < max {
left = mid + 1
continue
}
right = mid
}
return m
}
func (res *Resolver) configureHTTP() {
// webserver + available routes
ws := new(restful.WebService)
ws.Route(ws.GET("/v1/version").To(res.RestVersion))
ws.Route(ws.GET("/v1/config").To(res.RestConfig))
ws.Route(ws.GET("/v1/hosts/{host}").To(res.RestHost))
ws.Route(ws.GET("/v1/hosts/{host}/ports").To(res.RestPorts))
ws.Route(ws.GET("/v1/services/{service}").To(res.RestService))
restful.Add(ws)
}
// LaunchHTTP starts an HTTP server for the Resolver, returning a error channel
// to which errors are asynchronously sent.
func (res *Resolver) LaunchHTTP() <-chan error {
defer util.HandleCrash()
res.configureHTTP()
portString := ":" + strconv.Itoa(res.config.HTTPPort)
errCh := make(chan error, 1)
go func() {
var err error
defer func() { errCh <- err }()
if err = http.ListenAndServe(portString, nil); err != nil {
err = fmt.Errorf("Failed to setup http server: %v", err)
} else {
logging.Error.Println("Not serving http requests any more.")
}
}()
return errCh
}
// RestConfig handles HTTP requests of Resolver configuration.
func (res *Resolver) RestConfig(req *restful.Request, resp *restful.Response) {
if err := resp.WriteAsJson(res.config); err != nil {
logging.Error.Println(err)
}
}
// RestVersion handles HTTP requests of Mesos-DNS version.
func (res *Resolver) RestVersion(req *restful.Request, resp *restful.Response) {
err := resp.WriteAsJson(map[string]string{
"Service": "Mesos-DNS",
"Version": res.version,
"URL": "https://github.com/mesosphere/mesos-dns",
})
if err != nil {
logging.Error.Println(err)
}
}
// RestHost handles HTTP requests of DNS A records of the given host.
func (res *Resolver) RestHost(req *restful.Request, resp *restful.Response) {
host := req.PathParameter("host")
// clean up host name
dom := strings.ToLower(cleanWild(host))
if dom[len(dom)-1] != '.' {
dom += "."
}
rs := res.records()
type record struct {
Host string `json:"host"`
IP string `json:"ip"`
}
aRRs := rs.As[dom]
records := make([]record, 0, len(aRRs))
for _, ip := range aRRs {
records = append(records, record{dom, ip})
}
if len(records) == 0 {
records = append(records, record{})
}
if err := resp.WriteAsJson(records); err != nil {
logging.Error.Println(err)
}
stats(dom, res.config.Domain+".", len(aRRs) > 0)
}
func stats(domain, zone string, success bool) {
if strings.HasSuffix(domain, zone) {
logging.CurLog.MesosRequests.Inc()
if success {
logging.CurLog.MesosSuccess.Inc()
} else {
logging.CurLog.MesosNXDomain.Inc()
}
} else {
logging.CurLog.NonMesosRequests.Inc()
logging.CurLog.NonMesosFailed.Inc()
}
}
// RestPorts is an HTTP handler which is currently not implemented.
func (res *Resolver) RestPorts(req *restful.Request, resp *restful.Response) {
err := resp.WriteErrorString(http.StatusNotImplemented, "To be implemented...")
if err != nil {
logging.Error.Println(err)
}
}
// RestService handles HTTP requests of DNS SRV records for the given name.
func (res *Resolver) RestService(req *restful.Request, resp *restful.Response) {
service := req.PathParameter("service")
// clean up service name
dom := strings.ToLower(cleanWild(service))
if dom[len(dom)-1] != '.' {
dom += "."
}
rs := res.records()
type record struct {
Service string `json:"service"`
Host string `json:"host"`
IP string `json:"ip"`
Port string `json:"port"`
}
srvRRs := rs.SRVs[dom]
records := make([]record, 0, len(srvRRs))
for _, s := range srvRRs {
host, port, _ := net.SplitHostPort(s)
var ip string
if r := rs.As[host]; len(r) != 0 {
ip = r[0]
}
records = append(records, record{service, host, ip, port})
}
if len(records) == 0 {
records = append(records, record{})
}
if err := resp.WriteAsJson(records); err != nil {
logging.Error.Println(err)
}
stats(dom, res.config.Domain+".", len(srvRRs) > 0)
}
// panicRecover catches any panics from the resolvers and sets an error
// code of server failure
func panicRecover(f func(w dns.ResponseWriter, r *dns.Msg)) func(w dns.ResponseWriter, r *dns.Msg) {
return func(w dns.ResponseWriter, r *dns.Msg) {
defer func() {
if rec := recover(); rec != nil {
m := new(dns.Msg)
m.SetRcode(r, 2)
_ = w.WriteMsg(m)
logging.Error.Println(rec)
}
}()
f(w, r)
}
}
// cleanWild strips any wildcards out thus mapping cleanly to the
// original serviceName
func cleanWild(name string) string {
if strings.Contains(name, ".*") {
return strings.Replace(name, ".*", "", -1)
}
return name
}
type multiError []error
func (e *multiError) Add(err ...error) {
for _, e1 := range err {
if me, ok := e1.(multiError); ok {
*e = append(*e, me...)
} else if e1 != nil {
*e = append(*e, e1)
}
}
}
func (e multiError) Error() string {
errs := make([]string, len(e))
for i := range errs {
if e[i] != nil {
errs[i] = e[i].Error()
}
}
return strings.Join(errs, "; ")
}
func (e multiError) Nil() bool {
for _, err := range e {
if err != nil {
return false
}
}
return true
}