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-09-07 11:17:05 +02:00
|
|
|
pBuf[nRead] = '\0'; // Null terminate the string buffer containing our banned list.
|
2022-09-01 01:07:16 +02:00
|
|
|
|
2023-09-07 11:17:05 +02:00
|
|
|
rapidjson::Document document;
|
|
|
|
if (document.Parse(pBuf.get()).HasParseError())
|
2021-12-25 22:36:38 +01:00
|
|
|
{
|
2023-09-07 11:17:05 +02:00
|
|
|
Warning(eDLL_T::SERVER, "%s: JSON parse error at position %zu: %s\n",
|
|
|
|
__FUNCTION__, document.GetErrorOffset(), rapidjson::GetParseError_En(document.GetParseError()));
|
|
|
|
return;
|
|
|
|
}
|
2021-12-25 22:36:38 +01:00
|
|
|
|
2023-09-07 20:15:22 +01:00
|
|
|
if (!document.IsObject())
|
|
|
|
{
|
|
|
|
Warning(eDLL_T::SERVER, "%s: JSON root was not an object\n", __FUNCTION__);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2023-09-07 11:17:05 +02:00
|
|
|
uint64_t nTotalBans = 0;
|
|
|
|
if (document.HasMember("totalBans") && document["totalBans"].IsUint64())
|
|
|
|
{
|
|
|
|
nTotalBans = document["totalBans"].GetUint64();
|
|
|
|
}
|
2021-12-25 22:36:38 +01:00
|
|
|
|
2023-09-07 11:17:05 +02:00
|
|
|
for (uint64_t i = 0; i < nTotalBans; i++)
|
|
|
|
{
|
|
|
|
char idx[64]; _ui64toa(i, idx, 10);
|
|
|
|
|
|
|
|
if (document.HasMember(idx) && document[idx].IsObject())
|
2021-12-25 22:36:38 +01:00
|
|
|
{
|
2023-09-07 11:17:05 +02:00
|
|
|
const rapidjson::Value& entry = document[idx];
|
|
|
|
if (entry.HasMember("ipAddress") && entry["ipAddress"].IsString() &&
|
|
|
|
entry.HasMember("nucleusId") && entry["nucleusId"].IsUint64())
|
2021-12-25 22:36:38 +01:00
|
|
|
{
|
2023-08-31 00:16:25 +02:00
|
|
|
Banned_t banned;
|
2023-09-07 11:17:05 +02:00
|
|
|
banned.m_Address = entry["ipAddress"].GetString();
|
|
|
|
banned.m_NucleusID = entry["nucleusId"].GetUint64();
|
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-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
|
|
|
}
|
|
|
|
|
2023-09-07 11:17:05 +02:00
|
|
|
rapidjson::Document document;
|
|
|
|
document.SetObject();
|
2023-08-31 00:16:25 +02:00
|
|
|
|
2023-09-07 11:17:05 +02:00
|
|
|
rapidjson::Document::AllocatorType& allocator = document.GetAllocator();
|
2023-08-31 00:16:25 +02:00
|
|
|
|
2023-09-07 11:17:05 +02:00
|
|
|
FOR_EACH_VEC(m_BannedList, i)
|
|
|
|
{
|
|
|
|
const Banned_t& banned = m_BannedList[i];
|
|
|
|
char idx[64]; _ui64toa(i, idx, 10);
|
2022-09-01 01:07:16 +02:00
|
|
|
|
2023-09-07 11:17:05 +02:00
|
|
|
rapidjson::Value obj(rapidjson::kObjectType);
|
|
|
|
obj.AddMember("ipAddress", rapidjson::Value(banned.m_Address.String(), allocator), allocator);
|
|
|
|
obj.AddMember("nucleusId", banned.m_NucleusID, allocator);
|
2022-09-01 01:07:16 +02:00
|
|
|
|
2023-09-07 11:17:05 +02:00
|
|
|
document.AddMember(rapidjson::Value(idx, allocator), obj, allocator);
|
2022-09-01 01:07:16 +02:00
|
|
|
}
|
2021-12-25 22:36:38 +01:00
|
|
|
|
2023-09-07 11:17:05 +02:00
|
|
|
document.AddMember("totalBans", m_BannedList.Count(), allocator);
|
|
|
|
|
|
|
|
rapidjson::StringBuffer buffer;
|
|
|
|
JSON_DocumentToBufferDeserialize(document, buffer);
|
|
|
|
|
|
|
|
FileSystem()->Write(buffer.GetString(), buffer.GetSize(), pFile);
|
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();
|