Move rate throttling to function in newsportal.php

This commit is contained in:
Retro_Guy 2021-06-18 10:10:34 +00:00
parent 58090b0120
commit 4766775a64
6 changed files with 41 additions and 22 deletions

View File

@ -9,6 +9,9 @@
include "auth.inc";
include "$file_newsportal";
$logfile=$logdir.'/newsportal.log';
throttle_hits();
// register parameters
$id=$_REQUEST["id"];
$group=_rawurldecode($_REQUEST["group"]);
@ -23,28 +26,11 @@
if(isset($_REQUEST["first"]))
$first=$_REQUEST["first"];
$logfile=$logdir.'/newsportal.log';
if(!isset($_SESSION['starttime'])) {
$_SESSION['starttime'] = time();
$_SESSION['views'] = 0;
}
$_SESSION['views']++;
// $loadrate = allowed article request per second
$loadrate = .2;
$rate = ($_SESSION['views'] / (time() - $_SESSION['starttime']));
if (($rate > $loadrate) && ($_SESSION['views'] > 5)) {
header("HTTP/1.0 429 Too Many Requests");
if(!isset($_SESSION['throttled'])) {
file_put_contents($logfile, "\n".format_log_date()." ".$config_name." Too many requests from ".$_SERVER['REMOTE_ADDR']." throttling", FILE_APPEND);
$_SESSION['throttled'] = true;
}
exit(0);
}
$_SESSION['rsactive'] = true;
$location = $_SERVER['REQUEST_URI'].$_SERVER['REQUEST_STRING'];
// preg_match('/id=(.*)&/', $location, $hash);
// $_SESSION['return_page'] = $location.'#'.$hash[1];
$_SESSION['return_page'] = $location.'#'.$id;
file_put_contents('/var/spool/rslight/log/access.log', "\n".format_log_date()." ".$config_name." ".$group.":".$id, FILE_APPEND);

View File

@ -1,10 +1,13 @@
<?php
session_start();
header("Expires: ".gmdate("D, d M Y H:i:s",time()+(3600*24))." GMT");
include "config.inc.php";
include "auth.inc";
include "$file_newsportal";
throttle_hits();
// register parameters
$id=$_REQUEST["id"];
$group=_rawurldecode($_REQUEST["group"]);

View File

@ -1420,10 +1420,10 @@ function np_get_db_article($article, $group, $makearray=1, $dbh=null) {
$dbh = null;
}
if($ok_article !== 1) {
file_put_contents($logfile, "\n".format_log_date()." ".$config_name." DEBUG: ".$article." from ".$group." not found in database", FILE_APPEND);
// file_put_contents($logfile, "\n".format_log_date()." ".$config_name." DEBUG: ".$article." from ".$group." not found in database", FILE_APPEND);
return FALSE;
}
file_put_contents($logfile, "\n".format_log_date()." ".$config_name." DEBUG: fetched: ".$article." from ".$group, FILE_APPEND);
// file_put_contents($logfile, "\n".format_log_date()." ".$config_name." DEBUG: fetched: ".$article." from ".$group, FILE_APPEND);
if($makearray == 1) {
$thisarticle = preg_split("/\r\n|\n|\r/", trim($msg2));
array_pop($thisarticle);
@ -1454,4 +1454,26 @@ function get_config_value($configfile,$request) {
return FALSE;
}
}
function throttle_hits() {
global $CONFIG, $logdir;
$logfile=$logdir.'/newsportal.log';
if(!isset($_SESSION['starttime'])) {
$_SESSION['starttime'] = time();
$_SESSION['views'] = 0;
}
$_SESSION['views']++;
// $loadrate = allowed article request per second
$loadrate = .15;
$rate = ($_SESSION['views'] / (time() - $_SESSION['starttime']));
if (($rate > $loadrate) && ($_SESSION['views'] > 5)) {
header("HTTP/1.0 429 Too Many Requests");
if(!isset($_SESSION['throttled'])) {
file_put_contents($logfile, "\n".format_log_date()." ".$config_name." Too many requests from ".$_SERVER['REMOTE_ADDR']." throttling", FILE_APPEND);
$_SESSION['throttled'] = true;
}
exit(0);
}
}
?>

View File

@ -22,10 +22,13 @@
?>
<?php
session_start();
include "config.inc.php";
include "auth.inc";
include "$file_newsportal";
throttle_hits();
if(isset($frames_on) && $frames_on === true) {
?>
<script>
@ -52,7 +55,7 @@ $CONFIG = include($config_file);
if (isset($_GET['thisgroup'])) {
$article_age = 30;
} else {
$article_age = 7;
$article_age = 30;
}
# Maximum number of articles to show

View File

@ -5,6 +5,8 @@ session_start();
include "config.inc.php";
include "newsportal.php";
throttle_hits();
$snippet_size = 100;
if(!isset($_POST['key']) || !password_verify($CONFIG['thissitekey'], $_POST['key'])) {

View File

@ -9,6 +9,9 @@ include "config.inc.php";
include("$file_newsportal");
include "auth.inc";
$logfile=$logdir.'/newsportal.log';
throttle_hits();
// register parameters
$group=_rawurldecode($_REQUEST["group"]);
if(isset($_REQUEST["first"]))