From 7c768b941f687242fc2f8b53c06e26410ae040df Mon Sep 17 00:00:00 2001 From: momo5502 Date: Sat, 13 Oct 2018 17:37:37 +0200 Subject: [PATCH] [Bans] Add unbanclient command --- src/Components/Modules/Bans.cpp | 84 ++++++++++++++++++++++++++++++--- src/Components/Modules/Bans.hpp | 5 +- 2 files changed, 82 insertions(+), 7 deletions(-) diff --git a/src/Components/Modules/Bans.cpp b/src/Components/Modules/Bans.cpp index 49732d52..de2fe09c 100644 --- a/src/Components/Modules/Bans.cpp +++ b/src/Components/Modules/Bans.cpp @@ -2,7 +2,7 @@ namespace Components { - std::mutex Bans::AccessMutex; + std::recursive_mutex Bans::AccessMutex; bool Bans::IsBanned(Bans::Entry entry) { @@ -36,11 +36,11 @@ namespace Components void Bans::InsertBan(Bans::Entry entry) { + std::lock_guard _(Bans::AccessMutex); + Bans::BanList list; Bans::LoadBans(&list); - std::lock_guard _(Bans::AccessMutex); - if (entry.first.bits) { bool found = false; @@ -77,15 +77,22 @@ namespace Components } } + Bans::SaveBans(&list); + } + + void Bans::SaveBans(BanList* list) + { + std::lock_guard _(Bans::AccessMutex); + std::vector idVector; std::vector ipVector; - for (auto& idEntry : list.idList) + for (auto& idEntry : list->idList) { idVector.push_back(Utils::String::VA("%llX", idEntry.bits)); } - for (auto& ipEntry : list.ipList) + for (auto& ipEntry : list->ipList) { ipVector.push_back(Utils::String::VA("%u.%u.%u.%u", ipEntry.bytes[0] & 0xFF, @@ -106,7 +113,7 @@ namespace Components void Bans::LoadBans(Bans::BanList* list) { - std::lock_guard _(Bans::AccessMutex); + std::lock_guard _(Bans::AccessMutex); FileSystem::File bans("bans.json"); @@ -181,6 +188,46 @@ namespace Components Game::SV_KickClientError(client, reason); } + void Bans::UnbanClient(SteamID id) + { + std::lock_guard _(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 _(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); + } + Bans::Bans() { Command::Add("banclient", [](Command::Params* params) @@ -193,6 +240,31 @@ namespace Components Bans::BanClientNum(atoi(params->get(1)), reason); }); + 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)); + } + }); + // Verify the list on startup Scheduler::Once([]() { diff --git a/src/Components/Modules/Bans.hpp b/src/Components/Modules/Bans.hpp index 6482b896..8666d5d0 100644 --- a/src/Components/Modules/Bans.hpp +++ b/src/Components/Modules/Bans.hpp @@ -11,6 +11,8 @@ namespace Components ~Bans(); static void BanClientNum(int num, std::string reason); + static void UnbanClient(SteamID id); + static void UnbanClient(Game::netIP_t ip); static bool IsBanned(Entry entry); static void InsertBan(Entry entry); @@ -23,7 +25,8 @@ namespace Components std::vector ipList; }; - static std::mutex AccessMutex; + static std::recursive_mutex AccessMutex; static void LoadBans(BanList* list); + static void SaveBans(BanList* list); }; }