-
Notifications
You must be signed in to change notification settings - Fork 26
/
client_manager.go
463 lines (395 loc) · 13.3 KB
/
client_manager.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
package netcode
import (
"bytes"
"log"
"net"
)
type connectTokenEntry struct {
mac []byte
address *net.UDPAddr
time float64
}
type encryptionEntry struct {
expireTime float64
lastAccess float64
timeout int
address *net.UDPAddr
sendKey []byte
recvKey []byte
}
type ClientManager struct {
maxClients int
maxEntries int
timeout float64
instances []*ClientInstance
connectedClientIds []uint64 // slice of connected clientIds
connectTokensEntries []*connectTokenEntry
cryptoEntries []*encryptionEntry
numCryptoEntries int
emptyMac []byte // used to ensure empty mac (all empty bytes) doesn't match
emptyWriteKey []byte // used to test for empty write key
}
func NewClientManager(timeout float64, maxClients int) *ClientManager {
m := &ClientManager{}
m.maxClients = maxClients
m.maxEntries = maxClients * 8
m.connectedClientIds = make([]uint64, maxClients)
m.timeout = timeout
m.emptyMac = make([]byte, MAC_BYTES)
m.emptyWriteKey = make([]byte, KEY_BYTES)
m.resetClientInstances()
m.resetTokenEntries()
m.resetCryptoEntries()
return m
}
func (m *ClientManager) setTimeout(timeout float64) {
m.timeout = timeout
}
func (m *ClientManager) resetClientInstances() {
m.instances = make([]*ClientInstance, m.maxClients)
for i := 0; i < m.maxClients; i += 1 {
instance := NewClientInstance()
m.instances[i] = instance
}
}
// preallocate the token buffers so we don't have to do nil checks
func (m *ClientManager) resetTokenEntries() {
m.connectTokensEntries = make([]*connectTokenEntry, m.maxEntries)
for i := 0; i < m.maxEntries; i += 1 {
entry := &connectTokenEntry{}
m.clearTokenEntry(entry)
m.connectTokensEntries[i] = entry
}
}
func (m *ClientManager) clearTokenEntry(entry *connectTokenEntry) {
entry.mac = make([]byte, MAC_BYTES)
entry.address = nil
entry.time = -1
}
// preallocate the crypto entries so we don't have to do nil checks
func (m *ClientManager) resetCryptoEntries() {
m.cryptoEntries = make([]*encryptionEntry, m.maxEntries)
for i := 0; i < m.maxEntries; i += 1 {
entry := &encryptionEntry{}
m.clearCryptoEntry(entry)
m.cryptoEntries[i] = entry
}
}
func (m *ClientManager) clearCryptoEntry(entry *encryptionEntry) {
entry.expireTime = -1
entry.lastAccess = -1000
entry.address = nil
entry.sendKey = make([]byte, KEY_BYTES)
entry.recvKey = make([]byte, KEY_BYTES)
}
func (m *ClientManager) FindFreeClientIndex() int {
for i := 0; i < m.maxClients; i += 1 {
if !m.instances[i].connected {
return i
}
}
return -1
}
// Returns the clientIds of the connected clients. To avoid allocating a buffer everytime this is called
// we simply re-add all connected clients to the connectedClientIds buffer and return the slice of how
// many we were able to add.
func (m *ClientManager) ConnectedClients() []uint64 {
i := 0
for clientIndex := 0; clientIndex < m.maxClients; clientIndex += 1 {
client := m.instances[clientIndex]
if client.connected && client.address != nil {
m.connectedClientIds[i] = client.clientId
i++
}
}
return m.connectedClientIds[0:i]
}
func (m *ClientManager) ConnectedClientCount() int {
return len(m.ConnectedClients())
}
// Initializes the client with the clientId
func (m *ClientManager) ConnectClient(addr *net.UDPAddr, challengeToken *ChallengeToken) *ClientInstance {
clientIndex := m.FindFreeClientIndex()
if clientIndex == -1 {
log.Printf("failure to find free client index\n")
return nil
}
client := m.instances[clientIndex]
client.clientIndex = clientIndex
client.connected = true
client.sequence = 0
client.clientId = challengeToken.ClientId
client.address = addr
copy(client.userData, challengeToken.UserData.Bytes())
return client
}
// Disconnects the client referenced by the provided clientIndex.
func (m *ClientManager) DisconnectClient(clientIndex int, sendDisconnect bool, serverTime float64) {
instance := m.instances[clientIndex]
m.disconnectClient(instance, sendDisconnect, serverTime)
}
// Finds the client index referenced by the provided UDPAddr.
func (m *ClientManager) FindClientIndexByAddress(addr *net.UDPAddr) int {
for i := 0; i < m.maxClients; i += 1 {
instance := m.instances[i]
if instance.address != nil && instance.connected && addressEqual(instance.address, addr) {
return i
}
}
return -1
}
// Finds the client index via the provided clientId.
func (m *ClientManager) FindClientIndexById(clientId uint64) int {
for i := 0; i < m.maxClients; i += 1 {
instance := m.instances[i]
if instance.address != nil && instance.connected && instance.clientId == clientId {
return i
}
}
return -1
}
// Finds the encryption index via the provided clientIndex, returns -1 if not found.
func (m *ClientManager) FindEncryptionIndexByClientIndex(clientIndex int) int {
if clientIndex < 0 || clientIndex > m.maxClients {
return -1
}
return m.instances[clientIndex].encryptionIndex
}
// Finds an encryption entry index via the provided UDPAddr.
func (m *ClientManager) FindEncryptionEntryIndex(addr *net.UDPAddr, serverTime float64) int {
for i := 0; i < m.numCryptoEntries; i += 1 {
entry := m.cryptoEntries[i]
if entry == nil || entry.address == nil {
continue
}
lastAccessTimeout := entry.lastAccess + m.timeout
if addressEqual(entry.address, addr) && serverTimedout(lastAccessTimeout, serverTime) && (entry.expireTime < 0 || entry.expireTime >= serverTime) {
entry.lastAccess = serverTime
return i
}
}
return -1
}
// Finds or adds a token entry to our token entry slice.
func (m *ClientManager) FindOrAddTokenEntry(connectTokenMac []byte, addr *net.UDPAddr, serverTime float64) bool {
var oldestTime float64
tokenIndex := -1
oldestIndex := -1
if bytes.Equal(connectTokenMac, m.emptyMac) {
return false
}
// find the matching entry for the token mac and the oldest token entry.
for i := 0; i < m.maxEntries; i += 1 {
if bytes.Equal(m.connectTokensEntries[i].mac, connectTokenMac) {
tokenIndex = i
}
if oldestIndex == -1 || m.connectTokensEntries[i].time < oldestTime {
oldestTime = m.connectTokensEntries[i].time
oldestIndex = i
}
}
// if no entry is found with the mac, this is a new connect token. replace the oldest token entry.
if tokenIndex == -1 {
m.connectTokensEntries[oldestIndex].time = serverTime
m.connectTokensEntries[oldestIndex].address = addr
m.connectTokensEntries[oldestIndex].mac = connectTokenMac
log.Printf("new connect token added for %s\n", addr.String())
return true
}
// allow connect tokens we have already seen from the same address
if addressEqual(m.connectTokensEntries[tokenIndex].address, addr) {
return true
}
return false
}
// Adds a new encryption mapping of client/server keys.
func (m *ClientManager) AddEncryptionMapping(connectToken *ConnectTokenPrivate, addr *net.UDPAddr, serverTime, expireTime float64) bool {
// already list
for i := 0; i < m.maxEntries; i += 1 {
entry := m.cryptoEntries[i]
lastAccessTimeout := entry.lastAccess + m.timeout
if entry.address != nil && addressEqual(entry.address, addr) && serverTimedout(lastAccessTimeout, serverTime) {
entry.expireTime = expireTime
entry.lastAccess = serverTime
copy(entry.sendKey, connectToken.ServerKey)
copy(entry.recvKey, connectToken.ClientKey)
log.Printf("re-added encryption mapping for %s encIdx: %d\n", addr.String(), i)
return true
}
}
// not in our list.
for i := 0; i < m.maxEntries; i += 1 {
entry := m.cryptoEntries[i]
if entry.lastAccess+m.timeout < serverTime || (entry.expireTime >= 0 && entry.expireTime < serverTime) {
entry.address = addr
entry.expireTime = expireTime
entry.lastAccess = serverTime
copy(entry.sendKey, connectToken.ServerKey)
copy(entry.recvKey, connectToken.ClientKey)
if i+1 > m.numCryptoEntries {
m.numCryptoEntries = i + 1
}
return true
}
}
return false
}
// Update the encryption entry for the provided encryption index.
func (m *ClientManager) TouchEncryptionEntry(encryptionIndex int, addr *net.UDPAddr, serverTime float64) bool {
if encryptionIndex < 0 || encryptionIndex > m.numCryptoEntries {
return false
}
if !addressEqual(m.cryptoEntries[encryptionIndex].address, addr) {
return false
}
m.cryptoEntries[encryptionIndex].lastAccess = serverTime
return true
}
// Sets the expiration for this encryption entry.
func (m *ClientManager) SetEncryptionEntryExpiration(encryptionIndex int, expireTime float64) bool {
if encryptionIndex < 0 || encryptionIndex > m.numCryptoEntries {
return false
}
m.cryptoEntries[encryptionIndex].expireTime = expireTime
return true
}
// Removes the encryption entry for this UDPAddr.
func (m *ClientManager) RemoveEncryptionEntry(addr *net.UDPAddr, serverTime float64) bool {
for i := 0; i < m.numCryptoEntries; i += 1 {
entry := m.cryptoEntries[i]
if !addressEqual(entry.address, addr) {
continue
}
m.clearCryptoEntry(entry)
if i+1 == m.numCryptoEntries {
index := i - 1
for index >= 0 {
lastAccessTimeout := m.cryptoEntries[index].lastAccess + m.timeout
if serverTimedout(lastAccessTimeout, serverTime) && (m.cryptoEntries[index].expireTime < 0 || m.cryptoEntries[index].expireTime > serverTime) {
break
}
index--
}
m.numCryptoEntries = index + 1
}
return true
}
return false
}
// Returns the encryption send key.
func (m *ClientManager) GetEncryptionEntrySendKey(index int) []byte {
return m.getEncryptionEntryKey(index, true)
}
// Returns the encryption recv key.
func (m *ClientManager) GetEncryptionEntryRecvKey(index int) []byte {
return m.getEncryptionEntryKey(index, false)
}
func (m *ClientManager) getEncryptionEntryKey(index int, sendKey bool) []byte {
if index == -1 || index < 0 || index > m.numCryptoEntries {
return nil
}
if sendKey {
return m.cryptoEntries[index].sendKey
}
return m.cryptoEntries[index].recvKey
}
func (m *ClientManager) sendPayloads(payloadData []byte, serverTime float64) {
for i := 0; i < m.maxClients; i += 1 {
m.sendPayloadToInstance(i, payloadData, serverTime)
}
}
func (m *ClientManager) sendPayloadToInstance(index int, payloadData []byte, serverTime float64) {
instance := m.instances[index]
if instance.encryptionIndex == -1 {
return
}
writePacketKey := m.GetEncryptionEntrySendKey(instance.encryptionIndex)
if bytes.Equal(writePacketKey, m.emptyWriteKey) || instance.address == nil {
return
}
if !instance.confirmed {
packet := &KeepAlivePacket{}
packet.ClientIndex = uint32(instance.clientIndex)
packet.MaxClients = uint32(m.maxClients)
instance.SendPacket(packet, writePacketKey, serverTime)
}
if instance.connected {
if !m.TouchEncryptionEntry(instance.encryptionIndex, instance.address, serverTime) {
log.Printf("error: encryption mapping is out of date for client %d\n", instance.clientIndex)
return
}
packet := NewPayloadPacket(payloadData)
instance.SendPacket(packet, writePacketKey, serverTime)
}
}
// Send keep alives to all connected clients.
func (m *ClientManager) SendKeepAlives(serverTime float64) {
for i := 0; i < m.maxClients; i += 1 {
instance := m.instances[i]
if !instance.connected {
continue
}
writePacketKey := m.GetEncryptionEntrySendKey(instance.encryptionIndex)
if bytes.Equal(writePacketKey, m.emptyWriteKey) || instance.address == nil {
continue
}
shouldSendTime := instance.lastSendTime + float64(1.0/PACKET_SEND_RATE)
if shouldSendTime < serverTime || floatEquals(shouldSendTime, serverTime) {
if !m.TouchEncryptionEntry(instance.encryptionIndex, instance.address, serverTime) {
log.Printf("error: encryption mapping is out of date for client %d\n", instance.clientIndex)
continue
}
packet := &KeepAlivePacket{}
packet.ClientIndex = uint32(instance.clientIndex)
packet.MaxClients = uint32(m.maxClients)
instance.SendPacket(packet, writePacketKey, serverTime)
}
}
}
// Checks and disconnects any clients that have timed out.
func (m *ClientManager) CheckTimeouts(serverTime float64) {
for i := 0; i < m.maxClients; i += 1 {
instance := m.instances[i]
timeout := instance.lastRecvTime + m.timeout
if instance.connected && (timeout < serverTime || floatEquals(timeout, serverTime)) {
log.Printf("server timed out client: %d\n", i)
m.disconnectClient(instance, false, serverTime)
}
}
}
func (m *ClientManager) disconnectClients(serverTime float64) {
for clientIndex := 0; clientIndex < m.maxClients; clientIndex += 1 {
instance := m.instances[clientIndex]
m.disconnectClient(instance, true, serverTime)
}
}
func (m *ClientManager) disconnectClient(client *ClientInstance, sendDisconnect bool, serverTime float64) {
if !client.connected {
return
}
if sendDisconnect {
packet := &DisconnectPacket{}
writePacketKey := m.GetEncryptionEntrySendKey(client.encryptionIndex)
if writePacketKey == nil {
log.Printf("error: unable to retrieve encryption key for client for disconnect: %d\n", client.clientId)
} else {
for i := 0; i < NUM_DISCONNECT_PACKETS; i += 1 {
client.SendPacket(packet, writePacketKey, serverTime)
}
}
}
log.Printf("removing encryption entry for: %s", client.address.String())
m.RemoveEncryptionEntry(client.address, serverTime)
client.Clear()
}
const EPSILON float64 = 0.000001
func floatEquals(a, b float64) bool {
if (a-b) < EPSILON && (b-a) < EPSILON {
return true
}
return false
}
// checks if last access + timeout is > or = to serverTime.
func serverTimedout(lastAccessTimeout, serverTime float64) bool {
return (lastAccessTimeout > serverTime || floatEquals(lastAccessTimeout, serverTime))
}