Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support port checkers #2210

Draft
wants to merge 9 commits into
base: master
Choose a base branch
from
4 changes: 2 additions & 2 deletions NitroxServer/Communication/LiteNetLib/LiteNetLibServer.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using System.Buffers;
using System.Buffers;
using System.Threading;
using System.Threading.Tasks;
using LiteNetLib;
Expand All @@ -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()
Expand Down
30 changes: 30 additions & 0 deletions NitroxServer/Communication/LiteNetLib/PortCheckerSupport.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
using System.Net;
using LiteNetLib.Layers;
using System.Net.Sockets;
namespace NitroxServer.Communication.LiteNetLib;

public class PortCheckerSupport : PacketLayerBase
{
public static bool active;
private readonly UdpClient udpClient = new();
public PortCheckerSupport() : base(0)
{
active = false;
}

public override void ProcessInboundPacket(ref IPEndPoint endPoint, ref byte[] data, ref int length)
{
if (active)
{
Log.Warn("WARNING: Port Checker must be disabled to allow players to join the server");
byte[] datacopy = (byte[])data.Clone();
length = 0; // Set length to 0 so NetManager stops processing packet immediately
udpClient.Send(datacopy, datacopy.Length, endPoint);
}

}
public override void ProcessOutBoundPacket(ref IPEndPoint endPoint, ref byte[] data, ref int offset, ref int length)
{

}
}
20 changes: 20 additions & 0 deletions NitroxServer/ConsoleCommands/PortCheckerToggleCommand.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
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
{
public PortCheckerToggleCommand() : base("toggleportchecker", Perms.CONSOLE, "Enable/Disable the port forwarding tester")
{

}

protected override void Execute(CallArgs args)
{
PortCheckerSupport.active = !PortCheckerSupport.active;
Log.Info("Togggled port checker " + PortCheckerSupport.active);
}
}