From 52d1840003c82b83ae2654c4cf97f44a684cc72f Mon Sep 17 00:00:00 2001 From: ohayo Date: Tue, 4 Feb 2025 11:07:10 +1000 Subject: [PATCH] Packet filtering for those who never finish the handshake This also kicks those who never finish handshaking upon chat now. (To free up the resources a bit I guess?) Before a player finishes the connection handshake, the server will check if it's necessary to send them a packet - it checks against a list of necessary packets like: - ContinueConnecting (Send User Slot), - PlayerSpawnSelf (CompleteConnectionAndSpawn), - WorldInfo (Which the server does a further check if the player is at the appropriate state to be sent the world info) - Status - Disconnection - TileFrameSection & TileSendSection --- TShockAPI/TSPlayer.cs | 28 ++++++++++++++++++++++++++++ TShockAPI/TShock.cs | 1 + 2 files changed, 29 insertions(+) diff --git a/TShockAPI/TSPlayer.cs b/TShockAPI/TSPlayer.cs index cb66649a..cb418b0e 100644 --- a/TShockAPI/TSPlayer.cs +++ b/TShockAPI/TSPlayer.cs @@ -2102,6 +2102,28 @@ namespace TShockAPI SendData(PacketTypes.PlayerAddBuff, number: Index, number2: type, number3: time); } + + /// + /// Determines if an outgoing packet is necessary to send to a player before they have finished the connection handshake. + /// + /// The packet type to check against the necessary list. + /// + private bool NecessaryPacket(PacketTypes msgType) + { + List ConnectionPackets = new List() + { + PacketTypes.ContinueConnecting, + PacketTypes.WorldInfo, + PacketTypes.Status, + PacketTypes.Disconnect, + PacketTypes.TileFrameSection, + PacketTypes.TileSendSection, + PacketTypes.PlayerSpawnSelf + }; + + return ConnectionPackets.Contains(msgType); + } + //Todo: Separate this into a few functions. SendTo, SendToAll, etc /// /// Sends data to the player. @@ -2119,6 +2141,12 @@ namespace TShockAPI if (RealPlayer && !ConnectionAlive) return; + if (!NecessaryPacket(msgType) && !FinishedHandshake) + return; + + if (msgType == PacketTypes.WorldInfo && State < 3) //If the player has requested the world data, their state will be 3. + return; + NetMessage.SendData((int)msgType, Index, -1, text == null ? null : NetworkText.FromLiteral(text), number, number2, number3, number4, number5); } diff --git a/TShockAPI/TShock.cs b/TShockAPI/TShock.cs index 061d05fa..64f23ec3 100644 --- a/TShockAPI/TShock.cs +++ b/TShockAPI/TShock.cs @@ -1494,6 +1494,7 @@ namespace TShockAPI if (!tsplr.FinishedHandshake) { + tsplr.Kick(GetString("Your client didn't finish the handshake."), true); args.Handled = true; return; }