Fixes whitelisting in db
parent
a0747cfbc8
commit
bc1089be21
|
@ -57,17 +57,21 @@ class RateLimiter {
|
||||||
|
|
||||||
// Check if IP is whitelisted
|
// Check if IP is whitelisted
|
||||||
private function isIpWhitelisted($ip) {
|
private function isIpWhitelisted($ip) {
|
||||||
// Check exact IP match
|
// Check exact IP match and CIDR ranges
|
||||||
if (in_array($ip, $this->whitelistedIps)) {
|
$stmt = $this->db->prepare("SELECT ip_address, is_network FROM {$this->whitelistTable}");
|
||||||
|
$stmt->execute();
|
||||||
|
|
||||||
|
while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
|
||||||
|
if ($row['is_network']) {
|
||||||
|
if ($this->ipInRange($ip, $row['ip_address'])) {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
} else {
|
||||||
// Check CIDR ranges
|
if ($ip === $row['ip_address']) {
|
||||||
foreach ($this->whitelistedNetworks as $network) {
|
|
||||||
if ($this->ipInRange($ip, $network)) {
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
@ -84,29 +88,30 @@ class RateLimiter {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Add to whitelist
|
// Add to whitelist
|
||||||
public function addToWhitelist($ip, $isNetwork = false) {
|
public function addToWhitelist($ip, $isNetwork = false, $description = '', $createdBy = 'system') {
|
||||||
if ($isNetwork) {
|
$stmt = $this->db->prepare("INSERT INTO {$this->whitelistTable}
|
||||||
if (!in_array($ip, $this->whitelistedNetworks)) {
|
(ip_address, is_network, description, created_by)
|
||||||
$this->whitelistedNetworks[] = $ip;
|
VALUES (?, ?, ?, ?)
|
||||||
}
|
ON DUPLICATE KEY UPDATE
|
||||||
} else {
|
is_network = VALUES(is_network),
|
||||||
if (!in_array($ip, $this->whitelistedIps)) {
|
description = VALUES(description),
|
||||||
$this->whitelistedIps[] = $ip;
|
created_by = VALUES(created_by)");
|
||||||
}
|
|
||||||
}
|
return $stmt->execute([$ip, $isNetwork, $description, $createdBy]);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Remove from whitelist
|
// Remove from whitelist
|
||||||
public function removeFromWhitelist($ip) {
|
public function removeFromWhitelist($ip) {
|
||||||
$indexIp = array_search($ip, $this->whitelistedIps);
|
$stmt = $this->db->prepare("DELETE FROM {$this->whitelistTable} WHERE ip_address = ?");
|
||||||
if ($indexIp !== false) {
|
|
||||||
unset($this->whitelistedIps[$indexIp]);
|
return $stmt->execute([$ip]);
|
||||||
}
|
}
|
||||||
|
|
||||||
$indexNetwork = array_search($ip, $this->whitelistedNetworks);
|
public function getWhitelistedIps() {
|
||||||
if ($indexNetwork !== false) {
|
$stmt = $this->db->prepare("SELECT * FROM {$this->whitelistTable} ORDER BY created_at DESC");
|
||||||
unset($this->whitelistedNetworks[$indexNetwork]);
|
$stmt->execute();
|
||||||
}
|
|
||||||
|
return $stmt->fetchAll(PDO::FETCH_ASSOC);
|
||||||
}
|
}
|
||||||
|
|
||||||
public function attempt($username, $ipAddress) {
|
public function attempt($username, $ipAddress) {
|
||||||
|
|
Loading…
Reference in New Issue