Light banning code improvements

* Removed extraneous std::string constructors (everything using the bansystem used raw char pointers).
* Improved logging and function comments.
This commit is contained in:
Kawe Mazidjatari 2023-04-29 12:15:51 +02:00
parent ab9d36e0d8
commit 3ea49766c6
3 changed files with 47 additions and 37 deletions

View File

@ -24,16 +24,19 @@ void SV_IsClientBanned(const string& svIPAddr, const uint64_t nNucleusID, const
{
if (!ThreadInMainThread())
{
g_TaskScheduler->Dispatch([svError, svIPAddr]
g_TaskScheduler->Dispatch([svError, svIPAddr, nNucleusID]
{
g_pBanSystem->KickPlayerById(svIPAddr.c_str(), svError.c_str());
Warning(eDLL_T::SERVER, "Removed client '%s' ('%llu' is banned globally!)\n",
svIPAddr.c_str(), nNucleusID);
}, 0);
}
//Warning(eDLL_T::SERVER, "Added '%s' to refused list ('%llu' is banned from the master server!)\n", svIPAddr.c_str(), nNucleusID);
}
}
//-----------------------------------------------------------------------------
// Purpose: checks if particular client is banned on the master server
//-----------------------------------------------------------------------------
void SV_ProcessBulkCheck(const BannedVec_t& bannedVec)
{
BannedVec_t outBannedVec;
@ -48,6 +51,10 @@ void SV_ProcessBulkCheck(const BannedVec_t& bannedVec)
}
}
//-----------------------------------------------------------------------------
// Purpose: creates a snapshot of the currently connected clients
// Input : *pBannedVec - if passed, will check for bans and kick the clients
//-----------------------------------------------------------------------------
void SV_CheckForBan(const BannedVec_t* pBannedVec /*= nullptr*/)
{
Assert(ThreadInMainThread());
@ -77,7 +84,7 @@ void SV_CheckForBan(const BannedVec_t* pBannedVec /*= nullptr*/)
{
if (it.second == pClient->GetNucleusID())
{
Warning(eDLL_T::SERVER, "Removing client '%s' from slot '%i' ('%llu' is banned from this server!)\n", szIPAddr, c, nNucleusID);
Warning(eDLL_T::SERVER, "Removing client '%s' from slot '%i' ('%llu' is banned globally!)\n", szIPAddr, c, nNucleusID);
pClient->Disconnect(Reputation_t::REP_MARK_BAD, "%s", it.first.c_str());
}
}

View File

@ -98,25 +98,27 @@ void CBanSystem::Save(void) const
//-----------------------------------------------------------------------------
// Purpose: adds a banned player entry to the banned list
// Input : &svIpAddress -
// nNucleusID -
// Input : *ipAddress -
// nucleusId -
//-----------------------------------------------------------------------------
bool CBanSystem::AddEntry(const string& svIpAddress, const uint64_t nNucleusID)
bool CBanSystem::AddEntry(const char* ipAddress, const uint64_t nucleusId)
{
Assert(!svIpAddress.empty());
Assert(VALID_CHARSTAR(ipAddress));
const auto idPair = std::make_pair(string(ipAddress), nucleusId);
if (IsBanListValid())
{
auto it = std::find(m_vBanList.begin(), m_vBanList.end(), std::make_pair(svIpAddress, nNucleusID));
auto it = std::find(m_vBanList.begin(), m_vBanList.end(), idPair);
if (it == m_vBanList.end())
{
m_vBanList.push_back(std::make_pair(svIpAddress, nNucleusID));
m_vBanList.push_back(idPair);
return true;
}
}
else
{
m_vBanList.push_back(std::make_pair(svIpAddress, nNucleusID));
m_vBanList.push_back(idPair);
return true;
}
@ -125,18 +127,21 @@ bool CBanSystem::AddEntry(const string& svIpAddress, const uint64_t nNucleusID)
//-----------------------------------------------------------------------------
// Purpose: deletes an entry in the banned list
// Input : &svIpAddress -
// nNucleusID -
// Input : *ipAddress -
// nucleusId -
//-----------------------------------------------------------------------------
bool CBanSystem::DeleteEntry(const string& svIpAddress, const uint64_t nNucleusID)
bool CBanSystem::DeleteEntry(const char* ipAddress, const uint64_t nucleusId)
{
Assert(!svIpAddress.empty());
Assert(VALID_CHARSTAR(ipAddress));
if (IsBanListValid())
{
auto it = std::find_if(m_vBanList.begin(), m_vBanList.end(),
[&](const pair<const string, const uint64_t>& element)
{ return (svIpAddress.compare(element.first) == NULL || element.second == nNucleusID); });
{
return (strcmp(ipAddress, element.first.c_str()) == NULL
|| element.second == nucleusId);
});
if (it != m_vBanList.end())
{
@ -150,25 +155,25 @@ bool CBanSystem::DeleteEntry(const string& svIpAddress, const uint64_t nNucleusI
//-----------------------------------------------------------------------------
// Purpose: checks if specified ip address or nucleus id is banned
// Input : &svIpAddress -
// nNucleusID -
// Input : *ipAddress -
// nucleusId -
// Output : true if banned, false if not banned
//-----------------------------------------------------------------------------
bool CBanSystem::IsBanned(const string& svIpAddress, const uint64_t nNucleusID) const
bool CBanSystem::IsBanned(const char* ipAddress, const uint64_t nucleusId) const
{
for (size_t i = 0; i < m_vBanList.size(); i++)
{
const string& ipAddress = m_vBanList[i].first;
const uint64_t nucleusID = m_vBanList[i].second;
const string& bannedIpAddress = m_vBanList[i].first;
const uint64_t bannedNucleusID = m_vBanList[i].second;
if (ipAddress.empty() ||
!nucleusID) // Cannot be null.
if (bannedIpAddress.empty()
|| !bannedNucleusID) // Cannot be null.
{
continue;
}
if (ipAddress.compare(svIpAddress) == NULL ||
nNucleusID == nucleusID)
if (bannedIpAddress.compare(ipAddress) == NULL
|| nucleusId == bannedNucleusID)
{
return true;
}
@ -239,23 +244,23 @@ void CBanSystem::BanPlayerById(const char* playerHandle, const char* reason)
//-----------------------------------------------------------------------------
// Purpose: unbans a player by given nucleus id or ip address
// Input : &svCriteria -
// Input : *criteria -
//-----------------------------------------------------------------------------
void CBanSystem::UnbanPlayer(const string& svCriteria)
void CBanSystem::UnbanPlayer(const char* criteria)
{
try
{
bool bSave = false;
if (StringIsDigit(svCriteria)) // Check if we have an ip address or nucleus id.
if (StringIsDigit(criteria)) // Check if we have an ip address or nucleus id.
{
if (DeleteEntry("<<invalid>>", std::stoll(svCriteria))) // Delete ban entry.
if (DeleteEntry("<<invalid>>", std::stoll(criteria))) // Delete ban entry.
{
bSave = true;
}
}
else
{
if (DeleteEntry(svCriteria, 0)) // Delete ban entry.
if (DeleteEntry(criteria, 0)) // Delete ban entry.
{
bSave = true;
}
@ -264,7 +269,7 @@ void CBanSystem::UnbanPlayer(const string& svCriteria)
if (bSave)
{
Save(); // Save modified vector to file.
DevMsg(eDLL_T::SERVER, "Removed '%s' from banned list\n", svCriteria.c_str());
DevMsg(eDLL_T::SERVER, "Removed '%s' from banned list\n", criteria);
}
}
catch (const std::exception& e)
@ -371,8 +376,6 @@ void CBanSystem::AuthorPlayerById(const char* playerHandle, const bool shouldBan
if (shouldBan && AddEntry(pNetChan->GetAddress(), pClient->GetNucleusID()) && !bSave)
bSave = true;
pClient->Disconnect(REP_MARK_BAD, reason);
bDisconnect = true;
}

View File

@ -8,10 +8,10 @@ public:
void Load(void);
void Save(void) const;
bool AddEntry(const string& svIpAddress, const uint64_t nNucleusID);
bool DeleteEntry(const string& svIpAddress, const uint64_t nNucleusID);
bool AddEntry(const char* ipAddress, const uint64_t nucleusId);
bool DeleteEntry(const char* ipAddress, const uint64_t nucleusId);
bool IsBanned(const string& svIpAddress, const uint64_t nNucleusID) const;
bool IsBanned(const char* ipAddress, const uint64_t nucleusId) const;
bool IsBanListValid(void) const;
void KickPlayerByName(const char* playerName, const char* reason = nullptr);
@ -20,7 +20,7 @@ public:
void BanPlayerByName(const char* playerName, const char* reason = nullptr);
void BanPlayerById(const char* playerHandle, const char* reason = nullptr);
void UnbanPlayer(const string& svCriteria);
void UnbanPlayer(const char* criteria);
private:
void AuthorPlayerByName(const char* playerName, const bool bBan, const char* reason = nullptr);