/
service.go
275 lines (226 loc) · 6.46 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
// 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
SendRequest(req *Request)
RequestChan() <-chan *Request
IsActive() bool
SetActive()
// Returns channels that fire during Pause/Resume operations
PauseChan() <-chan struct{}
ResumeChan() <-chan struct{}
RequestLen() int
// 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
queue *utils.Queue
requests chan *Request
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(),
queue: utils.NewQueue(),
requests: make(chan *Request, 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.processRequests()
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 an Service
// that has code to execute during service stop.
func (bas *BaseService) OnStop() error {
return nil
}
// SendRequest adds the request provided by the parameter to the service request channel.
func (bas *BaseService) SendRequest(req *Request) {
bas.queue.Append(req)
}
// RequestChan returns the channel that provides new service requests.
func (bas *BaseService) RequestChan() <-chan *Request {
return bas.requests
}
func (bas *BaseService) processRequests() {
curIdx := 0
maxIdx := 7
delays := []int{25, 50, 75, 100, 150, 250, 500, 750}
for {
select {
case <-bas.Quit():
return
default:
element, ok := bas.queue.Next()
if !ok {
time.Sleep(time.Duration(delays[curIdx]) * time.Millisecond)
if curIdx < maxIdx {
curIdx++
}
continue
}
curIdx = 0
bas.requests <- element.(*Request)
}
}
}
// RequestLen returns the current length of the request queue.
func (bas *BaseService) RequestLen() int {
return bas.queue.Len()
}
// IsActive returns true if SetActive has been called for the service within the last 10 seconds.
func (bas *BaseService) IsActive() bool {
bas.activeLock.Lock()
defer bas.activeLock.Unlock()
if time.Now().Sub(bas.active) > 10*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)
}