forked from owasp-amass/amass
-
Notifications
You must be signed in to change notification settings - Fork 0
/
service.go
440 lines (369 loc) · 10.9 KB
/
service.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
// 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 core
import (
"errors"
"sync"
"time"
"github.com/OWASP/Amass/amass/utils"
)
const (
// ServiceRequestChanLength is the length of the chan that pulls requests off the queue.
ServiceRequestChanLength int = 1000
)
// Possible values for the AmassService.APIKeyRequired field.
const (
APIKeyRequired int = iota
APIKeyNotRequired
APIkeyOptional
)
// ServiceStats provides metrics from an Amass service.
type ServiceStats struct {
DNSQueriesPerSec int
NamesRemaining int
AddrsRemaining int
}
// Service is the object type for a service running within the Amass enumeration architecture.
type Service interface {
// Start the service
Start() error
OnStart() error
// Pause the service
Pause() error
OnPause() error
// Resume the service
Resume() error
OnResume() error
// Stop the service
Stop() error
OnStop() error
// Architecture is ready for more names
LowNumberOfNames() error
OnLowNumberOfNames() error
// Methods to support processing of DNSRequests
SendDNSRequest(req *DNSRequest)
DNSRequestChan() <-chan *DNSRequest
DNSRequestLen() int
// Methods to support processing of AddrRequests
SendAddrRequest(req *AddrRequest)
AddrRequestChan() <-chan *AddrRequest
AddrRequestLen() int
// Methods to support processing of ASNRequests
SendASNRequest(req *ASNRequest)
ASNRequestChan() <-chan *ASNRequest
ASNRequestLen() int
// Methods to support processing of WhoisRequests
SendWhoisRequest(req *WhoisRequest)
WhoisRequestChan() <-chan *WhoisRequest
WhoisRequestLen() int
IsActive() bool
SetActive()
// Returns channels that fire during Pause/Resume operations
PauseChan() <-chan struct{}
ResumeChan() <-chan struct{}
// Returns a channel that is closed when the service is stopped
Quit() <-chan struct{}
// String description of the service
String() string
// Returns the configuration for the enumeration this service supports
Config() *Config
// Returns the event bus that handles communication for the enumeration
Bus() *EventBus
// Returns current ServiceStats that provide performance metrics
Stats() *ServiceStats
}
// BaseService provides common mechanisms to all Amass services in the enumeration architecture.
// It is used to compose a type that completely meets the AmassService interface.
type BaseService struct {
name string
started bool
stopped bool
activeLock sync.Mutex
active time.Time
dnsQueue *utils.Queue
dnsRequests chan *DNSRequest
addrQueue *utils.Queue
addrRequests chan *AddrRequest
asnQueue *utils.Queue
asnRequests chan *ASNRequest
whoisQueue *utils.Queue
whoisRequests chan *WhoisRequest
pause chan struct{}
resume chan struct{}
quit chan struct{}
// The specific service embedding BaseAmassService
service Service
// The global configuration for the enumeration this service supports
config *Config
// The event bus that handles message passing for the enumeration
bus *EventBus
}
// NewBaseService returns an initialized BaseService object.
func NewBaseService(srv Service, name string, config *Config, bus *EventBus) *BaseService {
return &BaseService{
name: name,
active: time.Now(),
dnsQueue: utils.NewQueue(),
dnsRequests: make(chan *DNSRequest, ServiceRequestChanLength),
addrQueue: utils.NewQueue(),
addrRequests: make(chan *AddrRequest, ServiceRequestChanLength),
asnQueue: utils.NewQueue(),
asnRequests: make(chan *ASNRequest, ServiceRequestChanLength),
whoisQueue: utils.NewQueue(),
whoisRequests: make(chan *WhoisRequest, ServiceRequestChanLength),
pause: make(chan struct{}, 10),
resume: make(chan struct{}, 10),
quit: make(chan struct{}),
service: srv,
config: config,
bus: bus,
}
}
// Start calls the OnStart method implemented for the Service.
func (bas *BaseService) Start() error {
if bas.started {
return errors.New(bas.name + " has already been started")
} else if bas.stopped {
return errors.New(bas.name + " has been stopped")
}
bas.started = true
go bas.processDNSRequests()
go bas.processAddrRequests()
go bas.processASNRequests()
go bas.processWhoisRequests()
return bas.service.OnStart()
}
// OnStart is a placeholder that should be implemented by an Service
// that has code to execute during service start.
func (bas *BaseService) OnStart() error {
return nil
}
// Pause implements the Service interface
func (bas *BaseService) Pause() error {
err := bas.service.OnPause()
go func() {
bas.pause <- struct{}{}
}()
return err
}
// OnPause implements the Service interface
func (bas *BaseService) OnPause() error {
return nil
}
// Resume implements the Service interface
func (bas *BaseService) Resume() error {
err := bas.service.OnResume()
go func() {
bas.resume <- struct{}{}
}()
return err
}
// OnResume implements the Service interface
func (bas *BaseService) OnResume() error {
return nil
}
// Stop calls the OnStop method implemented for the Service.
func (bas *BaseService) Stop() error {
if bas.stopped {
return errors.New(bas.name + " has already been stopped")
}
bas.Resume()
err := bas.service.OnStop()
bas.stopped = true
close(bas.quit)
return err
}
// OnStop is a placeholder that should be implemented by a Service
// that has code to execute during service stop.
func (bas *BaseService) OnStop() error {
return nil
}
// LowNumberOfNames calls the OnLowNumberOfNames method implemented for the Service.
func (bas *BaseService) LowNumberOfNames() error {
err := bas.service.OnLowNumberOfNames()
return err
}
// OnLowNumberOfNames is a placeholder that should be implemented by a Service
// that has code to be executed when the enumeration is low in names to resolve.
func (bas *BaseService) OnLowNumberOfNames() error {
return nil
}
// SendDNSRequest adds the request provided by the parameter to the service request channel.
func (bas *BaseService) SendDNSRequest(req *DNSRequest) {
bas.dnsQueue.Append(req)
}
// DNSRequestChan returns the channel that provides new service requests.
func (bas *BaseService) DNSRequestChan() <-chan *DNSRequest {
return bas.dnsRequests
}
func (bas *BaseService) processDNSRequests() {
curIdx := 0
maxIdx := 7
delays := []int{25, 50, 75, 100, 150, 250, 500, 750}
for {
select {
case <-bas.Quit():
return
default:
element, ok := bas.dnsQueue.Next()
if !ok {
time.Sleep(time.Duration(delays[curIdx]) * time.Millisecond)
if curIdx < maxIdx {
curIdx++
}
continue
}
curIdx = 0
bas.dnsRequests <- element.(*DNSRequest)
}
}
}
// DNSRequestLen returns the current length of the request queue.
func (bas *BaseService) DNSRequestLen() int {
return bas.dnsQueue.Len()
}
// SendAddrRequest adds the request provided by the parameter to the service request channel.
func (bas *BaseService) SendAddrRequest(req *AddrRequest) {
bas.addrQueue.Append(req)
}
// AddrRequestChan returns the channel that provides new service requests.
func (bas *BaseService) AddrRequestChan() <-chan *AddrRequest {
return bas.addrRequests
}
func (bas *BaseService) processAddrRequests() {
curIdx := 0
maxIdx := 7
delays := []int{25, 50, 75, 100, 150, 250, 500, 750}
for {
select {
case <-bas.Quit():
return
default:
element, ok := bas.addrQueue.Next()
if !ok {
time.Sleep(time.Duration(delays[curIdx]) * time.Millisecond)
if curIdx < maxIdx {
curIdx++
}
continue
}
curIdx = 0
bas.addrRequests <- element.(*AddrRequest)
}
}
}
// AddrRequestLen returns the current length of the request queue.
func (bas *BaseService) AddrRequestLen() int {
return bas.addrQueue.Len()
}
// SendASNRequest adds the request provided by the parameter to the service request channel.
func (bas *BaseService) SendASNRequest(req *ASNRequest) {
bas.asnQueue.Append(req)
}
// ASNRequestChan returns the channel that provides new service requests.
func (bas *BaseService) ASNRequestChan() <-chan *ASNRequest {
return bas.asnRequests
}
func (bas *BaseService) processASNRequests() {
curIdx := 0
maxIdx := 7
delays := []int{25, 50, 75, 100, 150, 250, 500, 750}
for {
select {
case <-bas.Quit():
return
default:
element, ok := bas.asnQueue.Next()
if !ok {
time.Sleep(time.Duration(delays[curIdx]) * time.Millisecond)
if curIdx < maxIdx {
curIdx++
}
continue
}
curIdx = 0
bas.asnRequests <- element.(*ASNRequest)
}
}
}
// ASNRequestLen returns the current length of the request queue.
func (bas *BaseService) ASNRequestLen() int {
return bas.asnQueue.Len()
}
// SendWhoisRequest adds the request provided by the parameter to the service request channel.
func (bas *BaseService) SendWhoisRequest(req *WhoisRequest) {
bas.whoisQueue.Append(req)
}
// WhoisRequestChan returns the channel that provides new service requests.
func (bas *BaseService) WhoisRequestChan() <-chan *WhoisRequest {
return bas.whoisRequests
}
func (bas *BaseService) processWhoisRequests() {
curIdx := 0
maxIdx := 7
delays := []int{25, 50, 75, 100, 150, 250, 500, 750}
for {
select {
case <-bas.Quit():
return
default:
element, ok := bas.whoisQueue.Next()
if !ok {
time.Sleep(time.Duration(delays[curIdx]) * time.Millisecond)
if curIdx < maxIdx {
curIdx++
}
continue
}
curIdx = 0
bas.whoisRequests <- element.(*WhoisRequest)
}
}
}
// WhoisRequestLen returns the current length of the request queue.
func (bas *BaseService) WhoisRequestLen() int {
return bas.whoisQueue.Len()
}
// IsActive returns true if SetActive has been called for the service within the last 3 seconds.
func (bas *BaseService) IsActive() bool {
bas.activeLock.Lock()
defer bas.activeLock.Unlock()
if time.Now().Sub(bas.active) > 5*time.Second {
return false
}
return true
}
// SetActive marks the service as being active at time.Now() for future checks performed by the IsActive method.
func (bas *BaseService) SetActive() {
bas.activeLock.Lock()
defer bas.activeLock.Unlock()
bas.active = time.Now()
}
// PauseChan returns the pause channel for the service.
func (bas *BaseService) PauseChan() <-chan struct{} {
return bas.pause
}
// ResumeChan returns the resume channel for the service.
func (bas *BaseService) ResumeChan() <-chan struct{} {
return bas.resume
}
// Quit return the quit channel for the service.
func (bas *BaseService) Quit() <-chan struct{} {
return bas.quit
}
// String returns the name of the service.
func (bas *BaseService) String() string {
return bas.name
}
// Config returns the Config for the enumeration this service supports.
func (bas *BaseService) Config() *Config {
return bas.config
}
// Bus returns the EventBus that handles communication for the enumeration.
func (bas *BaseService) Bus() *EventBus {
return bas.bus
}
// Stats returns current ServiceStats that provide performance metrics
func (bas *BaseService) Stats() *ServiceStats {
return new(ServiceStats)
}