From 8712f7db816f2537cc386cc41330885d1c071f8b Mon Sep 17 00:00:00 2001 From: Kawe Mazidjatari <48657826+Mauler125@users.noreply.github.com> Date: Tue, 30 Aug 2022 01:22:53 +0200 Subject: [PATCH] Fix concurrency of g_svNetKey * Fix concurrency of g_svNetKey. * Improve logging. * Overall code cleanup. --- r5dev/engine/net.cpp | 33 +++++++++++---------------------- r5dev/engine/net.h | 3 +++ r5dev/engine/server/server.cpp | 12 +----------- r5dev/gameui/IBrowser.cpp | 3 +++ r5dev/public/inetmsghandler.h | 4 +++- r5dev/public/iserver.h | 3 ++- r5dev/squirrel/sqscript.cpp | 7 +------ r5dev/vstdlib/callback.cpp | 2 +- r5dev/windows/id3dx.cpp | 2 +- 9 files changed, 26 insertions(+), 43 deletions(-) diff --git a/r5dev/engine/net.cpp b/r5dev/engine/net.cpp index 476e307c..a7fda782 100644 --- a/r5dev/engine/net.cpp +++ b/r5dev/engine/net.cpp @@ -65,15 +65,15 @@ int NET_SendDatagram(SOCKET s, void* pPayload, int iLenght, v_netadr_t* pAdr, bo //----------------------------------------------------------------------------- void NET_SetKey(const string& svNetKey) { + std::lock_guard l(g_NetKeyMutex); + g_svNetKey.clear(); g_svNetKey = svNetKey; - DevMsg(eDLL_T::ENGINE, "______________________________________________________________\n"); - DevMsg(eDLL_T::ENGINE, "] NET_KEY ----------------------------------------------------\n"); - DevMsg(eDLL_T::ENGINE, "] BASE64: %s%s%s\n", g_svGreyB.c_str(), g_svNetKey.c_str(), g_svReset.c_str()); - DevMsg(eDLL_T::ENGINE, "--------------------------------------------------------------\n"); - v_NET_SetKey(g_pNetKey, g_svNetKey.c_str()); + + DevMsg(eDLL_T::ENGINE, "Installed NetKey: '%s%s%s'\n", + g_svGreyB.c_str(), g_svNetKey.c_str(), g_svReset.c_str()); } //----------------------------------------------------------------------------- @@ -81,8 +81,8 @@ void NET_SetKey(const string& svNetKey) //----------------------------------------------------------------------------- void NET_GenerateKey() { - g_svNetKey.clear(); - net_useRandomKey->SetValue(1); + if (!net_useRandomKey->GetBool()) + net_useRandomKey->SetValue(1); BCRYPT_ALG_HANDLE hAlgorithm; if (BCryptOpenAlgorithmProvider(&hAlgorithm, L"RNG", 0, 0) < 0) @@ -90,26 +90,15 @@ void NET_GenerateKey() Error(eDLL_T::ENGINE, false, "Failed to open rng algorithm\n"); return; } - unsigned char pBuffer[0x10u]; - if (BCryptGenRandom(hAlgorithm, pBuffer, 0x10u, 0) < 0) + + uint8_t pBuffer[AES_128_KEY_SIZE]; + if (BCryptGenRandom(hAlgorithm, pBuffer, AES_128_KEY_SIZE, 0) < 0) { Error(eDLL_T::ENGINE, false, "Failed to generate random data\n"); return; } - for (int i = 0; i < 0x10u; i++) - { - g_svNetKey += pBuffer[i]; - } - - g_svNetKey = Base64Encode(g_svNetKey); - - DevMsg(eDLL_T::ENGINE, "______________________________________________________________\n"); - DevMsg(eDLL_T::ENGINE, "] NET_KEY ----------------------------------------------------\n"); - DevMsg(eDLL_T::ENGINE, "] BASE64: %s%s%s\n", g_svGreyB.c_str(), g_svNetKey.c_str(), g_svReset.c_str()); - DevMsg(eDLL_T::ENGINE, "--------------------------------------------------------------\n"); - - v_NET_SetKey(g_pNetKey, g_svNetKey.c_str()); + NET_SetKey(Base64Encode(string(reinterpret_cast(&pBuffer), AES_128_KEY_SIZE))); } //----------------------------------------------------------------------------- diff --git a/r5dev/engine/net.h b/r5dev/engine/net.h index c2cd859f..cadbf175 100644 --- a/r5dev/engine/net.h +++ b/r5dev/engine/net.h @@ -13,6 +13,8 @@ #define NETMSG_LENGTH_BITS 12 // 512 bytes (11 in Valve Source, 256 bytes). #define NET_MIN_MESSAGE 5 // Even connectionless packets require int32 value (-1) + 1 byte content +#define AES_128_KEY_SIZE 16 + /* ==== CNETCHAN ======================================================================================================================================================== */ inline CMemory p_NET_Init; inline auto v_NET_Init = p_NET_Init.RCast(); @@ -47,6 +49,7 @@ void NET_Detach(); /////////////////////////////////////////////////////////////////////////////// extern string g_svNetKey; extern uintptr_t g_pNetKey; +inline std::mutex g_NetKeyMutex; /////////////////////////////////////////////////////////////////////////////// class VNet : public IDetour diff --git a/r5dev/engine/server/server.cpp b/r5dev/engine/server/server.cpp index 8e7e2882..00232250 100644 --- a/r5dev/engine/server/server.cpp +++ b/r5dev/engine/server/server.cpp @@ -68,13 +68,7 @@ CClient* CServer::Authenticate(CServer* pServer, user_creds_s* pInpacket) string svIpAddress = pInpacket->m_nAddr.GetAddress(); if (sv_showconnecting->GetBool()) { - DevMsg(eDLL_T::SERVER, "\n"); - DevMsg(eDLL_T::SERVER, "______________________________________________________________\n"); - DevMsg(eDLL_T::SERVER, "] AUTHENTICATION ---------------------------------------------\n"); - DevMsg(eDLL_T::SERVER, "] UID : | '%s'\n", pInpacket->m_pUserID); - DevMsg(eDLL_T::SERVER, "] OID : | '%llu'\n", pInpacket->m_nNucleusID); - DevMsg(eDLL_T::SERVER, "] ADR : | '%s'\n", svIpAddress.c_str()); - DevMsg(eDLL_T::SERVER, "--------------------------------------------------------------\n"); + DevMsg(eDLL_T::SERVER, "Processing connectionless challenge from '%s' ('%llu')\n", svIpAddress.c_str(), pInpacket->m_nNucleusID); } if (g_pBanSystem->IsBanListValid()) // Is the banlist vector valid? @@ -90,10 +84,6 @@ CClient* CServer::Authenticate(CServer* pServer, user_creds_s* pInpacket) return nullptr; } } - if (sv_showconnecting->GetBool()) - { - DevMsg(eDLL_T::SERVER, "\n"); - } if (g_bCheckCompBanDB) { diff --git a/r5dev/gameui/IBrowser.cpp b/r5dev/gameui/IBrowser.cpp index 7b34e2c2..92b5d207 100644 --- a/r5dev/gameui/IBrowser.cpp +++ b/r5dev/gameui/IBrowser.cpp @@ -586,6 +586,7 @@ void CBrowser::UpdateHostingStatus(void) break; } + std::lock_guard l(g_NetKeyMutex); NetGameServer_t netGameServer // !FIXME: create from main thread. { g_pServerListManager->m_Server.m_svHostName, @@ -670,6 +671,8 @@ void CBrowser::SettingsPanel(void) { ProcessCommand(fmt::format("{:s} \"{:s}\"", "pylon_matchmaking_hostname", m_szMatchmakingHostName).c_str()); } + + std::lock_guard l(g_NetKeyMutex); ImGui::InputText("Netkey", const_cast(g_svNetKey.c_str()), ImGuiInputTextFlags_ReadOnly); if (ImGui::Button("Regenerate Encryption Key")) { diff --git a/r5dev/public/inetmsghandler.h b/r5dev/public/inetmsghandler.h index 5d510bf7..28817b2e 100644 --- a/r5dev/public/inetmsghandler.h +++ b/r5dev/public/inetmsghandler.h @@ -13,11 +13,13 @@ #if !defined( INETMSGHANDLER_H ) #define INETMSGHANDLER_H +typedef struct netpacket_s netpacket_t; + abstract_class IConnectionlessPacketHandler { public: virtual ~IConnectionlessPacketHandler(void) = 0; - virtual bool ProcessConnectionlessPacket(void* packet) = 0; + virtual bool ProcessConnectionlessPacket(netpacket_t* packet) = 0; }; abstract_class INetMessageHandler diff --git a/r5dev/public/iserver.h b/r5dev/public/iserver.h index 3a0c3818..43c7b6c4 100644 --- a/r5dev/public/iserver.h +++ b/r5dev/public/iserver.h @@ -1,8 +1,9 @@ #ifndef ISERVER_H #define ISERVER_H #include "inetchannel.h" +#include "inetmsghandler.h" -class IServer +abstract_class IServer : public IConnectionlessPacketHandler { public: virtual ~IServer(void) = 0; diff --git a/r5dev/squirrel/sqscript.cpp b/r5dev/squirrel/sqscript.cpp index 47bd4450..d28319ca 100644 --- a/r5dev/squirrel/sqscript.cpp +++ b/r5dev/squirrel/sqscript.cpp @@ -214,12 +214,7 @@ SQInteger Script_LoadRson(const SQChar* szRsonName) { if (sq_showrsonloading->GetBool()) { - DevMsg(eDLL_T::ENGINE, "\n"); - DevMsg(eDLL_T::ENGINE, "______________________________________________________________\n"); - DevMsg(eDLL_T::ENGINE, "] RSON ]------------------------------------------------------\n"); - DevMsg(eDLL_T::ENGINE, "] PATH: '%s'\n", szRsonName); - DevMsg(eDLL_T::ENGINE, "--------------------------------------------------------------\n"); - DevMsg(eDLL_T::ENGINE, "\n"); + DevMsg(eDLL_T::ENGINE, "Loading RSON: '%s'\n", szRsonName); } return v_Script_LoadRson(szRsonName); } diff --git a/r5dev/vstdlib/callback.cpp b/r5dev/vstdlib/callback.cpp index 4cb346af..2766d7fe 100644 --- a/r5dev/vstdlib/callback.cpp +++ b/r5dev/vstdlib/callback.cpp @@ -525,7 +525,7 @@ void RTech_StringToGUID_f(const CCommand& args) unsigned long long guid = g_pRTech->StringToGuid(args.Arg(1)); DevMsg(eDLL_T::RTECH, "______________________________________________________________\n"); - DevMsg(eDLL_T::RTECH, "] RTECH_HASH -------------------------------------------------\n"); + DevMsg(eDLL_T::RTECH, "] RTECH_HASH ]------------------------------------------------\n"); DevMsg(eDLL_T::RTECH, "] GUID: '0x%llX'\n", guid); } diff --git a/r5dev/windows/id3dx.cpp b/r5dev/windows/id3dx.cpp index 63722503..acebbfa0 100644 --- a/r5dev/windows/id3dx.cpp +++ b/r5dev/windows/id3dx.cpp @@ -345,7 +345,7 @@ void DestroyRenderTarget() if (mat_showdxoutput->GetBool()) { DevMsg(eDLL_T::MS, "+----------------------------------------------------------------+\n"); - DevMsg(eDLL_T::MS, "| >>>>>>>>>>>>>>>| RENDER TARGET VIEW DESTROYED |<<<<<<<<<<<<<<< |\n"); + DevMsg(eDLL_T::MS, "| >>>>>>>>>>>>| !! RENDER TARGET VIEW DESTROYED !! |<<<<<<<<<<<< |\n"); DevMsg(eDLL_T::MS, "+----------------------------------------------------------------+\n"); } }