-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.go
195 lines (183 loc) · 5.2 KB
/
main.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
package main
import (
"bufio"
"encoding/binary"
"errors"
"fmt"
"github.com/google/gopacket"
"github.com/google/gopacket/layers"
"github.com/google/gopacket/pcap"
"io"
"log"
"net"
"os"
"strconv"
"strings"
"time"
)
type Interface struct {
pcap.Interface
SendBuffer chan gopacket.Packet
}
var (
retransmitBufferSize int32 = 1024
snapshot_len int32 = 2048
promiscuous bool = false
timeout time.Duration = 500 * time.Millisecond
proxyTTL uint8 = 64
)
func InterfaceFromName(name string) (Interface, error) {
devices, err := pcap.FindAllDevs()
if err != nil {
return Interface{}, err
}
for _, device := range devices {
if device.Name == name {
return Interface{device, make(chan gopacket.Packet, retransmitBufferSize)}, nil
}
}
return Interface{}, nil
}
func portInPorts(port uint16, ports []uint16) bool {
for _, p := range ports {
if p == port {
return true
}
}
return false
}
func lastAddr(n *net.IPNet) (net.IP, error) { // works when the n is a prefix, otherwise...
if n.IP.To4() == nil {
return net.IP{}, errors.New("Does not support IPv6 addresses.")
}
ip := make(net.IP, len(n.IP.To4()))
binary.BigEndian.PutUint32(ip, binary.BigEndian.Uint32(n.IP.To4())|^binary.BigEndian.Uint32(net.IP(n.Mask).To4()))
return ip, nil
}
func bufferRetransmit(packet gopacket.Packet, destinationDevices []Interface, ports *[]uint16) {
ipLayer := packet.Layer(layers.LayerTypeIPv4)
udpLayer := packet.Layer(layers.LayerTypeUDP)
if (ipLayer != nil) && (udpLayer != nil) {
ipPacket, _ := ipLayer.(*layers.IPv4)
udpPacket, _ := udpLayer.(*layers.UDP)
if ipPacket.TTL != proxyTTL && portInPorts(uint16(udpPacket.DstPort), *ports) {
for _, destinationDevice := range destinationDevices {
fmt.Printf("%s:%d -> %s:%d\n", ipPacket.SrcIP, udpPacket.SrcPort, ipPacket.DstIP, udpPacket.DstPort)
fmt.Println("sending to ", destinationDevice.Name)
destinationDevice.SendBuffer <- packet
}
}
}
}
func interfaceWorker(device Interface, destinationDevices []Interface, ports *[]uint16) error {
// Open device
handle, err := pcap.OpenLive(device.Name, snapshot_len, promiscuous, timeout)
if err != nil {
return err
}
defer handle.Close()
bpfFilter := "udp and (dst host 255.255.255.255"
for _, address := range device.Addresses {
fmt.Println(address.Broadaddr)
bpfFilter = bpfFilter + " or dst host " + address.Broadaddr.String()
}
bpfFilter = bpfFilter + ")"
handle.SetBPFFilter(bpfFilter)
fmt.Println("using filter '", bpfFilter, "'")
fmt.Println("for interface", device, "->", destinationDevices)
packetSource := gopacket.NewPacketSource(handle, handle.LinkType())
for {
select {
case incomingPacket := <-packetSource.Packets():
bufferRetransmit(incomingPacket, destinationDevices, ports)
case outgoingPacket := <-device.SendBuffer:
//send Packet and rewrite the destAddr
ipLayer := outgoingPacket.Layer(layers.LayerTypeIPv4)
udpLayer := outgoingPacket.Layer(layers.LayerTypeUDP)
if ipLayer != nil && udpLayer != nil {
ipPacket, _ := ipLayer.(*layers.IPv4)
udpPacket, _ := udpLayer.(*layers.UDP)
for _, address := range device.Addresses {
ipPacket.DstIP = address.Broadaddr
ipPacket.TTL = proxyTTL
options := gopacket.SerializeOptions{
ComputeChecksums: true,
FixLengths: true,
}
udpPacket.SetNetworkLayerForChecksum(ipPacket)
newBuffer := gopacket.NewSerializeBuffer()
err := gopacket.SerializePacket(newBuffer, options, outgoingPacket)
if err != nil {
return err
}
outgoingPacket := newBuffer.Bytes()
handle.WritePacketData(outgoingPacket)
}
}
}
}
return nil
}
func readPortList(confFile string) ([]uint16, error) {
csvFile, err := os.Open(confFile)
if err != nil {
return nil, err
}
ports := make([]uint16, 0)
lineReader := bufio.NewReader(csvFile)
for {
line, _, err := lineReader.ReadLine()
if err != nil {
if err == io.EOF {
break
} else {
return ports, err
}
}
lineStr := string(line)
if len(lineStr) > 0 && lineStr[0] == '#' {
continue
}
columns := strings.SplitN(lineStr, ",", 2)
port, err := strconv.ParseUint(columns[0], 10, 16)
if err != nil {
log.Println(err)
continue
}
ports = append(ports, uint16(port))
}
return ports, nil
}
func main() {
if len(os.Args) == 2 && os.Args[1] == "-h" {
fmt.Println("Usage: ", os.Args[0], "[INTERFACE] ...")
return
}
ports, err := readPortList("games.csv")
if err != nil {
log.Fatal(err)
}
devices := []Interface{}
for _, deviceName := range os.Args[1:] {
device, _ := InterfaceFromName(deviceName)
devices = append(devices, device)
}
for i, v := range devices {
otherDevices := make([]Interface, len(devices)-1, len(devices)-1)
for otherDeviceIndex := 0; otherDeviceIndex < len(devices); otherDeviceIndex++ {
if otherDeviceIndex != i {
if otherDeviceIndex > i {
otherDevices[otherDeviceIndex-1] = devices[otherDeviceIndex]
} else {
otherDevices[otherDeviceIndex] = devices[otherDeviceIndex]
}
}
}
fmt.Println("starting worker on", v.Name)
if i == len(devices)-1 {
interfaceWorker(v, otherDevices, &ports)
} else {
go interfaceWorker(v, otherDevices, &ports)
}
}
}