-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathmonitor_packet.go
51 lines (41 loc) · 1.13 KB
/
monitor_packet.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
package main
import (
"fmt"
"time"
"github.com/google/gopacket"
"github.com/google/gopacket/layers"
"github.com/google/gopacket/pcap"
)
//StartMonitor monitor redis packet destination or source port
func StartMonitor(devicename string, redisport uint16, snaplen int32) error {
// + -> 0x2b, $ -> 0x24, * -> 0x2A
bpffilter := fmt.Sprintf(`port %d and tcp[((tcp[12:1] & 0xf0) >> 2):1] = 0x2A
|| tcp[((tcp[12:1] & 0xf0) >> 2):1] = 0x24
|| tcp[((tcp[12:1] & 0xf0) >> 2):1] = 0x2b`, redisport)
handle, err := pcap.OpenLive(devicename, snaplen, false, -1*time.Second)
if err != nil {
return fmt.Errorf("error opening device %s: %v", devicename, err)
}
defer handle.Close()
handle.SetBPFFilter(bpffilter)
packetSource := gopacket.NewPacketSource(handle, handle.LinkType())
for packet := range packetSource.Packets() {
ipLayer := packet.Layer(layers.LayerTypeIPv4)
if ipLayer == nil {
continue
}
tcpLayer := packet.Layer(layers.LayerTypeTCP)
if tcpLayer == nil {
continue
}
tcp, ok := tcpLayer.(*layers.TCP)
if !ok {
continue
}
if len(tcp.Payload) < 1 {
continue
}
tcpchan <- tcp
}
return nil
}