-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpacp_linux.go
172 lines (145 loc) Β· 3.68 KB
/
pacp_linux.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
//go:build linux
package main
import (
"context"
"fmt"
"time"
"github.com/google/gopacket"
"github.com/google/gopacket/afpacket"
"github.com/google/gopacket/layers"
"github.com/google/gopacket/pcap"
"github.com/pkg/errors"
"golang.org/x/net/bpf"
"github.com/chenjiandongx/dnstrack/formatter"
)
type pcapHandler struct {
device string
handle *afpacket.TPacket
}
type PcapClient struct {
ctx context.Context
cancel context.CancelFunc
opt Options
handlers []*pcapHandler
common *CommonClient
maxIfaceLen int
}
func NewPcapClient(opt Options) (*PcapClient, error) {
client := &PcapClient{opt: opt}
client.ctx, client.cancel = context.WithCancel(context.Background())
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)
if err != nil {
return errors.Wrapf(err, "get device(%s) name failed", device.Name)
}
if err = c.setBPFFilter(handler, bpfFilter); err != nil {
return errors.Wrapf(err, "set bpf-filter on device(%s) 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 string) (*afpacket.TPacket, error) {
return afpacket.NewTPacket(afpacket.OptInterface(device))
}
func (c *PcapClient) setBPFFilter(h *afpacket.TPacket, filter string) error {
pcapBPF, err := pcap.CompileBPFFilter(layers.LinkTypeEthernet, 65535, filter)
if err != nil {
return err
}
var bpfIns []bpf.RawInstruction
for _, ins := range pcapBPF {
bpfIns = append(bpfIns, bpf.RawInstruction{
Op: ins.Code,
Jt: ins.Jt,
Jf: ins.Jf,
K: ins.K,
})
}
return h.SetBPF(bpfIns)
}
func (c *PcapClient) parsePacket(data []byte) *SP {
var ether layers.Ethernet
var err error
if err = ether.DecodeFromBytes(data, gopacket.NilDecodeFeedback); err != nil {
return nil
}
switch ether.EthernetType {
case layers.EthernetTypeIPv4, layers.EthernetTypeIPv6:
default:
return nil
}
var ipv4 layers.IPv4
if err = ipv4.DecodeFromBytes(ether.Payload, gopacket.NilDecodeFeedback); err != nil {
return nil
}
srcIP := ipv4.SrcIP.String()
dstIP := ipv4.DstIP.String()
var pkg layers.UDP
if err = pkg.DecodeFromBytes(ipv4.Payload, gopacket.NilDecodeFeedback); err != nil {
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) {
for {
select {
case <-c.ctx.Done():
return
// decode packets followed by layers
// 1) Ethernet Layer
// 2) IP Layer
// 3) UDP Layer
default:
pkt, _, err := ph.handle.ZeroCopyReadPacketData()
if err != nil {
continue
}
sp := c.parsePacket(pkt)
if sp == nil {
continue
}
c.common.Display(sp, ph.device, time.Now()) // approximate time
}
}
}
func (c *PcapClient) Stats() Stats {
return c.common.Stats()
}
func (c *PcapClient) Close() {
c.cancel()
for _, handler := range c.handlers {
handler.handle.Close()
}
}