forked from rancher/rancher-dns
/
answers.go
190 lines (161 loc) · 5.4 KB
/
answers.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
package main
import (
"math/rand"
"net"
log "github.com/Sirupsen/logrus"
"github.com/miekg/dns"
)
// The top-level key in the JSON for the default (not client-specific answers)
const DEFAULT_KEY = "default"
// The 2nd-level key in the JSON for the recursive resolver addresses
const RECURSE_KEY = "recurse"
func (answers *Answers) Recursers(clientIp string) []string {
var hosts []string
more := answers.recursersFor(clientIp)
if len(more) > 0 {
hosts = append(hosts, more...)
}
more = answers.recursersFor(DEFAULT_KEY)
if len(more) > 0 {
hosts = append(hosts, more...)
}
return hosts
}
func (answers *Answers) recursersFor(clientIp string) []string {
var hosts []string
client, ok := (*answers)[clientIp]
if ok {
if ok && len(client.Recurse) > 0 {
hosts = client.Recurse
}
}
return hosts
}
func (answers *Answers) Addresses(clientIp string, fqdn string, cnameParents []dns.RR, depth int) (records []dns.RR, ok bool) {
fqdn = dns.Fqdn(fqdn)
log.WithFields(log.Fields{"fqdn": fqdn, "client": clientIp, "depth": depth}).Debug("Trying to resolve addresses")
// Limit recursing for non-obvious loops
if len(cnameParents) >= 10 {
log.WithFields(log.Fields{"fqdn": fqdn, "client": clientIp}).Warn("Followed CNAME too many times ", cnameParents)
return nil, false
}
// Look for a CNAME entry
result, ok := answers.Matching(dns.TypeCNAME, clientIp, fqdn)
if ok && len(result) > 0 {
cname := result[0].(*dns.CNAME)
log.WithFields(log.Fields{"fqdn": fqdn, "client": clientIp}).Debug("Matched CNAME ", cname.Target)
// Stop obvious loops
if dns.Fqdn(cname.Target) == fqdn {
log.WithFields(log.Fields{"fqdn": fqdn, "client": clientIp}).Warn("CNAME is a loop ", cname.Target)
return nil, false
}
// Recurse to find the eventual A for this CNAME
children, ok := answers.Addresses(clientIp, dns.Fqdn(cname.Target), append(cnameParents, cname), depth+1)
if ok && len(children) > 0 {
log.WithFields(log.Fields{"fqdn": fqdn, "target": cname.Target, "client": clientIp}).Debug("Resolved CNAME ", children)
records = append(records, cname)
records = append(records, children...)
return records, true
}
}
// Look for an A entry
result, ok = answers.Matching(dns.TypeA, clientIp, fqdn)
if ok && len(result) > 0 {
log.WithFields(log.Fields{"fqdn": fqdn, "client": clientIp}).Debug("Matched A ", result)
shuffle(&result)
return result, true
}
// Try the default section of the config
if clientIp != DEFAULT_KEY {
return answers.Addresses(DEFAULT_KEY, fqdn, cnameParents, depth+1)
}
// When resolving CNAMES, check recursive server
if len(cnameParents) > 0 {
log.WithFields(log.Fields{"fqdn": fqdn, "client": clientIp}).Debug("Trying recursive servers")
r := new(dns.Msg)
r.SetQuestion(fqdn, dns.TypeA)
msg, err := ResolveTryAll(r, answers.Recursers(clientIp))
if err == nil {
return msg.Answer, true
}
}
log.WithFields(log.Fields{"fqdn": fqdn, "client": clientIp}).Debug("Did not match anything")
return nil, false
}
func (answers *Answers) Matching(qtype uint16, clientIp string, fqdn string) (records []dns.RR, ok bool) {
client, ok := (*answers)[clientIp]
if ok {
switch qtype {
case dns.TypeA:
log.WithFields(log.Fields{"qtype": "A", "client": clientIp, "fqdn": fqdn}).Debug("Searching for A")
res, ok := client.A[fqdn]
if ok && len(res.Answer) > 0 {
ttl := uint32(*defaultTtl)
if res.Ttl != nil {
ttl = *res.Ttl
}
for i := 0; i < len(res.Answer); i++ {
hdr := dns.RR_Header{Name: fqdn, Rrtype: dns.TypeA, Class: dns.ClassINET, Ttl: ttl}
ip := net.ParseIP(res.Answer[i])
record := &dns.A{Hdr: hdr, A: ip}
records = append(records, record)
}
shuffle(&records)
}
case dns.TypeCNAME:
log.WithFields(log.Fields{"qtype": "CNAME", "client": clientIp, "fqdn": fqdn}).Debug("Searching for CNAME")
res, ok := client.Cname[fqdn]
ttl := uint32(*defaultTtl)
if res.Ttl != nil {
ttl = *res.Ttl
}
if ok {
hdr := dns.RR_Header{Name: fqdn, Rrtype: dns.TypeCNAME, Class: dns.ClassINET, Ttl: ttl}
record := &dns.CNAME{Hdr: hdr, Target: res.Answer}
records = append(records, record)
}
case dns.TypePTR:
log.WithFields(log.Fields{"qtype": "PTR", "client": clientIp, "fqdn": fqdn}).Debug("Searching for PTR")
res, ok := client.Ptr[fqdn]
ttl := uint32(*defaultTtl)
if res.Ttl != nil {
ttl = *res.Ttl
}
if ok {
hdr := dns.RR_Header{Name: fqdn, Rrtype: dns.TypePTR, Class: dns.ClassINET, Ttl: ttl}
record := &dns.PTR{Hdr: hdr, Ptr: res.Answer}
records = append(records, record)
}
case dns.TypeTXT:
log.WithFields(log.Fields{"qtype": "TXT", "client": clientIp, "fqdn": fqdn}).Debug("Searching for TXT")
res, ok := client.Txt[fqdn]
ttl := uint32(*defaultTtl)
if res.Ttl != nil {
ttl = *res.Ttl
}
if ok {
for i := 0; i < len(res.Answer); i++ {
hdr := dns.RR_Header{Name: fqdn, Rrtype: dns.TypeTXT, Class: dns.ClassINET, Ttl: ttl}
str := res.Answer[i]
if len(str) > 255 {
log.WithFields(log.Fields{"qtype": "TXT", "client": clientIp, "fqdn": fqdn}).Warn("TXT record too long: ", str)
return nil, false
}
record := &dns.TXT{Hdr: hdr, Txt: []string{str}}
records = append(records, record)
}
}
}
}
if len(records) > 0 {
return records, true
} else {
return nil, false
}
}
func shuffle(items *[]dns.RR) {
for i := range *items {
j := rand.Intn(i + 1)
(*items)[i], (*items)[j] = (*items)[j], (*items)[i]
}
}