-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpcap_others.go
147 lines (120 loc) Β· 2.86 KB
/
pcap_others.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
//go:build !linux
package main
import (
"fmt"
"github.com/google/gopacket"
"github.com/google/gopacket/layers"
"github.com/google/gopacket/pcap"
"github.com/pkg/errors"
"github.com/chenjiandongx/dnstrack/formatter"
)
type pcapHandler struct {
device string
handle *pcap.Handle
}
type PcapClient struct {
opt Options
handlers []*pcapHandler
common *CommonClient
maxIfaceLen int
}
func NewPcapClient(opt Options) (*PcapClient, error) {
client := &PcapClient{opt: opt}
if err := client.getAvailableDevices(); err != nil {
return nil, err
}
client.common = NewCommonClient(formatter.New(opt.Format, opt.Server, opt.Type, client.maxIfaceLen))
for _, handler := range client.handlers {
go client.listen(handler)
}
return client, nil
}
func (c *PcapClient) getAvailableDevices() error {
devs, err := filterDevices(c.opt.Devices, c.opt.AllDevices)
if err != nil {
return err
}
for _, device := range devs {
handler, err := c.getHandler(device.Name, bpfFilter)
if err != nil {
return errors.Wrapf(err, "get device(%s) name failed", device.Name)
}
if c.maxIfaceLen < len(device.Name) {
c.maxIfaceLen = len(device.Name)
}
c.handlers = append(c.handlers, &pcapHandler{
device: device.Name,
handle: handler,
})
}
if len(c.handlers) == 0 {
return errors.New("no available devices found")
}
return nil
}
func (c *PcapClient) getHandler(device, filter string) (*pcap.Handle, error) {
handle, err := pcap.OpenLive(device, 65535, false, pcap.BlockForever)
if err != nil {
return nil, err
}
if err := handle.SetBPFFilter(filter); err != nil {
handle.Close()
return nil, err
}
return handle, nil
}
func (c *PcapClient) parsePacket(packet gopacket.Packet) *SP {
ipLayer := packet.Layer(layers.LayerTypeIPv4)
if ipLayer == nil {
return nil
}
ipv4pkg := ipLayer.(*layers.IPv4)
if ipv4pkg == nil {
return nil
}
srcIP := ipv4pkg.SrcIP.String()
dstIP := ipv4pkg.DstIP.String()
layer := packet.Layer(layers.LayerTypeUDP)
pkg, ok := layer.(*layers.UDP)
if !ok {
return nil
}
srcPort := uint16(pkg.SrcPort)
dstPort := uint16(pkg.DstPort)
var server string
if srcPort == dnsPort {
server = fmt.Sprintf("%s:%d", srcIP, srcPort)
} else {
server = fmt.Sprintf("%s:%d", dstIP, dstPort)
}
return &SP{
Server: server,
Payload: pkg.Payload,
}
}
func (c *PcapClient) listen(ph *pcapHandler) {
packetSource := gopacket.NewPacketSource(ph.handle, ph.handle.LinkType())
packetSource.Lazy = true
packetSource.NoCopy = true
for {
select {
case packet, ok := <-packetSource.Packets():
if !ok {
return
}
sp := c.parsePacket(packet)
if sp == nil {
continue
}
c.common.Display(sp, ph.device, packet.Metadata().Timestamp)
}
}
}
func (c *PcapClient) Stats() Stats {
return c.common.Stats()
}
func (c *PcapClient) Close() {
for _, handler := range c.handlers {
handler.handle.Close()
}
}