From f0907ca9f15835ac3a634a6347ceed918d02c86e Mon Sep 17 00:00:00 2001 From: Kawe Mazidjatari <48657826+Mauler125@users.noreply.github.com> Date: Sun, 4 Feb 2024 12:55:56 +0100 Subject: [PATCH] BanSystem: refactor & fix bugs * Make sure we read the banlist from platform as well, just like how we write to it. * Use strtoull() instead of atoll() for nucleus id values, nucleus id is unsigned so we must convert to unsigned. * Remove extraneous try catches, these were previously used for integral type conversions that threw exceptions, but the current ones do not throw so the try catch is unnecessary. --- r5dev/networksystem/bansystem.cpp | 150 ++++++++++++++---------------- 1 file changed, 69 insertions(+), 81 deletions(-) diff --git a/r5dev/networksystem/bansystem.cpp b/r5dev/networksystem/bansystem.cpp index a91357e7..d6dbf24a 100644 --- a/r5dev/networksystem/bansystem.cpp +++ b/r5dev/networksystem/bansystem.cpp @@ -6,6 +6,7 @@ //=====================================================================================// #include "core/stdafx.h" +#include "tier1/strtools.h" #include "engine/net.h" #include "engine/server/server.h" #include "engine/client/client.h" @@ -20,7 +21,7 @@ void CBanSystem::LoadList(void) if (IsBanListValid()) m_BannedList.Purge(); - FileHandle_t pFile = FileSystem()->Open("banlist.json", "rt"); + FileHandle_t pFile = FileSystem()->Open("banlist.json", "rt", "PLATFORM"); if (!pFile) return; @@ -259,34 +260,29 @@ void CBanSystem::BanPlayerById(const char* playerHandle, const char* reason) //----------------------------------------------------------------------------- void CBanSystem::UnbanPlayer(const char* criteria) { - try + bool bSave = false; + if (V_IsAllDigit(criteria)) // Check if we have an ip address or nucleus id. { - bool bSave = false; - if (StringIsDigit(criteria)) // Check if we have an ip address or nucleus id. - { - if (DeleteEntry("-<[InVaLiD]>-", atoll(criteria))) // Delete ban entry. - { - bSave = true; - } - } - else - { - if (DeleteEntry(criteria, 0)) // Delete ban entry. - { - bSave = true; - } - } + char* pEnd = nullptr; + const uint64_t nTargetID = strtoull(criteria, &pEnd, 10); - if (bSave) + if (DeleteEntry("-<[InVaLiD]>-", nTargetID)) // Delete ban entry. { - SaveList(); // Save modified vector to file. - Msg(eDLL_T::SERVER, "Removed '%s' from banned list\n", criteria); + bSave = true; } } - catch (const std::exception& e) + else { - Error(eDLL_T::SERVER, NO_ERROR, "%s - %s\n", __FUNCTION__, e.what()); - return; + if (DeleteEntry(criteria, 0)) // Delete ban entry. + { + bSave = true; + } + } + + if (bSave) + { + SaveList(); // Save modified vector to file. + Msg(eDLL_T::SERVER, "Removed '%s' from banned list\n", criteria); } } @@ -311,7 +307,7 @@ void CBanSystem::AuthorPlayerByName(const char* playerName, const bool shouldBan if (!pClient) continue; - CNetChan* pNetChan = pClient->GetNetChan(); + const CNetChan* pNetChan = pClient->GetNetChan(); if (!pNetChan) continue; @@ -349,78 +345,70 @@ void CBanSystem::AuthorPlayerById(const char* playerHandle, const bool shouldBan { Assert(VALID_CHARSTAR(playerHandle)); - try + bool bOnlyDigits = V_IsAllDigit(playerHandle); + bool bDisconnect = false; + bool bSave = false; + + if (!reason) + reason = shouldBan ? "Banned from server" : "Kicked from server"; + + for (int i = 0; i < g_ServerGlobalVariables->m_nMaxClients; i++) { - bool bOnlyDigits = StringIsDigit(playerHandle); - bool bDisconnect = false; - bool bSave = false; + CClient* pClient = g_pServer->GetClient(i); + if (!pClient) + continue; - if (!reason) - reason = shouldBan ? "Banned from server" : "Kicked from server"; + const CNetChan* pNetChan = pClient->GetNetChan(); + if (!pNetChan) + continue; - for (int i = 0; i < g_ServerGlobalVariables->m_nMaxClients; i++) + if (bOnlyDigits) { - CClient* pClient = g_pServer->GetClient(i); - if (!pClient) - continue; + char* pEnd = nullptr; + const uint64_t nTargetID = strtoull(playerHandle, &pEnd, 10); - CNetChan* pNetChan = pClient->GetNetChan(); - if (!pNetChan) - continue; - - if (bOnlyDigits) + if (nTargetID >= MAX_PLAYERS) // Is it a possible nucleusID? { - char* pEnd = nullptr; - uint64_t nTargetID = strtoull(playerHandle, &pEnd, 10); + const NucleusID_t nNucleusID = pClient->GetNucleusID(); - if (nTargetID > MAX_PLAYERS) // Is it a possible nucleusID? - { - NucleusID_t nNucleusID = pClient->GetNucleusID(); - - if (nNucleusID != nTargetID) - continue; - } - else // If its not try by handle. - { - uint64_t nClientID = static_cast(pClient->GetHandle()); - - if (nClientID != nTargetID) - continue; - } - - if (shouldBan && AddEntry(pNetChan->GetAddress(), pClient->GetNucleusID()) && !bSave) - bSave = true; - - pClient->Disconnect(REP_MARK_BAD, reason); - bDisconnect = true; - } - else - { - if (strcmp(playerHandle, pNetChan->GetAddress()) != NULL) + if (nNucleusID != nTargetID) continue; - - if (shouldBan && AddEntry(pNetChan->GetAddress(), pClient->GetNucleusID()) && !bSave) - bSave = true; - - pClient->Disconnect(REP_MARK_BAD, reason); - bDisconnect = true; } - } + else // If its not try by handle. + { + const edict_t nClientID = pClient->GetHandle(); - if (bSave) - { - SaveList(); - Msg(eDLL_T::SERVER, "Added '%s' to banned list\n", playerHandle); + if (nClientID != nTargetID) + continue; + } + + if (shouldBan && AddEntry(pNetChan->GetAddress(), pClient->GetNucleusID()) && !bSave) + bSave = true; + + pClient->Disconnect(REP_MARK_BAD, reason); + bDisconnect = true; } - else if (bDisconnect) + else { - Msg(eDLL_T::SERVER, "Kicked '%s' from server\n", playerHandle); + if (strcmp(playerHandle, pNetChan->GetAddress()) != NULL) + continue; + + if (shouldBan && AddEntry(pNetChan->GetAddress(), pClient->GetNucleusID()) && !bSave) + bSave = true; + + pClient->Disconnect(REP_MARK_BAD, reason); + bDisconnect = true; } } - catch (const std::exception& e) + + if (bSave) { - Error(eDLL_T::SERVER, NO_ERROR, "%s - %s\n", __FUNCTION__, e.what()); - return; + SaveList(); + Msg(eDLL_T::SERVER, "Added '%s' to banned list\n", playerHandle); + } + else if (bDisconnect) + { + Msg(eDLL_T::SERVER, "Kicked '%s' from server\n", playerHandle); } }