-
Notifications
You must be signed in to change notification settings - Fork 3
/
egressguy.go
146 lines (118 loc) · 3.19 KB
/
egressguy.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 egressguy
import (
"log"
"net"
"sync"
"github.com/gopacket/gopacket"
"github.com/gopacket/gopacket/layers"
"github.com/gopacket/gopacket/pcap"
)
type EgressGuy struct {
Src net.IP
ethernetLayer []byte
Interface *net.Interface
Handle *pcap.Handle
Listeners []PacketListener
listenerLock sync.Mutex
Traffic uint64
}
func NewEgressGuy(iface *net.Interface, src net.IP, gw net.IP) (*EgressGuy, error) {
handle, err := pcap.OpenLive(iface.Name, int32(iface.MTU), true, pcap.BlockForever)
if err != nil {
return nil, err
}
hwAddr, err := GetHwAddr(handle, iface, src, gw)
if err != nil {
return nil, err
}
handle.SetBPFFilter("tcp")
handle.SetDirection(pcap.DirectionIn)
eth := layers.Ethernet{
SrcMAC: iface.HardwareAddr,
DstMAC: hwAddr,
EthernetType: layers.EthernetTypeIPv4,
}
buf := gopacket.NewSerializeBuffer()
if err := eth.SerializeTo(buf, GOPACKETS_OPTS); err != nil {
return nil, err
}
b := make([]byte, 14)
copy(b, buf.Bytes())
return &EgressGuy{
Src: src,
ethernetLayer: b,
Interface: iface,
Handle: handle,
Listeners: make([]PacketListener, 0),
}, nil
}
func (eg *EgressGuy) Close() {
eg.Handle.Close()
}
func (eg *EgressGuy) SendPacket(layersArr ...gopacket.SerializableLayer) error {
buf := gopacket.NewSerializeBuffer()
if err := gopacket.SerializeLayers(buf, GOPACKETS_OPTS, layersArr...); err != nil {
return err
}
if b, err := buf.PrependBytes(14); err != nil {
return err
} else {
copy(b, eg.ethernetLayer)
}
buf.PushLayer(layers.LayerTypeEthernet)
if err := eg.Handle.WritePacketData(buf.Bytes()); err != nil {
return err
}
return nil
}
func (eg *EgressGuy) ListenPackets() {
decoder := eg.Handle.LinkType()
for {
d, ci, err := eg.Handle.ZeroCopyReadPacketData()
if err != nil {
log.Println(err)
return
}
if d[12] != 0x08 || d[13] != 0x00 {
continue
}
packet := gopacket.NewPacket(d, decoder, gopacket.NoCopy)
ip4 := packet.Layer(layers.LayerTypeIPv4).(*layers.IPv4)
// eg.listenerLock.Lock()
for _, listener := range eg.Listeners {
proto, src, dst, srcPort, dstPort := listener.ConnectionTuple()
if proto != ip4.Protocol || !src.Equal(ip4.DstIP) || !dst.Equal(ip4.SrcIP) {
continue
}
if proto == layers.IPProtocolTCP {
if layerTCP := packet.Layer(layers.LayerTypeTCP); layerTCP != nil {
tcp := layerTCP.(*layers.TCP)
if srcPort == tcp.DstPort && dstPort == tcp.SrcPort {
listener.HandlePacket(packet, layerTCP)
eg.Traffic += uint64(ci.Length)
}
}
}
}
// eg.listenerLock.Unlock()
}
}
func (eg *EgressGuy) AddListener(listener PacketListener) {
eg.listenerLock.Lock()
defer eg.listenerLock.Unlock()
eg.Listeners = append(eg.Listeners, listener)
}
func (eg *EgressGuy) RemoveListener(listener PacketListener) {
eg.listenerLock.Lock()
defer eg.listenerLock.Unlock()
for i, l := range eg.Listeners {
if l == listener {
eg.Listeners = append(eg.Listeners[:i], eg.Listeners[i+1:]...)
break
}
}
}
type PacketListener interface {
ConnectionTuple() (proto layers.IPProtocol, src, dst net.IP, srcPort, dstPort layers.TCPPort)
HandlePacket(packet gopacket.Packet, layer gopacket.Layer)
}