-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathmain.go
62 lines (49 loc) · 1.25 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
package main
import (
"context"
"encoding/json"
"flag"
"fmt"
"math/rand"
"time"
"cloud.google.com/go/pubsub"
"github.com/censys/scan-takehome/pkg/scanning"
)
var (
services = []string{"HTTP", "SSH", "DNS"}
)
func main() {
projectId := flag.String("project", "test-project", "GCP Project ID")
topicId := flag.String("topic", "scan-topic", "GCP PubSub Topic ID")
flag.Parse()
ctx := context.Background()
client, err := pubsub.NewClient(ctx, *projectId)
if err != nil {
panic(err)
}
topic := client.Topic(*topicId)
for range time.Tick(time.Second) {
scan := &scanning.Scan{
Ip: fmt.Sprintf("1.1.1.%d", rand.Intn(255)),
Port: uint32(rand.Intn(65535)),
Service: services[rand.Intn(len(services))],
Timestamp: time.Now().Unix(),
}
serviceResp := fmt.Sprintf("service response: %d", rand.Intn(100))
if rand.Intn(2) == 0 {
scan.DataVersion = scanning.V1
scan.Data = &scanning.V1Data{ResponseBytesUtf8: []byte(serviceResp)}
} else {
scan.DataVersion = scanning.V2
scan.Data = &scanning.V2Data{ResponseStr: serviceResp}
}
encoded, err := json.Marshal(scan)
if err != nil {
panic(err)
}
_, err = topic.Publish(ctx, &pubsub.Message{Data: encoded}).Get(ctx)
if err != nil {
panic(err)
}
}
}