From 528f4829af489ae690da660586ab194ace8ce106 Mon Sep 17 00:00:00 2001 From: Yasen Pramatarov Date: Fri, 3 Jan 2025 17:58:19 +0200 Subject: [PATCH] Check if blacklisted --- app/classes/ratelimitrer.php | 26 +++++++++++++++++++++++++- 1 file changed, 25 insertions(+), 1 deletion(-) diff --git a/app/classes/ratelimitrer.php b/app/classes/ratelimitrer.php index 4aecd2c..cbea523 100644 --- a/app/classes/ratelimitrer.php +++ b/app/classes/ratelimitrer.php @@ -86,7 +86,6 @@ class RateLimiter { } - // Check if IP is whitelisted private function isIpWhitelisted($ip) { // Check exact IP match and CIDR ranges $stmt = $this->db->prepare("SELECT ip_address, is_network FROM {$this->whitelistTable}"); @@ -107,6 +106,31 @@ class RateLimiter { return false; } + private function isIpBlacklisted($ip) { + // First check if IP is explicitly blacklisted or in a blacklisted range + $stmt = $this->db->prepare("SELECT ip_address, is_network, expiry_time FROM {$this->blacklistTable}"); + $stmt->execute(); + + while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) { + // Skip expired entries + if ($row['expiry_time'] !== null && strtotime($row['expiry_time']) < time()) { + continue; + } + + if ($row['is_network']) { + if ($this->ipInRange($ip, $row['ip_address'])) { + return true; + } + } else { + if ($ip === $row['ip_address']) { + return true; + } + } + } + + return false; + } + private function ipInRange($ip, $cidr) { list($subnet, $bits) = explode('/', $cidr);