Adds comments
parent
645e98cd6a
commit
38e4b002c8
|
@ -14,6 +14,7 @@ class RateLimiter {
|
||||||
$this->loadWhitelist();
|
$this->loadWhitelist();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Database preparation
|
||||||
private function createTableIfNotExists() {
|
private function createTableIfNotExists() {
|
||||||
$sql = "CREATE TABLE IF NOT EXISTS {$this->tableName} (
|
$sql = "CREATE TABLE IF NOT EXISTS {$this->tableName} (
|
||||||
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||||
|
@ -26,8 +27,9 @@ class RateLimiter {
|
||||||
$this->db->exec($sql);
|
$this->db->exec($sql);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// List of IPs to bypass rate limiting
|
||||||
private function loadWhitelist() {
|
private function loadWhitelist() {
|
||||||
// Load from database or config
|
// FIXME Load from database or config
|
||||||
$this->whitelistedIps = [
|
$this->whitelistedIps = [
|
||||||
'127.0.0.1', // localhost
|
'127.0.0.1', // localhost
|
||||||
'::1' // localhost IPv6
|
'::1' // localhost IPv6
|
||||||
|
@ -40,6 +42,7 @@ class RateLimiter {
|
||||||
];
|
];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Check if IP is whitelisted
|
||||||
private function isIpWhitelisted($ip) {
|
private function isIpWhitelisted($ip) {
|
||||||
// Check exact IP match
|
// Check exact IP match
|
||||||
if (in_array($ip, $this->whitelistedIps)) {
|
if (in_array($ip, $this->whitelistedIps)) {
|
||||||
|
@ -67,6 +70,7 @@ class RateLimiter {
|
||||||
return ($ip & $mask) == $subnet;
|
return ($ip & $mask) == $subnet;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Add to whitelist
|
||||||
public function addToWhitelist($ip, $isNetwork = false) {
|
public function addToWhitelist($ip, $isNetwork = false) {
|
||||||
if ($isNetwork) {
|
if ($isNetwork) {
|
||||||
if (!in_array($ip, $this->whitelistedNetworks)) {
|
if (!in_array($ip, $this->whitelistedNetworks)) {
|
||||||
|
@ -79,6 +83,7 @@ class RateLimiter {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Remove from whitelist
|
||||||
public function removeFromWhitelist($ip) {
|
public function removeFromWhitelist($ip) {
|
||||||
$indexIp = array_search($ip, $this->whitelistedIps);
|
$indexIp = array_search($ip, $this->whitelistedIps);
|
||||||
if ($indexIp !== false) {
|
if ($indexIp !== false) {
|
||||||
|
|
Loading…
Reference in New Issue