From e98a6c31e917cf7bd326a368838196b8f640b410 Mon Sep 17 00:00:00 2001 From: Kawe Mazidjatari <48657826+Mauler125@users.noreply.github.com> Date: Sun, 12 Feb 2023 02:28:03 +0100 Subject: [PATCH] Improve netkey logic Removed extraneous copy of the base64 netkey. The base64 key is now directly obtained from the netkey singleton. --- r5dev/engine/net.cpp | 9 ++++----- r5dev/engine/net.h | 2 -- r5dev/gameui/IBrowser.cpp | 9 ++++----- 3 files changed, 8 insertions(+), 12 deletions(-) diff --git a/r5dev/engine/net.cpp b/r5dev/engine/net.cpp index e0040ad5..b90ab9c1 100644 --- a/r5dev/engine/net.cpp +++ b/r5dev/engine/net.cpp @@ -67,15 +67,15 @@ int NET_SendDatagram(SOCKET s, void* pPayload, int iLenght, netadr_t* pAdr, bool //----------------------------------------------------------------------------- void NET_SetKey(const string& svNetKey) { - std::lock_guard l(g_NetKeyMutex); + string svTokenizedKey; if (svNetKey.size() == AES_128_B64_ENCODED_SIZE && - IsValidBase64(svNetKey, &g_svNetKey)) // Results are tokenized by 'IsValidBase64()'. + IsValidBase64(svNetKey, &svTokenizedKey)) // Results are tokenized by 'IsValidBase64()'. { - v_NET_SetKey(g_pNetKey, g_svNetKey.c_str()); + v_NET_SetKey(g_pNetKey, svTokenizedKey.c_str()); DevMsg(eDLL_T::ENGINE, "Installed NetKey: %s'%s%s%s'\n", - g_svReset.c_str(), g_svGreyB.c_str(), g_svNetKey.c_str(), g_svReset.c_str()); + g_svReset.c_str(), g_svGreyB.c_str(), g_pNetKey->GetBase64NetKey(), g_svReset.c_str()); } else { @@ -301,6 +301,5 @@ void VNet::Detach() const } /////////////////////////////////////////////////////////////////////////////// -string g_svNetKey = DEFAULT_NET_ENCRYPTION_KEY; netkey_t* g_pNetKey = nullptr; #endif // !NETCONSOLE diff --git a/r5dev/engine/net.h b/r5dev/engine/net.h index 45c4a144..90bf551f 100644 --- a/r5dev/engine/net.h +++ b/r5dev/engine/net.h @@ -46,9 +46,7 @@ void NET_Shutdown(void* thisptr, const char* szReason, uint8_t bBadRep, bool bRe void NET_RemoveChannel(CClient* pClient, int nIndex, const char* szReason, uint8_t bBadRep, bool bRemoveNow); /////////////////////////////////////////////////////////////////////////////// -extern string g_svNetKey; extern netkey_t* g_pNetKey; -inline std::mutex g_NetKeyMutex; /////////////////////////////////////////////////////////////////////////////// class VNet : public IDetour diff --git a/r5dev/gameui/IBrowser.cpp b/r5dev/gameui/IBrowser.cpp index b25c8aa7..dd7f0b9e 100644 --- a/r5dev/gameui/IBrowser.cpp +++ b/r5dev/gameui/IBrowser.cpp @@ -708,7 +708,6 @@ void CBrowser::UpdateHostingStatus(void) break; } - g_NetKeyMutex.lock(); NetGameServer_t netGameServer // !FIXME: create from main thread. { g_pServerListManager->m_Server.m_svHostName, @@ -718,7 +717,7 @@ void CBrowser::UpdateHostingStatus(void) mp_gamemode->GetString(), hostip->GetString(), hostport->GetString(), - g_svNetKey, + g_pNetKey->GetBase64NetKey(), std::to_string(*g_nServerRemoteChecksum), SDK_VERSION, std::to_string(g_pServer->GetNumHumanPlayers() + g_pServer->GetNumFakeClients()), @@ -727,7 +726,6 @@ void CBrowser::UpdateHostingStatus(void) std::chrono::system_clock::now().time_since_epoch() ).count() }; - g_NetKeyMutex.unlock(); std::thread post(&CBrowser::SendHostingPostRequest, this, netGameServer); post.detach(); @@ -795,8 +793,9 @@ void CBrowser::SettingsPanel(void) ProcessCommand(fmt::format("{:s} \"{:s}\"", "pylon_matchmaking_hostname", m_szMatchmakingHostName).c_str()); } - std::lock_guard l(g_NetKeyMutex); - ImGui::InputTextWithHint("Netkey", "Network encryption key", const_cast(g_svNetKey.c_str()), ImGuiInputTextFlags_ReadOnly); + // The 'const' qualifier has been casted away, however the readonly flag is set. + // Still a hack, but better than modifying the Dear ImGui lib even more.. + ImGui::InputTextWithHint("Netkey", "Network encryption key", const_cast(g_pNetKey->GetBase64NetKey()), ImGuiInputTextFlags_ReadOnly); if (ImGui::Button("Regenerate encryption key")) { g_TaskScheduler->Dispatch(NET_GenerateKey, 0);