30 lines
1.1 KiB
PHP
30 lines
1.1 KiB
PHP
<?php
|
|
// Reject the observed homepage flood before loading ThinkPHP or opening MySQL.
|
|
// Nginx also enforces homepage rate limits before a PHP worker is allocated.
|
|
function txiaw_should_block_home_request($server, $query)
|
|
{
|
|
$method = isset($server['REQUEST_METHOD']) ? strtoupper($server['REQUEST_METHOD']) : '';
|
|
if ($method !== 'GET' && $method !== 'HEAD') {
|
|
return false;
|
|
}
|
|
$requestUri = isset($server['REQUEST_URI']) ? $server['REQUEST_URI'] : '';
|
|
$path = explode('?', $requestUri, 2);
|
|
$path = rawurldecode($path[0]);
|
|
if ($path !== '/' && $path !== '/index.php') {
|
|
return false;
|
|
}
|
|
$userAgent = isset($server['HTTP_USER_AGENT']) ? trim($server['HTTP_USER_AGENT']) : '';
|
|
return $userAgent === '' && array_key_exists('r', $query);
|
|
}
|
|
|
|
if (PHP_SAPI !== 'cli' && txiaw_should_block_home_request($_SERVER, $_GET)) {
|
|
http_response_code(429);
|
|
header('Retry-After: 60');
|
|
header('Cache-Control: no-store');
|
|
header('Content-Type: text/plain; charset=UTF-8');
|
|
if ($_SERVER['REQUEST_METHOD'] !== 'HEAD') {
|
|
echo "Too many requests. Please retry later.\n";
|
|
}
|
|
exit;
|
|
}
|