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

Fixed: several connection logic issues #906

Merged
merged 4 commits into from
Apr 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);
Hual marked this conversation as resolved.
Show resolved Hide resolved
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
Loading