-
Notifications
You must be signed in to change notification settings - Fork 13
/
main.go
146 lines (139 loc) · 4.41 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
package main
import (
"flag"
"fmt"
"os"
"strconv"
"time"
"github.com/google/gopacket"
"github.com/google/gopacket/layers"
"github.com/google/gopacket/pcap"
// "github.com/google/gopacket/pcapgo"
)
var (
snapshotLen int32 = 1024
promiscuous bool = true
err error
// timeout time.Duration = 3 * time.Second
handle *pcap.Handle
ipLayer *layers.IPv4
tcpLayer *layers.TCP
deviceName *string
zkPort *int
pcapFile *string
)
type ConnInfo struct {
Timestamp time.Time
ClientAddr string
ServerAddr string
OpType string
Path string
Zxid int64
ReqLen int
RespLen int
Latency string
Error int32
}
func init() {
deviceName = flag.String("device", "eth0", "read packet from network device")
zkPort = flag.Int("port", 2181, "zookeeper server port")
pcapFile = flag.String("file", "", "read packet from pcap file")
flag.Parse()
}
func main() {
if *pcapFile == "" {
handle, err = pcap.OpenLive(*deviceName, snapshotLen, promiscuous, pcap.BlockForever)
if err != nil {
fmt.Printf("Error opening device %s: %v", *deviceName, err)
os.Exit(1)
}
} else {
handle, err = pcap.OpenOffline(*pcapFile)
if err != nil {
fmt.Printf("Error opening file %s: %v\n", *pcapFile, err)
os.Exit(1)
}
}
defer handle.Close()
filter := fmt.Sprintf("tcp and port %d", *zkPort)
err := handle.SetBPFFilter(filter)
if err != nil {
fmt.Printf("Error set filter: %s", err)
os.Exit(1)
}
// Start processing packets
// fmt.Println("Start capture packet...")
packetSource := gopacket.NewPacketSource(handle, handle.LinkType())
con_map := make(map[uint32]ConnInfo)
var raw_p []byte
for packet := range packetSource.Packets() {
if len(packet.Layers()) > 3 { //LayerTypeEthernet&&LayerTypeIPv4&&LayerTypeTCP&&LayerCustom
info := ConnInfo{}
var ack_id uint32
var seq_id uint32
for _, layer := range packet.Layers() {
switch layer.LayerType() {
case layers.LayerTypeEthernet:
case layers.LayerTypeLinuxSLL:
case layers.LayerTypeIPv4:
ipP := gopacket.NewPacket(layer.LayerContents(), layers.LayerTypeIPv4, gopacket.NoCopy)
ipLayer = ipP.Layers()[0].(*layers.IPv4)
info.ClientAddr = ipLayer.SrcIP.String()
info.ServerAddr = ipLayer.DstIP.String()
case layers.LayerTypeTCP:
ipP := gopacket.NewPacket(layer.LayerContents(), layers.LayerTypeTCP, gopacket.NoCopy)
tcpLayer = ipP.Layers()[0].(*layers.TCP)
ack_id = tcpLayer.Ack
seq_id = tcpLayer.Seq
info.ClientAddr = fmt.Sprintf("%s:%d", info.ClientAddr, tcpLayer.SrcPort)
info.ServerAddr = fmt.Sprintf("%s:%d", ipLayer.DstIP, tcpLayer.DstPort)
// case gopacket.LayerTypePayload:
default:
if int(tcpLayer.DstPort) == *zkPort {
newPacket := layer.LayerContents()
if len(layer.LayerContents()) == 4 {
raw_p = layer.LayerContents()
continue
} else {
newPacket = append(raw_p, newPacket...)
raw_p = []byte{}
}
p := gopacket.NewPacket(newPacket, LayerTypeZKReq, gopacket.NoCopy)
zkReqLayer, ok := p.Layers()[0].(*ZKReq)
if !ok {
continue
}
info.Path = zkReqLayer.Path
info.OpType = zkReqLayer.Optype
info.ReqLen = len(layer.LayerContents()) - 4 //去掉4个无用字节
info.Timestamp = packet.Metadata().Timestamp
con_map[ack_id] = info
}
if int(tcpLayer.SrcPort) == *zkPort {
reqInfo, ok := con_map[seq_id]
if ok {
con_map = map[uint32]ConnInfo{} //清空map,防止map无限增大
p := gopacket.NewPacket(layer.LayerContents(), LayerTypeZKResp, gopacket.NoCopy)
zkRespLayer, ok := p.Layers()[0].(*ZKResp)
if !ok {
continue
}
reqInfo.Zxid = zkRespLayer.Zxid
reqInfo.Error = int32(zkRespLayer.Err)
if reqInfo.OpType == "CONNECT" {
reqInfo.Error = 0
}
if reqInfo.Path == "" {
reqInfo.Path = "None"
}
reqInfo.RespLen = len(layer.LayerContents()) - 4 //去掉4个无用字节
reqInfo.Latency = fmt.Sprintf("%.4f", float32(packet.Metadata().CaptureInfo.Timestamp.Sub(reqInfo.Timestamp).Nanoseconds())/1000.0/1000.0)
fmt.Printf("%s %s %s %s %s 0x%s %d %d %s %d\n", reqInfo.Timestamp.Local().Format("2006-01-02 15:04:05.000"), reqInfo.ClientAddr,
reqInfo.ServerAddr, reqInfo.OpType, reqInfo.Path, strconv.FormatInt(reqInfo.Zxid, 16), reqInfo.ReqLen, reqInfo.RespLen, reqInfo.Latency, reqInfo.Error)
}
}
}
}
}
}
}