Файл: app/Http/Middleware/CheckThrottle.php
Строк: 60
<?php
namespace AppHttpMiddleware;
use AppModelsBan;
use Closure;
use IlluminateHttpRequest;
use IlluminateSupportFacadesCache;
class CheckThrottle
{
/**
* Handle an incoming request.
*/
public function handle(Request $request, Closure $next)
{
$ip = getIp();
// Проверка на бан
if ($this->isBanned($request, $ip)) {
return redirect()->route('ipban')
->setStatusCode(429)
->withHeaders(['Retry-After' => 3600]);
}
$limit = setting('doslimit');
if (! $limit) {
return $next($request);
}
$key = 'throttle_' . $ip;
$requests = Cache::add($key, 0, 60) ? 1 : Cache::increment($key);
/* Автоматическая блокировка */
if ($requests > $limit) {
$bannedIps = Cache::get('ipBan', []);
if (! isset($bannedIps[$ip])) {
$inserted = Ban::query()->insertOrIgnore([
'ip' => $ip,
'created_at' => now(),
]);
if ($inserted) {
$bannedIps[$ip] = true;
Cache::forever('ipBan', $bannedIps);
}
}
clearCache($key);
saveErrorLog(666);
return redirect()->route('ipban')
->setStatusCode(429)
->withHeaders(['Retry-After' => 60]);
}
return $next($request);
}
/**
* Проверка на ip-бан
*/
private function isBanned(Request $request, string $ip): bool
{
if (isAdmin() || $request->is('ipban', 'captcha')) {
return false;
}
$bannedIps = Cache::rememberForever('ipBan', static function () {
return Ban::query()->pluck('id', 'ip')->toArray();
});
return isset($bannedIps[$ip]);
}
}