From 394c39c4e79216d72bb523d2c7599cacc7180de6 Mon Sep 17 00:00:00 2001 From: Adrien Bourdeaux Date: Fri, 8 Nov 2024 16:56:11 -0500 Subject: [PATCH 1/9] Add packet layer --- .../LiteNetLib/LiteNetLibServer.cs | 4 ++-- .../LiteNetLib/PortCheckerSupport.cs | 24 +++++++++++++++++++ 2 files changed, 26 insertions(+), 2 deletions(-) create mode 100644 NitroxServer/Communication/LiteNetLib/PortCheckerSupport.cs diff --git a/NitroxServer/Communication/LiteNetLib/LiteNetLibServer.cs b/NitroxServer/Communication/LiteNetLib/LiteNetLibServer.cs index c8d45f0190..3526218afd 100644 --- a/NitroxServer/Communication/LiteNetLib/LiteNetLibServer.cs +++ b/NitroxServer/Communication/LiteNetLib/LiteNetLibServer.cs @@ -1,4 +1,4 @@ -using System.Buffers; +using System.Buffers; using System.Threading; using System.Threading.Tasks; using LiteNetLib; @@ -21,7 +21,7 @@ public class LiteNetLibServer : NitroxServer public LiteNetLibServer(PacketHandler packetHandler, PlayerManager playerManager, EntitySimulation entitySimulation, ServerConfig serverConfig) : base(packetHandler, playerManager, entitySimulation, serverConfig) { listener = new EventBasedNetListener(); - server = new NetManager(listener); + server = new NetManager(listener, new PortCheckerSupport()); } public override bool Start() diff --git a/NitroxServer/Communication/LiteNetLib/PortCheckerSupport.cs b/NitroxServer/Communication/LiteNetLib/PortCheckerSupport.cs new file mode 100644 index 0000000000..5b0add86a3 --- /dev/null +++ b/NitroxServer/Communication/LiteNetLib/PortCheckerSupport.cs @@ -0,0 +1,24 @@ +using System; +using System.Net; +using LiteNetLib.Layers; +using LiteNetLib; +using System.Collections.Generic; + +namespace NitroxServer.Communication.LiteNetLib; + +public class PortCheckerSupport : PacketLayerBase +{ + public PortCheckerSupport() : base(0) + { + + } + + public override void ProcessInboundPacket(ref IPEndPoint endPoint, ref byte[] data, ref int length) + { + Log.Info("$\"{endPoint} - {length} bytes: {BitConverter.ToString(data).Replace('-', '\\0')}\""); + } + public override void ProcessOutBoundPacket(ref IPEndPoint endPoint, ref byte[] data, ref int offset, ref int length) + { + + } +} From 7ac52530f66cd534528241653e3def59a33c6de6 Mon Sep 17 00:00:00 2001 From: Adrien Bourdeaux Date: Thu, 19 Dec 2024 11:27:01 -0500 Subject: [PATCH 2/9] Port checker toggling command not working, but have created the structure. The python testing scripts I am using are on another branch because I don't want to merge them in through the PR --- .../LiteNetLib/LiteNetLibServer.cs | 4 ++- .../LiteNetLib/PortCheckerSupport.cs | 28 ++++++++++++++++--- .../PortCheckerToggleCommand.cs | 27 ++++++++++++++++++ 3 files changed, 54 insertions(+), 5 deletions(-) create mode 100644 NitroxServer/ConsoleCommands/PortCheckerToggleCommand.cs diff --git a/NitroxServer/Communication/LiteNetLib/LiteNetLibServer.cs b/NitroxServer/Communication/LiteNetLib/LiteNetLibServer.cs index 3526218afd..dccdb6d2e2 100644 --- a/NitroxServer/Communication/LiteNetLib/LiteNetLibServer.cs +++ b/NitroxServer/Communication/LiteNetLib/LiteNetLibServer.cs @@ -21,7 +21,9 @@ public class LiteNetLibServer : NitroxServer public LiteNetLibServer(PacketHandler packetHandler, PlayerManager playerManager, EntitySimulation entitySimulation, ServerConfig serverConfig) : base(packetHandler, playerManager, entitySimulation, serverConfig) { listener = new EventBasedNetListener(); - server = new NetManager(listener, new PortCheckerSupport()); + PortCheckerSupport portChecker = new PortCheckerSupport(); + server = new NetManager(listener, portChecker); + portChecker.netManager = server; } public override bool Start() diff --git a/NitroxServer/Communication/LiteNetLib/PortCheckerSupport.cs b/NitroxServer/Communication/LiteNetLib/PortCheckerSupport.cs index 5b0add86a3..41fa7db364 100644 --- a/NitroxServer/Communication/LiteNetLib/PortCheckerSupport.cs +++ b/NitroxServer/Communication/LiteNetLib/PortCheckerSupport.cs @@ -3,22 +3,42 @@ using LiteNetLib.Layers; using LiteNetLib; using System.Collections.Generic; - +using LiteNetLib.Utils; +using System.Net.Sockets; namespace NitroxServer.Communication.LiteNetLib; public class PortCheckerSupport : PacketLayerBase { + public bool active; + public NetManager netManager; public PortCheckerSupport() : base(0) { - + active = true; } public override void ProcessInboundPacket(ref IPEndPoint endPoint, ref byte[] data, ref int length) { - Log.Info("$\"{endPoint} - {length} bytes: {BitConverter.ToString(data).Replace('-', '\\0')}\""); + if (active) + { + Log.Info("Incoming packet"); + UdpClient client = new(); + client.Send(data, length, endPoint); + client.Dispose(); + } + } public override void ProcessOutBoundPacket(ref IPEndPoint endPoint, ref byte[] data, ref int offset, ref int length) { - + + } + public void Deactivate() + { + active = false; + Log.Info("Deactivated port forward checker"); + } + public void Activate() + { + active = true; + Log.Info("Activated port forward checker"); } } diff --git a/NitroxServer/ConsoleCommands/PortCheckerToggleCommand.cs b/NitroxServer/ConsoleCommands/PortCheckerToggleCommand.cs new file mode 100644 index 0000000000..2415c72937 --- /dev/null +++ b/NitroxServer/ConsoleCommands/PortCheckerToggleCommand.cs @@ -0,0 +1,27 @@ +using LiteNetLib; +using NitroxModel.DataStructures.GameLogic; +using NitroxServer.Communication.LiteNetLib; +using NitroxServer.ConsoleCommands.Abstract; +using NitroxServer.ConsoleCommands.Abstract.Type; + +namespace NitroxServer.ConsoleCommands; +public class PortCheckerToggleCommand : Command +{ + PortCheckerSupport packetLayer; + public PortCheckerToggleCommand() : base("toggleportchecker", Perms.ADMIN, PermsFlag.NO_CONSOLE, "Enable the port forwarding tester") + { + + } + + protected override void Execute(CallArgs args) + { + Log.Info("Command executed"); + if (packetLayer.active) + { + packetLayer.Deactivate(); + return; + } + packetLayer.Activate(); + return; + } +} From d94f93fb3355b6397d06c5b6fb8011681a8fb340 Mon Sep 17 00:00:00 2001 From: Adrien Bourdeaux Date: Thu, 19 Dec 2024 20:09:56 -0500 Subject: [PATCH 3/9] Use netmanager to send return messages --- NitroxServer/Communication/LiteNetLib/PortCheckerSupport.cs | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/NitroxServer/Communication/LiteNetLib/PortCheckerSupport.cs b/NitroxServer/Communication/LiteNetLib/PortCheckerSupport.cs index 41fa7db364..49f1895bf0 100644 --- a/NitroxServer/Communication/LiteNetLib/PortCheckerSupport.cs +++ b/NitroxServer/Communication/LiteNetLib/PortCheckerSupport.cs @@ -21,9 +21,7 @@ public override void ProcessInboundPacket(ref IPEndPoint endPoint, ref byte[] da if (active) { Log.Info("Incoming packet"); - UdpClient client = new(); - client.Send(data, length, endPoint); - client.Dispose(); + netManager.SendUnconnectedMessage(data, endPoint); } } From 84ffd6bd5d1d638939878ec988b26048eed483f5 Mon Sep 17 00:00:00 2001 From: Adrien Bourdeaux Date: Thu, 19 Dec 2024 21:22:35 -0500 Subject: [PATCH 4/9] Created command to toggle port forwarding checker on or off, starts off by default --- .../Communication/LiteNetLib/PortCheckerSupport.cs | 14 ++------------ .../ConsoleCommands/PortCheckerToggleCommand.cs | 11 +++++------ 2 files changed, 7 insertions(+), 18 deletions(-) diff --git a/NitroxServer/Communication/LiteNetLib/PortCheckerSupport.cs b/NitroxServer/Communication/LiteNetLib/PortCheckerSupport.cs index 49f1895bf0..6a1fb457f4 100644 --- a/NitroxServer/Communication/LiteNetLib/PortCheckerSupport.cs +++ b/NitroxServer/Communication/LiteNetLib/PortCheckerSupport.cs @@ -9,11 +9,11 @@ namespace NitroxServer.Communication.LiteNetLib; public class PortCheckerSupport : PacketLayerBase { - public bool active; + public static bool active; public NetManager netManager; public PortCheckerSupport() : base(0) { - active = true; + active = false; } public override void ProcessInboundPacket(ref IPEndPoint endPoint, ref byte[] data, ref int length) @@ -29,14 +29,4 @@ public override void ProcessOutBoundPacket(ref IPEndPoint endPoint, ref byte[] d { } - public void Deactivate() - { - active = false; - Log.Info("Deactivated port forward checker"); - } - public void Activate() - { - active = true; - Log.Info("Activated port forward checker"); - } } diff --git a/NitroxServer/ConsoleCommands/PortCheckerToggleCommand.cs b/NitroxServer/ConsoleCommands/PortCheckerToggleCommand.cs index 2415c72937..d4eb982a79 100644 --- a/NitroxServer/ConsoleCommands/PortCheckerToggleCommand.cs +++ b/NitroxServer/ConsoleCommands/PortCheckerToggleCommand.cs @@ -7,21 +7,20 @@ namespace NitroxServer.ConsoleCommands; public class PortCheckerToggleCommand : Command { - PortCheckerSupport packetLayer; - public PortCheckerToggleCommand() : base("toggleportchecker", Perms.ADMIN, PermsFlag.NO_CONSOLE, "Enable the port forwarding tester") + public PortCheckerToggleCommand() : base("toggleportchecker", Perms.CONSOLE, "Enable the port forwarding tester") { } protected override void Execute(CallArgs args) { - Log.Info("Command executed"); - if (packetLayer.active) + Log.Info("Togggled port checker " + (!PortCheckerSupport.active).ToString()); + if (PortCheckerSupport.active) { - packetLayer.Deactivate(); + PortCheckerSupport.active = false; return; } - packetLayer.Activate(); + PortCheckerSupport.active = true; return; } } From 37a812ce4665349278f3881ade93ac835d03a7d1 Mon Sep 17 00:00:00 2001 From: Adrien Bourdeaux Date: Thu, 19 Dec 2024 21:26:46 -0500 Subject: [PATCH 5/9] Edited string --- NitroxServer/ConsoleCommands/PortCheckerToggleCommand.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/NitroxServer/ConsoleCommands/PortCheckerToggleCommand.cs b/NitroxServer/ConsoleCommands/PortCheckerToggleCommand.cs index d4eb982a79..323dec094d 100644 --- a/NitroxServer/ConsoleCommands/PortCheckerToggleCommand.cs +++ b/NitroxServer/ConsoleCommands/PortCheckerToggleCommand.cs @@ -7,7 +7,7 @@ namespace NitroxServer.ConsoleCommands; public class PortCheckerToggleCommand : Command { - public PortCheckerToggleCommand() : base("toggleportchecker", Perms.CONSOLE, "Enable the port forwarding tester") + public PortCheckerToggleCommand() : base("toggleportchecker", Perms.CONSOLE, "Enable/Disable the port forwarding tester") { } From 3b770d8382780e4f68aaa0a611cec93c3404b9b2 Mon Sep 17 00:00:00 2001 From: Adrien Bourdeaux Date: Fri, 20 Dec 2024 23:30:05 -0500 Subject: [PATCH 6/9] Cleaned up toggle port checker command Co-authored-by: Measurity <1107063+Measurity@users.noreply.github.com> --- .../ConsoleCommands/PortCheckerToggleCommand.cs | 10 ++-------- 1 file changed, 2 insertions(+), 8 deletions(-) diff --git a/NitroxServer/ConsoleCommands/PortCheckerToggleCommand.cs b/NitroxServer/ConsoleCommands/PortCheckerToggleCommand.cs index 323dec094d..840ad719f9 100644 --- a/NitroxServer/ConsoleCommands/PortCheckerToggleCommand.cs +++ b/NitroxServer/ConsoleCommands/PortCheckerToggleCommand.cs @@ -14,13 +14,7 @@ public PortCheckerToggleCommand() : base("toggleportchecker", Perms.CONSOLE, "En protected override void Execute(CallArgs args) { - Log.Info("Togggled port checker " + (!PortCheckerSupport.active).ToString()); - if (PortCheckerSupport.active) - { - PortCheckerSupport.active = false; - return; - } - PortCheckerSupport.active = true; - return; + PortCheckerSupport.active = !PortCheckerSupport.active; + Log.Info("Togggled port checker " + PortCheckerSupport.active); } } From 067fcbc0dd815ac612e4adb7dda94c8ddddd2656 Mon Sep 17 00:00:00 2001 From: Adrien Bourdeaux Date: Fri, 20 Dec 2024 23:33:57 -0500 Subject: [PATCH 7/9] Added warning that port checker must be disabled for joining and changed data to blank array so LiteNetLib ignores it Co-authored-by: Measurity <1107063+Measurity@users.noreply.github.com> --- NitroxServer/Communication/LiteNetLib/PortCheckerSupport.cs | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/NitroxServer/Communication/LiteNetLib/PortCheckerSupport.cs b/NitroxServer/Communication/LiteNetLib/PortCheckerSupport.cs index 6a1fb457f4..286948127b 100644 --- a/NitroxServer/Communication/LiteNetLib/PortCheckerSupport.cs +++ b/NitroxServer/Communication/LiteNetLib/PortCheckerSupport.cs @@ -20,7 +20,9 @@ public override void ProcessInboundPacket(ref IPEndPoint endPoint, ref byte[] da { if (active) { - Log.Info("Incoming packet"); + Log.Warn("WARNING: Port Checker must be disabled to allow players to join the server"); + byte[] datacopy = data; + data = []; netManager.SendUnconnectedMessage(data, endPoint); } From 46e2c8d304c592bd7a3feaf0beb93d89b6d64b40 Mon Sep 17 00:00:00 2001 From: Adrien Bourdeaux Date: Sat, 21 Dec 2024 00:13:10 -0500 Subject: [PATCH 8/9] Use UdpClient instead of NetManager to send response Co-authored-by: Measurity <1107063+Measurity@users.noreply.github.com> --- .../Communication/LiteNetLib/LiteNetLibServer.cs | 4 +--- .../Communication/LiteNetLib/PortCheckerSupport.cs | 10 +++------- 2 files changed, 4 insertions(+), 10 deletions(-) diff --git a/NitroxServer/Communication/LiteNetLib/LiteNetLibServer.cs b/NitroxServer/Communication/LiteNetLib/LiteNetLibServer.cs index dccdb6d2e2..3526218afd 100644 --- a/NitroxServer/Communication/LiteNetLib/LiteNetLibServer.cs +++ b/NitroxServer/Communication/LiteNetLib/LiteNetLibServer.cs @@ -21,9 +21,7 @@ public class LiteNetLibServer : NitroxServer public LiteNetLibServer(PacketHandler packetHandler, PlayerManager playerManager, EntitySimulation entitySimulation, ServerConfig serverConfig) : base(packetHandler, playerManager, entitySimulation, serverConfig) { listener = new EventBasedNetListener(); - PortCheckerSupport portChecker = new PortCheckerSupport(); - server = new NetManager(listener, portChecker); - portChecker.netManager = server; + server = new NetManager(listener, new PortCheckerSupport()); } public override bool Start() diff --git a/NitroxServer/Communication/LiteNetLib/PortCheckerSupport.cs b/NitroxServer/Communication/LiteNetLib/PortCheckerSupport.cs index 286948127b..6768135666 100644 --- a/NitroxServer/Communication/LiteNetLib/PortCheckerSupport.cs +++ b/NitroxServer/Communication/LiteNetLib/PortCheckerSupport.cs @@ -1,16 +1,12 @@ -using System; using System.Net; using LiteNetLib.Layers; -using LiteNetLib; -using System.Collections.Generic; -using LiteNetLib.Utils; using System.Net.Sockets; namespace NitroxServer.Communication.LiteNetLib; public class PortCheckerSupport : PacketLayerBase { public static bool active; - public NetManager netManager; + private readonly UdpClient udpClient = new(); public PortCheckerSupport() : base(0) { active = false; @@ -21,9 +17,9 @@ public override void ProcessInboundPacket(ref IPEndPoint endPoint, ref byte[] da if (active) { Log.Warn("WARNING: Port Checker must be disabled to allow players to join the server"); - byte[] datacopy = data; + byte[] datacopy = (byte[])data.Clone(); data = []; - netManager.SendUnconnectedMessage(data, endPoint); + udpClient.Send(datacopy, datacopy.Length, endPoint); } } From 1279bba0a11d8409d0210829fc088afecde61118 Mon Sep 17 00:00:00 2001 From: Adrien Bourdeaux Date: Sat, 21 Dec 2024 00:22:53 -0500 Subject: [PATCH 9/9] Fixed process to get NetManager to stop processing packet --- NitroxServer/Communication/LiteNetLib/PortCheckerSupport.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/NitroxServer/Communication/LiteNetLib/PortCheckerSupport.cs b/NitroxServer/Communication/LiteNetLib/PortCheckerSupport.cs index 6768135666..3d9a05da85 100644 --- a/NitroxServer/Communication/LiteNetLib/PortCheckerSupport.cs +++ b/NitroxServer/Communication/LiteNetLib/PortCheckerSupport.cs @@ -18,7 +18,7 @@ public override void ProcessInboundPacket(ref IPEndPoint endPoint, ref byte[] da { Log.Warn("WARNING: Port Checker must be disabled to allow players to join the server"); byte[] datacopy = (byte[])data.Clone(); - data = []; + length = 0; // Set length to 0 so NetManager stops processing packet immediately udpClient.Send(datacopy, datacopy.Length, endPoint); }