forked from zmap/zgrab
-
Notifications
You must be signed in to change notification settings - Fork 0
/
processing.go
86 lines (69 loc) · 1.4 KB
/
processing.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
package zlib
import (
"encoding/json"
"github.com/zmap/zgrab/ztools/processing"
)
// GrabWorker implements ztools.processing.Worker
type GrabWorker struct {
success uint
failure uint
statuses chan status
config *Config
}
type status uint
const (
status_success status = iota
status_failure status = iota
)
func (g *GrabWorker) Success() uint {
return g.success
}
func (g *GrabWorker) Failure() uint {
return g.failure
}
func (g *GrabWorker) Total() uint {
return g.success + g.failure
}
func (g *GrabWorker) RunCount() uint {
return g.config.ConnectionsPerHost
}
func (g *GrabWorker) Done() {
close(g.statuses)
}
func (g *GrabWorker) MakeHandler(id uint) processing.Handler {
return func(v interface{}) interface{} {
target, ok := v.(GrabTarget)
if !ok {
return nil
}
grab := GrabBanner(g.config, &target)
s := grab.status()
g.statuses <- s
return grab
}
}
func NewGrabWorker(config *Config) processing.Worker {
w := new(GrabWorker)
w.statuses = make(chan status, config.Senders*4)
w.config = config
go func() {
for s := range w.statuses {
switch s {
case status_success:
w.success++
case status_failure:
w.failure++
default:
continue
}
}
}()
return w
}
type grabMarshaler struct{}
func (gm *grabMarshaler) Marshal(v interface{}) ([]byte, error) {
return json.Marshal(v)
}
func NewGrabMarshaler() processing.Marshaler {
return new(grabMarshaler)
}