parent
08d7a61718
commit
0049f0a983
1 changed files with 144 additions and 21 deletions
|
|
@ -4,6 +4,93 @@ class bot_protection{
|
|||
|
||||
public function __construct($frontend, $get, $filters, $page, $output){
|
||||
|
||||
if(config::RATELIMIT_BURST !== 0){
|
||||
|
||||
// ratelimit requests per IP
|
||||
if(config::RATELIMIT_IP_SOURCE === false){
|
||||
|
||||
$source = $_SERVER["REMOTE_ADDR"];
|
||||
}else{
|
||||
|
||||
$source = $_SERVER[explode(";", strtoupper(str_replace("-", "_", config::IP_SOURCE)))[0]];
|
||||
}
|
||||
|
||||
// check if its not a local IP. Probably local tor bridge, dont ratelimit
|
||||
if(
|
||||
filter_var(
|
||||
$source,
|
||||
FILTER_VALIDATE_IP,
|
||||
FILTER_FLAG_NO_PRIV_RANGE | FILTER_FLAG_NO_RES_RANGE
|
||||
) !== false
|
||||
){
|
||||
|
||||
$source = "r." . $source;
|
||||
|
||||
// counter
|
||||
// resets on every req
|
||||
$counter = apcu_fetch($source);
|
||||
$counter = (int)$counter + 1;
|
||||
|
||||
if(
|
||||
(
|
||||
CONFIG::RATELIMIT_PUNISH !== 0 &&
|
||||
$counter > config::RATELIMIT_BURST + 1
|
||||
) &&
|
||||
(
|
||||
(
|
||||
$output === false &&
|
||||
CONFIG::RATELIMIT_PUNISH_API === true
|
||||
) ||
|
||||
$output === true
|
||||
)
|
||||
){
|
||||
|
||||
$block_time = config::RATELIMIT_TIME + (($counter - 1) * config::RATELIMIT_PUNISH);
|
||||
}else{
|
||||
|
||||
$block_time = config::RATELIMIT_TIME;
|
||||
}
|
||||
|
||||
apcu_store($source, $counter, $block_time);
|
||||
|
||||
if($counter > config::RATELIMIT_BURST){
|
||||
|
||||
apcu_inc(intdiv(time(), 3600) . ".bot_requests", 1, $s, 262800);
|
||||
|
||||
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();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// check if we want captcha
|
||||
if(config::BOT_PROTECTION !== 1){
|
||||
|
||||
|
|
@ -186,7 +273,7 @@ class bot_protection{
|
|||
}
|
||||
}
|
||||
|
||||
apcu_inc(intdiv(time(), 3600) . ".bot_requests", 1, $s, 259200);
|
||||
apcu_inc(intdiv(time(), 3600) . ".bot_requests", 1, $s, 262800);
|
||||
$key = "c" . apcu_inc("captcha_gen", 1) . "." . $this->randomchars();
|
||||
|
||||
$payload = [
|
||||
|
|
@ -257,6 +344,20 @@ class bot_protection{
|
|||
die();
|
||||
}
|
||||
|
||||
private function format_time($seconds){
|
||||
|
||||
$hours = floor($seconds / 3600);
|
||||
$minutes = floor(($seconds % 3600) / 60);
|
||||
$secs = $seconds % 60;
|
||||
|
||||
if($hours > 0){
|
||||
|
||||
return sprintf('%dh%02dm%02ds', $hours, $minutes, $secs);
|
||||
}
|
||||
|
||||
return sprintf('%dm%02ds', $minutes, $secs);
|
||||
}
|
||||
|
||||
private function randomchars(){
|
||||
|
||||
$chars =
|
||||
|
|
|
|||
Loading…
Reference in a new issue