-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpcap.go
50 lines (43 loc) · 870 Bytes
/
pcap.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
//go:build !linux
package main
import (
"fmt"
"log"
"time"
"github.com/google/gopacket/pcap"
)
func KokiStart(receiver chan []byte, sender chan []byte, config Config) {
log.Println("pcap version")
devices, err := pcap.FindAllDevs()
if err != nil {
log.Fatal(err)
}
for _, device := range devices {
fmt.Println(device.Name, device.Description)
}
handle, err := pcap.OpenLive(config.Device, 128, config.Promisc, time.Millisecond)
if err != nil {
log.Fatal(err)
}
var filter string = "ip and tcp and tcp[13] & 2 != 0"
err = handle.SetBPFFilter(filter)
if err != nil {
log.Fatal(err)
}
defer handle.Close()
go func() {
for {
pkt := <-sender
if err = handle.WritePacketData(pkt); err != nil {
log.Fatal(err)
}
}
}()
for {
data, _, err := handle.ReadPacketData()
if err != nil {
continue
}
receiver <- data
}
}