dont ratelimit tor users
This commit is contained in:
+64
-52
@@ -9,73 +9,85 @@ class bot_protection{
|
|||||||
// ratelimit requests per IP
|
// ratelimit requests per IP
|
||||||
if(config::RATELIMIT_IP_SOURCE === false){
|
if(config::RATELIMIT_IP_SOURCE === false){
|
||||||
|
|
||||||
$source = "r" . $_SERVER["REMOTE_ADDR"];
|
$source = $_SERVER["REMOTE_ADDR"];
|
||||||
}else{
|
}else{
|
||||||
|
|
||||||
$source = "r" . $_SERVER[explode(";", strtoupper(str_replace("-", "_", config::IP_SOURCE)))[0]];
|
$source = $_SERVER[explode(";", strtoupper(str_replace("-", "_", config::IP_SOURCE)))[0]];
|
||||||
}
|
}
|
||||||
|
|
||||||
// counter
|
// check if its not a local IP. Probably local tor bridge, dont ratelimit
|
||||||
// resets on every req
|
|
||||||
$counter = apcu_fetch($source);
|
|
||||||
$counter = (int)$counter + 1;
|
|
||||||
|
|
||||||
if(
|
if(
|
||||||
(
|
filter_var(
|
||||||
CONFIG::RATELIMIT_PUNISH !== 0 &&
|
$source,
|
||||||
$counter > config::RATELIMIT_BURST + 1
|
FILTER_VALIDATE_IP,
|
||||||
) &&
|
FILTER_FLAG_NO_PRIV_RANGE | FILTER_FLAG_NO_RES_RANGE
|
||||||
(
|
) !== false
|
||||||
(
|
|
||||||
$output === false &&
|
|
||||||
CONFIG::RATELIMIT_PUNISH_API === true
|
|
||||||
) ||
|
|
||||||
$output === true
|
|
||||||
)
|
|
||||||
){
|
){
|
||||||
|
|
||||||
$block_time = config::RATELIMIT_TIME + (($counter - 1) * config::RATELIMIT_PUNISH);
|
$source = "r." . $source;
|
||||||
}else{
|
|
||||||
|
|
||||||
$block_time = config::RATELIMIT_TIME;
|
// counter
|
||||||
}
|
// resets on every req
|
||||||
|
$counter = apcu_fetch($source);
|
||||||
|
$counter = (int)$counter + 1;
|
||||||
|
|
||||||
apcu_store($source, $counter, $block_time);
|
if(
|
||||||
|
(
|
||||||
|
CONFIG::RATELIMIT_PUNISH !== 0 &&
|
||||||
|
$counter > config::RATELIMIT_BURST + 1
|
||||||
|
) &&
|
||||||
|
(
|
||||||
|
(
|
||||||
|
$output === false &&
|
||||||
|
CONFIG::RATELIMIT_PUNISH_API === true
|
||||||
|
) ||
|
||||||
|
$output === true
|
||||||
|
)
|
||||||
|
){
|
||||||
|
|
||||||
if($counter > config::RATELIMIT_BURST){
|
$block_time = config::RATELIMIT_TIME + (($counter - 1) * config::RATELIMIT_PUNISH);
|
||||||
|
}else{
|
||||||
|
|
||||||
apcu_inc(intdiv(time(), 3600) . ".bot_requests", 1, $s, 262800);
|
$block_time = config::RATELIMIT_TIME;
|
||||||
|
|
||||||
if($output === false){
|
|
||||||
|
|
||||||
http_response_code(429);
|
|
||||||
echo json_encode([
|
|
||||||
"status" => "You love 4get too much. Try again in " . $this->format_time($block_time) . "."
|
|
||||||
]);
|
|
||||||
die();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
$payload = [
|
apcu_store($source, $counter, $block_time);
|
||||||
"timetaken" => microtime(true),
|
|
||||||
"class" => "",
|
|
||||||
"right-left" => "",
|
|
||||||
"right-right" => "",
|
|
||||||
"left" =>
|
|
||||||
'<div class="infobox">' .
|
|
||||||
'<h1>You\'re damaging my calm</h1>' .
|
|
||||||
'You love 4get too much. Try again in <b>' . $this->format_time($block_time) . '</b>.' .
|
|
||||||
'</div>'
|
|
||||||
];
|
|
||||||
|
|
||||||
$frontend->loadheader(
|
if($counter > config::RATELIMIT_BURST){
|
||||||
$get,
|
|
||||||
$filters,
|
|
||||||
$page,
|
|
||||||
false
|
|
||||||
);
|
|
||||||
|
|
||||||
echo $frontend->load("search.html", $payload);
|
apcu_inc(intdiv(time(), 3600) . ".bot_requests", 1, $s, 262800);
|
||||||
die();
|
|
||||||
|
if($output === false){
|
||||||
|
|
||||||
|
http_response_code(429);
|
||||||
|
echo json_encode([
|
||||||
|
"status" => "You love 4get too much. Try again in " . $this->format_time($block_time) . "."
|
||||||
|
]);
|
||||||
|
die();
|
||||||
|
}
|
||||||
|
|
||||||
|
$payload = [
|
||||||
|
"timetaken" => microtime(true),
|
||||||
|
"class" => "",
|
||||||
|
"right-left" => "",
|
||||||
|
"right-right" => "",
|
||||||
|
"left" =>
|
||||||
|
'<div class="infobox">' .
|
||||||
|
'<h1>You\'re damaging my calm</h1>' .
|
||||||
|
'You love 4get too much. Try again in <b>' . $this->format_time($block_time) . '</b>.' .
|
||||||
|
'</div>'
|
||||||
|
];
|
||||||
|
|
||||||
|
$frontend->loadheader(
|
||||||
|
$get,
|
||||||
|
$filters,
|
||||||
|
$page,
|
||||||
|
false
|
||||||
|
);
|
||||||
|
|
||||||
|
echo $frontend->load("search.html", $payload);
|
||||||
|
die();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user