-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathresolve.go
124 lines (106 loc) · 3.38 KB
/
resolve.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
package main
import (
"fmt"
"net"
"strings"
"time"
"github.com/miekg/dns"
)
func (h *handler) ServeDNS(w dns.ResponseWriter, r *dns.Msg) {
h.log.Info("received dns request", "req", r)
// TODO: handle multiple questions?
// Not handling it right now as most of the DNS resolver handles only first question and typeA is our only use case now.
// Even with multiple DNS questions, the resulting MsgHdr has only global Rcode not for each Question.
switch r.Question[0].Qtype {
case dns.TypeA:
h.log.Info("handling A question", "domain", r.Question[0].Name)
names := h.dnsNamesToUse(r.Question[0].Name)
res, err := h.resolveDnsNames(names)
if err != nil {
h.log.Info("no resolv error, sending error")
if res != nil {
res.SetReply(r)
w.WriteMsg(res)
} else {
m := new(dns.Msg)
m.SetReply(r)
m.SetRcode(r, dns.RcodeServerFailure)
w.WriteMsg(m)
}
return
}
if a, ok := res.Answer[0].(*dns.A); ok {
a.Hdr.Name = r.Question[0].Name
}
res.SetReply(r)
h.log.Info("sending response", "response", res)
w.WriteMsg(res)
}
}
func (h *handler) resolveDnsNames(names []string) (*dns.Msg, error) {
dnsClient := h.dnsClient
dnsServer := h.dnsServer
for _, name := range names {
h.log.Info("forwarding request", "resolver", dnsServer)
attempt := 1
Redo:
dnsRequest := new(dns.Msg)
dnsRequest.SetQuestion(name, dns.TypeA)
dnsRequest.SetEdns0(4096, true)
h.log.Info("doing dns lookup", "req", dnsRequest)
ans, rtt, err := dnsClient.Exchange(dnsRequest, dnsServer)
if err != nil {
if inputConfig.MaxRetries >= attempt {
backOffTime := h.backoff.Next(attempt)
time.Sleep(backOffTime)
h.log.Info(fmt.Sprintf("Received Error: %v. Retrying dns lookup. Attempt: %v after %v", err, attempt, backOffTime))
// if it is a dial error, reset dnsServer to inputConfig.dnsServer
if _, ok := err.(*net.OpError); ok {
dnsServer = inputConfig.dnsServer
}
attempt++
goto Redo
}
h.log.Error(err, "cannot resolve dns", "dns-request", dnsRequest)
return nil, err
}
if ans.MsgHdr.Truncated {
if inputConfig.MaxRetries >= attempt {
dnsClient = new(dns.Client)
dnsClient.Net = "tcp"
h.log.Info(fmt.Sprintf("Received Truncated response. Retrying dns lookup. Attempt: %v", attempt))
attempt++
goto Redo
}
}
if len(ans.Answer) == 0 {
h.log.Info("lookup failed with no answers, ignoring this domain", "domain", name)
continue
}
h.log.Info("dns lookup finished", "ans-rcode", ans.MsgHdr.Rcode, "resp-time", rtt)
switch ans.MsgHdr.Rcode {
case dns.RcodeNameError:
h.log.Info("lookup failed with nxdomain, ignoring this domain", "domain", name)
continue
case dns.RcodeServerFailure:
return ans, fmt.Errorf("Name server encountered an internal failure while processing this request (SERVFAIL)")
case dns.RcodeRefused:
return ans, fmt.Errorf("Name server refused to process the request (REFUSED)")
case dns.RcodeSuccess:
return ans, nil
default:
return ans, fmt.Errorf("Name server returned error, rcode=%v", ans.MsgHdr.Rcode)
}
}
return nil, fmt.Errorf("cannot resolve dns name(s): %v", names)
}
func (h *handler) dnsNamesToUse(s string) []string {
// currentPhysicalZoneId = "use1-az1"
if currentPhysicalZoneId == "" {
return []string{s}
}
if !strings.HasSuffix(s, ".") {
s = s + "."
}
return []string{currentPhysicalZoneId + inputConfig.prefixSeparator + s, s}
}