2022-02-19 02:31:16 +01:00
|
|
|
//=====================================================================================//
|
|
|
|
//
|
|
|
|
// Purpose: Implementation of the CBanSystem class.
|
|
|
|
//
|
|
|
|
// $NoKeywords: $
|
|
|
|
//=====================================================================================//
|
|
|
|
|
2021-12-25 22:36:38 +01:00
|
|
|
#include "core/stdafx.h"
|
2022-04-02 02:48:54 +02:00
|
|
|
#include "engine/net.h"
|
2022-05-20 11:52:19 +02:00
|
|
|
#include "engine/client/client.h"
|
2022-09-01 01:07:16 +02:00
|
|
|
#include "filesystem/filesystem.h"
|
2022-08-09 17:34:10 +02:00
|
|
|
#include "networksystem/bansystem.h"
|
2021-12-25 22:36:38 +01:00
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
2022-09-15 23:13:37 +02:00
|
|
|
// Purpose: loads and parses the banned list
|
2021-12-25 22:36:38 +01:00
|
|
|
//-----------------------------------------------------------------------------
|
2022-02-19 02:31:16 +01:00
|
|
|
void CBanSystem::Load(void)
|
2021-12-25 22:36:38 +01:00
|
|
|
{
|
2022-08-29 17:03:14 +02:00
|
|
|
if (IsBanListValid())
|
|
|
|
m_vBanList.clear();
|
|
|
|
|
2022-09-01 01:07:16 +02:00
|
|
|
FileHandle_t pFile = FileSystem()->Open("banlist.json", "rt");
|
|
|
|
if (!pFile)
|
|
|
|
return;
|
|
|
|
|
|
|
|
uint32_t nLen = FileSystem()->Size(pFile);
|
2023-01-14 15:35:46 +00:00
|
|
|
char* pBuf = MemAllocSingleton()->Alloc<char>(nLen + 1);
|
2022-09-01 01:07:16 +02:00
|
|
|
|
|
|
|
int nRead = FileSystem()->Read(pBuf, nLen, pFile);
|
|
|
|
FileSystem()->Close(pFile);
|
|
|
|
|
|
|
|
pBuf[nRead] = '\0'; // Null terminate the string buffer containing our banned list.
|
|
|
|
|
|
|
|
try
|
2021-12-25 22:36:38 +01:00
|
|
|
{
|
2022-09-01 01:07:16 +02:00
|
|
|
nlohmann::json jsIn = nlohmann::json::parse(pBuf);
|
2021-12-25 22:36:38 +01:00
|
|
|
|
2022-09-01 01:07:16 +02:00
|
|
|
size_t nTotalBans = 0;
|
2022-06-14 20:56:55 +02:00
|
|
|
if (!jsIn.is_null())
|
2021-12-25 22:36:38 +01:00
|
|
|
{
|
2022-04-02 02:48:54 +02:00
|
|
|
if (!jsIn["totalBans"].is_null())
|
2022-09-01 01:07:16 +02:00
|
|
|
nTotalBans = jsIn["totalBans"].get<size_t>();
|
2021-12-25 22:36:38 +01:00
|
|
|
}
|
|
|
|
|
2022-09-01 01:07:16 +02:00
|
|
|
for (size_t i = 0; i < nTotalBans; i++)
|
2021-12-25 22:36:38 +01:00
|
|
|
{
|
2022-08-09 17:57:43 +02:00
|
|
|
nlohmann::json jsEntry = jsIn[std::to_string(i)];
|
2022-09-01 01:07:16 +02:00
|
|
|
if (!jsEntry.is_null())
|
2021-12-25 22:36:38 +01:00
|
|
|
{
|
2022-09-01 01:07:16 +02:00
|
|
|
string svIpAddress = jsEntry["ipAddress"].get<string>();
|
|
|
|
uint64_t nNucleusID = jsEntry["nucleusId"].get<uint64_t>();
|
2021-12-25 22:36:38 +01:00
|
|
|
|
2022-09-01 01:07:16 +02:00
|
|
|
m_vBanList.push_back(std::make_pair(svIpAddress, nNucleusID));
|
|
|
|
}
|
2021-12-25 22:36:38 +01:00
|
|
|
}
|
|
|
|
}
|
2022-09-01 01:07:16 +02:00
|
|
|
catch (const std::exception& ex)
|
|
|
|
{
|
|
|
|
Warning(eDLL_T::SERVER, "%s: Exception while parsing banned list:\n%s\n", __FUNCTION__, ex.what());
|
|
|
|
}
|
|
|
|
|
|
|
|
MemAllocSingleton()->Free(pBuf);
|
2021-12-25 22:36:38 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
2022-09-09 19:47:31 +02:00
|
|
|
// Purpose: saves the banned list
|
2021-12-25 22:36:38 +01:00
|
|
|
//-----------------------------------------------------------------------------
|
2022-02-19 02:31:16 +01:00
|
|
|
void CBanSystem::Save(void) const
|
2021-12-25 22:36:38 +01:00
|
|
|
{
|
2022-09-01 01:07:16 +02:00
|
|
|
FileHandle_t pFile = FileSystem()->Open("banlist.json", "wt", "PLATFORM");
|
|
|
|
if (!pFile)
|
2021-12-25 22:36:38 +01:00
|
|
|
{
|
2022-09-14 01:14:51 +02:00
|
|
|
Error(eDLL_T::SERVER, NO_ERROR, "%s - Unable to write to '%s' (read-only?)\n", __FUNCTION__, "banlist.json");
|
2022-09-01 01:07:16 +02:00
|
|
|
return;
|
2021-12-25 22:36:38 +01:00
|
|
|
}
|
|
|
|
|
2022-09-01 01:07:16 +02:00
|
|
|
try
|
|
|
|
{
|
|
|
|
nlohmann::json jsOut;
|
|
|
|
for (size_t i = 0; i < m_vBanList.size(); i++)
|
|
|
|
{
|
|
|
|
jsOut[std::to_string(i)]["ipAddress"] = m_vBanList[i].first;
|
|
|
|
jsOut[std::to_string(i)]["nucleusId"] = m_vBanList[i].second;
|
|
|
|
}
|
|
|
|
|
|
|
|
jsOut["totalBans"] = m_vBanList.size();
|
|
|
|
string svJsOut = jsOut.dump(4);
|
|
|
|
|
2023-04-02 16:03:01 +02:00
|
|
|
FileSystem()->Write(svJsOut.data(), int(svJsOut.size()), pFile);
|
2022-09-01 01:07:16 +02:00
|
|
|
}
|
|
|
|
catch (const std::exception& ex)
|
|
|
|
{
|
|
|
|
Warning(eDLL_T::SERVER, "%s: Exception while parsing banned list:\n%s\n", __FUNCTION__, ex.what());
|
|
|
|
}
|
2021-12-25 22:36:38 +01:00
|
|
|
|
2022-09-01 01:07:16 +02:00
|
|
|
FileSystem()->Close(pFile);
|
2021-12-25 22:36:38 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
2022-09-09 19:47:31 +02:00
|
|
|
// Purpose: adds a banned player entry to the banned list
|
2023-04-29 12:15:51 +02:00
|
|
|
// Input : *ipAddress -
|
|
|
|
// nucleusId -
|
2021-12-25 22:36:38 +01:00
|
|
|
//-----------------------------------------------------------------------------
|
2023-04-29 12:15:51 +02:00
|
|
|
bool CBanSystem::AddEntry(const char* ipAddress, const uint64_t nucleusId)
|
2021-12-25 22:36:38 +01:00
|
|
|
{
|
2023-04-29 12:15:51 +02:00
|
|
|
Assert(VALID_CHARSTAR(ipAddress));
|
|
|
|
const auto idPair = std::make_pair(string(ipAddress), nucleusId);
|
2022-08-30 12:07:09 +02:00
|
|
|
|
|
|
|
if (IsBanListValid())
|
2021-12-25 22:36:38 +01:00
|
|
|
{
|
2023-04-29 12:15:51 +02:00
|
|
|
auto it = std::find(m_vBanList.begin(), m_vBanList.end(), idPair);
|
|
|
|
|
2022-06-14 20:56:55 +02:00
|
|
|
if (it == m_vBanList.end())
|
2022-01-09 14:35:43 +01:00
|
|
|
{
|
2023-04-29 12:15:51 +02:00
|
|
|
m_vBanList.push_back(idPair);
|
2022-08-27 23:45:58 +02:00
|
|
|
return true;
|
2022-01-09 14:35:43 +01:00
|
|
|
}
|
2021-12-25 22:36:38 +01:00
|
|
|
}
|
2022-08-30 12:07:09 +02:00
|
|
|
else
|
|
|
|
{
|
2023-04-29 12:15:51 +02:00
|
|
|
m_vBanList.push_back(idPair);
|
2022-08-30 12:07:09 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2022-08-27 23:45:58 +02:00
|
|
|
return false;
|
2021-12-25 22:36:38 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
2022-09-09 19:47:31 +02:00
|
|
|
// Purpose: deletes an entry in the banned list
|
2023-04-29 12:15:51 +02:00
|
|
|
// Input : *ipAddress -
|
|
|
|
// nucleusId -
|
2021-12-25 22:36:38 +01:00
|
|
|
//-----------------------------------------------------------------------------
|
2023-04-29 12:15:51 +02:00
|
|
|
bool CBanSystem::DeleteEntry(const char* ipAddress, const uint64_t nucleusId)
|
2021-12-25 22:36:38 +01:00
|
|
|
{
|
2023-04-29 12:15:51 +02:00
|
|
|
Assert(VALID_CHARSTAR(ipAddress));
|
2022-08-30 12:07:09 +02:00
|
|
|
|
|
|
|
if (IsBanListValid())
|
2021-12-25 22:36:38 +01:00
|
|
|
{
|
2022-08-30 12:07:09 +02:00
|
|
|
auto it = std::find_if(m_vBanList.begin(), m_vBanList.end(),
|
|
|
|
[&](const pair<const string, const uint64_t>& element)
|
2023-04-29 12:15:51 +02:00
|
|
|
{
|
|
|
|
return (strcmp(ipAddress, element.first.c_str()) == NULL
|
|
|
|
|| element.second == nucleusId);
|
|
|
|
});
|
2022-08-30 12:07:09 +02:00
|
|
|
|
|
|
|
if (it != m_vBanList.end())
|
2021-12-25 22:36:38 +01:00
|
|
|
{
|
2022-08-30 12:07:09 +02:00
|
|
|
m_vBanList.erase(it);
|
|
|
|
return true;
|
2021-12-25 22:36:38 +01:00
|
|
|
}
|
|
|
|
}
|
2022-08-30 12:07:09 +02:00
|
|
|
|
|
|
|
return false;
|
2021-12-25 22:36:38 +01:00
|
|
|
}
|
|
|
|
|
2022-06-14 20:56:55 +02:00
|
|
|
//-----------------------------------------------------------------------------
|
2022-09-15 23:13:37 +02:00
|
|
|
// Purpose: checks if specified ip address or nucleus id is banned
|
2023-04-29 12:15:51 +02:00
|
|
|
// Input : *ipAddress -
|
|
|
|
// nucleusId -
|
2022-06-14 20:56:55 +02:00
|
|
|
// Output : true if banned, false if not banned
|
|
|
|
//-----------------------------------------------------------------------------
|
2023-04-29 12:15:51 +02:00
|
|
|
bool CBanSystem::IsBanned(const char* ipAddress, const uint64_t nucleusId) const
|
2022-06-14 20:56:55 +02:00
|
|
|
{
|
|
|
|
for (size_t i = 0; i < m_vBanList.size(); i++)
|
|
|
|
{
|
2023-04-29 12:15:51 +02:00
|
|
|
const string& bannedIpAddress = m_vBanList[i].first;
|
|
|
|
const uint64_t bannedNucleusID = m_vBanList[i].second;
|
2022-06-14 20:56:55 +02:00
|
|
|
|
2023-04-29 12:15:51 +02:00
|
|
|
if (bannedIpAddress.empty()
|
|
|
|
|| !bannedNucleusID) // Cannot be null.
|
2022-06-14 20:56:55 +02:00
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2023-04-29 12:15:51 +02:00
|
|
|
if (bannedIpAddress.compare(ipAddress) == NULL
|
|
|
|
|| nucleusId == bannedNucleusID)
|
2022-06-14 20:56:55 +02:00
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2021-12-25 22:36:38 +01:00
|
|
|
//-----------------------------------------------------------------------------
|
2022-09-15 23:13:37 +02:00
|
|
|
// Purpose: checks if banned list is valid
|
2021-12-25 22:36:38 +01:00
|
|
|
//-----------------------------------------------------------------------------
|
2022-02-19 02:31:16 +01:00
|
|
|
bool CBanSystem::IsBanListValid(void) const
|
2021-12-25 22:36:38 +01:00
|
|
|
{
|
2022-06-14 20:56:55 +02:00
|
|
|
return !m_vBanList.empty();
|
2021-12-25 22:36:38 +01:00
|
|
|
}
|
2022-09-15 23:13:37 +02:00
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
// Purpose: kicks a player by given name
|
2023-04-28 23:47:58 +02:00
|
|
|
// Input : *playerName -
|
|
|
|
// *reason -
|
2022-09-15 23:13:37 +02:00
|
|
|
//-----------------------------------------------------------------------------
|
2023-04-28 23:47:58 +02:00
|
|
|
void CBanSystem::KickPlayerByName(const char* playerName, const char* reason)
|
2022-09-15 23:13:37 +02:00
|
|
|
{
|
2023-04-28 23:47:58 +02:00
|
|
|
if (!VALID_CHARSTAR(playerName))
|
2022-09-15 23:13:37 +02:00
|
|
|
return;
|
|
|
|
|
2023-04-28 23:47:58 +02:00
|
|
|
AuthorPlayerByName(playerName, false);
|
2022-09-15 23:13:37 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
// Purpose: kicks a player by given handle or id
|
2023-04-28 23:47:58 +02:00
|
|
|
// Input : *playerHandle -
|
|
|
|
// *reason -
|
2022-09-15 23:13:37 +02:00
|
|
|
//-----------------------------------------------------------------------------
|
2023-04-28 23:47:58 +02:00
|
|
|
void CBanSystem::KickPlayerById(const char* playerHandle, const char* reason)
|
2022-09-15 23:13:37 +02:00
|
|
|
{
|
2023-04-28 23:47:58 +02:00
|
|
|
if (!VALID_CHARSTAR(playerHandle))
|
2022-09-15 23:13:37 +02:00
|
|
|
return;
|
|
|
|
|
2023-04-28 23:47:58 +02:00
|
|
|
AuthorPlayerById(playerHandle, false);
|
2023-02-23 23:55:55 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
// Purpose: bans a player by given name
|
2023-04-28 23:47:58 +02:00
|
|
|
// Input : *playerName -
|
|
|
|
// *reason -
|
2023-02-23 23:55:55 +01:00
|
|
|
//-----------------------------------------------------------------------------
|
2023-04-28 23:47:58 +02:00
|
|
|
void CBanSystem::BanPlayerByName(const char* playerName, const char* reason)
|
2023-02-23 23:55:55 +01:00
|
|
|
{
|
2023-04-28 23:47:58 +02:00
|
|
|
if (!VALID_CHARSTAR(playerName))
|
2023-02-23 23:55:55 +01:00
|
|
|
return;
|
|
|
|
|
2023-04-28 23:47:58 +02:00
|
|
|
AuthorPlayerByName(playerName, true);
|
2023-02-23 23:55:55 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
// Purpose: bans a player by given handle or id
|
2023-04-28 23:47:58 +02:00
|
|
|
// Input : *playerHandle -
|
|
|
|
// *reason -
|
2023-02-23 23:55:55 +01:00
|
|
|
//-----------------------------------------------------------------------------
|
2023-04-28 23:47:58 +02:00
|
|
|
void CBanSystem::BanPlayerById(const char* playerHandle, const char* reason)
|
2023-02-23 23:55:55 +01:00
|
|
|
{
|
2023-04-28 23:47:58 +02:00
|
|
|
if (!VALID_CHARSTAR(playerHandle))
|
2023-02-23 23:55:55 +01:00
|
|
|
return;
|
|
|
|
|
2023-04-28 23:47:58 +02:00
|
|
|
AuthorPlayerById(playerHandle, true);
|
2023-02-23 23:55:55 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
// Purpose: unbans a player by given nucleus id or ip address
|
2023-04-29 12:15:51 +02:00
|
|
|
// Input : *criteria -
|
2023-02-23 23:55:55 +01:00
|
|
|
//-----------------------------------------------------------------------------
|
2023-04-29 12:15:51 +02:00
|
|
|
void CBanSystem::UnbanPlayer(const char* criteria)
|
2023-02-23 23:55:55 +01:00
|
|
|
{
|
2022-09-15 23:13:37 +02:00
|
|
|
try
|
|
|
|
{
|
2023-02-23 23:55:55 +01:00
|
|
|
bool bSave = false;
|
2023-04-29 12:15:51 +02:00
|
|
|
if (StringIsDigit(criteria)) // Check if we have an ip address or nucleus id.
|
2022-09-15 23:13:37 +02:00
|
|
|
{
|
2023-04-29 12:15:51 +02:00
|
|
|
if (DeleteEntry("<<invalid>>", std::stoll(criteria))) // Delete ban entry.
|
2022-09-15 23:13:37 +02:00
|
|
|
{
|
2023-02-23 23:55:55 +01:00
|
|
|
bSave = true;
|
2022-09-15 23:13:37 +02:00
|
|
|
}
|
2023-02-23 23:55:55 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2023-04-29 12:15:51 +02:00
|
|
|
if (DeleteEntry(criteria, 0)) // Delete ban entry.
|
2022-09-15 23:13:37 +02:00
|
|
|
{
|
2023-02-23 23:55:55 +01:00
|
|
|
bSave = true;
|
2022-09-15 23:13:37 +02:00
|
|
|
}
|
|
|
|
}
|
2023-02-23 23:55:55 +01:00
|
|
|
|
|
|
|
if (bSave)
|
|
|
|
{
|
|
|
|
Save(); // Save modified vector to file.
|
2023-04-29 12:15:51 +02:00
|
|
|
DevMsg(eDLL_T::SERVER, "Removed '%s' from banned list\n", criteria);
|
2023-02-23 23:55:55 +01:00
|
|
|
}
|
2022-09-15 23:13:37 +02:00
|
|
|
}
|
|
|
|
catch (const std::exception& e)
|
|
|
|
{
|
2022-11-25 23:03:56 +01:00
|
|
|
Error(eDLL_T::SERVER, NO_ERROR, "%s - %s\n", __FUNCTION__, e.what());
|
2022-09-15 23:13:37 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
2023-02-23 23:55:55 +01:00
|
|
|
// Purpose: authors player by given name
|
2023-04-28 23:47:58 +02:00
|
|
|
// Input : *playerName -
|
|
|
|
// shouldBan - (only kicks if false)
|
|
|
|
// *reason -
|
2022-09-15 23:13:37 +02:00
|
|
|
//-----------------------------------------------------------------------------
|
2023-04-28 23:47:58 +02:00
|
|
|
void CBanSystem::AuthorPlayerByName(const char* playerName, const bool shouldBan, const char* reason)
|
2022-09-15 23:13:37 +02:00
|
|
|
{
|
2023-04-28 23:47:58 +02:00
|
|
|
Assert(VALID_CHARSTAR(playerName));
|
2023-02-23 23:55:55 +01:00
|
|
|
bool bDisconnect = false;
|
2022-09-15 23:13:37 +02:00
|
|
|
bool bSave = false;
|
|
|
|
|
2023-04-28 23:47:58 +02:00
|
|
|
if (!reason)
|
|
|
|
reason = shouldBan ? "Banned from server" : "Kicked from server";
|
|
|
|
|
2023-04-30 01:50:38 +02:00
|
|
|
for (int i = 0; i < g_ServerGlobalVariables->m_nMaxClients; i++)
|
2022-09-15 23:13:37 +02:00
|
|
|
{
|
2023-02-23 23:55:55 +01:00
|
|
|
CClient* pClient = g_pClient->GetClient(i);
|
|
|
|
if (!pClient)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
CNetChan* pNetChan = pClient->GetNetChan();
|
|
|
|
if (!pNetChan)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if (strlen(pNetChan->GetName()) > 0)
|
2022-09-15 23:13:37 +02:00
|
|
|
{
|
2023-04-28 23:47:58 +02:00
|
|
|
if (strcmp(playerName, pNetChan->GetName()) == NULL) // Our wanted name?
|
2022-09-15 23:13:37 +02:00
|
|
|
{
|
2023-04-28 23:47:58 +02:00
|
|
|
if (shouldBan && AddEntry(pNetChan->GetAddress(), pClient->GetNucleusID()) && !bSave)
|
2023-02-23 23:55:55 +01:00
|
|
|
bSave = true;
|
2022-09-15 23:13:37 +02:00
|
|
|
|
2023-04-28 23:47:58 +02:00
|
|
|
pClient->Disconnect(REP_MARK_BAD, reason);
|
2023-02-23 23:55:55 +01:00
|
|
|
bDisconnect = true;
|
2022-09-15 23:13:37 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (bSave)
|
2023-02-23 23:55:55 +01:00
|
|
|
{
|
2022-09-15 23:13:37 +02:00
|
|
|
Save();
|
2023-04-28 23:47:58 +02:00
|
|
|
DevMsg(eDLL_T::SERVER, "Added '%s' to banned list\n", playerName);
|
2023-02-23 23:55:55 +01:00
|
|
|
}
|
|
|
|
else if (bDisconnect)
|
|
|
|
{
|
2023-04-28 23:47:58 +02:00
|
|
|
DevMsg(eDLL_T::SERVER, "Kicked '%s' from server\n", playerName);
|
2023-02-23 23:55:55 +01:00
|
|
|
}
|
2022-09-15 23:13:37 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
2023-02-23 23:55:55 +01:00
|
|
|
// Purpose: authors player by given nucleus id or ip address
|
2023-04-28 23:47:58 +02:00
|
|
|
// Input : *playerHandle -
|
|
|
|
// shouldBan - (only kicks if false)
|
|
|
|
// *reason -
|
2022-09-15 23:13:37 +02:00
|
|
|
//-----------------------------------------------------------------------------
|
2023-04-28 23:47:58 +02:00
|
|
|
void CBanSystem::AuthorPlayerById(const char* playerHandle, const bool shouldBan, const char* reason)
|
2022-09-15 23:13:37 +02:00
|
|
|
{
|
2023-04-28 23:47:58 +02:00
|
|
|
Assert(VALID_CHARSTAR(playerHandle));
|
2022-09-15 23:13:37 +02:00
|
|
|
|
|
|
|
try
|
|
|
|
{
|
2023-04-28 23:47:58 +02:00
|
|
|
bool bOnlyDigits = StringIsDigit(playerHandle);
|
2023-02-23 23:55:55 +01:00
|
|
|
bool bDisconnect = false;
|
2022-09-15 23:13:37 +02:00
|
|
|
bool bSave = false;
|
|
|
|
|
2023-04-28 23:47:58 +02:00
|
|
|
if (!reason)
|
|
|
|
reason = shouldBan ? "Banned from server" : "Kicked from server";
|
|
|
|
|
2023-04-30 01:50:38 +02:00
|
|
|
for (int i = 0; i < g_ServerGlobalVariables->m_nMaxClients; i++)
|
2022-09-15 23:13:37 +02:00
|
|
|
{
|
|
|
|
CClient* pClient = g_pClient->GetClient(i);
|
|
|
|
if (!pClient)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
CNetChan* pNetChan = pClient->GetNetChan();
|
|
|
|
if (!pNetChan)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if (bOnlyDigits)
|
|
|
|
{
|
2023-04-28 23:47:58 +02:00
|
|
|
uint64_t nTargetID = static_cast<uint64_t>(std::stoll(playerHandle));
|
2022-09-15 23:13:37 +02:00
|
|
|
if (nTargetID > static_cast<uint64_t>(MAX_PLAYERS)) // Is it a possible nucleusID?
|
|
|
|
{
|
|
|
|
uint64_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;
|
|
|
|
}
|
|
|
|
|
2023-04-28 23:47:58 +02:00
|
|
|
if (shouldBan && AddEntry(pNetChan->GetAddress(), pClient->GetNucleusID()) && !bSave)
|
2022-09-15 23:13:37 +02:00
|
|
|
bSave = true;
|
|
|
|
|
2023-04-28 23:47:58 +02:00
|
|
|
pClient->Disconnect(REP_MARK_BAD, reason);
|
2023-02-23 23:55:55 +01:00
|
|
|
bDisconnect = true;
|
2022-09-15 23:13:37 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2023-04-28 23:47:58 +02:00
|
|
|
if (strcmp(playerHandle, pNetChan->GetAddress()) != NULL)
|
2022-09-15 23:13:37 +02:00
|
|
|
continue;
|
|
|
|
|
2023-04-28 23:47:58 +02:00
|
|
|
if (shouldBan && AddEntry(pNetChan->GetAddress(), pClient->GetNucleusID()) && !bSave)
|
2022-09-15 23:13:37 +02:00
|
|
|
bSave = true;
|
|
|
|
|
2023-04-28 23:47:58 +02:00
|
|
|
pClient->Disconnect(REP_MARK_BAD, reason);
|
2023-02-23 23:55:55 +01:00
|
|
|
bDisconnect = true;
|
2022-09-15 23:13:37 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (bSave)
|
2022-09-16 00:51:35 +02:00
|
|
|
{
|
2023-02-23 23:55:55 +01:00
|
|
|
Save();
|
2023-04-28 23:47:58 +02:00
|
|
|
DevMsg(eDLL_T::SERVER, "Added '%s' to banned list\n", playerHandle);
|
2022-09-16 00:51:35 +02:00
|
|
|
}
|
2023-02-23 23:55:55 +01:00
|
|
|
else if (bDisconnect)
|
2022-09-16 00:51:35 +02:00
|
|
|
{
|
2023-04-28 23:47:58 +02:00
|
|
|
DevMsg(eDLL_T::SERVER, "Kicked '%s' from server\n", playerHandle);
|
2022-09-16 00:51:35 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
catch (const std::exception& e)
|
|
|
|
{
|
2022-11-25 23:03:56 +01:00
|
|
|
Error(eDLL_T::SERVER, NO_ERROR, "%s - %s\n", __FUNCTION__, e.what());
|
2022-09-16 00:51:35 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-25 22:36:38 +01:00
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
2022-02-28 01:05:55 +01:00
|
|
|
CBanSystem* g_pBanSystem = new CBanSystem();
|