-
Notifications
You must be signed in to change notification settings - Fork 2
/
resolver.go
282 lines (237 loc) · 7.74 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
// @license
// Copyright (C) 2019 Dinko Korunic
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
package main
import (
"errors"
"fmt"
"net"
"strconv"
"strings"
"time"
"github.com/dkorunic/dnstrace/cache"
"github.com/dkorunic/dnstrace/hints"
"github.com/fatih/color"
"github.com/miekg/dns"
)
const (
defaultDNSTimeout = 2000 * time.Millisecond
)
var (
roots *hints.Hints
aCache *cache.Cache
ErrParseClientSubnet = errors.New("failure to parse client-subnet IP")
ErrIdMismatch = errors.New("id mismatch")
ErrResolve = errors.New("unable to resolve")
)
func doDNSQuery(qname string, qtype uint16, nsIP, nsLabel, zone string, rttIn time.Duration, sub bool) (time.Duration, error) {
fmt.Printf("Querying about %q/%v @ %v(%v, %q authority)\n", qname,
dns.TypeToString[qtype], nsIP, nsLabel, zone)
// Check cache first
if qtype == dns.TypeA {
if v, ok := aCache.Get(qname); ok {
fmt.Printf("Answer RTT: 0ms (cached from previous responses)\n")
if sub {
color.Green("Final response (sub-query):")
} else {
color.Green("Final response:")
}
for _, rr := range v {
color.Green("%v", rr)
}
return rttIn, nil
}
}
m := new(dns.Msg)
m.Id = dns.Id()
m.RecursionDesired = *recurse
m.Question = make([]dns.Question, 1)
m.Question[0] = dns.Question{Name: qname, Qtype: qtype, Qclass: dns.ClassINET}
c := new(dns.Client)
c.Timeout = defaultDNSTimeout
if *tcp {
c.Net = "tcp"
}
// Enable EDNS and 4096 buffer size; using Client-Subnet also requires EDNS
if *edns || *client != "" {
o := new(dns.OPT)
o.Hdr.Name = "."
o.Hdr.Rrtype = dns.TypeOPT
o.SetUDPSize(dns.DefaultMsgSize)
// Set EDNS Client-Subnet
if *client != "" {
e := &dns.EDNS0_SUBNET{
Code: dns.EDNS0SUBNET,
Address: net.ParseIP(*client),
Family: 1, // IP4
SourceNetmask: net.IPv4len * 8,
}
if e.Address == nil {
return rttIn, fmt.Errorf("%w: %v", ErrParseClientSubnet, *client)
}
if e.Address.To4() == nil {
e.Family = 2 // IP6
e.SourceNetmask = net.IPv6len * 8
}
o.Option = append(o.Option, e)
}
m.Extra = append(m.Extra, o)
}
r, rtt, err := c.Exchange(m, net.JoinHostPort(nsIP, strconv.Itoa(int(*port))))
Retry:
// Truncated responses and UDP timeouts are candidates for retry
if m.Truncated ||
(err != nil && strings.HasPrefix(err.Error(), "read udp") &&
strings.HasSuffix(err.Error(), "i/o timeout")) {
if *fallback {
// Enable EDNS and 4096 buffer size if previously not enabled
if !*edns {
color.Red("! Answer truncated, retrying with EDNS enabled and 4096 bytes as advertised payload size")
o := new(dns.OPT)
o.Hdr.Name = "."
o.Hdr.Rrtype = dns.TypeOPT
o.SetUDPSize(dns.DefaultMsgSize)
m.Extra = append(m.Extra, o)
r, rtt, err = c.Exchange(m, net.JoinHostPort(nsIP, strconv.Itoa(int(*port))))
*edns = true
goto Retry
} else {
// Retry with TCP
color.Red("! Answer truncated, retrying with TCP")
c.Net = "tcp"
r, rtt, err = c.Exchange(m, net.JoinHostPort(nsIP, strconv.Itoa(int(*port))))
*fallback = false
goto Retry
}
}
} else if err != nil {
return rttIn, err
}
// Response ID mismatch
if r.Id != m.Id {
return rttIn, fmt.Errorf("%w", ErrIdMismatch)
}
fmt.Printf("Answer RTT: %v from %v\n", rtt, nsIP)
// Update A cache from A type RRs in ANSWER section
for _, rr := range r.Answer {
if rr.Header().Rrtype == dns.TypeA {
if a, ok := rr.(*dns.A); ok {
rrn := strings.ToLower(rr.Header().Name)
aCache.Add(rrn, a)
}
}
}
// Update A cache from A type RRs in ADDITIONAL section
for _, rr := range r.Extra {
if rr.Header().Rrtype == dns.TypeA {
if a, ok := rr.(*dns.A); ok {
rrn := strings.ToLower(rr.Header().Name)
aCache.Add(rrn, a)
}
}
}
// Process ANSWER section
if len(r.Answer) > 0 {
// Process ANSWER RR subset matching query name and query type
rrTypeSub := getRRset(r.Answer, qname, qtype)
if len(rrTypeSub) > 0 {
if sub {
color.Green("Final response (sub-query):")
} else {
color.Green("Final response:")
}
for _, rr := range rrTypeSub {
color.Green("%v", rr)
}
return rttIn + rtt, nil
}
// Process ANSWER RR subset matching query name and CNAME
rrCnameSub := getRRset(r.Answer, qname, dns.TypeCNAME)
if len(rrCnameSub) > 0 {
color.Green("Got CNAME in response:")
for _, rr := range rrCnameSub {
if c, ok := rr.(*dns.CNAME); ok {
color.Green("%v", rr)
color.Cyan("~ Following CNAME: %q/CNAME -> %q", rr.Header().Name,
c.Target)
// In-zone CNAME target so we can continue with current nameserver
if strings.HasSuffix(c.Target, "."+zone) {
fmt.Printf("\n")
return doDNSQuery(c.Target, qtype, nsIP, nsLabel, zone, rtt+rttIn, sub)
}
// Start sub-query from the root nameservers
nsLabel, nsIP, _ = roots.GetRand()
color.Yellow("~ Out of zone CNAME target, sub-query will restart from \".\"")
fmt.Printf("\n")
return doDNSQuery(c.Target, qtype, nsIP, nsLabel, ".", rtt+rttIn, sub)
}
}
}
// End of response processing by default
return rttIn + rtt, nil
}
// Process AUTHORITY section
for _, ns := range r.Ns {
if t, ok := ns.(*dns.NS); ok {
nextNs := t.Ns
resolvedNs := false
// Attempt to match NS entries from AUTHORITY with A records in ADDITIONAL section
if v, ok := aCache.GetRand(nextNs); ok {
color.Cyan("+ Matched delegated NS and glue in additional section: %v(%v)",
v.A, v.Header().Name)
nextNs = v.A.String()
resolvedNs = true
}
// We haven't managed to match glue (AUTHORITY/ADDITIONAL section records), so we need to resolve A
// records for a NS in AUTHORITY section
if !resolvedNs && !*ignoresub {
color.Yellow("- No NS/glue match, we need extra lookups for %v", nextNs)
fmt.Printf("\n")
rLabel, rIP, _ := roots.GetRand()
rtt2, err := doDNSQuery(nextNs, dns.TypeA, rIP, rLabel, ".", 0, true)
if err != nil {
return rttIn, err
}
// Check cached response
if v, ok := aCache.GetRand(nextNs); ok {
nextNs = v.A.String()
rtt += rtt2
} else {
return rttIn, fmt.Errorf("%w: %q/A (following authority NS)", ErrResolve, nextNs)
}
}
fmt.Print("\n")
// Send query to the next server down the authority chain
return doDNSQuery(qname, qtype, nextNs, t.Ns, t.Header().Name, rttIn+rtt, sub)
}
}
return rtt + rttIn, fmt.Errorf("%w: %q/%v @ %v(%v)", ErrResolve, qname, dns.TypeToString[qtype],
nsIP, nsLabel)
}
// getRRset returns RR set matching the given qname and qtype
func getRRset(rr []dns.RR, qname string, qtype uint16) []dns.RR {
var rr1 []dns.RR
for _, rr := range rr {
if strings.EqualFold(rr.Header().Name, qname) && rr.Header().Rrtype == qtype {
rr1 = append(rr1, rr)
}
}
return rr1
}