-
Notifications
You must be signed in to change notification settings - Fork 46
/
config.go
201 lines (176 loc) · 5.29 KB
/
config.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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
package pcapx
import (
"errors"
"github.com/google/gopacket"
"github.com/google/gopacket/layers"
"github.com/yaklang/yaklang/common/log"
"github.com/yaklang/yaklang/common/utils"
"sync"
)
var (
ifaceHandler sync.Map
)
type Config struct {
Iface string
ToServerSet bool
ToServer bool
IsHttps bool
TCPLocalAddress string
TCPRemoteAddress string
}
type ConfigOption func(config *Config)
func WithIface(iface string) ConfigOption {
return func(config *Config) {
config.Iface = iface
}
}
func WithLocalAddress(addr string) ConfigOption {
return func(config *Config) {
config.TCPLocalAddress = addr
}
}
func WithRemoteAddress(addr string) ConfigOption {
return func(config *Config) {
config.TCPRemoteAddress = addr
}
}
func WithToServer() ConfigOption {
return func(config *Config) {
config.ToServerSet = true
config.ToServer = true
}
}
func WithToClient() ConfigOption {
return func(config *Config) {
config.ToServerSet = true
config.ToServer = false
}
}
func ParseTCPRaw(raw []byte) (*layers.IPv4, *layers.TCP, gopacket.Payload, error) {
var ipv4 *layers.IPv4
var ok bool
var tcp *layers.TCP
packet := gopacket.NewPacket(raw, layers.LayerTypeEtherIP, gopacket.Default)
transportLayer := packet.TransportLayer()
ipLayer := packet.NetworkLayer()
tcp, ok = transportLayer.(*layers.TCP)
if !ok {
log.Error("Not a TCP packet")
return nil, nil, nil, errors.New("Not a TCP packet")
}
if ipLayer.LayerType() == layers.LayerTypeIPv4 {
ipv4, ok = ipLayer.(*layers.IPv4)
if !ok {
return nil, nil, nil, utils.Error("parse ipv4 layer failed")
}
}
var payload gopacket.Payload
if ret := packet.TransportLayer(); ret.LayerType() == layers.LayerTypeTCP {
tcp, ok = ret.(*layers.TCP)
if !ok {
return nil, nil, nil, utils.Error("parse tcp layer failed")
}
payload = ret.LayerPayload()
}
return ipv4, tcp, payload, nil
}
func ParseTCPIPv4(raw []byte) (*layers.IPv4, *layers.TCP, gopacket.Payload, error) {
packet := gopacket.NewPacket(raw, layers.LayerTypeIPv4, gopacket.Default)
if packet.ErrorLayer() != nil {
return nil, nil, nil, utils.Error("parse packet(ip4+tcp) failed")
}
ipLayer := packet.NetworkLayer()
var ipv4 *layers.IPv4
var ok bool
if ipLayer.LayerType() == layers.LayerTypeIPv4 {
ipv4, ok = ipLayer.(*layers.IPv4)
if !ok {
return nil, nil, nil, utils.Error("parse ipv4 layer failed")
}
}
var tcp *layers.TCP
var payload gopacket.Payload
if ret := packet.TransportLayer(); ret != nil && ret.LayerType() == layers.LayerTypeTCP {
tcp, ok = ret.(*layers.TCP)
if !ok {
return nil, nil, nil, utils.Error("parse tcp layer failed")
}
payload = ret.LayerPayload()
}
if tcp == nil {
return nil, nil, nil, utils.Error("parse tcp layer failed")
}
return ipv4, tcp, payload, nil
}
func ParseUDPIPv4(raw []byte) (*layers.IPv4, *layers.UDP, gopacket.Payload, error) {
packet := gopacket.NewPacket(raw, layers.LayerTypeIPv4, gopacket.Default)
ipLayer := packet.NetworkLayer()
var ipv4 *layers.IPv4
var ok bool
if ipLayer.LayerType() == layers.LayerTypeIPv4 {
ipv4, ok = ipLayer.(*layers.IPv4)
if !ok {
return nil, nil, nil, utils.Error("parse ipv4 layer failed")
}
}
var udp *layers.UDP
var payload gopacket.Payload
if ret := packet.TransportLayer(); ret.LayerType() == layers.LayerTypeUDP {
udp, ok = ret.(*layers.UDP)
if !ok {
return nil, nil, nil, utils.Error("parse udp layer failed")
}
payload = ret.LayerPayload()
}
return ipv4, udp, payload, nil
}
func ParseICMPIPv4(raw []byte) (*layers.IPv4, *layers.ICMPv4, gopacket.Payload, error) {
packet := gopacket.NewPacket(raw, layers.LayerTypeIPv4, gopacket.Default)
ipLayer := packet.NetworkLayer()
var ipv4 *layers.IPv4
var ok bool
if ipLayer.LayerType() == layers.LayerTypeIPv4 {
ipv4, ok = ipLayer.(*layers.IPv4)
if !ok {
return nil, nil, nil, utils.Error("parse ipv4 layer failed")
}
}
var icmp *layers.ICMPv4
var payload gopacket.Payload
icmpLayer := packet.Layer(layers.LayerTypeICMPv4)
if icmpLayer != nil {
icmp, ok = icmpLayer.(*layers.ICMPv4)
if ok {
payload = icmpLayer.LayerPayload()
} else {
return nil, nil, nil, utils.Error("parse icmp layer failed")
}
}
return ipv4, icmp, payload, nil
}
func parseFromLinkLayer(layerType layers.LinkType, raw []byte) (gopacket.LinkLayer, gopacket.NetworkLayer, gopacket.TransportLayer, gopacket.Payload, error) {
packet := gopacket.NewPacket(raw, layerType, gopacket.Default)
if packet.ErrorLayer() != nil && packet.ErrorLayer().Error() != nil {
return nil, nil, nil, nil, utils.Error("parse packet(ethernet) failed")
}
linkLayer := packet.LinkLayer()
if linkLayer == nil {
return nil, nil, nil, nil, utils.Error("parse ethernet layer failed")
}
netLayer := packet.NetworkLayer()
transLayer := packet.TransportLayer()
var payload = linkLayer.LayerPayload()
if netLayer != nil {
payload = netLayer.LayerPayload()
}
if transLayer != nil {
payload = transLayer.LayerPayload()
}
return linkLayer, netLayer, transLayer, payload, nil
}
func ParseEthernetLinkLayer(raw []byte) (gopacket.LinkLayer, gopacket.NetworkLayer, gopacket.TransportLayer, gopacket.Payload, error) {
return parseFromLinkLayer(layers.LinkTypeEthernet, raw)
}
func ParseRaw(raw []byte) (gopacket.LinkLayer, gopacket.NetworkLayer, gopacket.TransportLayer, gopacket.Payload, error) {
return parseFromLinkLayer(layers.LinkTypeRaw, raw)
}