-
Notifications
You must be signed in to change notification settings - Fork 26
/
Copy pathserver.go
525 lines (433 loc) · 14.4 KB
/
server.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
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
package netcode
import (
"fmt"
"log"
"net"
"sync/atomic"
"time"
)
const TIMEOUT_SECONDS = 5 // default timeout for clients
const MAX_SERVER_PACKETS = 64
type Server struct {
serverConn *NetcodeConn
serverAddr *net.UDPAddr
shutdownCh chan struct{}
serverTime float64
running bool
maxClients int
connectedClients int
timeout float64
clientManager *ClientManager
globalSequence uint64
ignoreRequests bool
ignoreResponses bool
allowedPackets []byte
protocolId uint64
privateKey []byte
challengeKey []byte
challengeSequence uint64
recvBytes int
packetCh chan *NetcodeData
}
func NewServer(serverAddress *net.UDPAddr, privateKey []byte, protocolId uint64, maxClients int) *Server {
s := &Server{}
s.serverAddr = serverAddress
s.protocolId = protocolId
s.privateKey = privateKey
s.maxClients = maxClients
s.globalSequence = uint64(1) << 63
s.timeout = float64(TIMEOUT_SECONDS)
s.clientManager = NewClientManager(s.timeout, maxClients)
s.packetCh = make(chan *NetcodeData, s.maxClients*MAX_SERVER_PACKETS*2)
s.shutdownCh = make(chan struct{})
// set allowed packets for this server
s.allowedPackets = make([]byte, ConnectionNumPackets)
s.allowedPackets[ConnectionRequest] = 1
s.allowedPackets[ConnectionResponse] = 1
s.allowedPackets[ConnectionKeepAlive] = 1
s.allowedPackets[ConnectionPayload] = 1
s.allowedPackets[ConnectionDisconnect] = 1
return s
}
func (s *Server) SetAllowedPackets(allowedPackets []byte) {
s.allowedPackets = allowedPackets
}
func (s *Server) SetTimeout(duration time.Duration) {
s.timeout = duration.Seconds()
s.clientManager.setTimeout(s.timeout)
}
func (s *Server) SetIgnoreRequests(val bool) {
s.ignoreRequests = val
}
func (s *Server) SetIgnoreResponses(val bool) {
s.ignoreResponses = val
}
// increments the challenge sequence and returns the un-incremented value
func (s *Server) incChallengeSequence() uint64 {
val := atomic.AddUint64(&s.challengeSequence, 1)
return val - 1
}
func (s *Server) incGlobalSequence() uint64 {
val := atomic.AddUint64(&s.globalSequence, 1)
return val - 1
}
func (s *Server) Init() error {
var err error
s.challengeKey, err = GenerateKey()
if err != nil {
return err
}
s.serverConn = NewNetcodeConn()
s.serverConn.SetReadBuffer(SOCKET_RCVBUF_SIZE * s.maxClients)
s.serverConn.SetWriteBuffer(SOCKET_SNDBUF_SIZE * s.maxClients)
s.serverConn.SetRecvHandler(s.handleNetcodeData)
return nil
}
func (s *Server) Listen() error {
s.running = true
addr := &net.UDPAddr{
Port: s.serverAddr.Port,
IP: net.ParseIP("0.0.0.0"),
}
if err := s.serverConn.Listen(addr); err != nil {
return err
}
return nil
}
func (s *Server) SendPayloads(payloadData []byte) {
if !s.running {
return
}
s.clientManager.sendPayloads(payloadData, s.serverTime)
}
// Sends the payload to the client specified by their clientId.
func (s *Server) SendPayloadToClient(clientId uint64, payloadData []byte) error {
clientIndex, err := s.GetClientIndexByClientId(clientId)
if err != nil {
return err
}
s.clientManager.sendPayloadToInstance(clientIndex, payloadData, s.serverTime)
return nil
}
func (s *Server) Update(time float64) error {
if !s.running {
return ErrServerShutdown
}
s.serverTime = time
// empty recv'd data from channel so we can have safe access to client manager data structures
for {
select {
case recv := <-s.packetCh:
s.OnPacketData(recv.data, recv.from)
default:
goto DONE
}
}
DONE:
s.clientManager.SendKeepAlives(s.serverTime)
s.clientManager.CheckTimeouts(s.serverTime)
return nil
}
// Disconnects a single client via the specified clientId
func (s *Server) DisconnectClient(clientId uint64, sendDisconnect bool) error {
clientIndex, err := s.GetClientIndexByClientId(clientId)
if err != nil {
return err
}
s.clientManager.DisconnectClient(clientIndex, sendDisconnect, s.serverTime)
return nil
}
func (s *Server) GetClientIndexByClientId(clientId uint64) (int, error) {
if !s.running {
return -1, ErrServerNotRunning
}
clientIndex := s.clientManager.FindClientIndexById(clientId)
if clientIndex == -1 {
return -1, fmt.Errorf("unknown client id %d", clientId)
}
return clientIndex, nil
}
// write the netcodeData to our buffered packet channel. The NetcodeConn verifies
// that the recv'd data is > 0 < maxBytes and is of a valid packet type before
// this is even called.
// NOTE: we will block the netcodeConn from processing which is what we want since
// we want to synchronize access from the Update call.
func (s *Server) handleNetcodeData(packetData *NetcodeData) {
s.packetCh <- packetData
}
func (s *Server) OnPacketData(packetData []byte, addr *net.UDPAddr) {
var readPacketKey []byte
var replayProtection *ReplayProtection
if !s.running {
return
}
size := len(packetData)
encryptionIndex := -1
clientIndex := s.clientManager.FindClientIndexByAddress(addr)
if clientIndex != -1 {
encryptionIndex = s.clientManager.FindEncryptionIndexByClientIndex(clientIndex)
} else {
encryptionIndex = s.clientManager.FindEncryptionEntryIndex(addr, s.serverTime)
}
readPacketKey = s.clientManager.GetEncryptionEntryRecvKey(encryptionIndex)
timestamp := uint64(time.Now().Unix())
packet := NewPacket(packetData)
if clientIndex != -1 {
client := s.clientManager.instances[clientIndex]
replayProtection = client.replayProtection
}
if err := packet.Read(packetData, size, s.protocolId, timestamp, readPacketKey, s.privateKey, s.allowedPackets, replayProtection); err != nil {
log.Printf("error reading packet: %s from %s\n", err, addr)
return
}
s.processPacket(clientIndex, encryptionIndex, packet, addr)
}
func (s *Server) processPacket(clientIndex, encryptionIndex int, packet Packet, addr *net.UDPAddr) {
switch packet.GetType() {
case ConnectionRequest:
if s.ignoreRequests {
return
}
log.Printf("server received connection request from %s\n", addr.String())
s.processConnectionRequest(packet, addr)
case ConnectionResponse:
if s.ignoreResponses {
return
}
log.Printf("server received connection response from %s\n", addr.String())
s.processConnectionResponse(clientIndex, encryptionIndex, packet, addr)
case ConnectionKeepAlive:
if clientIndex == -1 {
return
}
client := s.clientManager.instances[clientIndex]
client.lastRecvTime = s.serverTime
if !client.confirmed {
client.confirmed = true
log.Printf("server confirmed connection to client %d:%s\n", client.clientId, client.address.String())
}
case ConnectionPayload:
if clientIndex == -1 {
return
}
client := s.clientManager.instances[clientIndex]
client.lastRecvTime = s.serverTime
if !client.confirmed {
client.confirmed = true
log.Printf("server confirmed connection to client %d:%s\n", client.clientId, client.address.String())
}
client.packetQueue.Push(packet)
case ConnectionDisconnect:
if clientIndex == -1 {
return
}
client := s.clientManager.instances[clientIndex]
log.Printf("server received disconnect packet from client %d:%s\n", client.clientId, client.address.String())
s.clientManager.disconnectClient(client, false, s.serverTime)
}
}
func (s *Server) processConnectionRequest(packet Packet, addr *net.UDPAddr) {
requestPacket, ok := packet.(*RequestPacket)
if !ok {
return
}
if len(requestPacket.Token.ServerAddrs) == 0 {
log.Printf("server ignored connection request. server address not in connect token whitelist\n")
return
}
addrFound := false
for _, tokenAddr := range requestPacket.Token.ServerAddrs {
if addressEqual(s.serverAddr, &tokenAddr) {
addrFound = true
break
}
}
if !addrFound {
log.Printf("server ignored connection request. server address not in connect token whitelist\n")
return
}
clientIndex := s.clientManager.FindClientIndexByAddress(addr)
if clientIndex != -1 {
log.Printf("server ignored connection request. a client with this address is already connected\n")
return
}
clientIndex = s.clientManager.FindClientIndexById(requestPacket.Token.ClientId)
if clientIndex != -1 {
log.Printf("server ignored connection request. a client with this id has already been used\n")
return
}
if !s.clientManager.FindOrAddTokenEntry(requestPacket.Token.Mac(), addr, s.serverTime) {
log.Printf("server ignored connection request. connect token has already been used\n")
return
}
if s.clientManager.ConnectedClientCount() == s.maxClients {
log.Printf("server denied connection request. server is full\n")
s.sendDeniedPacket(requestPacket.Token.ServerKey, addr)
return
}
if !s.clientManager.AddEncryptionMapping(requestPacket.Token, addr, s.serverTime, s.serverTime+s.timeout) {
log.Printf("server ignored connection request. failed to add encryption mapping\n")
return
}
s.sendChallengePacket(requestPacket, addr)
}
func (s *Server) sendChallengePacket(requestPacket *RequestPacket, addr *net.UDPAddr) {
var bytesWritten int
var err error
challenge := NewChallengeToken(requestPacket.Token.ClientId)
challengeBuf := challenge.Write(requestPacket.Token.UserData)
challengeSequence := s.incChallengeSequence()
if err := EncryptChallengeToken(challengeBuf, challengeSequence, s.challengeKey); err != nil {
log.Printf("server ignored connection request. failed to encrypt challenge token\n")
return
}
challengePacket := &ChallengePacket{}
challengePacket.ChallengeTokenData = challengeBuf
challengePacket.ChallengeTokenSequence = challengeSequence
buffer := make([]byte, MAX_PACKET_BYTES)
if bytesWritten, err = challengePacket.Write(buffer, s.protocolId, s.incGlobalSequence(), requestPacket.Token.ServerKey); err != nil {
log.Printf("server error while writing challenge packet\n")
return
}
s.sendGlobalPacket(buffer[:bytesWritten], addr)
}
func (s *Server) sendGlobalPacket(packetBuffer []byte, addr *net.UDPAddr) {
if _, err := s.serverConn.WriteTo(packetBuffer, addr); err != nil {
log.Printf("error sending packet to %s\n", addr.String())
}
}
func (s *Server) processConnectionResponse(clientIndex, encryptionIndex int, packet Packet, addr *net.UDPAddr) {
var err error
var tokenBuffer []byte
var challengeToken *ChallengeToken
responsePacket, ok := packet.(*ResponsePacket)
if !ok {
return
}
if tokenBuffer, err = DecryptChallengeToken(responsePacket.ChallengeTokenData, responsePacket.ChallengeTokenSequence, s.challengeKey); err != nil {
log.Printf("failed to decrypt challenge token: %s\n", err)
return
}
if challengeToken, err = ReadChallengeToken(tokenBuffer); err != nil {
log.Printf("failed to read challenge token: %s\n", err)
return
}
sendKey := s.clientManager.GetEncryptionEntrySendKey(encryptionIndex)
if sendKey == nil {
log.Printf("server ignored connection response. no packet send key\n")
return
}
if s.clientManager.FindClientIndexByAddress(addr) != -1 {
log.Printf("server ignored connection response. a client with this address is already connected")
return
}
if s.clientManager.FindClientIndexById(challengeToken.ClientId) != -1 {
log.Printf("server ignored connection response. a client with this id is already connected")
return
}
if s.clientManager.ConnectedClientCount() == s.maxClients {
log.Printf("server denied connection response. server is full\n")
s.sendDeniedPacket(sendKey, addr)
return
}
s.connectClient(encryptionIndex, challengeToken, addr)
return
}
func (s *Server) sendDeniedPacket(sendKey []byte, addr *net.UDPAddr) {
var bytesWritten int
var err error
deniedPacket := &DeniedPacket{}
packetBuffer := make([]byte, MAX_PACKET_BYTES)
if bytesWritten, err = deniedPacket.Write(packetBuffer, s.protocolId, s.incGlobalSequence(), sendKey); err != nil {
log.Printf("error creating denied packet: %s\n", err)
return
}
s.sendGlobalPacket(packetBuffer[:bytesWritten], addr)
}
func (s *Server) connectClient(encryptionIndex int, challengeToken *ChallengeToken, addr *net.UDPAddr) {
if s.clientManager.ConnectedClientCount() > s.maxClients {
log.Printf("maxium number of clients reached")
return
}
s.clientManager.SetEncryptionEntryExpiration(encryptionIndex, -1)
client := s.clientManager.ConnectClient(addr, challengeToken)
if client == nil {
return
}
client.serverConn = s.serverConn
client.encryptionIndex = encryptionIndex
client.protocolId = s.protocolId
client.lastSendTime = s.serverTime
client.lastRecvTime = s.serverTime
log.Printf("server accepted client %d from %s in slot: %d\n", client.clientId, addr.String(), client.clientIndex)
s.sendKeepAlive(client)
}
func (s *Server) sendKeepAlive(client *ClientInstance) {
clientIndex := client.clientIndex
packet := &KeepAlivePacket{}
packet.ClientIndex = uint32(clientIndex)
packet.MaxClients = uint32(s.maxClients)
if !s.clientManager.TouchEncryptionEntry(client.encryptionIndex, client.address, s.serverTime) {
log.Printf("error: encryption mapping is out of date for client %d encIndex: %d addr: %s\n", clientIndex, client.encryptionIndex, client.address.String())
return
}
writePacketKey := s.clientManager.GetEncryptionEntrySendKey(client.encryptionIndex)
if writePacketKey == nil {
log.Printf("error: unable to retrieve encryption key for client: %d\n", clientIndex)
return
}
if err := client.SendPacket(packet, writePacketKey, s.serverTime); err != nil {
log.Printf("%s\n", err)
}
}
func (s *Server) GetConnectedClientIds() []uint64 {
return s.clientManager.ConnectedClients()
}
func (s *Server) MaxClients() int {
return s.maxClients
}
func (s *Server) HasClients() int {
return s.clientManager.ConnectedClientCount()
}
func (s *Server) GetClientUserData(clientId uint64) ([]byte, error) {
i, err := s.GetClientIndexByClientId(clientId)
if err != nil {
return nil, err
}
return s.clientManager.instances[i].userData, nil
}
func (s *Server) Stop() error {
if !s.running {
return nil
}
s.clientManager.disconnectClients(s.serverTime)
s.running = false
s.maxClients = 0
s.globalSequence = 0
s.challengeSequence = 0
s.challengeKey = make([]byte, KEY_BYTES)
s.clientManager.resetCryptoEntries()
s.clientManager.resetTokenEntries()
close(s.shutdownCh)
s.running = false
s.serverConn.Close()
return nil
}
func (s *Server) RecvPayload(clientIndex int) ([]byte, uint64) {
packet := s.clientManager.instances[clientIndex].packetQueue.Pop()
if packet == nil {
return []byte{}, 0
}
p, ok := packet.(*PayloadPacket)
if !ok {
log.Printf("not a payload packet")
return []byte{}, 0
}
return p.PayloadData, p.sequence
}
func addressEqual(addr1, addr2 *net.UDPAddr) bool {
if addr1 == nil || addr2 == nil {
return false
}
return addr1.IP.Equal(addr2.IP) && addr1.Port == addr2.Port
}