-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
119 lines (107 loc) · 2.67 KB
/
main.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
package main
import (
"runtime"
scandaloriantypes "github.com/charles-d-burton/scandalorian-types"
jsoniter "github.com/json-iterator/go"
log "github.com/sirupsen/logrus"
"github.com/spf13/viper"
)
const (
dequeueTopic = "scan-reversedns-queue"
enqueueTopic = "collector-reversedns-queue"
)
//MessageBus Interface for making generic connections to message busses
type MessageBus interface {
Connect(host, port string) error
Publish(data []byte) error
Subscribe(topic string) (chan []byte, error)
Close()
}
var (
messageBus MessageBus
workQueue = make(chan *scandaloriantypes.Scan, 10)
)
//ReverseDNSWorker Object to run scans
type ReverseDNSWorker struct {
}
func main() {
var json = jsoniter.ConfigCompatibleWithStandardLibrary
log.SetFormatter(&log.JSONFormatter{})
v := viper.New()
v.SetEnvPrefix("reversedns")
v.AutomaticEnv()
if !v.IsSet("port") || !v.IsSet("host") {
log.Fatal("Must set host and port for message bus")
}
bus, err := connectBus(v)
if err != nil {
log.Fatal(err)
}
defer bus.Close()
messageBus = bus
dch, err := bus.Subscribe(dequeueTopic)
if err != nil {
log.Fatal(err)
}
err = createWorkerPool(runtime.NumCPU()) //Start the workers
if err != nil {
log.Fatal(err)
}
for data := range dch { //Wait for incoming scan requests
log.Info(string(data))
var scan scandaloriantypes.Scan
err := json.Unmarshal(data, &scan)
if err != nil {
log.Error(err)
continue
}
workQueue <- &scan //publish work
}
}
//createWorkerPool generates the worker queues and the workers to process them
func createWorkerPool(workers int) error {
for w := 1; w <= workers; w++ {
var worker ReverseDNSWorker
go worker.start(w)
}
return nil
}
func (worker *ReverseDNSWorker) start(id int) error {
log.Infof("Starting ReverseDNS Worker %d", id)
for msg := range workQueue {
log.Info("Reverse DNS Scan for: ", msg.IP)
}
return nil
}
//Connect to a message bus, this is abstracted to an interface so implementations of other busses e.g. Rabbit are easier
//TODO: Clean this mess up
func connectBus(v *viper.Viper) (MessageBus, error) {
var bus MessageBus
if v.IsSet("bus_type") {
busType := v.GetString("bus_type")
switch busType {
case "nats":
var natsConn NatsConn
err := natsConn.Connect(v.GetString("host"), v.GetString("port"))
if err != nil {
return nil, err
}
bus = &natsConn
default:
var natsConn NatsConn
err := natsConn.Connect(v.GetString("host"), v.GetString("port"))
if err != nil {
return nil, err
}
bus = &natsConn
}
} else {
var natsConn NatsConn
err := natsConn.Connect(v.GetString("host"), v.GetString("port"))
if err != nil {
return nil, err
}
bus = &natsConn
}
return bus, nil
}