Explorar o código

added ratelimit settings

lolcat hai 1 mes
pai
achega
9135f9395c
Modificáronse 2 ficheiros con 117 adicións e 6 borrados
  1. 30 6
      data/config.php
  2. 87 0
      lib/bot_protection.php

+ 30 - 6
data/config.php

@@ -42,6 +42,29 @@ class config{
 	// Or "https://your-endpoint.com/my-endpoint" (excluding get parameters like &pw)
 	const FPLAY_EXTERNAL_ENDPOINT = null;
 	
+	//
+	// RATELIMITING
+	// Limits also applies to the captcha page
+	//
+	
+	// Amount of requests allowed before we block
+	// Set to 0 to disable ratelimiting
+	const RATELIMIT_BURST = 2;
+	
+	// IP source for ratelimiting. Choices: false for getting the IP directly,
+	// or "X-Header-Name" to get the IP from a forwarded header
+	const RATELIMIT_IP_SOURCE = false;
+	
+	// How long till the counter resets? Note: counter resets everytime
+	// a new request occurs while being blocked.
+	const RATELIMIT_TIME = 2;
+	
+	// How many seconds to add every time a new request occurs while being blocked?
+	const RATELIMIT_PUNISH = 1;
+	
+	// Add seconds of punishment time to users of the API?
+	const RATELIMIT_PUNISH_API = false;
+	
 	//
 	// BOT PROTECTION
 	//
@@ -96,11 +119,11 @@ class config{
 	const MAX_SEARCHES = 100;
 	
 	// List of domains that point to your servers. Include your tor/i2p
-	// addresses here! Must be a valid URL. Won't affect links placed on
-	// the homepage.
-	const ALT_ADDRESSES = [
-		//"https://4get.alt-tld",
-		//"http://4getwebfrq5zr4sxugk6htxvawqehxtdgjrbcn2oslllcol2vepa23yd.onion"
+	// addresses here! Must be a valid URL.
+	const ADDRESSES = [
+		//"Clearnet" => "https://4get.ca",
+		//"Clearnet (alt)" => "https://4get.alt-tld",
+		//"Tor" => "http://4getwebfrq5zr4sxugk6htxvawqehxtdgjrbcn2oslllcol2vepa23yd.onion"
 	];
 	
 	// Known 4get instances. MUST use the https protocol if your instance uses
@@ -133,7 +156,7 @@ class config{
 	
 	// Default user agent to use for scraper requests. Sometimes ignored to get specific webpages
 	// Changing this might break things.
-	const USER_AGENT = "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:155.0) Gecko/20100101 Firefox/155.0";
+	const USER_AGENT = "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:157.0) Gecko/20100101 Firefox/157.0";
 	
 	// User agent to use with 4get-friendly APIs
 	const USER_AGENT_FRIENDLY = "4get-scrapist (+https://4get.ca)";
@@ -167,6 +190,7 @@ class config{
 	const PROXY_ARCHIVEORG = false;
 	const PROXY_SEPIASEARCH = false;
 	const PROXY_ODYSEE = false;
+	const PROXY_DAILYMOTION = false;
 	const PROXY_VIMEO = false;
 	const PROXY_YEP = false;
 	const PROXY_PINTEREST = false;

+ 87 - 0
lib/bot_protection.php

@@ -4,6 +4,79 @@ class bot_protection{
 	
 	public function __construct($frontend, $get, $filters, $page, $output){
 		
+		if(config::RATELIMIT_BURST !== 0){
+			
+			// ratelimit requests per IP
+			if(config::RATELIMIT_IP_SOURCE === false){
+				
+				$source = "r" . $_SERVER["REMOTE_ADDR"];
+			}else{
+				
+				$source = "r" . $_SERVER[explode(";", strtoupper(str_replace("-", "_", config::IP_SOURCE)))[0]];
+			}
+			
+			// counter
+			// resets on every req	
+			$counter = apcu_fetch($source);
+			$counter = (int)$counter + 1;
+			
+			if(
+				(
+					CONFIG::RATELIMIT_PUNISH !== 0 &&
+					$counter > config::RATELIMIT_BURST + 1
+				) &&
+				(
+					(
+						$output === false &&
+						CONFIG::RATELIMIT_PUNISH_API === true
+					) ||
+					$output === true
+				)
+			){
+				
+				$block_time = config::RATELIMIT_TIME + (($counter - 1) * config::RATELIMIT_PUNISH);
+			}else{
+				
+				$block_time = config::RATELIMIT_TIME;
+			}
+			
+			apcu_store($source, $counter, $block_time);
+			
+			if($counter > config::RATELIMIT_BURST){
+				
+				if($output === false){
+					
+					http_response_code(429);
+					echo json_encode([
+						"status" => "You love 4get too much. Try again in " . $this->format_time($block_time) . "."
+					]);
+					die();
+				}
+				
+				$payload = [
+					"timetaken" => microtime(true),
+					"class" => "",
+					"right-left" => "",
+					"right-right" => "",
+					"left" =>
+						'<div class="infobox">' .
+							'<h1>You\'re damaging my calm</h1>' .
+							'You love 4get too much. Try again in <b>' . $this->format_time($block_time) . '</b>.' .
+						'</div>'
+				];
+				
+				$frontend->loadheader(
+					$get,
+					$filters,
+					$page,
+					false
+				);
+				
+				echo $frontend->load("search.html", $payload);
+				die();
+			}
+		}
+				
 		// check if we want captcha
 		if(config::BOT_PROTECTION !== 1){
 			
@@ -257,6 +330,20 @@ class bot_protection{
 		die();
 	}
 	
+	private function format_time($seconds){
+		
+		$hours = floor($seconds / 3600);
+		$minutes = floor(($seconds % 3600) / 60);
+		$secs = $seconds % 60;
+		
+		if($hours > 0){
+			
+			return sprintf('%dh%02dm%02ds', $hours, $minutes, $secs);
+		}
+		
+		return sprintf('%dm%02ds', $minutes, $secs);
+	}
+	
 	private function randomchars(){
 		
 		$chars =