33 lines
1.0 KiB
PHP
33 lines
1.0 KiB
PHP
<?php
|
|
|
|
|
|
class RateLimitRule implements RuleInterface
|
|
{
|
|
const MAX_404_REQUESTS = 20; // Allow a maximum of 20 dead links...
|
|
const TIME_WINDOW = 300; // ...within 300 seconds (5 minutes)
|
|
|
|
public function execute()
|
|
{
|
|
$ip = BotLogger::getRealIp();
|
|
if (BotLogger::isWhitelisted($ip)) {
|
|
return true;
|
|
}
|
|
$context = Context::getContext();
|
|
|
|
// 1. Instantly skip if this is NOT a 404 error page.
|
|
// During the hookActionFrontControllerInitBefore hook, PrestaShop has already
|
|
// resolved the route. If it failed, the controller is set to PageNotFoundController.
|
|
if (!($context->controller instanceof PageNotFoundController)) {
|
|
return true;
|
|
}
|
|
|
|
if (RateLimiter::checkIsRateLimited($ip, '404_spam', self::MAX_404_REQUESTS, self::TIME_WINDOW)) {
|
|
BotLogger::logBan($ip, '404_RATE_LIMIT_EXCEEDED');
|
|
header('HTTP/1.1 429 Too Many Requests');
|
|
die('429 Too Many Requests - Stop Scanning');
|
|
}
|
|
|
|
return true;
|
|
}
|
|
}
|