forked from owasp-amass/amass
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathresolver_state.go
422 lines (359 loc) · 8.7 KB
/
resolver_state.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
// Copyright 2017 Jeff Foley. All rights reserved.
// Use of this source code is governed by Apache 2 LICENSE that can be found in the LICENSE file.
package resolvers
import (
"net"
"time"
"github.com/miekg/dns"
)
// Index values into the stats map.
const (
QueryAttempts = 64
QueryTimeouts = 65
QueryRTT = 66
QueryCompletions = 67
)
const defaultConnRotation = 30 * time.Second
type resolverStopped struct {
Stopped chan bool
}
type updateResolverStat struct {
Stat int
Amount int64
}
type getResolverStat struct {
Stat int
Ch chan int64
}
type allResolverStats struct {
MapCh chan map[int]int64
}
type resolverStateChans struct {
Done chan struct{}
StopResolver chan struct{}
StoppedState chan *resolverStopped
UpdateRTT chan time.Duration
AddToStat chan *updateResolverStat
GetStat chan *getResolverStat
SetStat chan *updateResolverStat
AllStats chan *allResolverStats
ClearStats chan struct{}
}
func initStateManagement() *resolverStateChans {
stateChs := &resolverStateChans{
Done: make(chan struct{}, 2),
StopResolver: make(chan struct{}, 2),
StoppedState: make(chan *resolverStopped, 10),
UpdateRTT: make(chan time.Duration, 10),
AddToStat: make(chan *updateResolverStat, 10),
GetStat: make(chan *getResolverStat, 10),
SetStat: make(chan *updateResolverStat),
AllStats: make(chan *allResolverStats, 10),
ClearStats: make(chan struct{}, 10),
}
go manageResolverState(stateChs)
return stateChs
}
func manageResolverState(chs *resolverStateChans) {
var stopped bool
var numrtt int64
stats := make(map[int]int64)
for {
select {
case <-chs.Done:
return
case <-chs.StopResolver:
stopped = true
case isst := <-chs.StoppedState:
isst.Stopped <- stopped
case rtt := <-chs.UpdateRTT:
numrtt++
avg := stats[QueryRTT]
stats[QueryRTT] = avg + ((int64(rtt) - avg) / numrtt)
case add := <-chs.AddToStat:
stats[add.Stat] = stats[add.Stat] + add.Amount
case get := <-chs.GetStat:
get.Ch <- stats[get.Stat]
case set := <-chs.SetStat:
stats[set.Stat] = set.Amount
case all := <-chs.AllStats:
c := make(map[int]int64)
for k, v := range stats {
c[k] = v
}
all.MapCh <- c
case <-chs.ClearStats:
numrtt = 0
for k := range stats {
stats[k] = 0
}
}
}
}
// IsStopped implements the Resolver interface.
func (r *BaseResolver) IsStopped() bool {
ch := make(chan bool, 2)
r.stateChannels.StoppedState <- &resolverStopped{Stopped: ch}
return <-ch
}
// Stop causes the Resolver to stop sending DNS queries and closes the network connection.
func (r *BaseResolver) Stop() error {
if r.IsStopped() {
return nil
}
r.stateChannels.StopResolver <- struct{}{}
close(r.Done)
close(r.stateChannels.Done)
close(r.xchgsChannels.Done)
return nil
}
func (r *BaseResolver) updateTimeouts(t int) {
r.stateChannels.AddToStat <- &updateResolverStat{
Stat: QueryTimeouts,
Amount: int64(t),
}
}
func (r *BaseResolver) updateAttempts() {
r.stateChannels.AddToStat <- &updateResolverStat{
Stat: QueryAttempts,
Amount: 1,
}
}
func (r *BaseResolver) updateRTT(rtt time.Duration) {
r.stateChannels.UpdateRTT <- rtt
}
func (r *BaseResolver) updateStat(rcode int, value int64) {
r.stateChannels.AddToStat <- &updateResolverStat{
Stat: rcode,
Amount: value,
}
}
func (r *BaseResolver) getStat(stat int) int64 {
ch := make(chan int64, 2)
r.stateChannels.GetStat <- &getResolverStat{
Stat: stat,
Ch: ch,
}
return <-ch
}
// Stats returns performance counters.
func (r *BaseResolver) Stats() map[int]int64 {
ch := make(chan map[int]int64, 2)
r.stateChannels.AllStats <- &allResolverStats{MapCh: ch}
return <-ch
}
// WipeStats clears the performance counters.
func (r *BaseResolver) WipeStats() {
r.stateChannels.ClearStats <- struct{}{}
}
type pullRequestMsg struct {
ID uint16
Timeout time.Duration
Ch chan *resolveRequest
}
type updateTimeoutMsg struct {
ID uint16
Timeout time.Time
}
type xchgsChans struct {
Done chan struct{}
GetID chan chan uint16
AddTimeout chan uint16
DelTimeout chan uint16
UpdateTimeout chan *updateTimeoutMsg
AllTimeoutIDs chan chan []uint16
AddRequest chan *resolveRequest
PullRequest chan *pullRequestMsg
}
func initXchgsManagement() *xchgsChans {
xchgsChs := &xchgsChans{
Done: make(chan struct{}, 2),
GetID: make(chan chan uint16, 10),
AddTimeout: make(chan uint16, 10),
DelTimeout: make(chan uint16, 10),
UpdateTimeout: make(chan *updateTimeoutMsg, 10),
AllTimeoutIDs: make(chan chan []uint16, 10),
AddRequest: make(chan *resolveRequest, 10),
PullRequest: make(chan *pullRequestMsg, 10),
}
go manageXchgState(xchgsChs)
return xchgsChs
}
func manageXchgState(chs *xchgsChans) {
var timeouts []uint16
xchgs := make(map[uint16]*resolveRequest)
for {
select {
case <-chs.Done:
return
case c := <-chs.GetID:
var id uint16
for {
id = dns.Id()
if _, found := xchgs[id]; !found {
xchgs[id] = &resolveRequest{Timestamp: time.Now()}
break
}
}
c <- id
case addTimeout := <-chs.AddTimeout:
timeouts = append(timeouts, addTimeout)
case delTimeout := <-chs.DelTimeout:
var n int
// Remove the element identified by id
for _, i := range timeouts {
if i != delTimeout {
timeouts[n] = i
n++
}
}
timeouts = timeouts[:n]
case ut := <-chs.UpdateTimeout:
if req, found := xchgs[ut.ID]; found {
req.Timestamp = ut.Timeout
}
case all := <-chs.AllTimeoutIDs:
var ids []uint16
for _, id := range timeouts {
ids = append(ids, id)
}
all <- ids
case addReq := <-chs.AddRequest:
xchgs[addReq.ID] = addReq
case pReq := <-chs.PullRequest:
tPassed := true
r, found := xchgs[pReq.ID]
if found && pReq.Timeout != 0 && time.Now().Before(r.Timestamp.Add(pReq.Timeout)) {
tPassed = false
}
if found && tPassed {
delete(xchgs, pReq.ID)
pReq.Ch <- r
} else {
pReq.Ch <- nil
}
}
}
}
func (r *BaseResolver) addTimeout(id uint16) {
r.xchgsChannels.AddTimeout <- id
}
func (r *BaseResolver) delTimeout(id uint16) {
r.xchgsChannels.DelTimeout <- id
}
func (r *BaseResolver) allTimeoutIDs() []uint16 {
ch := make(chan []uint16, 2)
r.xchgsChannels.AllTimeoutIDs <- ch
return <-ch
}
func (r *BaseResolver) getID() uint16 {
ch := make(chan uint16, 2)
r.xchgsChannels.GetID <- ch
return <-ch
}
func (r *BaseResolver) queueRequest(id uint16, req *resolveRequest) {
req.ID = id
req.Timestamp = time.Now()
r.xchgsChannels.AddRequest <- req
r.addTimeout(id)
}
func (r *BaseResolver) pullRequest(id uint16) *resolveRequest {
ch := make(chan *resolveRequest, 2)
r.delTimeout(id)
r.xchgsChannels.PullRequest <- &pullRequestMsg{
ID: id,
Ch: ch,
}
return <-ch
}
func (r *BaseResolver) pullRequestAfterTimeout(id uint16, timeout time.Duration) *resolveRequest {
ch := make(chan *resolveRequest, 2)
r.xchgsChannels.PullRequest <- &pullRequestMsg{
ID: id,
Timeout: timeout,
Ch: ch,
}
req := <-ch
if req != nil {
r.delTimeout(id)
}
return req
}
func (r *BaseResolver) updateRequestTimeout(id uint16, timeout time.Time) {
r.delTimeout(id)
r.xchgsChannels.UpdateTimeout <- &updateTimeoutMsg{
ID: id,
Timeout: timeout,
}
r.addTimeout(id)
}
type rotationChans struct {
Rotate chan struct{}
Current chan chan *dns.Conn
Last chan chan *dns.Conn
}
func initRotationChans() *rotationChans {
return &rotationChans{
Rotate: make(chan struct{}, 10),
Current: make(chan chan *dns.Conn, 10),
Last: make(chan chan *dns.Conn, 10),
}
}
func (r *BaseResolver) periodicRotations(chs *rotationChans) {
var current, last net.Conn
t := time.NewTicker(defaultConnRotation)
defer t.Stop()
for {
select {
case <-r.Done:
if current != nil {
current.Close()
}
if last != nil {
last.Close()
}
return
case <-t.C:
go r.rotateConnections()
case lch := <-chs.Last:
if last == nil {
lch <- nil
} else {
lch <- &dns.Conn{Conn: last}
}
case cch := <-chs.Current:
if current == nil {
cch <- nil
} else {
cch <- &dns.Conn{Conn: current}
}
case <-chs.Rotate:
if last != nil {
last.Close()
}
last = current
var err error
for {
d := &net.Dialer{}
current, err = d.Dial("udp", r.address+":"+r.port)
if err == nil {
break
}
time.Sleep(time.Duration(randomInt(1, 10)) * time.Millisecond)
}
}
}
}
func (r *BaseResolver) rotateConnections() {
r.rotationChannels.Rotate <- struct{}{}
}
func (r *BaseResolver) currentConnection() *dns.Conn {
ch := make(chan *dns.Conn, 2)
r.rotationChannels.Current <- ch
return <-ch
}
func (r *BaseResolver) lastConnection() *dns.Conn {
ch := make(chan *dns.Conn, 2)
r.rotationChannels.Last <- ch
return <-ch
}