Compare commits
No commits in common. "08394be35ec1c63803652c5a1bb4ab881c293660" and "f5499402492fce8f48c91d3922a0607ce24534de" have entirely different histories.
08394be35e
...
f549940249
|
@ -5,13 +5,10 @@ class RateLimiter {
|
|||
private $maxAttempts = 5; // Maximum login attempts
|
||||
private $decayMinutes = 15; // Time window in minutes
|
||||
private $tableName = 'login_attempts';
|
||||
private $whitelistedIps = []; // Whitelisted IPs
|
||||
private $whitelistedNetworks = []; // Whitelisted CIDR ranges
|
||||
|
||||
public function __construct($database) {
|
||||
$this->db = $database->getConnection();
|
||||
$this->createTableIfNotExists();
|
||||
$this->loadWhitelist();
|
||||
}
|
||||
|
||||
private function createTableIfNotExists() {
|
||||
|
@ -26,71 +23,6 @@ class RateLimiter {
|
|||
$this->db->exec($sql);
|
||||
}
|
||||
|
||||
private function loadWhitelist() {
|
||||
// Load from database or config
|
||||
$this->whitelistedIps = [
|
||||
'127.0.0.1', // localhost
|
||||
'::1' // localhost IPv6
|
||||
];
|
||||
|
||||
$this->whitelistedNetworks = [
|
||||
'10.0.0.0/8', // Private network
|
||||
'172.16.0.0/12', // Private network
|
||||
'192.168.0.0/16' // Private network
|
||||
];
|
||||
}
|
||||
|
||||
private function isIpWhitelisted($ip) {
|
||||
// Check exact IP match
|
||||
if (in_array($ip, $this->whitelistedIps)) {
|
||||
return true;
|
||||
}
|
||||
|
||||
// Check CIDR ranges
|
||||
foreach ($this->whitelistedNetworks as $network) {
|
||||
if ($this->ipInRange($ip, $network)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
private function ipInRange($ip, $cidr) {
|
||||
list($subnet, $bits) = explode('/', $cidr);
|
||||
|
||||
$ip = ip2long($ip);
|
||||
$subnet = ip2long($subnet);
|
||||
$mask = -1 << (32 - $bits);
|
||||
$subnet &= $mask;
|
||||
|
||||
return ($ip & $mask) == $subnet;
|
||||
}
|
||||
|
||||
public function addToWhitelist($ip, $isNetwork = false) {
|
||||
if ($isNetwork) {
|
||||
if (!in_array($ip, $this->whitelistedNetworks)) {
|
||||
$this->whitelistedNetworks[] = $ip;
|
||||
}
|
||||
} else {
|
||||
if (!in_array($ip, $this->whitelistedIps)) {
|
||||
$this->whitelistedIps[] = $ip;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public function removeFromWhitelist($ip) {
|
||||
$indexIp = array_search($ip, $this->whitelistedIps);
|
||||
if ($indexIp !== false) {
|
||||
unset($this->whitelistedIps[$indexIp]);
|
||||
}
|
||||
|
||||
$indexNetwork = array_search($ip, $this->whitelistedNetworks);
|
||||
if ($indexNetwork !== false) {
|
||||
unset($this->whitelistedNetworks[$indexNetwork]);
|
||||
}
|
||||
}
|
||||
|
||||
public function attempt($username, $ipAddress) {
|
||||
// Clean old attempts
|
||||
$this->clearOldAttempts();
|
||||
|
|
Loading…
Reference in New Issue