diff options
author | MadMaurice <madmaurice@zom.bi> | 2018-08-30 16:08:01 +0300 |
---|---|---|
committer | MadMaurice <madmaurice@zom.bi> | 2018-08-30 20:53:05 +0300 |
commit | 0daec57f5cfc4225aa4527b537b4ec4fbbc35635 (patch) | |
tree | d4f9329d8b150a4ff7115988bc57d60e607b3c67 /src/murmur/ServerUser.h | |
parent | f672eddc4f4849a9ad518ced43d0b5a327d8a45e (diff) |
Prevent instability and crash due to message flood
This patch adds a rate limiting to selected patches. The underlying rate limiter
used is the Leaky-Bucket algorithm. It allows for a burst of messages, but
limits them after a specified amount of messages within a time frame.
Diffstat (limited to 'src/murmur/ServerUser.h')
-rw-r--r-- | src/murmur/ServerUser.h | 29 |
1 files changed, 29 insertions, 0 deletions
diff --git a/src/murmur/ServerUser.h b/src/murmur/ServerUser.h index 28e582739..0a3828205 100644 --- a/src/murmur/ServerUser.h +++ b/src/murmur/ServerUser.h @@ -14,6 +14,13 @@ #include <winsock2.h> #endif +// <chrono> was introduced in C++11 +#if __cplusplus > 199711LL +#include <chrono> +#else +#include <ctime> +#endif + #include "Connection.h" #include "Timer.h" #include "User.h" @@ -55,6 +62,26 @@ struct WhisperTarget { class Server; +#if __cplusplus > 199711L + typedef std::chrono::time_point<std::chrono::steady_clock> time_point; +#else + typedef clock_t time_point; +#endif + +// Simple algorithm for rate limiting +class LeakyBucket { + private: + unsigned int tokensPerSec, maxTokens; + long currentTokens; + time_point lastUpdate; + + public: + // Returns true if packets should be dropped + bool ratelimit(int tokens); + + LeakyBucket(); +}; + class ServerUser : public Connection, public User { private: Q_OBJECT @@ -103,6 +130,8 @@ class ServerUser : public Connection, public User { QMap<int, TargetCache> qmTargetCache; QMap<QString, QString> qmWhisperRedirect; + LeakyBucket leakyBucket; + int iLastPermissionCheck; QMap<int, unsigned int> qmPermissionSent; #ifdef Q_OS_UNIX |