Add working permban command

This commit is contained in:
momo5502 2016-08-18 13:26:05 +02:00
parent 464ea2f82b
commit 8e72395ab9
2 changed files with 89 additions and 7 deletions

View File

@ -6,7 +6,8 @@ namespace Components
bool Bans::IsBanned(Bans::Entry entry)
{
auto list = Bans::LoadBans();
Bans::BanList list;
Bans::LoadBans(&list);
if (entry.first.Bits)
{
@ -35,7 +36,8 @@ namespace Components
void Bans::InsertBan(Bans::Entry entry)
{
auto list = Bans::LoadBans();
Bans::BanList list;
Bans::LoadBans(&list);
Bans::AccessMutex.lock();
@ -75,20 +77,93 @@ namespace Components
}
}
// TODO: Write bans
std::vector<std::string> idVector;
std::vector<std::string> ipVector;
for (auto& idEntry : list.IDList)
{
idVector.push_back(fmt::sprintf("%llX", idEntry.Bits));
}
for (auto& ipEntry : list.IPList)
{
ipVector.push_back(fmt::sprintf("%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());
Bans::AccessMutex.unlock();
}
Bans::BanList Bans::LoadBans()
void Bans::LoadBans(Bans::BanList* list)
{
Bans::BanList list;
Bans::AccessMutex.lock();
// TODO: Read bans
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());
}
if (!list)
{
Bans::AccessMutex.unlock();
return;
}
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);
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());
}
}
}
}
}
Bans::AccessMutex.unlock();
return list;
}
void Bans::BanClientNum(int num, std::string reason)
@ -126,6 +201,13 @@ namespace Components
Bans::BanClientNum(atoi(params[1]), reason);
});
// Verify the list on startup
QuickPatch::Once([] ()
{
Bans::BanList list;
Bans::LoadBans(&list);
});
}
Bans::~Bans()

View File

@ -27,6 +27,6 @@ namespace Components
static std::mutex AccessMutex;
static BanList LoadBans();
static void LoadBans(BanList* list);
};
}