forked from wchan2/bloodhound
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapplication.go
49 lines (43 loc) · 1.18 KB
/
application.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
package main
import (
"fmt"
"log"
"time"
"github.com/google/gopacket"
"github.com/google/gopacket/pcap"
)
type Application struct {
applicationConfig Config
trafficFilter TrafficFilter
trafficMonitor TrafficMonitor
alert Alert
}
type Config struct {
NetworkInterface string
Protocol string
Port string
}
func NewApplication(config Config, filter TrafficFilter, monitor TrafficMonitor, alert Alert) *Application {
return &Application{
applicationConfig: config,
trafficFilter: filter,
trafficMonitor: monitor,
alert: alert,
}
}
func (a *Application) Run() {
handle, err := pcap.OpenLive(a.applicationConfig.NetworkInterface, 1024, false, 1*time.Second)
if err != nil {
log.Fatalf("Unable to ", err.Error())
}
defer handle.Close()
defer a.trafficMonitor.Stop()
handle.SetBPFFilter(fmt.Sprintf("%s port %s", a.applicationConfig.Protocol, a.applicationConfig.Port))
packetSource := gopacket.NewPacketSource(handle, handle.LinkType())
for packet := range packetSource.Packets() {
if event, filtered := a.trafficFilter.Filter(packet); !filtered {
a.trafficMonitor.Monitor(event)
a.alert.Check(event)
}
}
}