Compare commits

...

2 Commits

Author SHA1 Message Date
Yasen Pramatarov 9b8f92f2eb Enhances add to whitelist 2024-12-21 17:14:31 +02:00
Yasen Pramatarov 8d0518c7ff Fixes sqlite syntax 2024-12-21 17:11:15 +02:00
1 changed files with 39 additions and 18 deletions

View File

@ -19,9 +19,9 @@ class RateLimiter {
// Login attempts table // Login attempts table
$sql = "CREATE TABLE IF NOT EXISTS {$this->ratelimitTable} ( $sql = "CREATE TABLE IF NOT EXISTS {$this->ratelimitTable} (
id INTEGER PRIMARY KEY AUTOINCREMENT, id INTEGER PRIMARY KEY AUTOINCREMENT,
ip_address VARCHAR(45) NOT NULL, ip_address TEXT NOT NULL,
username VARCHAR(255) NOT NULL, username TEXT NOT NULL,
attempted_at DATETIME DEFAULT CURRENT_TIMESTAMP, attempted_at TEXT DEFAULT (DATETIME('now')),
INDEX idx_ip_username (ip_address, username) INDEX idx_ip_username (ip_address, username)
)"; )";
$this->db->exec($sql); $this->db->exec($sql);
@ -29,11 +29,11 @@ class RateLimiter {
// IP whitelist table // IP whitelist table
$sql = "CREATE TABLE IF NOT EXISTS {$this->whitelistTable} ( $sql = "CREATE TABLE IF NOT EXISTS {$this->whitelistTable} (
id INTEGER PRIMARY KEY AUTOINCREMENT, id INTEGER PRIMARY KEY AUTOINCREMENT,
ip_address VARCHAR(45) NOT NULL, ip_address TEXT NOT NULL,
is_network BOOLEAN DEFAULT FALSE, is_network BOOLEAN DEFAULT 0 CHECK(is_network IN (0,1)),
description VARCHAR(255), description TEXT,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP, created_at TEXT DEFAULT (DATETIME('now')),
created_by VARCHAR(255), created_by TEXT,
UNIQUE KEY unique_ip (ip_address) UNIQUE KEY unique_ip (ip_address)
)"; )";
$this->db->exec($sql); $this->db->exec($sql);
@ -48,7 +48,7 @@ class RateLimiter {
]; ];
// Insert default whitelisted IPs if they don't exist // Insert default whitelisted IPs if they don't exist
$stmt = $this->db->prepare("INSERT IGNORE INTO {$this->whitelistTable} $stmt = $this->db->prepare("INSERT OR IGNORE INTO {$this->whitelistTable}
(ip_address, is_network, description, created_by) (ip_address, is_network, description, created_by)
VALUES (?, ?, ?, 'system')"); VALUES (?, ?, ?, 'system')");
foreach ($defaultIps as $ip) { foreach ($defaultIps as $ip) {
@ -89,16 +89,37 @@ class RateLimiter {
} }
// Add to whitelist // Add to whitelist
public function addToWhitelist($ip, $isNetwork = false, $description = '', $createdBy = 'system') { public function addToWhitelist($ip, $isNetwork = false, $description = '', $createdBy = 'system', $userId = null) {
$stmt = $this->db->prepare("INSERT INTO {$this->whitelistTable} try {
(ip_address, is_network, description, created_by) $stmt = $this->db->prepare("INSERT INTO {$this->whitelistTable}
VALUES (?, ?, ?, ?) (ip_address, is_network, description, created_by)
ON DUPLICATE KEY UPDATE VALUES (?, ?, ?, ?)
is_network = VALUES(is_network), ON DUPLICATE KEY UPDATE
description = VALUES(description), is_network = VALUES(is_network),
created_by = VALUES(created_by)"); description = VALUES(description),
created_by = VALUES(created_by)");
return $stmt->execute([$ip, $isNetwork, $description, $createdBy]); $result = $stmt->execute([$ip, $isNetwork, $description, $createdBy]);
if ($result) {
$logMessage = sprintf(
'IP Whitelist: Added %s "%s" by %s. Description: %s',
$isNetwork ? 'network' : 'IP',
$ip,
$createdBy,
$description
);
$this->log->insertLog($userId ?? 0, $logMessage, 'system');
}
return $result;
} catch (Exception $e) {
if ($userId) {
$this->log->insertLog($userId, "IP Whitelist: Failed to add {$ip}: " . $e->getMessage(), 'system');
}
return false;
}
} }
// Remove from whitelist // Remove from whitelist