-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathRateLimiter.class.php
More file actions
executable file
·169 lines (154 loc) · 3.8 KB
/
RateLimiter.class.php
File metadata and controls
executable file
·169 lines (154 loc) · 3.8 KB
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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
<?php
require_once('util.php');
require_once('RedisDAO.class.php');
/*
* limit request rate, prevent spam
* normally used in fields such as login, register
* based on fatigue degree
*
* fatigue degree grow algorithm rule, call increase($degree)
* rule format
* array(
* 'degree'=>100, //min point to reach this punishment
* 'interval'=>300 //count interval, degree will expire after interval
* )
*
* rules are stored in redis db
* will auto grow by fatigue degree, call setAutoIncrease($bool)
* punish directly, call punish($arr)
*/
class RateLimiter
{
private static $keyPrefix = 'rl';
private static $id = '';
private static $rules = array();
/*
* @param $key customize your own key, default is ip2long(ip)
*/
public static function configure(CRObject $config)
{
self::$keyPrefix = $config->get('key_prefix', self::$keyPrefix);
self::$id = $config->get('id', cr_get_client_ip());
self::$rules = $config->get('rules', self::$rules);
}
/*
* @param
*
*/
public static function increase($degree)
{
if (!is_numeric($degree)) {
return false;
}
$lua_script = <<<LUA
local degree = redis.call('incrby', KEYS[1], ARGV[1])
if degree == tonumber(ARGV[1]) then
redis.call('expire', KEYS[1], ARGV[2])
end
return degree
LUA;
$redis = RedisDAO::instance();
if ($redis === null) {
return false;
}
foreach (self::$rules as $rule) {
$interval = $rule['interval'];
$key = self::$keyPrefix . ':degree:' . self::$id . '-' . $interval;
$redis->eval($lua_script, 1, $key, $degree, $interval);
}
$redis->disconnect();
$rule = self::whichRuleToPunish();
if ($rule !== null) {
self::punish($rule);
}
return true;
}
/**/
public static function punish($rule, $id = null)
{
if ($id === null) {
$id = self::$id;
}
$redis = RedisDAO::instance();
if ($redis === null) {
return false;
}
$redis->set(self::$keyPrefix . ':punishing:' . $id, $rule['degree'], 'EX', $rule['interval']);
$redis->disconnect();
return true;
}
/*
* get punish time left, negative means not being punished
*/
public static function getFreezeTime($id = null)
{
if ($id === null) {
$id = self::$id;
}
$redis = RedisDAO::instance();
if ($redis === null) {
return 0;
}
$freezeTime = (int)$redis->ttl(self::$keyPrefix . ':punishing:' . $id);
$redis->disconnect();
return $freezeTime;
}
/*
* get which rule to punish current user
* mostly of the time, you dont have to call this, as it is called automatically
*/
private static function whichRuleToPunish()
{
$redis = RedisDAO::instance();
if ($redis === null) {
return null;
}
foreach (self::$rules as $rule) {
$interval = $rule['interval'];
$key = self::$keyPrefix . ':degree:' . self::$id . '-' . $interval;
$degree = (int)$redis->get($key);
if ($degree > $rule['degree']) {
$redis->disconnect();
return $rule;
}
}
$redis->disconnect();
return null;
}
/* clear degree count and punishing state */
public static function clear($id = null)
{
if ($id === null) {
$id = self::$id;
}
$redis = RedisDAO::instance();
if ($redis === null) {
return null;
}
foreach (self::$rules as $rule) {
$interval = $rule['interval'];
$key = self::$keyPrefix . ':degree:' . $id . '-' . $interval;
$redis->del(array($key));
}
$redis->del(array(self::$keyPrefix . ':punishing:' . $id));
$redis->disconnect();
return true;
}
/* list IDs being punished */
public static function listPunished()
{
$redis = RedisDAO::instance();
if ($redis === null) {
return false;
}
$redis_key = self::$keyPrefix . ':punishing:*';
$list = $redis->keys($redis_key);
$redis->disconnect();
$len = strlen(self::$keyPrefix . ':punishing:');
$ids = array();
foreach ($list as $item) {
$ids[]['id'] = mb_substr($item, $len);
}
return $ids;
}
}