forked from AscensionGameDev/Intersect-Engine
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLiteNetLibInterface.cs
More file actions
627 lines (542 loc) · 22.5 KB
/
Copy pathLiteNetLibInterface.cs
File metadata and controls
627 lines (542 loc) · 22.5 KB
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
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
using System.Collections.Concurrent;
using System.Diagnostics;
using System.Net;
using System.Net.Sockets;
using System.Security.Cryptography;
using System.Text;
using Intersect.Core;
using Intersect.Framework.Core;
using Intersect.Framework.Reflection;
using Intersect.Memory;
using Intersect.Network.Events;
using Intersect.Network.Packets;
using Intersect.Utilities;
using LiteNetLib;
using LiteNetLib.Utils;
using Microsoft.Extensions.Logging;
using NetPeer = LiteNetLib.NetPeer;
namespace Intersect.Network.LiteNetLib;
public sealed class LiteNetLibInterface : INetworkLayerInterface, INetEventListener
{
private sealed record InboundBuffer(IConnection Connection, IBuffer Buffer);
private readonly ConcurrentDictionary<int, Guid> _connectionIdLookup = new();
private readonly NetManager _manager;
private readonly INetwork _network;
private readonly RSA _asymmetricServer;
private readonly RSA _asymmetricUnconnected;
private readonly ConcurrentQueue<InboundBuffer> _pendingInboundBuffers = new();
static LiteNetLibInterface()
{
NetDebug.Logger = new LiteNetLibLogger();
}
public LiteNetLibInterface(INetwork network, RSAParameters asymmetricParameters)
{
_network = network;
_manager = new NetManager(this)
{
AllowPeerAddressChange = network.Configuration.IsServer,
AutoRecycle = true,
EnableStatistics = true,
#if DEBUG
DisconnectTimeout = 600_000,
#else
DisconnectTimeout = 60_000,
#endif
IPv6Enabled = true,
PingInterval = 5000,
UnconnectedMessagesEnabled = true,
UnsyncedEvents = true,
UnsyncedDeliveryEvent = true,
UnsyncedReceiveEvent = true,
};
_asymmetricServer = RSA.Create(asymmetricParameters);
_asymmetricUnconnected = RSA.Create(2048);
}
public void Dispose()
{
}
public event HandleConnectionEvent? OnConnected;
public event HandleConnectionEvent? OnConnectionApproved;
public event HandleConnectionEvent? OnConnectionDenied;
public event HandleConnectionRequest? OnConnectionRequested;
public event HandleConnectionEvent? OnDisconnected;
public event HandlePacketAvailable? OnPacketAvailable;
public event HandleUnconnectedMessage? OnUnconnectedMessage;
public bool TryGetInboundBuffer(out IBuffer? buffer, out IConnection? connection)
{
if (!_pendingInboundBuffers.TryDequeue(out var inboundBuffer))
{
buffer = default;
connection = default;
return false;
}
buffer = inboundBuffer.Buffer;
connection = inboundBuffer.Connection;
return true;
}
public void ReleaseInboundBuffer(IBuffer buffer) => buffer.Dispose();
public bool SendPacket(
IPacket packet,
IConnection? connection = null,
TransmissionMode transmissionMode = TransmissionMode.All
)
{
connection ??= _network.Connections.FirstOrDefault();
if (connection != default)
{
return connection.Send(packet, transmissionMode);
}
ApplicationContext.Context.Value?.Logger.LogDebug("No active connections.");
return false;
}
public bool SendPacket(IPacket packet, ICollection<IConnection> connections, TransmissionMode transmissionMode = TransmissionMode.All)
{
var data = NetDataWriter.FromBytes(packet.Data, false);
foreach (var connection in connections)
{
if (connection is LiteNetLibConnection liteNetLibConnection)
{
if (!liteNetLibConnection.Send(data, transmissionMode))
{
return false;
}
}
else if (!connection.Send(packet, transmissionMode))
{
return false;
}
}
return true;
}
public void Start()
{
if (!_network.Configuration.IsServer)
{
if (!_manager.Start())
{
ApplicationContext.Context.Value?.Logger.LogError("Failed to make the initial connection attempt.");
}
} else if (!_manager.Start(_network.Configuration.Port))
{
throw new Exception($"Failed to listen on port {_network.Configuration.Port}");
}
}
public void Stop(string reason = "stopping")
{
ApplicationContext.Context.Value?.Logger.LogDebug($"Stopping {nameof(LiteNetLibInterface)} (\"{reason}\")...");
var reasonData = Encoding.UTF8.GetBytes(reason);
_manager.DisconnectAll(reasonData, 0, reasonData.Length);
}
public bool Connect()
{
if (_network.Configuration.IsServer)
{
throw new NotSupportedException();
}
if (!_manager.IsRunning && !_manager.Start())
{
return false;
}
LiteNetLibConnection connection = new(_network, _manager, _asymmetricServer);
if (_network.AddConnection(connection))
{
return true;
}
ApplicationContext.Context.Value?.Logger.LogError("Failed to add connection to list.");
connection.Dispose();
return false;
}
public void Disconnect(IConnection connection, string? message) => Disconnect(new[] { connection }, message);
public void Disconnect(ICollection<IConnection> connections, string? message)
{
foreach (var connection in connections)
{
connection.Disconnect(message);
_network.RemoveConnection(connection);
}
}
public void OnPeerConnected(NetPeer peer)
{
ApplicationContext.Context.Value?.Logger.LogDebug($"CONN {peer}");
if (peer.Tag is not Guid guid)
{
ApplicationContext.Context.Value?.Logger.LogTrace("Peer tag is not a guid");
return;
}
if (!_connectionIdLookup.TryAdd(peer.Id, guid))
{
ApplicationContext.Context.Value?.Logger.LogTrace($"Failed to add connection ID {guid} for peer {peer.Id}");
return;
}
OnConnected?.Invoke(this, new ConnectionEventArgs
{
Connection = _network.FindConnection(guid),
NetworkStatus = NetworkStatus.Online,
});
}
public void OnPeerDisconnected(NetPeer peer, DisconnectInfo disconnectInfo)
{
if (disconnectInfo.Reason == DisconnectReason.Reconnect)
{
ApplicationContext.Context.Value?.Logger.LogDebug($"RECONNECT: {peer}");
return;
}
string? message = default;
if (disconnectInfo.Reason != DisconnectReason.DisconnectPeerCalled)
{
try
{
if (!disconnectInfo.AdditionalData.EndOfData)
{
message = disconnectInfo.AdditionalData.GetString();
}
ApplicationContext.Context.Value?.Logger.LogDebug(
$"DCON: {peer} \"{disconnectInfo.Reason}\" ({disconnectInfo.SocketErrorCode}): {message ?? "N/A"}"
);
}
catch (Exception exception)
{
ApplicationContext.Context.Value?.Logger.LogDebug($"DCON[ERR]: {peer} \"{disconnectInfo.Reason}\" ({disconnectInfo.SocketErrorCode}): {exception.Message}");
}
}
if (!_connectionIdLookup.TryRemove(peer.Id, out var connectionId))
{
ApplicationContext.Context.Value?.Logger.LogTrace($"No connection found for peer ID {peer.Id} ({_network.Connections.Count})");
return;
}
if (_network.FindConnection(connectionId) is not LiteNetLibConnection connection)
{
ApplicationContext.Context.Value?.Logger.LogTrace($"No connection found for {connectionId} ({_network.Connections.Count})");
return;
}
if (!_network.RemoveConnection(connection))
{
ApplicationContext.Context.Value?.Logger.LogWarning(
$"Failed to remove connection {connection.Guid}, was this already removed? ({_network.Connections.Count})"
);
return;
}
if (!Guid.TryParse(message, out var eventId))
{
// TODO: What the hell is this?
}
OnDisconnected?.Invoke(
this,
new ConnectionEventArgs
{
EventId = eventId,
Connection = connection,
NetworkStatus = NetworkStatus.Offline,
}
);
}
public void OnNetworkError(IPEndPoint endPoint, SocketError socketError)
{
ApplicationContext.Context.Value?.Logger.LogError($"NERR: {socketError} from {endPoint}");
}
public void OnNetworkReceive(
NetPeer peer,
NetPacketReader reader,
byte channelNumber,
DeliveryMethod deliveryMethod
)
{
if (!_connectionIdLookup.TryGetValue(peer.Id, out var connectionId))
{
ApplicationContext.Context.Value?.Logger.LogWarning($"RNIP: {peer} ({channelNumber}, {deliveryMethod})");
return;
}
var genericConnection = _network.FindConnection(connectionId);
if (genericConnection is not LiteNetLibConnection connection)
{
ApplicationContext.Context.Value?.Logger.LogWarning(
$"RNCP: {peer} ({channelNumber}, {deliveryMethod}) ({genericConnection?.Guid} / {genericConnection?.GetFullishName()}"
);
return;
}
if (!reader.TryGetByte(out var packetCode))
{
ApplicationContext.Context.Value?.Logger.LogWarning($"No packet code from {peer} / {connection.Guid}");
return;
}
// ReSharper disable once ConvertIfStatementToSwitchStatement
if (packetCode > 1)
{
if (packetCode != 0x20)
{
ApplicationContext.Context.Value?.Logger.LogWarning($"Invalid packet code from {peer} / {connection.Guid}");
return;
}
if (reader.GetByte() != 0x21)
{
ApplicationContext.Context.Value?.Logger.LogWarning($"Invalid packet code from {peer} / {connection.Guid}");
return;
}
if (reader.GetByte() != 0x22)
{
ApplicationContext.Context.Value?.Logger.LogWarning($"Invalid packet code from {peer} / {connection.Guid}");
return;
}
if (reader.GetByte() != 0x23)
{
ApplicationContext.Context.Value?.Logger.LogWarning($"Invalid packet code from {peer} / {connection.Guid}");
return;
}
}
if (packetCode == 0)
{
var approvalPacket = new ApprovalPacket
{
EncryptedData = reader.GetRemainingBytes(),
};
if (connection.TryProcessApproval(peer, approvalPacket))
{
OnConnectionApproved?.Invoke(this, new ConnectionEventArgs
{
Connection = connection,
NetworkStatus = NetworkStatus.Online,
});
}
else
{
ApplicationContext.Context.Value?.Logger.LogWarning($"Failed to process approval from {peer} / {connection.Guid}");
}
return;
}
if (!connection.TryProcessInboundMessage(peer, reader, channelNumber, deliveryMethod, out var buffer))
{
ApplicationContext.Context.Value?.Logger.LogWarning($"FPIM: {peer} / {connection.Guid}");
return;
}
_pendingInboundBuffers.Enqueue(new InboundBuffer(connection, buffer));
OnPacketAvailable?.Invoke(this);
}
public bool SendUnconnectedPacket(IPEndPoint target, ReadOnlySpan<byte> data) =>
_manager.SendUnconnectedMessage(data.ToArray(), target);
public bool SendUnconnectedPacket(IPEndPoint target, UnconnectedPacket packet)
{
using var tempAsymmetric = RSA.Create(_asymmetricServer.ExportParameters(false));
switch (packet)
{
case UnconnectedResponsePacket unconnectedResponsePacket:
tempAsymmetric.ImportRSAPublicKey(unconnectedResponsePacket.ResponseKey, out _);
break;
case UnconnectedRequestPacket unconnectedRequestPacket when unconnectedRequestPacket.ResponseKey.Length == 0:
unconnectedRequestPacket.ResponseKey = _asymmetricUnconnected.ExportRSAPublicKey();
break;
}
var encryptedPacket = new AsymmetricEncryptedPacket(
packet,
tempAsymmetric.ExportParameters(false)
);
var encryptedPacketData = encryptedPacket.EncryptedData;
if (encryptedPacketData == default)
{
ApplicationContext.Context.Value?.Logger.LogDebug($"Failed to encrypt data for {packet.GetFullishName()}");
return false;
}
#if DIAGNOSTIC
ApplicationContext.Context.Value?.Logger.LogDebug($"{nameof(SendUnconnectedPacket)} {nameof(encryptedPacketData)}({encryptedPacketData.Length})={Convert.ToHexString(encryptedPacketData)}");
#endif
NetDataWriter data = new(false, encryptedPacketData.Length + 1);
data.Put((byte)UnconnectedPacketType.AsymmetricEncrypted);
data.Put(encryptedPacketData);
return _manager.SendUnconnectedMessage(data, target);
}
private void HandleAsymmetricEncryptedUnconnectedPacket(
IPEndPoint remoteEndPoint,
NetDataReader reader,
UnconnectedMessageType messageType
)
{
try
{
var encryptedPacketData = reader.GetRemainingBytes();
var unconnectedAsymmetric = _network.Configuration.IsServer ? _asymmetricServer : _asymmetricUnconnected;
var encryptedPacket = new AsymmetricEncryptedPacket
{
EncryptedData = encryptedPacketData,
Parameters = unconnectedAsymmetric.ExportParameters(true),
};
var innerPacket = encryptedPacket.InnerPacket;
if (innerPacket == default)
{
ApplicationContext.Context.Value?.Logger.LogDebug("Inner packet is null.");
return;
}
if (innerPacket is not UnconnectedPacket unconnectedPacket)
{
ApplicationContext.Context.Value?.Logger.LogDebug($"Inner packet is not of type {typeof(UnconnectedPacket)}: {innerPacket.GetFullishName()}");
return;
}
if (!_network.Helper.HandlerRegistry.TryGetHandler(innerPacket, out var handlerInstance))
{
ApplicationContext.Context.Value?.Logger.LogWarning($"No handler for {innerPacket.GetFullishName()}");
return;
}
var packetSender = new LiteNetLibUnconnectedPacketSender(this, remoteEndPoint, _network);
if (!handlerInstance.Invoke(packetSender, unconnectedPacket))
{
ApplicationContext.Context.Value?.Logger.LogWarning($"Packet handler failed for {innerPacket.GetFullishName()}");
}
}
catch (Exception exception)
{
ApplicationContext.Context.Value?.Logger.LogDebug(exception, $"Failure in {nameof(HandleAsymmetricEncryptedUnconnectedPacket)}");
}
}
public void OnNetworkReceiveUnconnected(IPEndPoint remoteEndPoint, NetPacketReader reader, UnconnectedMessageType messageType)
{
try
{
if (!reader.TryGetByte(out var unconnectedPacketTypeAsByte))
{
return;
}
var unconnectedPacketType = (UnconnectedPacketType)unconnectedPacketTypeAsByte;
if (!Enum.IsDefined(typeof(UnconnectedPacketType), unconnectedPacketTypeAsByte))
{
unconnectedPacketType = UnconnectedPacketType.AsymmetricEncrypted;
}
switch (unconnectedPacketType)
{
case UnconnectedPacketType.Plaintext:
OnUnconnectedMessage?.Invoke(
new UnconnectedMessageSender(this, remoteEndPoint, default),
new LiteNetLibInboundBuffer(reader)
);
break;
case UnconnectedPacketType.AsymmetricEncrypted:
HandleAsymmetricEncryptedUnconnectedPacket(remoteEndPoint, reader, messageType);
break;
default: throw new UnreachableException();
}
}
catch (Exception exception)
{
ApplicationContext.Context.Value?.Logger.LogDebug(exception, $"Failure in {nameof(OnNetworkReceiveUnconnected)}");
}
}
public void OnNetworkLatencyUpdate(NetPeer peer, int latency)
{
#if DIAGNOSTIC
var logLatencyUpdate = true;
#else
if (latency < 0)
{
return;
}
var logLatencyUpdate = latency > 0;
#endif
if (logLatencyUpdate)
{
ApplicationContext.CurrentContext.Logger.LogTrace("LATENCY {Peer} {Latency}ms", peer, latency);
}
if (!_connectionIdLookup.TryGetValue(peer.Id, out var connectionId))
{
ApplicationContext.CurrentContext.Logger.LogWarning("Missing connection for {PeerId}", peer.Id);
return;
}
var genericConnection = _network.FindConnection(connectionId);
if (genericConnection is not LiteNetLibConnection connection)
{
var expectedTypeName = typeof(LiteNetLibConnection).GetName(qualified: true);
var actualTypeName = genericConnection.GetType().GetName(qualified: true);
ApplicationContext.CurrentContext.Logger.LogWarning(
"Connection is not {ExpectedType} for {PeerId}, was {ActualType}",
expectedTypeName,
peer.Id,
actualTypeName
);
return;
}
connection.Statistics.Ping = latency;
}
public void OnConnectionRequest(ConnectionRequest request)
{
NetDataWriter response = new();
var inboundBuffer = new LiteNetLibInboundBuffer(request.Data);
var deserializedBuffer = MessagePacker.Instance.Deserialize(inboundBuffer);
if (deserializedBuffer is not HailPacket hail || !hail.Decrypt(_asymmetricServer))
{
ApplicationContext.Context.Value?.Logger.LogDebug($"Rejecting (bad hail) {request.RemoteEndPoint}");
response.Put((ushort)400);
request.Reject(response);
return;
}
Debug.Assert(SharedConstants.VersionData != null, "SharedConstants.VERSION_DATA != null");
Debug.Assert(hail.VersionData != null, "hail.VersionData != null");
if (!SharedConstants.VersionData.SequenceEqual(hail.VersionData))
{
ApplicationContext.Context.Value?.Logger.LogDebug($"Rejecting (bad version) {request.RemoteEndPoint}");
response.Put((ushort)400);
response.Put(NetworkStatus.VersionMismatch.ToString());
request.Reject(response);
return;
}
ApplicationContext.Context.Value?.Logger.LogDebug($"hail Time={hail.Adjusted / TimeSpan.TicksPerMillisecond} Offset={hail.Offset / TimeSpan.TicksPerMillisecond} Real={hail.UTC / TimeSpan.TicksPerMillisecond}");
ApplicationContext.Context.Value?.Logger.LogDebug($"local Time={Timing.Global.Milliseconds} Offset={(long)Timing.Global.MillisecondsOffset} Real={Timing.Global.MillisecondsUtc}");
ApplicationContext.Context.Value?.Logger.LogDebug($"real delta={(Timing.Global.TicksUtc - hail.UTC) / TimeSpan.TicksPerMillisecond}");
if (_network.ConnectionCount >= Options.Instance.MaxClientConnections)
{
ApplicationContext.Context.Value?.Logger.LogDebug($"Rejecting (full) {request.RemoteEndPoint}");
response.Put((ushort)503);
response.Put(NetworkStatus.ServerFull.ToString());
request.Reject(response);
return;
}
var peer = request.Accept();
ApplicationContext.Context.Value?.Logger.LogDebug($"NCPing={peer.Ping}");
var symmetricKey = RandomNumberGenerator.GetBytes(32);
var connection = new LiteNetLibConnection(peer, hail.RsaParameters, hail.SymmetricVersion, symmetricKey);
if (!(OnConnectionRequested?.Invoke(this, connection) ?? true))
{
ApplicationContext.Context.Value?.Logger.LogDebug($"Rejecting (ban) {peer} ({connection.Guid})");
response.Put((ushort)403);
response.Put(NetworkStatus.Failed.ToString());
peer.Disconnect(response);
return;
}
if (!_network.AddConnection(connection))
{
ApplicationContext.Context.Value?.Logger.LogDebug($"Rejecting (error connection map) {peer} ({connection.Guid})");
response.Put((ushort)500);
response.Put(NetworkStatus.Failed.ToString());
peer.Disconnect(response);
return;
}
if (!_connectionIdLookup.TryAdd(peer.Id, connection.Guid))
{
ApplicationContext.Context.Value?.Logger.LogDebug($"Rejecting (error connection id lookup) {peer} ({connection.Guid})");
response.Put((ushort)500);
response.Put(NetworkStatus.Failed.ToString());
peer.Disconnect(response);
return;
}
ApprovalPacket approval = new(hail.RsaParameters, hail.HandshakeSecret, symmetricKey, connection.Guid);
approval.Encrypt();
var approvalData = approval.EncryptedData;
var connectionData = new NetDataWriter(false, approval.EncryptedData.Length + sizeof(byte));
connectionData.Put((byte)0);
connectionData.Put(approvalData);
#if DIAGNOSTIC
ApplicationContext.Context.Value?.Logger.LogDebug($"OnConnectionRequest approvalData({approvalData.Length})={Convert.ToHexString(approvalData)}");
ApplicationContext.Context.Value?.Logger.LogDebug($"OnConnectionRequest connectionData({connectionData.Length})={Convert.ToHexString(connectionData.Data)}");
#endif
peer.Send(connectionData, DeliveryMethod.ReliableOrdered);
OnConnectionApproved?.Invoke(this, new ConnectionEventArgs
{
Connection = connection,
NetworkStatus = NetworkStatus.Online,
});
OnConnected?.Invoke(
this,
new ConnectionEventArgs
{
Connection = connection,
NetworkStatus = NetworkStatus.Online,
}
);
ApplicationContext.Context.Value?.Logger.LogDebug($"Approved {peer} ({connection.Guid})");
}
}