diff --git a/data/config.php b/data/config.php index ea33077..ba9f1bf 100644 --- a/data/config.php +++ b/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 = 0; + + // 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,9 @@ 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 = [ + //"https://fossq.com", ]; // Known FOSS-Q instances. MUST use the https protocol if your instance uses @@ -110,30 +131,30 @@ class config{ const INSTANCES = [ "https://4get.ca", "https://search.fischbytes.de", - "https://FOSS-Q.nadeko.net", - "https://FOSS-Q.silly.computer", - "https://FOSS-Q.sijh.net", - "https://FOSS-Q.hbubli.cc", - "https://FOSS-Q.lunar.icu", - "https://FOSS-Q.dcs0.hu", - "https://FOSS-Q.neco.lol", - "https://FOSS-Q.edmateo.site", - "https://FOSS-Q.sudovanilla.org", + "https://4get.nadeko.net", + "https://4get.silly.computer", + "https://4get.sijh.net", + "https://4get.hbubli.cc", + "https://4get.lunar.icu", + "https://4get.dcs0.hu", + "https://4get.neco.lol", + "https://4get.edmateo.site", + "https://4get.sudovanilla.org", "https://search.mint.lgbt", - "https://FOSS-Q.aishiteiru.moe", + "https://4get.aishiteiru.moe", "https://4.nboeck.de", "https://search.yonderly.org", - "https://FOSS-Q.lurx.net", - "https://FOSS-Q.canine.tools", - "https://FOSS-Q.sny.sh", + "https://4get.lurx.net", + "https://4get.canine.tools", + "https://4get.sny.sh", "https://4g.opnxng.com", "https://s.307200.xyz", - "https://FOSS-Q.privadency.com/" + "https://4get.privadency.com/" ]; // 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 +188,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; diff --git a/lib/bot_protection.php b/lib/bot_protection.php index 73b68aa..9483f09 100644 --- a/lib/bot_protection.php +++ b/lib/bot_protection.php @@ -4,6 +4,93 @@ 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 = $_SERVER["REMOTE_ADDR"]; + }else{ + + $source = $_SERVER[explode(";", strtoupper(str_replace("-", "_", config::IP_SOURCE)))[0]]; + } + + // check if its not a local IP. Probably local tor bridge, dont ratelimit + if( + filter_var( + $source, + FILTER_VALIDATE_IP, + FILTER_FLAG_NO_PRIV_RANGE | FILTER_FLAG_NO_RES_RANGE + ) !== false + ){ + + $source = "r." . $source; + + // 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){ + + apcu_inc(intdiv(time(), 3600) . ".bot_requests", 1, $s, 262800); + + 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" => + '