-
Notifications
You must be signed in to change notification settings - Fork 1
/
traffic_filters.go
54 lines (46 loc) · 1.24 KB
/
traffic_filters.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
package main
import (
"bufio"
"bytes"
"fmt"
"io"
"io/ioutil"
"net/http"
"strings"
"time"
"github.com/google/gopacket"
)
var HTTPTrafficFilter = NewTrafficFilter(func(packet gopacket.Packet) (Event, bool) {
appLayer := packet.ApplicationLayer()
if appLayer != nil && strings.Contains(string(appLayer.Payload()), "HTTP") {
payloadReader := bytes.NewReader(appLayer.Payload())
bufferedPayloadReader := bufio.NewReader(payloadReader)
request, err := http.ReadRequest(bufferedPayloadReader)
if err != nil && err != io.EOF {
return Event{}, true
}
payload, err := ioutil.ReadAll(request.Body)
if err != nil {
return Event{}, true
}
return Event{
Payload: payload,
Destination: fmt.Sprintf("%s%s", request.Host, request.RequestURI),
Time: time.Now(),
}, false
}
return Event{}, true
})
type TrafficFilter interface {
Filter(gopacket.Packet) (Event, bool)
}
type PacketFilterFn func(gopacket.Packet) (Event, bool)
type trafficFilter struct {
packetFilterFn PacketFilterFn
}
func NewTrafficFilter(packetFilterFn PacketFilterFn) *trafficFilter {
return &trafficFilter{packetFilterFn: packetFilterFn}
}
func (t *trafficFilter) Filter(packet gopacket.Packet) (Event, bool) {
return t.packetFilterFn(packet)
}