From 69726a9b03f8b344c61cd7f9a20cf5a35864ddc0 Mon Sep 17 00:00:00 2001 From: 20dka Date: Fri, 4 Nov 2022 23:40:52 +0100 Subject: [PATCH] add owner check to other vehicle packets --- include/TServer.h | 1 + src/TServer.cpp | 41 +++++++++++++++++++++++++++++++++++++++-- 2 files changed, 40 insertions(+), 2 deletions(-) diff --git a/include/TServer.h b/include/TServer.h index 547073a..60a85ca 100644 --- a/include/TServer.h +++ b/include/TServer.h @@ -44,6 +44,7 @@ private: static bool IsUnicycle(TClient& c, const std::string& CarJson); static void Apply(TClient& c, int VID, const std::string& pckt); static bool HandlePosition(TClient& c, const std::string& Packet); + static bool HandleVehicleUpdate(TClient& c, const std::string& Packet); }; struct BufferView { diff --git a/src/TServer.cpp b/src/TServer.cpp index 660fb2d..401ee11 100644 --- a/src/TServer.cpp +++ b/src/TServer.cpp @@ -152,8 +152,11 @@ void TServer::GlobalParser(const std::weak_ptr& Client, std::vector= 86) { - PPSMonitor.IncrementInternalPPS(); - Network.SendToAll(LockedClient.get(), Packet, false, false); + if (HandleVehicleUpdate(*LockedClient, StringPacket)){ + Network.SendToAll(LockedClient.get(), Packet, false, false); + } else { + beammp_debugf("Invalid vehicle update packet received from '{}' ({}), ignoring it", LockedClient->GetName(), LockedClient->GetID()); + } return; } switch (Code) { @@ -469,3 +472,37 @@ bool TServer::HandlePosition(TClient& c, const std::string& Packet) { } return false; } + +bool TServer::HandleVehicleUpdate(TClient& c, const std::string& Packet) { + if (Packet.size() < 3) { + // invalid packet + return false; + } + // (Vi/We/Yl):serverVehicleID:data + // (Vi/We/Yl):serverVehicleID:data + std::string withoutCode = Packet.substr(3); + auto NameDataSep = withoutCode.find(':', 2); + if (NameDataSep == std::string::npos || NameDataSep < 2) { + // invalid packet + return false; + } + // FIXME: ensure that -2 does what it should... it seems weird. + std::string ServerVehicleID = withoutCode.substr(2, NameDataSep - 2); + if (NameDataSep + 1 > withoutCode.size()) { + // invalid packet + return false; + } + std::string Data = withoutCode.substr(NameDataSep + 1); + + // parse veh ID + auto MaybePidVid = GetPidVid(ServerVehicleID); + if (MaybePidVid) { + int PID = -1; + int VID = -1; + std::tie(PID, VID) = MaybePidVid.value(); + if (PID == c.GetID()) { + return true; + } + } + return false; +}