-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathonline.go
206 lines (174 loc) · 5.88 KB
/
online.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
202
203
204
205
206
package gowireshark
/*
#cgo pkg-config: glib-2.0
#cgo CFLAGS: -I${SRCDIR}/include
#cgo CFLAGS: -I${SRCDIR}/include/wireshark
#cgo CFLAGS: -I${SRCDIR}/include/libpcap
#include "lib.h"
#include "online.h"
#include "offline.h"
*/
import "C"
import (
"encoding/json"
"log/slog"
"time"
"github.com/pkg/errors"
)
// FrameDataChan dissect result chan map
var FrameDataChan = make(map[string]chan FrameData)
// PcapAddr represents an individual address (including address, netmask, broadcast address, and destination address).
type PcapAddr struct {
Addr string `json:"addr,omitempty"` // Address (could be IPv4, IPv6, MAC, etc.)
Netmask string `json:"netmask,omitempty"` // Netmask (if available)
Broadaddr string `json:"broadaddr,omitempty"` // Broadcast address (if available)
Dstaddr string `json:"dstaddr,omitempty"` // Destination address (if available)
}
// IFace represents a network interface.
type IFace struct {
Name string `json:"name"` // Interface name
Description string `json:"description,omitempty"` // Description (can be empty)
Flags uint32 `json:"flags"` // Interface flags
Addresses []PcapAddr `json:"addresses"` // List of addresses associated with the interface
}
// ParseIFace Unmarshal interface device
func ParseIFace(src string) (iFaces []IFace, err error) {
err = json.Unmarshal([]byte(src), &iFaces)
if err != nil {
return
}
return iFaces, nil
}
// GetIFaces Get interface list
func GetIFaces() (iFaces []IFace, err error) {
iFaces, err = ParseIFace(CChar2GoStr(C.get_if_list()))
if err != nil {
return
}
return iFaces, nil
}
// GetIFaceNonblockStatus
//
// @Description: Check if a network interface is in non-blocking mode.
// @param interfaceName: Name of the network interface.
// @return isNonblock: True if the interface is in non-blocking mode, false otherwise.
func GetIFaceNonblockStatus(interfaceName string) (isNonblock bool, err error) {
if interfaceName == "" {
err = errors.Wrap(err, "interface name is blank")
return
}
nonblockStatus := C.get_if_nonblock_status(C.CString(interfaceName))
if nonblockStatus == 0 {
isNonblock = false
} else if nonblockStatus == 1 {
isNonblock = true
} else {
err = errors.Wrapf(ErrFromCLogic, "nonblockStatus:%v", nonblockStatus)
}
return
}
// SetIFaceNonblockStatus Set interface nonblock status
func SetIFaceNonblockStatus(interfaceName string, isNonblock bool) (status bool, err error) {
if interfaceName == "" {
err = errors.Wrap(err, "interface name is blank")
return
}
setNonblockCode := 0
if isNonblock {
setNonblockCode = 1
}
nonblockStatus := C.set_if_nonblock_status(C.CString(interfaceName), C.int(setNonblockCode))
if nonblockStatus == 0 {
status = false
} else if nonblockStatus == 1 {
status = true
} else {
err = errors.Wrapf(ErrFromCLogic, "nonblockStatus:%v", nonblockStatus)
}
return
}
//export GetDataCallback
func GetDataCallback(data *C.char, length C.int, interfaceName *C.char) {
goPacket := ""
if data != nil {
goPacket = C.GoStringN(data, length)
}
interfaceNameStr := ""
if interfaceName != nil {
interfaceNameStr = C.GoString(interfaceName)
}
// unmarshal each pkg dissect result
singleFrameRes, err := ParseFrameData(goPacket)
if err != nil {
slog.Warn("Error:", "ParseFrameData", err)
if singleFrameRes != nil {
slog.Warn("Error:", "WsIndex", singleFrameRes.Index)
}
return // 如果解析失败,直接返回,不继续处理
}
if singleFrameRes == nil {
slog.Warn("Error: ParseFrameData returned nil result")
return
}
// write packet dissect result to go pipe
if ch, ok := FrameDataChan[interfaceNameStr]; ok {
ch <- *singleFrameRes
} else {
slog.Warn("Error: No channel found for interface", "interfaceName", interfaceNameStr)
}
}
// StartLivePacketCapture
//
// @Description: Set up a callback function, capture and dissect packets in real-time.
// @param interfaceName: Name of the network interface to capture packets from.
// @param bpfFilter: BPF filter expression to apply to the capture.
// @param packetCount: Number of packets to capture (0 for unlimited).
// @param promisc: 0 for non-promiscuous mode, any other value for promiscuous mode.
// @param timeout: Timeout for the capture operation in milliseconds.
// @param opts: Optional configuration for the capture.
func StartLivePacketCapture(interfaceName, bpfFilter string, packetCount, promisc, timeout int, opts ...Option) (err error) {
// Set up callback function
C.setDataCallback((C.DataCallback)(C.GetDataCallback))
if interfaceName == "" {
err = errors.Wrap(err, "device name is blank")
return
}
conf := NewConfig(opts...)
printCJson := 0
if conf.PrintCJson {
printCJson = 1
}
errMsg := C.handle_packet(C.CString(interfaceName), C.CString(bpfFilter), C.int(packetCount),
C.int(promisc), C.int(timeout), C.int(printCJson), C.CString(HandleConf(conf)))
if C.strlen(errMsg) != 0 {
// transfer c char to go string
errMsgStr := CChar2GoStr(errMsg)
err = errors.Errorf("fail to capture packet live:%s", errMsgStr)
return
}
return
}
// StopLivePacketCapture
//
// @Description: Stop the live packet capture and free all allocated memory.
// @param interfaceName: Name of the network interface to stop capturing from.
func StopLivePacketCapture(interfaceName string) (err error) {
if interfaceName == "" {
err = errors.Wrap(err, "device name is blank")
return
}
errMsg := C.stop_dissect_capture_pkg(C.CString(interfaceName))
if C.strlen(errMsg) != 0 {
// transfer c char to go string
errMsgStr := CChar2GoStr(errMsg)
err = errors.Errorf("fail to stop capture packet live:%s", errMsgStr)
return
}
return
}
// TimeEpoch2Time turn wireshark timestamp to time.Time
func TimeEpoch2Time(wiresharkTimestamp float64) (goTime time.Time) {
seconds := int64(wiresharkTimestamp)
microseconds := int64((wiresharkTimestamp - float64(seconds)) * 1e6)
return time.Unix(seconds, microseconds*1000)
}