wfRateLimit.php
7.67 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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
<?php
class wfRateLimit {
const TYPE_GLOBAL = 'global';
const TYPE_CRAWLER_VIEWS = 'crawler-views';
const TYPE_CRAWLER_404S = 'crawler-404s';
const TYPE_HUMAN_VIEWS = 'human-views';
const TYPE_HUMAN_404S = 'human-404s';
const HIT_TYPE_404 = '404';
const HIT_TYPE_NORMAL = 'hit';
const VISITOR_TYPE_HUMAN = 'human';
const VISITOR_TYPE_CRAWLER = 'crawler';
protected $_type;
protected static $_hitCount = false;
public static function table() {
return wfDB::networkTable('wfTrafficRates');
}
public static function trimData() {
$wfdb = wfDB::shared();
$table = self::table();
$wfdb->queryWrite("DELETE FROM {$table} WHERE eMin < FLOOR((UNIX_TIMESTAMP() - 60) / 60)");
}
public static function globalRateLimit() {
static $_cachedGlobal = null;
if ($_cachedGlobal === null) {
$_cachedGlobal = new wfRateLimit(self::TYPE_GLOBAL);
}
return $_cachedGlobal;
}
public static function crawlerViewsRateLimit() {
static $_cachedCrawlerViews = null;
if ($_cachedCrawlerViews === null) {
$_cachedCrawlerViews = new wfRateLimit(self::TYPE_CRAWLER_VIEWS);
}
return $_cachedCrawlerViews;
}
public static function crawler404sRateLimit() {
static $_cachedCrawler404s = null;
if ($_cachedCrawler404s === null) {
$_cachedCrawler404s = new wfRateLimit(self::TYPE_CRAWLER_404S);
}
return $_cachedCrawler404s;
}
public static function humanViewsRateLimit() {
static $_cachedHumanViews = null;
if ($_cachedHumanViews === null) {
$_cachedHumanViews = new wfRateLimit(self::TYPE_HUMAN_VIEWS);
}
return $_cachedHumanViews;
}
public static function human404sRateLimit() {
static $_cachedHuman404s = null;
if ($_cachedHuman404s === null) {
$_cachedHuman404s = new wfRateLimit(self::TYPE_HUMAN_404S);
}
return $_cachedHuman404s;
}
/**
* Returns whether or not humans and bots have the same rate limits configured.
*
* @return bool
*/
public static function identicalHumanBotRateLimits() {
$humanViews = self::humanViewsRateLimit();
$crawlerViews = self::crawlerViewsRateLimit();
if ($humanViews->isEnabled() != $crawlerViews->isEnabled()) {
return false;
}
if ($humanViews->limit() != $crawlerViews->limit()) {
return false;
}
$human404s = self::human404sRateLimit();
$crawler404s = self::crawler404sRateLimit();
if ($human404s->isEnabled() != $crawler404s->isEnabled()) {
return false;
}
if ($human404s->limit() != $crawler404s->limit()) {
return false;
}
return true;
}
public static function mightRateLimit($hitType) {
if (!wfConfig::get('firewallEnabled')) {
return false;
}
$IP = wfUtils::getIP();
if (wfBlock::isWhitelisted($IP)) {
return false;
}
if (wfConfig::get('neverBlockBG') == 'neverBlockUA' && wfCrawl::isGoogleCrawler()) {
return false;
}
if (wfConfig::get('neverBlockBG') == 'neverBlockVerified' && wfCrawl::isVerifiedGoogleCrawler()) {
return false;
}
if ($hitType == '404') {
$allowed404s = wfConfig::get('allowed404s');
if (is_string($allowed404s)) {
$allowed404s = array_filter(preg_split("/[\r\n]+/", $allowed404s));
$allowed404sPattern = '';
foreach ($allowed404s as $allowed404) {
$allowed404sPattern .= preg_replace('/\\\\\*/', '.*?', preg_quote($allowed404, '/')) . '|';
}
$uri = $_SERVER['REQUEST_URI'];
if (($index = strpos($uri, '?')) !== false) {
$uri = substr($uri, 0, $index);
}
if ($allowed404sPattern && preg_match('/^' . substr($allowed404sPattern, 0, -1) . '$/i', $uri)) {
return false;
}
}
}
if (self::globalRateLimit()->isEnabled()) {
return true;
}
$visitorType = self::visitorType();
if ($visitorType == self::VISITOR_TYPE_CRAWLER) {
if ($hitType == self::HIT_TYPE_NORMAL) {
if (self::crawlerViewsRateLimit()->isEnabled()) {
return true;
}
}
else {
if (self::crawler404sRateLimit()->isEnabled()) {
return true;
}
}
}
else {
if ($hitType == self::HIT_TYPE_NORMAL) {
if (self::humanViewsRateLimit()->isEnabled()) {
return true;
}
}
else {
if (self::human404sRateLimit()->isEnabled()) {
return true;
}
}
}
return false;
}
public static function countHit($hitType, $ip) {
$table = self::table();
wfDB::shared()->queryWrite("INSERT INTO {$table} (eMin, IP, hitType, hits) VALUES (FLOOR(UNIX_TIMESTAMP() / 60), %s, %s, @wfcurrenthits := 1) ON DUPLICATE KEY UPDATE hits = IF(@wfcurrenthits := hits + 1, hits + 1, hits + 1)", wfUtils::inet_pton($ip), $hitType);
}
/**
* Returns one of the VISITOR_TYPE_ constants for the purposes of determining which rate limit to apply.
*
* @return string
*/
public static function visitorType() {
static $_cachedVisitorType = null;
if ($_cachedVisitorType === null) {
$_cachedVisitorType = ((isset($_SERVER['HTTP_USER_AGENT']) && wfCrawl::isCrawler($_SERVER['HTTP_USER_AGENT'])) || empty($_SERVER['HTTP_USER_AGENT']) ? wfRateLimit::VISITOR_TYPE_CRAWLER : wfRateLimit::VISITOR_TYPE_HUMAN);
}
return $_cachedVisitorType;
}
protected function __construct($type) {
$this->_type = $type;
}
/**
* Returns whether or not this rate limit is configured in a way where it would run.
*
* @return bool
*/
public function isEnabled() {
switch ($this->_type) {
case self::TYPE_GLOBAL:
return wfConfig::get('maxGlobalRequests') != 'DISABLED' && wfConfig::getInt('maxGlobalRequests') > 0;
case self::TYPE_CRAWLER_VIEWS:
return wfConfig::get('maxRequestsCrawlers') != 'DISABLED' && wfConfig::getInt('maxRequestsCrawlers') > 0;
case self::TYPE_CRAWLER_404S:
return wfConfig::get('max404Crawlers') != 'DISABLED' && wfConfig::getInt('max404Crawlers') > 0;
case self::TYPE_HUMAN_VIEWS:
return wfConfig::get('maxRequestsHumans') != 'DISABLED' && wfConfig::getInt('maxRequestsHumans') > 0;
case self::TYPE_HUMAN_404S:
return wfConfig::get('max404Humans') != 'DISABLED' && wfConfig::getInt('max404Humans') > 0;
}
return true;
}
public function limit() {
switch ($this->_type) {
case self::TYPE_GLOBAL:
return wfConfig::getInt('maxGlobalRequests');
case self::TYPE_CRAWLER_VIEWS:
return wfConfig::getInt('maxRequestsCrawlers');
case self::TYPE_CRAWLER_404S:
return wfConfig::getInt('max404Crawlers');
case self::TYPE_HUMAN_VIEWS:
return wfConfig::getInt('maxRequestsHumans');
case self::TYPE_HUMAN_404S:
return wfConfig::getInt('max404Humans');
}
return -1;
}
public function shouldEnforce($hitType) {
switch ($this->_type) {
case self::TYPE_GLOBAL:
return $this->isEnabled() && $this->_hitCount() > max(wfConfig::getInt('maxGlobalRequests'), 1);
case self::TYPE_CRAWLER_VIEWS:
return self::visitorType() == self::VISITOR_TYPE_CRAWLER && $hitType == self::HIT_TYPE_NORMAL && $this->isEnabled() && $this->_hitCount() > wfConfig::getInt('maxRequestsCrawlers');
case self::TYPE_CRAWLER_404S:
return self::visitorType() == self::VISITOR_TYPE_CRAWLER && $hitType == self::HIT_TYPE_404 && $this->isEnabled() && $this->_hitCount() > wfConfig::getInt('max404Crawlers');
case self::TYPE_HUMAN_VIEWS:
return self::visitorType() == self::VISITOR_TYPE_HUMAN && $hitType == self::HIT_TYPE_NORMAL && $this->isEnabled() && $this->_hitCount() > wfConfig::getInt('maxRequestsHumans');
case self::TYPE_HUMAN_404S:
return self::visitorType() == self::VISITOR_TYPE_HUMAN && $hitType == self::HIT_TYPE_404 && $this->isEnabled() && $this->_hitCount() > wfConfig::getInt('max404Humans');
}
return false;
}
/**
* Returns the hit count corresponding to the current request type.
*
* @return int
*/
protected function _hitCount() {
if (self::$_hitCount === false) {
self::$_hitCount = (int) wfDB::shared()->querySingle("SELECT @wfcurrenthits");
}
return self::$_hitCount;
}
}