/
relay.go
387 lines (327 loc) · 12.1 KB
/
relay.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
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
package client
import (
"errors"
"github.com/alejzeis/vic2-multi-proxy/common"
"github.com/gorilla/websocket"
log "github.com/sirupsen/logrus"
"net"
"strconv"
"strings"
"sync"
)
func CreateStartRelay() *gameDataRelayImpl {
relay := new(gameDataRelayImpl)
relay.mutex = new(sync.Mutex)
relay.localProxies = make(map[uint64]*virtualGameProxy)
relay.remoteSignalChannel = make(chan channelSignal, 1)
relay.connectionsWaitgroup = new(sync.WaitGroup)
relay.hasClosed = false
return relay
}
type channelSignal uint8
const (
DISCONNECTED_MATCHMAKING channelSignal = iota
BEGIN_HOSTING_FORWARDING
BEGIN_LINKED_FORWARDING
STOP_FORWARDING
)
type GameDataRelay interface {
OnConnectedToServer(address string, auth string) bool
OnDisconnectedFromServer()
OnBeginHosting()
OnStopHosting()
OnBeginLinked()
OnStopLinked()
}
type gameDataRelayImpl struct {
mutex *sync.Mutex
serverAddress string
remoteConnection *websocket.Conn
localProxies map[uint64]*virtualGameProxy
connectionsWaitgroup *sync.WaitGroup
hasClosed bool
remoteSignalChannel chan channelSignal
}
// Represents a "proxied" game connected to the local actual game
// This is either one of various clients (if the local game is hosting)
// Or a virtual "server" (if the local game is linked to a remote lobby)
type virtualGameProxy struct {
// User ID that this proxied game belongs to
identifier uint64
socket *net.UDPConn
gameAddress *net.UDPAddr
relay GameDataRelay
signalChannel chan channelSignal
}
func (proxy *virtualGameProxy) initSocket(port uint16) {
bindAddr := "127.0.0.1:" + strconv.Itoa(int(port))
loopbackAddr, err := net.ResolveUDPAddr("udp", bindAddr)
if err != nil {
log.WithError(err).WithField("addr", bindAddr).Error("Failed to resolve loopback address")
panic(err)
}
listener, err := net.ListenUDP("udp", loopbackAddr)
if err != nil {
log.WithError(err).WithField("addr", bindAddr).Error("Failed to start listening locally")
panic(err)
}
proxy.socket = listener
log.WithFields(log.Fields{
"address": bindAddr,
"proxyIdentifier": proxy.identifier,
}).Debug("Created socket for relaying local game data")
}
// When hosting, each fake "client" will have their own UDP socket and thread instance of this
func (proxy *virtualGameProxy) relayDataLocalToRemote(hosting bool, waitgroup *sync.WaitGroup) {
defer func() {
defer waitgroup.Done()
err := proxy.socket.Close()
if err != nil {
log.WithError(err).WithFields(log.Fields{
"identifier": proxy.identifier,
"localAddress": proxy.socket.LocalAddr().String(),
}).Error("Failed to close local UDP socket for virtualGameProxy.")
}
log.WithField("proxyIdentifier", proxy.identifier).Debug("Stopped listening for local game data")
}()
if hosting {
gameAddr, err := net.ResolveUDPAddr("udp", "127.0.0.1:1630")
if err != nil {
log.WithError(err).WithField("address", "127.0.0.1:1630").Error("Failed to resolve game address")
return
} else {
proxy.gameAddress = gameAddr
}
}
waitgroup.Add(1) // Add ourselves to the waitgroup. This allows the shutdown() function to wait for all goroutines to exit
for {
buf := make([]byte, 2048)
length, address, err := proxy.socket.ReadFromUDP(buf)
if err != nil {
log.WithError(err).WithFields(log.Fields{
"localAddress": proxy.socket.LocalAddr().String(),
"remoteAddress": address,
"proxyIdentifier": proxy.identifier,
}).Error("Error while reading data from local listening socket")
} else if proxy.gameAddress == nil {
proxy.gameAddress = address
}
select {
case _, notClosed := <-proxy.signalChannel:
if !notClosed {
return // Exit function, stopping the thread
}
default:
// We want to do a non-blocking receive
}
container := common.GameDataContainer{
Identifier: proxy.identifier,
Data: buf[0:(length - 1)],
}
proxy.relay.sendContainerToServer(container)
}
}
func (proxy *virtualGameProxy) sendGamePacket(container common.GameDataContainer) error {
if proxy.gameAddress == nil {
return errors.New("game Address hasn't been obtained yet")
} else {
_, err := proxy.socket.WriteToUDP(container.Encode(), proxy.gameAddress)
if err != nil {
return err
} else {
return nil
}
}
}
func (relay *gameDataRelayImpl) sendContainerToServer(container common.GameDataContainer) {
err := relay.remoteConnection.WriteMessage(websocket.BinaryMessage, container.Encode())
if err != nil {
log.WithFields(log.Fields{
"relayAddr": relay.serverAddress,
"proxyIdentifier": container.Identifier,
"length": len(container.Data),
}).WithError(err).Warn("Failed to relay a packet from local game to relay server")
}
}
func (relay *gameDataRelayImpl) shutdown() {
for _, proxy := range relay.localProxies {
close(proxy.signalChannel)
proxy.socket.Close() // Close the socket, since the UDP reads are blocking we need to close the socket to get the goroutines to terminate
}
close(relay.remoteSignalChannel)
log.WithField("proxyCount", len(relay.localProxies)).Debug("Sent shutdown signal to all proxy goroutines, waiting for exits")
relay.connectionsWaitgroup.Wait()
log.WithField("proxyCount", len(relay.localProxies)).Debug("All proxy goroutines have exited")
relay.serverAddress = ""
relay.localProxies = make(map[uint64]*virtualGameProxy) // Overwrite the map, we don't need the old proxies anymore
}
func (relay *gameDataRelayImpl) relayDataRemoteToLocal() {
forwarding := false
hosting := false
defer func() {
err := relay.remoteConnection.Close()
if err != nil {
log.WithError(err).WithField("address", relay.serverAddress).Error("Failed to close websocket connection to relay server.")
}
relay.remoteConnection = nil
log.Debug("Remote to Local relay goroutine exited.")
}()
for {
select {
case signal, notClosed := <-relay.remoteSignalChannel:
if notClosed {
switch signal {
case DISCONNECTED_MATCHMAKING:
return // Exit function, stopping the thread
case STOP_FORWARDING:
forwarding = false
case BEGIN_HOSTING_FORWARDING:
forwarding = true
hosting = true
case BEGIN_LINKED_FORWARDING:
forwarding = true
}
} else { // Channel closed, that's the signal to terminate
return // Exit function, stopping the thread
}
break
default:
// We want to do a non-blocking receive
break
}
msgType, data, err := relay.remoteConnection.ReadMessage()
if err != nil {
if websocket.IsCloseError(err) {
log.WithError(err).Warn("Remote websocket connection closed by server")
} else {
log.WithError(err).Error("Failed to read message from remote websocket connection")
}
relay.onRemoteDisconnected()
} else if msgType == websocket.BinaryMessage && forwarding {
container := common.DecodeGameDataContainer(data)
relay.mutex.Lock()
proxy, found := relay.localProxies[container.Identifier]
if !found && hosting {
// Proxy instance wasn't found, so it must be a new connection.
// Create a new virtualGameProxy to represent this connection
proxy = &virtualGameProxy{
identifier: container.Identifier,
socket: nil,
relay: relay,
signalChannel: make(chan channelSignal, 1),
}
relay.localProxies[proxy.identifier] = proxy
proxy.initSocket(0) // Don't have a specific port to bind to since we are emulating a client
go proxy.relayDataLocalToRemote(true, relay.connectionsWaitgroup)
log.WithField("identifier", proxy.identifier).Debug("Created new virtualgame proxy")
}
if hosting {
err = proxy.sendGamePacket(container)
if err != nil {
log.WithError(err).WithFields(log.Fields{
"identifier": container.Identifier,
"length": len(container.Data),
"gameAddress": proxy.gameAddress,
"relayAddress": relay.serverAddress,
"hosting": hosting,
}).Warn("Failed to send packet from remote to local game")
}
}
relay.mutex.Unlock()
}
}
}
func (relay *gameDataRelayImpl) OnConnectedToServer(address string, auth string) bool {
relay.mutex.Lock()
defer relay.mutex.Unlock()
if relay.remoteConnection != nil {
return true // already connected to server
}
if strings.HasPrefix(address, "http://") {
relay.serverAddress = strings.ReplaceAll(address, "http://", "ws://")
} else if strings.HasPrefix(address, "https://") {
relay.serverAddress = strings.ReplaceAll(address, "https://", "wss://")
} else {
log.WithField("address", address).Error("Invalid address")
return false
}
connection, _, err := websocket.DefaultDialer.Dial(relay.serverAddress+"/relay", nil)
if err != nil {
log.WithError(err).WithField("address", relay.serverAddress).Error("Failed to connect to relay server")
return false
}
err = connection.WriteMessage(websocket.TextMessage, []byte(auth))
if err != nil {
log.WithError(err).WithField("address", relay.serverAddress).Error("Failed to send authentication string to relay server")
_ = connection.Close()
return false
}
relay.remoteConnection = connection
relay.hasClosed = false
go relay.relayDataRemoteToLocal()
return true
}
// Called from relayDataRemoteToLocal when the server closes the connection on us
func (relay *gameDataRelayImpl) onRemoteDisconnected() {
relay.mutex.Lock()
defer relay.mutex.Unlock()
relay.shutdown()
}
func (relay *gameDataRelayImpl) OnDisconnectedFromServer() {
relay.mutex.Lock()
defer relay.mutex.Unlock()
// Make sure we haven't already completed disconnect tasks
if relay.remoteConnection != nil {
err := relay.remoteConnection.WriteMessage(websocket.CloseMessage, websocket.FormatCloseMessage(websocket.CloseNormalClosure, "Logging out"))
if err != nil {
panic(err)
}
relay.hasClosed = true
}
}
func (relay *gameDataRelayImpl) OnBeginHosting() {
relay.mutex.Lock() // Lock mutex since we're reading the list of local proxies
defer relay.mutex.Unlock()
if len(relay.localProxies) < 1 { // There shouldn't be any virtualgameproxys on host start, or link start either
// No need to create any virtualgameproxy instances,
// the relayDataRemoteToLocal function will automatically create them as packets come in from Relay server
relay.remoteSignalChannel <- BEGIN_HOSTING_FORWARDING // Start allowing forwarding data from Relay server to the local game instance
}
}
func (relay *gameDataRelayImpl) OnStopHosting() {
relay.mutex.Lock() // Lock mutex since we're reading the list of local proxies
defer relay.mutex.Unlock()
relay.remoteSignalChannel <- STOP_FORWARDING
for _, proxy := range relay.localProxies {
close(proxy.signalChannel)
proxy.socket.Close() // Close the socket, since the UDP reads are blocking we need to close the socket to get the goroutines to terminate
}
relay.localProxies = make(map[uint64]*virtualGameProxy) // Overwrite the map, we don't need the old proxies anymore
}
func (relay *gameDataRelayImpl) OnBeginLinked() {
relay.mutex.Lock() // Lock mutex since we're reading the list of local proxies
defer relay.mutex.Unlock()
if len(relay.localProxies) < 1 { // There shouldn't be any virtualgameproxys on link start, or host start either
proxy := &virtualGameProxy{
identifier: 0, // ID zero for when we are emulating a hosted game; when linked we only need one Game proxy
socket: nil,
relay: relay,
signalChannel: make(chan channelSignal),
}
relay.localProxies[0] = proxy
proxy.initSocket(common.DefaultVic2GamePort) // We want to open our socket on the Vic2 game port since we are emulating a hosted game
go proxy.relayDataLocalToRemote(false, relay.connectionsWaitgroup)
relay.remoteSignalChannel <- BEGIN_LINKED_FORWARDING // Start allowing forwarding data from Relay server to the local game instance
}
}
func (relay *gameDataRelayImpl) OnStopLinked() {
relay.mutex.Lock() // Lock mutex since we're reading the list of local proxies
defer relay.mutex.Unlock()
relay.remoteSignalChannel <- STOP_FORWARDING
proxy, found := relay.localProxies[0] // ID zero for when we are emulating a hosted game; when linked we only need one Game proxy
if found {
close(proxy.signalChannel)
proxy.socket.Close() // Close the socket, since the UDP reads are blocking we need to close the socket to get the goroutines to terminate
delete(relay.localProxies, 0)
}
}