mirror of
https://git.neolegacy.dev/neoStudiosLCE/neoLegacy.git
synced 2026-12-02 22:18:09 +01:00
48 lines
1.1 KiB
C++
48 lines
1.1 KiB
C++
#pragma once
|
|
|
|
#include <string>
|
|
#include <deque>
|
|
#include <unordered_map>
|
|
|
|
#if defined(_WINDOWS64) || defined(__linux__)
|
|
#include <Windows.h>
|
|
#endif
|
|
|
|
namespace ServerRuntime
|
|
{
|
|
namespace Security
|
|
{
|
|
class RateLimiter
|
|
{
|
|
public:
|
|
RateLimiter();
|
|
~RateLimiter();
|
|
|
|
RateLimiter(const RateLimiter &) = delete;
|
|
RateLimiter &operator=(const RateLimiter &) = delete;
|
|
RateLimiter(RateLimiter &&) = delete;
|
|
RateLimiter &operator=(RateLimiter &&) = delete;
|
|
|
|
/**
|
|
* Returns true if the connection from this IP should be allowed.
|
|
* Returns false if the IP has exceeded maxPerWindow connections within windowMs milliseconds.
|
|
*/
|
|
bool AllowConnection(const std::string &ip, int maxPerWindow, int windowMs);
|
|
|
|
/**
|
|
* Removes stale entries older than evictionAgeMs from the tracking map.
|
|
*/
|
|
void EvictStale(int evictionAgeMs = 300000);
|
|
|
|
private:
|
|
CRITICAL_SECTION m_lock;
|
|
std::unordered_map<std::string, std::deque<ULONGLONG>> m_connectionTimes;
|
|
};
|
|
|
|
/**
|
|
* Global rate limiter instance for the dedicated server accept loop.
|
|
*/
|
|
RateLimiter &GetGlobalRateLimiter();
|
|
}
|
|
}
|