-
Notifications
You must be signed in to change notification settings - Fork 22
/
collector.go
76 lines (62 loc) · 2.24 KB
/
collector.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
package collectors
import (
"github.com/otterize/network-mapper/src/sniffer/pkg/mapperclient"
"github.com/otterize/nilable"
"github.com/sirupsen/logrus"
"time"
)
type UniqueRequest struct {
srcIP string
srcHostname string
destHostnameOrIP string // IP or hostname
destIP string
destPort nilable.Nilable[int]
}
type TimeAndTTL struct {
lastSeen time.Time
ttl nilable.Nilable[int]
}
// For each unique request info, we store the time of the last request (no need to report duplicates) and last seen TTL.
type capturesMap map[UniqueRequest]TimeAndTTL
type NetworkCollector struct {
capturedRequests capturesMap
}
func (c *NetworkCollector) resetData() {
c.capturedRequests = make(capturesMap)
}
func (c *NetworkCollector) addCapturedRequest(srcIp string, srcHost string, destNameOrIP string, destIP string, seenAt time.Time, ttl nilable.Nilable[int], destPort *int) {
req := UniqueRequest{srcIp, srcHost, destNameOrIP, destIP, nilable.FromPtr(destPort)}
c.capturedRequests[req] = TimeAndTTL{seenAt, ttl}
}
func (c *NetworkCollector) CollectResults() []mapperclient.RecordedDestinationsForSrc {
type srcInfo struct {
Ip string
Hostname string
}
srcToDests := make(map[srcInfo][]mapperclient.Destination)
for reqInfo, timeAndTTL := range c.capturedRequests {
src := srcInfo{Ip: reqInfo.srcIP, Hostname: reqInfo.srcHostname}
if _, ok := srcToDests[src]; !ok {
srcToDests[src] = make([]mapperclient.Destination, 0)
}
destination := mapperclient.Destination{
Destination: reqInfo.destHostnameOrIP,
DestinationIP: nilable.From(reqInfo.destIP),
DestinationPort: reqInfo.destPort,
LastSeen: timeAndTTL.lastSeen,
TTL: timeAndTTL.ttl,
}
srcToDests[src] = append(srcToDests[src], destination)
}
results := make([]mapperclient.RecordedDestinationsForSrc, 0)
for src, destinations := range srcToDests {
// Debug print the results
logrus.Debugf("%s (%s):\n", src.Ip, src.Hostname)
for _, dest := range destinations {
logrus.Debugf(" %s, %s", dest.Destination, dest.LastSeen)
}
results = append(results, mapperclient.RecordedDestinationsForSrc{SrcIp: src.Ip, SrcHostname: src.Hostname, Destinations: destinations})
}
c.resetData()
return results
}