-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
259 lines (233 loc) · 7.29 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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
package main
import (
"errors"
"fmt"
"log"
"net"
"os"
"os/signal"
"strings"
"sync"
"syscall"
"time"
flag "github.com/spf13/pflag"
)
const (
MAX_UDP_PACKET_SIZE = 65536
ADDR_RESOLVE_INTERVAL = time.Second * 60
)
type UDPPacket struct {
*net.UDPAddr
Data []byte
}
func parseAddress(address string, name string) (err error) {
if _, _, err := net.SplitHostPort(address); err != nil {
return errors.New(fmt.Sprintf("%s address: %s", name, err))
}
return nil
}
func parseFlags() (listenAddress string, destinationAddresses []string, err error) {
var listenAddressRaw string
var destinationAddressesRaw []string
flag.StringVar(&listenAddressRaw, "listen", "", "address that shall be multiplied")
flag.StringSliceVar(&destinationAddressesRaw, "destination", []string{}, "destination addresses, may be supplied multiple times or seperated by comma")
flag.Parse()
if listenAddressRaw == "" {
return "", []string{}, errors.New("Please set a listen address")
}
if err := parseAddress(listenAddressRaw, "listen"); err != nil {
return "", []string{}, err
}
if len(destinationAddressesRaw) == 0 {
return listenAddressRaw, []string{}, errors.New("Please provide at least one destination address")
}
for _, dest := range destinationAddressesRaw {
if err := parseAddress(dest, "destination"); err != nil {
return listenAddressRaw, []string{}, err
}
}
return listenAddressRaw, destinationAddressesRaw, nil
}
func toggleSpliceEnabled(spliceEnabled *bool, destinations []string) {
if (*spliceEnabled) {
log.Printf("Disabling operation on secondary destinations: %v", destinations[1:])
} else {
log.Printf("Resuming operation on all destinations")
}
*spliceEnabled = !(*spliceEnabled)
}
func readPump(conn *net.UDPConn, readChan chan UDPPacket, done chan bool) {
for {
readBuffer := make([]byte, MAX_UDP_PACKET_SIZE)
readBytes, addr, err := conn.ReadFromUDP(readBuffer)
if err != nil {
if !strings.Contains(err.Error(), "use of closed network connection") {
// unexpected
log.Printf("Read error: %s", err)
}
break
}
readChan <- UDPPacket{addr, readBuffer[:readBytes]}
}
done <- true
}
func sendPacketToDestinations(conn *net.UDPConn, destinations []*net.UDPAddr, data []byte) error {
resChan := make(chan bool)
for _, destination := range destinations {
go func(dest *net.UDPAddr) {
_, err := conn.WriteTo(data, dest)
if err != nil {
log.Printf("write error: %s", err)
}
resChan <- true
}(destination)
}
resCount := 0
for {
if resCount == len(destinations) {
return nil
}
resCount++
<- resChan
}
}
type DestinationAddrState struct {
DestinationStringAddresses *[]string
DestinationAddresses []*net.UDPAddr
Mutex sync.RWMutex
}
func destinationAddrResolver(terminateChan chan bool, state *DestinationAddrState) {
resolve := func() {
destinationAddrs := []*net.UDPAddr{}
for count, destinationStringAddress := range *state.DestinationStringAddresses {
addr, err := net.ResolveUDPAddr("udp", destinationStringAddress)
if err != nil {
log.Printf("destination resolve error: %s", err)
// if we cannot resolve the first addr, we must not confuse
// the sorting
if (count == 0) {
return
}
continue
}
destinationAddrs = append(destinationAddrs, addr)
}
state.Mutex.Lock()
state.DestinationAddresses = destinationAddrs
state.Mutex.Unlock()
}
resolve()
for {
select {
case <- time.After(ADDR_RESOLVE_INTERVAL):
resolve()
case <- terminateChan:
return
}
}
}
/*
Terminology:
Peer 1 ---------- [ ingressConn ] --- [ mainLoop ] --- [ primaryEgressConn ] --- first destination (Peer 2)
|----------[ secondaryEgressConn ] ---- Peer 3
|--- Peer 4
...
|___ Peer n
The connection between Peer 1 and Peer 2 is bi directional, if Peer 2 sends data back to primaryEgressConn, it will be
also forwarded to Peer 1 via ingressConn.
The addresses of Peer 2...n are solved periodically to keep them up to date as they might be DNS entries.
*/
func main() {
// state start
lastPacketSourceAddr := (*net.UDPAddr)(nil)
spliceEnabled := true
resolverState := DestinationAddrState{
DestinationAddresses: []*net.UDPAddr{},
DestinationStringAddresses: &[]string{},
Mutex: sync.RWMutex{},
}
// state end
listenAddressString, destinationStringAddresses, err := parseFlags()
if err != nil {
log.Fatalln(err)
}
listenAddress, err := net.ResolveUDPAddr("udp", listenAddressString)
if err != nil {
log.Fatalf("listener: %s", err)
}
ingressConn, err := net.ListenUDP("udp" , listenAddress)
if err != nil {
log.Fatalf("listener: %s", err)
}
defer ingressConn.Close()
primaryEgressConn, err := net.ListenUDP("udp", nil)
if err != nil {
log.Fatalf("primary egress connection: %s", err)
}
defer primaryEgressConn.Close()
secondaryEgressConn, err := net.ListenUDP("udp", nil)
if err != nil {
log.Fatalf("secondary egress connection: %s", err)
}
defer secondaryEgressConn.Close()
ingressReceiveChan := make(chan UDPPacket, 16)
primaryEgressReceiveChan := make(chan UDPPacket, 16)
ingressReadPumpDone := make(chan bool)
primaryEgressReadPumpDone := make(chan bool)
terminateDestinationAddrResolverChan := make(chan bool)
signalChan := make(chan os.Signal, 1)
signal.Notify(signalChan, syscall.SIGINT, syscall.SIGTERM, syscall.SIGUSR1)
go readPump(ingressConn, ingressReceiveChan, ingressReadPumpDone)
go readPump(primaryEgressConn, primaryEgressReceiveChan, primaryEgressReadPumpDone)
resolverState.DestinationStringAddresses = &destinationStringAddresses
go destinationAddrResolver(terminateDestinationAddrResolverChan, &resolverState)
defer func () { terminateDestinationAddrResolverChan <- true }()
mainLoop:
for {
select {
case packet := <- ingressReceiveChan:
func (){
resolverState.Mutex.RLock()
lastPacketSourceAddr = packet.UDPAddr
defer resolverState.Mutex.RUnlock()
if len(*resolverState.DestinationStringAddresses) == 0 {
return
}
err = sendPacketToDestinations(primaryEgressConn, resolverState.DestinationAddresses[:1], packet.Data)
if err != nil {
log.Printf("primary egress send error: %s", err)
return
}
if (!spliceEnabled) {
return
}
err = sendPacketToDestinations(secondaryEgressConn, resolverState.DestinationAddresses[1:], packet.Data)
if err != nil {
log.Printf("secondary egress send error: %s", err)
return
}
}()
case packet := <- primaryEgressReceiveChan:
// we received a packet on the primary egress conn / socket, we send it back
// to the last known address that sent to the ingress conn / socket
if (lastPacketSourceAddr != nil) {
ingressConn.WriteTo(packet.Data, lastPacketSourceAddr)
}
case signal := <- signalChan:
switch signal {
case syscall.SIGTERM:
fallthrough
case syscall.SIGINT:
break mainLoop
case syscall.SIGUSR1:
toggleSpliceEnabled(&spliceEnabled, destinationStringAddresses)
}
case <- ingressReadPumpDone:
log.Printf("UDP listener terminated")
break mainLoop
case <- primaryEgressReadPumpDone:
log.Printf("UDP listener terminated")
break mainLoop
}
}
}