forked from jselbie/stunserver
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathratelimiter.cpp
145 lines (112 loc) · 3.29 KB
/
ratelimiter.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
#include "commonincludes.hpp"
#include "socketaddress.h"
#include "fasthash.h"
#include "ratelimiter.h"
RateLimiter::RateLimiter(size_t tablesize, bool isUsingLock)
{
_table.InitTable(tablesize, tablesize/2);
this->_isUsingLock = isUsingLock;
pthread_mutex_init(&_mutex, NULL);
}
RateLimiter::~RateLimiter()
{
pthread_mutex_destroy(&_mutex);
}
time_t RateLimiter::get_time()
{
return time(NULL);
}
uint64_t RateLimiter::get_rate(const RateTracker* pRT)
{
if (pRT->count < MIN_COUNT_FOR_CONSIDERATION)
{
return 0;
}
time_t seconds = 1;
if (pRT->lastEntryTime > pRT->firstEntryTime)
{
seconds = pRT->lastEntryTime - pRT->firstEntryTime;
}
uint64_t rate = (pRT->count * 3600) / seconds;
return rate;
}
bool RateLimiter::RateCheck(const CSocketAddress& addr)
{
if (_isUsingLock)
{
pthread_mutex_lock(&_mutex);
}
bool result = RateCheckImpl(addr);
if (_isUsingLock)
{
pthread_mutex_unlock(&_mutex);
}
return result;
}
bool RateLimiter::RateCheckImpl(const CSocketAddress& addr)
{
RateTrackerAddress rtaddr;
addr.GetIP(rtaddr.addrbytes, sizeof(rtaddr.addrbytes));
time_t currentTime = get_time();
RateTracker* pRT = this->_table.Lookup(rtaddr);
uint64_t rate = 0;
// handle these cases differently:
// 1. Not in the table
// Insert into table
// if table is full, reset the table and re-insert
// return true
// 2. In the penalty box
// Check for parole eligibility
// otherwise, return false
// 3. Not in the penalty box.
// Remove from table if his previous record is sufficiently old (and return true)
//
if (pRT == NULL)
{
RateTracker rt;
rt.count = 1;
rt.firstEntryTime = currentTime;
rt.lastEntryTime = rt.firstEntryTime;
rt.penaltyTime = 0;
int result = _table.Insert(rtaddr, rt);
if (result == -1)
{
// the table is full - try again after dumping the table
// I've considered a half dozen alternatives to doing this, but this is the simplest
_table.Reset();
_table.Insert(rtaddr, rt);
}
return true;
}
pRT->count++;
pRT->lastEntryTime = currentTime;
rate = get_rate(pRT);
if (pRT->penaltyTime != 0)
{
if (pRT->penaltyTime >= currentTime)
{
return false;
}
if (rate < MAX_RATE)
{
pRT->penaltyTime = 0;
// not reseting firstEntryTime or lastEntryTime.
return true;
}
// he's out of penalty, but is still flooding us. Tack on another hour
pRT->penaltyTime = currentTime + PENALTY_TIME_SECONDS;
return false;
}
if (rate >= MAX_RATE)
{
pRT->penaltyTime = currentTime + PENALTY_TIME_SECONDS; // welcome to the penalty box
return false;
}
if ((pRT->lastEntryTime - pRT->firstEntryTime) > RESET_INTERVAL_SECONDS)
{
// he's been a good citizen this whole time, we can take him out of the table
// to save room for another entry
_table.Remove(rtaddr);
}
return true;
}