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.
This commit is contained in:
Kawe Mazidjatari 2024-02-04 12:55:56 +01:00
parent 0dd19ab2dd
commit ed5721eaa0

View File

@ -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<uint64_t>(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);
}
}