Add overrides option to specifiy rate limit for self identified bots.

This commit is contained in:
Retro_Guy 2024-05-18 04:10:52 -07:00
parent 453f025629
commit 768c95b0ee
2 changed files with 15 additions and 4 deletions

View File

@ -2089,25 +2089,33 @@ function disable_page_by_user_agent($client_device, $useragent, $script = "Page"
function throttle_hits($client_device) function throttle_hits($client_device)
{ {
global $CONFIG, $logdir; global $CONFIG, $OVERRIDES, $logdir, $config_name;
$client_device = get_client_user_agent_info(); $client_device = get_client_user_agent_info();
$_SESSION['rsactive'] = true; $_SESSION['rsactive'] = true;
// $loadrate = allowed article request per second
$loadrate = .15;
if ($client_device == "bot") { if ($client_device == "bot") {
$_SESSION['bot'] = 'true'; $_SESSION['bot'] = 'true';
if(isset($OVERRIDES['throttle_hits_bot_loadrate']) && trim($OVERRIDES['throttle_hits_bot_loadrate']) != '') {
$loadrate = $OVERRIDES['throttle_hits_bot_loadrate'];
} }
}
$logfile = $logdir . '/newsportal.log'; $logfile = $logdir . '/newsportal.log';
if (! isset($_SESSION['starttime'])) { if (! isset($_SESSION['starttime'])) {
$_SESSION['starttime'] = time(); $_SESSION['starttime'] = time();
$_SESSION['views'] = 0; $_SESSION['views'] = 0;
} }
$_SESSION['views'] ++; $_SESSION['views'] ++;
// $loadrate = allowed article request per second // $rate = current hits / seconds since start of session
$loadrate = .15;
$rate = fdiv($_SESSION['views'], (time() - $_SESSION['starttime'])); $rate = fdiv($_SESSION['views'], (time() - $_SESSION['starttime']));
// if $rate > greater than $loadrate, throttle hits
// but allow 50 hits at start of session to allow loading everything
if (($rate > $loadrate) && ($_SESSION['views'] > 50)) { if (($rate > $loadrate) && ($_SESSION['views'] > 50)) {
header("HTTP/1.0 429 Too Many Requests"); header("HTTP/1.0 429 Too Many Requests");
if (! isset($_SESSION['throttled'])) { if (! isset($_SESSION['throttled'])) {
file_put_contents($logfile, "\n" . format_log_date() . " " . $config_name . " Too many requests from " . $_SERVER['REMOTE_ADDR'] . " throttling", FILE_APPEND); file_put_contents($logfile, "\n" . format_log_date() . " " . $config_name . " Too many requests from " . $_SERVER['REMOTE_ADDR'] . " throttling" . " (" . $rate . " > " . $loadrate . ")", FILE_APPEND);
$_SESSION['throttled'] = true; $_SESSION['throttled'] = true;
} }
exit(0); exit(0);

View File

@ -69,6 +69,9 @@ return [
// Set to '' for no User-Agent: header // Set to '' for no User-Agent: header
'user_agent' => 'Rocksolid Light', 'user_agent' => 'Rocksolid Light',
// Rate per second that a 'bot' may load pages
'throttle_hits_bot_loadrate' => '.1',
// Just leave this here to avoid comma errors // Just leave this here to avoid comma errors
'comma' => true 'comma' => true
]; ];