iw4x-client/src/Components/Modules/Bans.cpp

283 lines
5.1 KiB
C++
Raw Normal View History

2022-02-27 07:53:44 -05:00
#include <STDInclude.hpp>
2017-01-19 16:23:59 -05:00
namespace Components
{
2018-10-13 11:37:37 -04:00
std::recursive_mutex Bans::AccessMutex;
2017-01-19 16:23:59 -05:00
bool Bans::IsBanned(Bans::Entry entry)
{
2021-12-17 13:49:25 -05:00
std::lock_guard<std::recursive_mutex> _(Bans::AccessMutex);
2017-01-19 16:23:59 -05:00
Bans::BanList list;
Bans::LoadBans(&list);
if (entry.first.bits)
2017-01-19 16:23:59 -05:00
{
for (auto& idEntry : list.idList)
{
if (idEntry.bits == entry.first.bits)
2017-01-19 16:23:59 -05:00
{
return true;
}
}
}
if (entry.second.full)
{
for (auto& ipEntry : list.ipList)
{
if (ipEntry.full == entry.second.full)
{
return true;
}
}
}
return false;
}
void Bans::InsertBan(Bans::Entry entry)
{
2018-10-13 11:37:37 -04:00
std::lock_guard<std::recursive_mutex> _(Bans::AccessMutex);
2017-01-19 16:23:59 -05:00
Bans::BanList list;
Bans::LoadBans(&list);
if (entry.first.bits)
2017-01-19 16:23:59 -05:00
{
bool found = false;
for (auto& idEntry : list.idList)
{
if (idEntry.bits == entry.first.bits)
2017-01-19 16:23:59 -05:00
{
found = true;
break;
}
}
if (!found)
{
list.idList.push_back(entry.first);
}
}
if (entry.second.full)
{
bool found = false;
for (auto& ipEntry : list.ipList)
{
if (ipEntry.full == entry.second.full)
{
found = true;
break;
}
}
if (!found)
{
list.ipList.push_back(entry.second);
}
}
2018-10-13 11:37:37 -04:00
Bans::SaveBans(&list);
}
void Bans::SaveBans(BanList* list)
{
std::lock_guard<std::recursive_mutex> _(Bans::AccessMutex);
2017-01-19 16:23:59 -05:00
std::vector<std::string> idVector;
std::vector<std::string> ipVector;
2018-10-13 11:37:37 -04:00
for (auto& idEntry : list->idList)
2017-01-19 16:23:59 -05:00
{
idVector.push_back(Utils::String::VA("%llX", idEntry.bits));
2017-01-19 16:23:59 -05:00
}
2018-10-13 11:37:37 -04:00
for (auto& ipEntry : list->ipList)
2017-01-19 16:23:59 -05:00
{
ipVector.push_back(Utils::String::VA("%u.%u.%u.%u",
ipEntry.bytes[0] & 0xFF,
ipEntry.bytes[1] & 0xFF,
ipEntry.bytes[2] & 0xFF,
ipEntry.bytes[3] & 0xFF));
}
json11::Json bans = json11::Json::object
{
{ "ip", ipVector },
{ "id", idVector },
};
FileSystem::FileWriter ban("bans.json");
ban.write(bans.dump());
}
void Bans::LoadBans(Bans::BanList* list)
{
2018-10-13 11:37:37 -04:00
std::lock_guard<std::recursive_mutex> _(Bans::AccessMutex);
2017-01-19 16:23:59 -05:00
FileSystem::File bans("bans.json");
if (bans.exists())
{
std::string error;
json11::Json banData = json11::Json::parse(bans.getBuffer(), error);
if (!error.empty())
{
Logger::Error("Failed to parse bans (bans.json): %s", error.data());
}
2017-02-28 13:58:03 -05:00
if (!list) return;
2017-01-19 16:23:59 -05:00
if (banData.is_object())
{
auto idList = banData["id"];
auto ipList = banData["ip"];
if (idList.is_array())
{
for (auto &idEntry : idList.array_items())
{
if (idEntry.is_string())
{
SteamID id;
id.bits = strtoull(idEntry.string_value().data(), nullptr, 16);
2017-01-19 16:23:59 -05:00
list->idList.push_back(id);
}
}
}
if (ipList.is_array())
{
for (auto &ipEntry : ipList.array_items())
{
if (ipEntry.is_string())
{
Network::Address addr(ipEntry.string_value());
list->ipList.push_back(addr.getIP());
}
}
}
}
}
}
2018-12-17 08:29:18 -05:00
void Bans::BanClientNum(int num, const std::string& reason)
2017-01-19 16:23:59 -05:00
{
if (!Dvar::Var("sv_running").get<bool>())
{
Logger::Print("Server is not running.\n");
return;
}
if (*Game::svs_numclients <= num)
{
Logger::Print("Player %d is not on the server\n", num);
return;
}
Game::client_t* client = &Game::svs_clients[num];
SteamID guid;
2021-09-08 05:19:30 -04:00
guid.bits = client->steamID;
2017-01-19 16:23:59 -05:00
2021-09-08 05:19:30 -04:00
Bans::InsertBan({ guid, client->netchan.remoteAddress.ip });
2017-01-19 16:23:59 -05:00
Game::SV_KickClientError(client, reason);
}
2018-10-13 11:37:37 -04:00
void Bans::UnbanClient(SteamID id)
{
std::lock_guard<std::recursive_mutex> _(Bans::AccessMutex);
Bans::BanList list;
Bans::LoadBans(&list);
auto entry = std::find_if(list.idList.begin(), list.idList.end(), [&id](SteamID& entry)
{
return id.bits == entry.bits;
});
if (entry != list.idList.end())
{
list.idList.erase(entry);
}
Bans::SaveBans(&list);
}
void Bans::UnbanClient(Game::netIP_t ip)
{
std::lock_guard<std::recursive_mutex> _(Bans::AccessMutex);
Bans::BanList list;
Bans::LoadBans(&list);
auto entry = std::find_if(list.ipList.begin(), list.ipList.end(), [&ip](Game::netIP_t& entry)
{
return ip.full == entry.full;
});
if (entry != list.ipList.end())
{
list.ipList.erase(entry);
}
Bans::SaveBans(&list);
}
2017-01-19 16:23:59 -05:00
Bans::Bans()
{
Command::Add("banclient", [](Command::Params* params)
2017-01-19 16:23:59 -05:00
{
if (params->length() < 2) return;
std::string reason = "EXE_ERR_BANNED_PERM";
if (params->length() >= 3) reason = params->join(2);
2017-01-19 16:23:59 -05:00
Bans::BanClientNum(atoi(params->get(1)), reason);
});
2018-10-13 11:37:37 -04:00
Command::Add("unbanclient", [](Command::Params* params)
{
if (params->length() < 2) return;
std::string type = params->get(1);
if (type == "ip"s)
{
Network::Address address(params->get(2));
Bans::UnbanClient(address.getIP());
Logger::Print("Unbanned IP %s\n", params->get(2));
}
else if (type == "guid"s)
{
SteamID id;
id.bits = strtoull(params->get(2), nullptr, 16);
Bans::UnbanClient(id);
Logger::Print("Unbanned GUID %s\n", params->get(2));
}
});
2017-01-19 16:23:59 -05:00
// Verify the list on startup
Scheduler::Once([]()
2017-01-19 16:23:59 -05:00
{
Bans::BanList list;
Bans::LoadBans(&list);
});
}
Bans::~Bans()
{
}
}