From 4dd88de3a0dbf624a08ac1accc69986788402f10 Mon Sep 17 00:00:00 2001 From: Edo Date: Tue, 25 Apr 2023 13:08:29 +0200 Subject: [PATCH] [Voice]: Check who sent packet (#973) --- src/Components/Modules/Network.cpp | 24 +++++++++++++++++++++--- src/Components/Modules/Network.hpp | 5 ++++- src/Components/Modules/Voice.cpp | 17 +++++++++++++---- src/Components/Modules/Voice.hpp | 2 +- 4 files changed, 39 insertions(+), 9 deletions(-) diff --git a/src/Components/Modules/Network.cpp b/src/Components/Modules/Network.cpp index 938173f9..800b4ce0 100644 --- a/src/Components/Modules/Network.cpp +++ b/src/Components/Modules/Network.cpp @@ -5,6 +5,7 @@ namespace Components Utils::Signal Network::StartupSignal; // Packet interception std::unordered_map Network::CL_Callbacks; + std::unordered_map Network::CL_RawCallbacks; Network::Address::Address() { @@ -274,17 +275,34 @@ namespace Components CL_Callbacks[Utils::String::ToLower(command)] = callback; } - bool Network::CL_HandleCommand(Game::netadr_t* address, const char* command, const Game::msg_t* message) + void Network::OnClientPacketRaw(const std::string& command, const networkRawCallback& callback) + { + CL_RawCallbacks[Utils::String::ToLower(command)] = callback; + } + + bool Network::CL_HandleCommand(Game::netadr_t* address, const char* command, Game::msg_t* message) { const auto command_ = Utils::String::ToLower(command); - const auto handler = CL_Callbacks.find(command_); const auto offset = command_.size() + 5; - if (static_cast(message->cursize) < offset || handler == CL_Callbacks.end()) + if (static_cast(message->cursize) < offset) { return false; } + if (const auto rawHandler = CL_RawCallbacks.find(command_); rawHandler != CL_RawCallbacks.end()) + { + rawHandler->second(address, message); + return true; + } + + const auto handler = CL_Callbacks.find(command_); + if (handler == CL_Callbacks.end()) + { + // Normal handler was not found, return + return false; + } + const std::string data(reinterpret_cast(message->data) + offset, message->cursize - offset); auto target = Address{ address }; diff --git a/src/Components/Modules/Network.hpp b/src/Components/Modules/Network.hpp index fcd8262a..547628ed 100644 --- a/src/Components/Modules/Network.hpp +++ b/src/Components/Modules/Network.hpp @@ -49,6 +49,7 @@ namespace Components typedef void(CallbackRaw)(); using networkCallback = std::function; + using networkRawCallback = std::function; Network(); @@ -73,17 +74,19 @@ namespace Components static void BroadcastAll(const std::string& data); static void OnClientPacket(const std::string& command, const networkCallback& callback); + static void OnClientPacketRaw(const std::string& command, const networkRawCallback& callback); private: static Utils::Signal StartupSignal; static std::unordered_map CL_Callbacks; + static std::unordered_map CL_RawCallbacks; static void NetworkStart(); static void NetworkStartStub(); static void PacketErrorCheck(); - static bool CL_HandleCommand(Game::netadr_t* address, const char* command, const Game::msg_t* message); + static bool CL_HandleCommand(Game::netadr_t* address, const char* command, Game::msg_t* message); static void CL_HandleCommandStub(); }; diff --git a/src/Components/Modules/Voice.cpp b/src/Components/Modules/Voice.cpp index c14b0c34..ea28d5d7 100644 --- a/src/Components/Modules/Voice.cpp +++ b/src/Components/Modules/Voice.cpp @@ -241,7 +241,7 @@ namespace Components void Voice::CL_WriteVoicePacket_Hk(const int localClientNum) { const auto connstate = Game::CL_GetLocalClientConnectionState(localClientNum); - const auto clc = Game::CL_GetLocalClientConnection(localClientNum); + const auto* clc = Game::CL_GetLocalClientConnection(localClientNum); const auto* vc = Game::CL_GetLocalClientVoiceCommunication(localClientNum); if (clc->demoplaying || (connstate < Game::CA_LOADING)) { @@ -315,8 +315,14 @@ namespace Components } } - void Voice::CL_VoicePacket_Hk(const int localClientNum, Game::msg_t* msg) + void Voice::CL_VoicePacket(Game::netadr_t* address, Game::msg_t* msg) { + auto* clc = Game::CL_GetLocalClientConnection(0); + if (!Game::NET_CompareBaseAdr(clc->serverAddress, *address)) + { + return; + } + const auto numPackets = Game::MSG_ReadByte(msg); if (numPackets < 0 || numPackets > MAX_SERVER_QUEUED_VOICE_PACKETS) { @@ -342,7 +348,7 @@ namespace Components return; } - if (!CL_IsPlayerMuted_Hk(nullptr, localClientNum, voicePacket.talker)) + if (!CL_IsPlayerMuted_Hk(nullptr, 0, voicePacket.talker)) { if ((*Game::cl_voice)->current.enabled) { @@ -395,7 +401,10 @@ namespace Components // Write voice packets to the server instead of other clients Utils::Hook(0x487935, CL_WriteVoicePacket_Hk, HOOK_CALL).install()->quick(); Utils::Hook(0x5AD945, CL_WriteVoicePacket_Hk, HOOK_CALL).install()->quick(); - Utils::Hook(0x5A9E06, CL_VoicePacket_Hk, HOOK_CALL).install()->quick(); + + // Disable 'v' OOB handler and use our own + Utils::Hook::Set(0x5A9E02, 0xEB); + Network::OnClientPacketRaw("v", CL_VoicePacket); Utils::Hook(0x4AE740, CL_IsPlayerTalking_Hk, HOOK_JUMP).install()->quick(); Utils::Hook(0x4B6250, CL_IsPlayerMuted_Hk, HOOK_JUMP).install()->quick(); diff --git a/src/Components/Modules/Voice.hpp b/src/Components/Modules/Voice.hpp index 908bbe54..bc5a2bc5 100644 --- a/src/Components/Modules/Voice.hpp +++ b/src/Components/Modules/Voice.hpp @@ -46,7 +46,7 @@ namespace Components static void CL_TogglePlayerMute(int localClientNum, int muteClientIndex); static void CL_WriteVoicePacket_Hk(int localClientNum); - static void CL_VoicePacket_Hk(int localClientNum, Game::msg_t* msg); + static void CL_VoicePacket(Game::netadr_t* address, Game::msg_t* msg); static void UI_Mute_player(int clientNum, int localClientNum); static void UI_Mute_Player_Stub();