Skip to content

Commit

Permalink
Merge pull request #906 from uifdev/connection-logic-fixes
Browse files Browse the repository at this point in the history
Fixed: several connection logic issues
  • Loading branch information
AmyrAhmady authored Apr 4, 2024
2 parents 2b6eb99 + 7d65932 commit 6941fee
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 74 deletions.
137 changes: 64 additions & 73 deletions Server/Components/LegacyNetwork/legacy_network_impl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -360,11 +360,6 @@ IPlayer* RakNetLegacyNetwork::OnPeerConnect(RakNet::RPCParameters* rpcParams, bo
RakNet::BitStream bss;
bss.Write(uint8_t(newConnectionResult.first));
rakNetServer.RPC(130, &bss, RakNet::HIGH_PRIORITY, RakNet::UNRELIABLE, 0, rid, false, false, RakNet::UNASSIGNED_NETWORK_ID, nullptr);

if (newConnectionResult.first != NewConnectionResult_VersionMismatch)
{
rakNetServer.Kick(rid);
}
}
return nullptr;
}
Expand All @@ -384,85 +379,84 @@ void RakNetLegacyNetwork::OnPlayerConnect(RakNet::RPCParameters* rpcParams, void
return;
}

if (remoteSystem->sampData.authType == SAMPRakNet::AuthType_Player)
if (remoteSystem->sampData.authType != SAMPRakNet::AuthType_Player)
{
NetworkBitStream bs = GetBitStream(*rpcParams);
NetCode::RPC::PlayerConnect playerConnectRPC;
if (playerConnectRPC.read(bs))
{
network->rakNetServer.Kick(rpcParams->sender);
return;
}

bool serialIsInvalid = true;
String serial;
ttmath::UInt<100> serialNumber;
ttmath::uint remainder = 0;
NetworkBitStream bs = GetBitStream(*rpcParams);
NetCode::RPC::PlayerConnect playerConnectRPC;
if (!playerConnectRPC.read(bs))
{
network->rakNetServer.Kick(rpcParams->sender);
return;
}

serialNumber.FromString(String(StringView(playerConnectRPC.Key)), 16);
serialNumber.DivInt(1001, remainder);
bool serialIsInvalid = true;
String serial;
ttmath::UInt<100> serialNumber;
ttmath::uint remainder = 0;

if (remainder == 0)
{
serial = serialNumber.ToString(16);
if (serial.size() != 0 && serial.size() < 50)
{
serialIsInvalid = false;
}
}
serialNumber.FromString(String(StringView(playerConnectRPC.Key)), 16);
serialNumber.DivInt(1001, remainder);

const bool versionIsInvalid = (playerConnectRPC.VersionString.length() > 24);
if (remainder == 0)
{
serial = serialNumber.ToString(16);
if (serial.size() != 0 && serial.size() < 50)
{
serialIsInvalid = false;
}
}

if (serialIsInvalid || versionIsInvalid)
{
PeerAddress address;
address.v4 = rpcParams->sender.binaryAddress;
address.ipv6 = false;
const bool versionIsInvalid = (playerConnectRPC.VersionString.length() > 24);

PeerAddress::AddressString addressString;
PeerAddress::ToString(address, addressString);
if (serialIsInvalid || versionIsInvalid)
{
PeerAddress address;
address.v4 = rpcParams->sender.binaryAddress;
address.ipv6 = false;

network->core->logLn(LogLevel::Warning, "Invalid client connecting from %.*s", int(addressString.length()), addressString.data());
network->rakNetServer.Kick(rpcParams->sender);
return;
}
PeerAddress::AddressString addressString;
PeerAddress::ToString(address, addressString);

remoteSystem->isLogon = true;
IPlayer* newPeer = network->OnPeerConnect(rpcParams, false, serial, playerConnectRPC.VersionNumber, playerConnectRPC.VersionString, playerConnectRPC.ChallengeResponse, playerConnectRPC.Name, playerConnectRPC.IsUsingOfficialClient);
if (newPeer)
{
if (!network->inEventDispatcher.stopAtFalse(
[newPeer, &bs](NetworkInEventHandler* handler)
{
bs.resetReadPointer();
return handler->onReceiveRPC(*newPeer, NetCode::RPC::PlayerConnect::PacketID, bs);
}))
{
return;
}
network->core->logLn(LogLevel::Warning, "Invalid client connecting from %.*s", int(addressString.length()), addressString.data());
network->rakNetServer.Kick(rpcParams->sender);
return;
}

if (!network->rpcInEventDispatcher.stopAtFalse(
NetCode::RPC::PlayerConnect::PacketID,
[newPeer, &bs](SingleNetworkInEventHandler* handler)
{
bs.resetReadPointer();
return handler->onReceive(*newPeer, bs);
}))
{
return;
}
IPlayer* newPeer = network->OnPeerConnect(rpcParams, false, serial, playerConnectRPC.VersionNumber, playerConnectRPC.VersionString, playerConnectRPC.ChallengeResponse, playerConnectRPC.Name, playerConnectRPC.IsUsingOfficialClient);
if (!newPeer)
{
network->rakNetServer.Kick(rpcParams->sender);
return;
}

network->networkEventDispatcher.dispatch(&NetworkEventHandler::onPeerConnect, *newPeer);
if (!network->inEventDispatcher.stopAtFalse(
[newPeer, &bs](NetworkInEventHandler* handler)
{
bs.resetReadPointer();
return handler->onReceiveRPC(*newPeer, NetCode::RPC::PlayerConnect::PacketID, bs);
}))
{
return;
}

network->playerRemoteSystem[newPeer->getID()] = remoteSystem;
return;
}
}
else
{
network->rakNetServer.Kick(rpcParams->sender);
}
if (!network->rpcInEventDispatcher.stopAtFalse(
NetCode::RPC::PlayerConnect::PacketID,
[newPeer, &bs](SingleNetworkInEventHandler* handler)
{
bs.resetReadPointer();
return handler->onReceive(*newPeer, bs);
}))
{
return;
}

network->rakNetServer.Kick(rpcParams->sender);
network->networkEventDispatcher.dispatch(&NetworkEventHandler::onPeerConnect, *newPeer);
network->playerRemoteSystem[newPeer->getID()] = remoteSystem;
remoteSystem->isLogon = true;
}

void RakNetLegacyNetwork::OnNPCConnect(RakNet::RPCParameters* rpcParams, void* extra)
Expand All @@ -481,7 +475,6 @@ void RakNetLegacyNetwork::OnNPCConnect(RakNet::RPCParameters* rpcParams, void* e
NetCode::RPC::NPCConnect NPCConnectRPC;
if (NPCConnectRPC.read(bs))
{
remoteSystem->isLogon = true;
IPlayer* newPeer = network->OnPeerConnect(rpcParams, true, "", NPCConnectRPC.VersionNumber, "npc", NPCConnectRPC.ChallengeResponse, NPCConnectRPC.Name);
if (newPeer)
{
Expand All @@ -507,12 +500,10 @@ void RakNetLegacyNetwork::OnNPCConnect(RakNet::RPCParameters* rpcParams, void* e
}

network->networkEventDispatcher.dispatch(&NetworkEventHandler::onPeerConnect, *newPeer);
return;
remoteSystem->isLogon = true;
}
}
}

network->rakNetServer.Kick(rpcParams->sender);
}

void RakNetLegacyNetwork::OnRakNetDisconnect(RakNet::PlayerIndex rid, PeerDisconnectReason reason)
Expand Down
2 changes: 1 addition & 1 deletion lib/RakNet
Submodule RakNet updated 1 files
+11 −0 Source/RakPeer.cpp

0 comments on commit 6941fee

Please sign in to comment.