-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.go
212 lines (189 loc) · 6.35 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
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
package main
import (
"encoding/json"
"fmt"
"io/ioutil"
"log"
"net"
"net/http"
"strconv"
"strings"
"sync"
"time"
"github.com/google/gopacket"
"github.com/google/gopacket/layers"
"github.com/google/gopacket/pcap"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promhttp"
)
var (
snapshotLen int32 = 1599
promiscuous bool = false
err error
timeout time.Duration = pcap.BlockForever
bytes = prometheus.NewCounterVec(
prometheus.CounterOpts{
Name: "dnetexporter_bytes_total",
Help: "Total number of bytes observed for IP pairs, including ports, by direction.",
},
[]string{"source", "destination", "destination_port", "direction"},
)
packets = prometheus.NewCounterVec(
prometheus.CounterOpts{
Name: "dnetexporter_packet_total",
Help: "Total number of packets observed for IP pairs, including ports, by direction.",
},
[]string{"source", "destination", "destination_port", "direction"},
)
ipPairActivities = make(map[string]ipPairActivity)
activityMutex sync.Mutex
metricsTimeout = time.Minute * 1
srcIP string
dstIP string
)
type ipPairActivity struct {
LastActivity time.Time
}
type Config struct {
Device string `json:"device"`
IPPairs []struct {
Source string `json:"source"`
Destination string `json:"destination"`
} `json:"ipPairs"`
}
var config Config
func readConfig() {
file, err := ioutil.ReadFile("config.json")
if err != nil {
log.Fatalf("Failed to read config file: %v", err)
}
err = json.Unmarshal(file, &config)
if err != nil {
log.Fatalf("Failed to unmarshal config JSON: %v", err)
}
}
func capturePackets(handle *pcap.Handle) {
if err != nil {
log.Fatalf("Could not open device %s: %v", config.Device, err)
}
defer handle.Close()
packetSource := gopacket.NewPacketSource(handle, handle.LinkType())
for packet := range packetSource.Packets() {
networkLayer := packet.NetworkLayer()
transportLayer := packet.TransportLayer()
if networkLayer == nil || transportLayer == nil {
continue
}
srcIP = networkLayer.NetworkFlow().Src().String()
dstIP = networkLayer.NetworkFlow().Dst().String()
var dstPort string
// var srcPort, srcportType string
switch tLayer := transportLayer.(type) {
case *layers.TCP:
dstPort = strconv.Itoa(int(tLayer.DstPort))
case *layers.UDP:
dstPort = strconv.Itoa(int(tLayer.DstPort))
default:
continue // If it's neither TCP nor UDP, skip
}
for _, pair := range config.IPPairs {
if isBidirectionalPair(srcIP, dstIP, pair) {
// Determine direction for labeling
direction := "forward"
if matchIPorSubnet(dstIP, pair.Source) && matchIPorSubnet(srcIP, pair.Destination) {
direction = "backward"
}
// Update packet metrics considering direction
labelValues := prometheus.Labels{
"source": srcIP,
"destination": dstIP,
"destination_port": dstPort,
"direction": direction,
}
bytes.With(labelValues).Add(float64(len(packet.Data())))
packets.With(labelValues).Add(1)
// Update activity for both directions
updateActivity(srcIP, dstIP)
break
}
}
}
}
func isBidirectionalPair(srcIP, dstIP string, pair struct {
Source string `json:"source"`
Destination string `json:"destination"`
}) bool {
matchForward := matchIPorSubnet(srcIP, pair.Source) && matchIPorSubnet(dstIP, pair.Destination)
matchBackward := matchIPorSubnet(dstIP, pair.Source) && matchIPorSubnet(srcIP, pair.Destination)
return matchForward || matchBackward
}
func updateActivity(srcIP, dstIP string) {
key := srcIP + "-" + dstIP
activityMutex.Lock()
ipPairActivities[key] = ipPairActivity{LastActivity: time.Now()}
activityMutex.Unlock()
}
func cleanupInactiveIPPairs() {
for {
time.Sleep(time.Minute) // Frequency of cleanup checks
now := time.Now()
activityMutex.Lock()
for key, activity := range ipPairActivities {
if now.Sub(activity.LastActivity) > metricsTimeout {
delete(ipPairActivities, key)
}
}
activityMutex.Unlock()
}
}
func init() {
prometheus.MustRegister(bytes)
prometheus.MustRegister(packets)
}
func main() {
readConfig()
handle, err := pcap.OpenLive(config.Device, snapshotLen, promiscuous, timeout)
if err != nil {
log.Fatalf("Could not open device %s: %v", config.Device, err)
}
defer handle.Close()
bpfFilter := constructBPF(&config)
if err := applyBPF(handle, bpfFilter); err != nil {
log.Fatalf("Could not apply BPF filter: %v", err)
}
go capturePackets(handle) // Adjusted to pass the pcap handle
go cleanupInactiveIPPairs()
http.Handle("/metrics", promhttp.Handler())
log.Println("Serving metrics on :9914...")
log.Fatal(http.ListenAndServe(":9914", nil))
}
func ipInSubnet(ipAddr, cidr string) bool {
ip := net.ParseIP(ipAddr)
_, subnet, err := net.ParseCIDR(cidr)
if err != nil {
log.Printf("Error parsing CIDR: %v\n", err)
return false
}
return subnet.Contains(ip)
}
func matchIPorSubnet(ipAddr, configIP string) bool {
if strings.Contains(configIP, "/") {
// configIP is a subnet
return ipInSubnet(ipAddr, configIP)
}
// Assume configIP is a single IP
return ipAddr == configIP
}
func constructBPF(config *Config) string {
var filters []string
for _, pair := range config.IPPairs {
// Example filter: "(src net 192.168.1.0/24 and dst net 192.168.2.0/24) or (src net 192.168.2.0/24 and dst net 192.168.1.0/24)"
forward := fmt.Sprintf("(src net %s and dst net %s)", pair.Source, pair.Destination)
backward := fmt.Sprintf("(src net %s and dst net %s)", pair.Destination, pair.Source)
filters = append(filters, forward, backward)
}
return strings.Join(filters, " or ")
}
func applyBPF(handle *pcap.Handle, filter string) error {
return handle.SetBPFFilter(filter)
}