Merge pull request #654 from diamante0018/develop

[RCon]: Tidy up code
This commit is contained in:
Edo 2022-12-17 10:27:38 +01:00 committed by GitHub
commit b9f5aa7eae
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 52 additions and 0 deletions

View File

@ -2,6 +2,8 @@
namespace Components
{
std::unordered_map<std::uint32_t, int> RCon::RateLimit;
RCon::Container RCon::RconContainer;
Utils::Cryptography::ECC::Key RCon::RconKey;
@ -71,6 +73,43 @@ namespace Components
});
}
bool RCon::RateLimitCheck(const Network::Address& address, const int time)
{
auto ip = address.getIP();
if (!RateLimit.contains(ip.full))
{
RateLimit[ip.full] = 0;
}
const auto lastTime = RateLimit[ip.full];
// Only one request every 500ms
if (lastTime && (time - lastTime) < 500)
{
return false; // Flooding
}
RateLimit[ip.full] = time;
return true;
}
void RCon::RateLimitCleanup(const int time)
{
for (auto i = RateLimit.begin(); i != RateLimit.end();)
{
// No longer at risk of flooding, remove
if ((time - i->second) > 500)
{
i = RateLimit.erase(i);
}
else
{
++i;
}
}
}
RCon::RCon()
{
AddCommands();
@ -126,6 +165,14 @@ namespace Components
Network::OnClientPacket("rcon", [](const Network::Address& address, [[maybe_unused]] const std::string& data)
{
const auto time = Game::Sys_Milliseconds();
if (!RateLimitCheck(address, time))
{
return;
}
RateLimitCleanup(time);
std::string data_ = data;
Utils::String::Trim(data_);

View File

@ -29,6 +29,8 @@ namespace Components
static Utils::Cryptography::ECC::Key GetKeyInternal();
};
static std::unordered_map<std::uint32_t, int> RateLimit;
static Container RconContainer;
static Utils::Cryptography::ECC::Key RconKey;
@ -38,5 +40,8 @@ namespace Components
static Dvar::Var RconLogRequests;
static void AddCommands();
static bool RateLimitCheck(const Network::Address& address, int time);
static void RateLimitCleanup(int time);
};
}