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"
|
2023-07-12 08:56:17 +02:00
|
|
|
#include "engine/server/server.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
|
|
|
//-----------------------------------------------------------------------------
|
2023-08-31 00:16:25 +02:00
|
|
|
void CBanSystem::LoadList(void)
|
2021-12-25 22:36:38 +01:00
|
|
|
{
|
2022-08-29 17:03:14 +02:00
|
|
|
if (IsBanListValid())
|
2023-08-31 00:16:25 +02:00
|
|
|
m_BannedList.Purge();
|
2022-08-29 17:03:14 +02:00
|
|
|
|
2022-09-01 01:07:16 +02:00
|
|
|
FileHandle_t pFile = FileSystem()->Open("banlist.json", "rt");
|
|
|
|
if (!pFile)
|
|
|
|
return;
|
|
|
|
|
2023-08-09 14:43:54 +02:00
|
|
|
const ssize_t nLen = FileSystem()->Size(pFile);
|
2023-06-26 22:34:24 +02:00
|
|
|
std::unique_ptr<char[]> pBuf(new char[nLen + 1]);
|
2022-09-01 01:07:16 +02:00
|
|
|
|
2023-08-09 14:43:54 +02:00
|
|
|
const ssize_t nRead = FileSystem()->Read(pBuf.get(), nLen, pFile);
|
2022-09-01 01:07:16 +02:00
|
|
|
FileSystem()->Close(pFile);
|
|
|
|
|
2023-06-26 22:34:24 +02:00
|
|
|
pBuf.get()[nRead] = '\0'; // Null terminate the string buffer containing our banned list.
|
2022-09-01 01:07:16 +02:00
|
|
|
|
|
|
|
try
|
2021-12-25 22:36:38 +01:00
|
|
|
{
|
2023-06-26 22:34:24 +02:00
|
|
|
nlohmann::json jsIn = nlohmann::json::parse(pBuf.get());
|
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
|
|
|
{
|
2023-08-31 00:16:25 +02:00
|
|
|
Banned_t banned;
|
|
|
|
banned.m_Address = jsEntry["ipAddress"].get<string>().c_str();
|
|
|
|
banned.m_NucleusID = jsEntry["nucleusId"].get<NucleusID_t>();
|
2021-12-25 22:36:38 +01:00
|
|
|
|
2023-08-31 00:16:25 +02:00
|
|
|
m_BannedList.AddToTail(banned);
|
2022-09-01 01:07:16 +02:00
|
|
|
}
|
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());
|
|
|
|
}
|
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
|
|
|
//-----------------------------------------------------------------------------
|
2023-08-31 00:16:25 +02:00
|
|
|
void CBanSystem::SaveList(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;
|
2023-08-31 00:16:25 +02:00
|
|
|
|
|
|
|
FOR_EACH_VEC(m_BannedList, i)
|
2022-09-01 01:07:16 +02:00
|
|
|
{
|
2023-08-31 00:16:25 +02:00
|
|
|
const Banned_t& banned = m_BannedList[i];
|
|
|
|
char idx[64]; itoa(i, idx, 10);
|
|
|
|
|
|
|
|
jsOut[idx]["ipAddress"] = banned.m_Address.String();
|
|
|
|
jsOut[idx]["nucleusId"] = banned.m_NucleusID;
|
2022-09-01 01:07:16 +02:00
|
|
|
}
|
|
|
|
|
2023-08-31 00:16:25 +02:00
|
|
|
jsOut["totalBans"] = m_BannedList.Count();
|
2022-09-01 01:07:16 +02:00
|
|
|
string svJsOut = jsOut.dump(4);
|
|
|
|
|
2023-08-09 14:43:54 +02:00
|
|
|
FileSystem()->Write(svJsOut.data(), 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-08-31 00:16:25 +02:00
|
|
|
bool CBanSystem::AddEntry(const char* ipAddress, const NucleusID_t nucleusId)
|
2021-12-25 22:36:38 +01:00
|
|
|
{
|
2023-04-29 12:15:51 +02:00
|
|
|
Assert(VALID_CHARSTAR(ipAddress));
|
2023-08-31 00:16:25 +02:00
|
|
|
const Banned_t banned(ipAddress, nucleusId);
|
2022-08-30 12:07:09 +02:00
|
|
|
|
|
|
|
if (IsBanListValid())
|
2021-12-25 22:36:38 +01:00
|
|
|
{
|
2023-08-31 00:16:25 +02:00
|
|
|
if (m_BannedList.Find(banned) == m_BannedList.InvalidIndex())
|
2022-01-09 14:35:43 +01:00
|
|
|
{
|
2023-08-31 00:16:25 +02:00
|
|
|
m_BannedList.AddToTail(banned);
|
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-08-31 00:16:25 +02:00
|
|
|
m_BannedList.AddToTail(banned);
|
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-08-31 00:16:25 +02:00
|
|
|
bool CBanSystem::DeleteEntry(const char* ipAddress, const NucleusID_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
|
|
|
{
|
2023-08-31 00:16:25 +02:00
|
|
|
FOR_EACH_VEC(m_BannedList, i)
|
2021-12-25 22:36:38 +01:00
|
|
|
{
|
2023-08-31 00:16:25 +02:00
|
|
|
const Banned_t& banned = m_BannedList[i];
|
|
|
|
|
|
|
|
if (banned.m_NucleusID == nucleusId ||
|
|
|
|
banned.m_Address.IsEqual_CaseInsensitive(ipAddress))
|
|
|
|
{
|
|
|
|
m_BannedList.Remove(i);
|
|
|
|
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-08-31 00:16:25 +02:00
|
|
|
bool CBanSystem::IsBanned(const char* ipAddress, const NucleusID_t nucleusId) const
|
2022-06-14 20:56:55 +02:00
|
|
|
{
|
2023-08-31 00:16:25 +02:00
|
|
|
|
|
|
|
FOR_EACH_VEC(m_BannedList, i)
|
2022-06-14 20:56:55 +02:00
|
|
|
{
|
2023-08-31 00:16:25 +02:00
|
|
|
const Banned_t& banned = m_BannedList[i];
|
2022-06-14 20:56:55 +02:00
|
|
|
|
2023-08-31 00:16:25 +02:00
|
|
|
if (banned.m_NucleusID == NULL ||
|
|
|
|
banned.m_Address.IsEmpty())
|
2022-06-14 20:56:55 +02:00
|
|
|
{
|
2023-08-31 00:16:25 +02:00
|
|
|
// Cannot be NULL.
|
2022-06-14 20:56:55 +02:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2023-08-31 00:16:25 +02:00
|
|
|
if (banned.m_NucleusID == nucleusId ||
|
|
|
|
banned.m_Address.IsEqual_CaseInsensitive(ipAddress))
|
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
|
|
|
{
|
2023-08-31 00:16:25 +02:00
|
|
|
return !m_BannedList.IsEmpty();
|
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-06-20 08:44:03 +02:00
|
|
|
AuthorPlayerByName(playerName, false, reason);
|
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-06-20 08:44:03 +02:00
|
|
|
AuthorPlayerById(playerHandle, false, reason);
|
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-06-20 08:44:03 +02:00
|
|
|
AuthorPlayerByName(playerName, true, reason);
|
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-06-20 08:44:03 +02:00
|
|
|
AuthorPlayerById(playerHandle, true, reason);
|
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-08-31 00:16:25 +02:00
|
|
|
if (DeleteEntry("-<[InVaLiD]>-", atoll(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)
|
|
|
|
{
|
2023-08-31 00:16:25 +02:00
|
|
|
SaveList(); // Save modified vector to file.
|
2023-08-21 19:12:29 +02:00
|
|
|
Msg(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)
|
2023-08-31 00:16:25 +02:00
|
|
|
// *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-07-12 08:56:17 +02:00
|
|
|
CClient* pClient = g_pServer->GetClient(i);
|
2023-02-23 23:55:55 +01:00
|
|
|
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
|
|
|
{
|
2023-08-31 00:16:25 +02:00
|
|
|
SaveList();
|
2023-08-21 19:12:29 +02:00
|
|
|
Msg(eDLL_T::SERVER, "Added '%s' to banned list\n", playerName);
|
2023-02-23 23:55:55 +01:00
|
|
|
}
|
|
|
|
else if (bDisconnect)
|
|
|
|
{
|
2023-08-21 19:12:29 +02:00
|
|
|
Msg(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)
|
2023-08-31 00:16:25 +02:00
|
|
|
// *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
|
|
|
{
|
2023-07-12 08:56:17 +02:00
|
|
|
CClient* pClient = g_pServer->GetClient(i);
|
2022-09-15 23:13:37 +02:00
|
|
|
if (!pClient)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
CNetChan* pNetChan = pClient->GetNetChan();
|
|
|
|
if (!pNetChan)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if (bOnlyDigits)
|
|
|
|
{
|
2023-08-31 00:16:25 +02:00
|
|
|
char* pEnd = nullptr;
|
|
|
|
uint64_t nTargetID = strtoull(playerHandle, &pEnd, 10);
|
|
|
|
|
|
|
|
if (nTargetID > MAX_PLAYERS) // Is it a possible nucleusID?
|
2022-09-15 23:13:37 +02:00
|
|
|
{
|
2023-08-31 00:16:25 +02:00
|
|
|
NucleusID_t nNucleusID = pClient->GetNucleusID();
|
|
|
|
|
2022-09-15 23:13:37 +02:00
|
|
|
if (nNucleusID != nTargetID)
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
else // If its not try by handle.
|
|
|
|
{
|
|
|
|
uint64_t nClientID = static_cast<uint64_t>(pClient->GetHandle());
|
2023-08-31 00:16:25 +02:00
|
|
|
|
2022-09-15 23:13:37 +02:00
|
|
|
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-08-31 00:16:25 +02:00
|
|
|
SaveList();
|
2023-08-21 19:12:29 +02:00
|
|
|
Msg(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-08-21 19:12:29 +02:00
|
|
|
Msg(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();
|