comparison server/flood_check.h @ 0:a4671277546c tip

created the repository for the thymian project
author ferencd
date Tue, 17 Aug 2021 11:19:54 +0200
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:a4671277546c
1 #ifndef FLOOD_CHECK
2 #define FLOOD_CHECK
3
4 #include <string>
5 #include <map>
6 #include <chrono>
7 #include <mutex>
8
9 /**
10 * @brief The flood_check class will check if there is a flood attempt from some specific
11 * IP. The following is the method: If it can identify that in a given second a given IP
12 * has tried more than 100 (configurable) requests it will reject the requests without
13 * going further.
14 */
15 class flood_check
16 {
17 public:
18
19 static void attempt(std::string ip);
20
21 private:
22
23 static std::mutex locker;
24
25 // holds the flood attempts and when they have first occured
26 struct count_started
27 {
28 int count = 0;
29 std::time_t first_time = std::chrono::duration_cast<std::chrono::seconds>(std::chrono::system_clock::now().time_since_epoch()).count();
30 };
31
32 // structures used to determine the flood rate / second
33 using flood_map = std::map<std::string, int>;
34 static std::map<std::time_t, flood_map> floods;
35
36 // holds the flood attempts of a host. For each IP we have an attempt counter
37 // how many times it tried to flood us and when the last attempt happened
38 static std::map<std::string, count_started> hostd_flood;
39
40 // holds the evil hosts that are flooding the system and how long they are locked out
41 // the content of this map is determined based on the content of the hostd_flood
42 // map. If a count from there reaches over 10 and the first_time of the
43 // entry is < 1 minute then we consider the host flooding us.
44 static std::map<std::string, std::time_t> rejected_hosts;
45
46 // holds how many times the given host was suspended. Each suspension will give another minute to the host
47 static std::map<std::string, int> suspension_times;
48 };
49
50 #endif // FLOOD_CHECK
51