-
Notifications
You must be signed in to change notification settings - Fork 108
/
engine.go
248 lines (213 loc) · 5.02 KB
/
engine.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
//go:generate mockgen -package scan -destination=mock_engine_test.go . PacketSource,Scanner
package scan
import (
"context"
"net"
"sync"
"time"
"github.com/v-byte-cpu/sx/pkg/packet"
)
type PortRange struct {
StartPort uint16
EndPort uint16
}
type Range struct {
Interface *net.Interface
DstSubnet *net.IPNet
SrcIP net.IP
SrcMAC net.HardwareAddr
Ports []*PortRange
}
type Engine interface {
Start(ctx context.Context, r *Range) (done <-chan interface{}, errc <-chan error)
}
type Resulter interface {
Results() <-chan Result
}
type EngineResulter interface {
Engine
Resulter
}
type engineResulter struct {
Engine
Resulter
}
func NewEngineResulter(e Engine, r Resulter) EngineResulter {
return &engineResulter{Engine: e, Resulter: r}
}
type PacketSource interface {
Packets(ctx context.Context, r *Range) <-chan *packet.BufferData
}
func NewPacketSource(reqgen RequestGenerator, pktgen PacketGenerator) PacketSource {
return &packetSource{reqgen, pktgen}
}
type packetSource struct {
reqgen RequestGenerator
pktgen PacketGenerator
}
func (s *packetSource) Packets(ctx context.Context, r *Range) <-chan *packet.BufferData {
requests, err := s.reqgen.GenerateRequests(ctx, r)
if err != nil {
out := make(chan *packet.BufferData, 1)
out <- &packet.BufferData{Err: err}
close(out)
return out
}
return s.pktgen.Packets(ctx, requests)
}
type PacketEngine struct {
src PacketSource
snd packet.Sender
rcv packet.Receiver
}
func NewPacketEngine(ps PacketSource, s packet.Sender, r packet.Receiver) *PacketEngine {
return &PacketEngine{src: ps, snd: s, rcv: r}
}
func (e *PacketEngine) Start(ctx context.Context, r *Range) (<-chan interface{}, <-chan error) {
packets := e.src.Packets(ctx, r)
done, errc1 := e.snd.SendPackets(ctx, packets)
errc2 := e.rcv.ReceivePackets(ctx)
return done, mergeErrChan(ctx, errc1, errc2)
}
// generics would be helpful :)
func mergeErrChan(ctx context.Context, channels ...<-chan error) <-chan error {
var wg sync.WaitGroup
wg.Add(len(channels))
out := make(chan error, 100)
multiplex := func(c <-chan error) {
defer wg.Done()
for {
select {
case <-ctx.Done():
return
case e, ok := <-c:
if !ok {
return
}
writeError(ctx, out, e)
}
}
}
for _, c := range channels {
go multiplex(c)
}
go func() {
wg.Wait()
close(out)
}()
return out
}
type PacketMethod interface {
PacketSource
packet.Processor
Resulter
}
func SetupPacketEngine(rw packet.ReadWriter, m PacketMethod) EngineResulter {
sender := packet.NewSender(rw)
receiver := packet.NewReceiver(rw, m)
engine := NewPacketEngine(m, sender, receiver)
return NewEngineResulter(engine, m)
}
type Scanner interface {
Scan(ctx context.Context, r *Request) (Result, error)
}
type RateLimiter interface {
// Take should block to make sure that the RPS is met.
Take() time.Time
}
type rateLimitScanner struct {
Scanner
limiter RateLimiter
}
func NewRateLimitScanner(delegate Scanner, limiter RateLimiter) Scanner {
return &rateLimitScanner{Scanner: delegate, limiter: limiter}
}
func (s *rateLimitScanner) Scan(ctx context.Context, r *Request) (Result, error) {
s.limiter.Take()
return s.Scanner.Scan(ctx, r)
}
type GenericEngine struct {
reqgen RequestGenerator
scanner Scanner
results ResultChan
workerCount int
}
// Assert that GenericEngine conforms to the scan.EngineResulter interface
var _ EngineResulter = (*GenericEngine)(nil)
type GenericEngineOption func(s *GenericEngine)
func WithScanWorkerCount(workerCount int) GenericEngineOption {
return func(s *GenericEngine) {
s.workerCount = workerCount
}
}
func NewScanEngine(reqgen RequestGenerator,
scanner Scanner, results ResultChan, opts ...GenericEngineOption) *GenericEngine {
s := &GenericEngine{
reqgen: reqgen,
scanner: scanner,
results: results,
workerCount: 100,
}
for _, o := range opts {
o(s)
}
return s
}
func (e *GenericEngine) Results() <-chan Result {
return e.results.Chan()
}
func (e *GenericEngine) Start(ctx context.Context, r *Range) (<-chan interface{}, <-chan error) {
done := make(chan interface{})
errc := make(chan error, 100)
requests, err := e.reqgen.GenerateRequests(ctx, r)
if err != nil {
errc <- err
close(errc)
close(done)
return done, errc
}
go func() {
defer close(done)
defer close(errc)
var wg sync.WaitGroup
for i := 1; i <= e.workerCount; i++ {
wg.Add(1)
go e.worker(ctx, &wg, requests, errc)
}
wg.Wait()
}()
return done, errc
}
func (e *GenericEngine) worker(ctx context.Context, wg *sync.WaitGroup,
requests <-chan *Request, errc chan<- error) {
defer wg.Done()
for {
select {
case <-ctx.Done():
return
case r, ok := <-requests:
if !ok {
return
}
if r.Err != nil {
writeError(ctx, errc, r.Err)
continue
}
result, err := e.scanner.Scan(ctx, r)
if err != nil {
writeError(ctx, errc, err)
continue
}
if result != nil {
e.results.Put(result)
}
}
}
}
func writeError(ctx context.Context, out chan<- error, err error) {
select {
case <-ctx.Done():
return
case out <- err:
}
}