36 lines
1.7 KiB
PHP
36 lines
1.7 KiB
PHP
<?php
|
|
/** admin2/api/ipbans.php — IP ban management. */
|
|
require_once __DIR__ . '/../lib/helpers.php';
|
|
$pdo = db();
|
|
|
|
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
|
|
$input = json_decode(file_get_contents('php://input'), true);
|
|
$action = $input['action'] ?? '';
|
|
try {
|
|
if ($action === 'add') {
|
|
$until = time() + intval($input['duration'] ?? 86400);
|
|
$pdo->prepare("INSERT INTO ip_bans (ip, banned_until, ban_count) VALUES (?, ?, 1) ON CONFLICT (ip) DO UPDATE SET banned_until = ?, ban_count = ip_bans.ban_count + 1")->execute([$input['ip'], $until, $until]);
|
|
json_response(['message' => 'IP banned']);
|
|
} elseif ($action === 'remove') {
|
|
$pdo->prepare("DELETE FROM ip_bans WHERE ip = ?")->execute([$input['ip']]);
|
|
json_response(['message' => 'Ban removed']);
|
|
}
|
|
} catch (PDOException $e) { json_response(['error' => $e->getMessage()]); }
|
|
}
|
|
|
|
$now = time();
|
|
$total = 0; $active = 0; $issued = 0;
|
|
$bans = [];
|
|
try {
|
|
$total = intval($pdo->query("SELECT count(*) FROM ip_bans")->fetchColumn());
|
|
$active = intval($pdo->query("SELECT count(*) FROM ip_bans WHERE banned_until > $now")->fetchColumn());
|
|
$issued = intval($pdo->query("SELECT COALESCE(sum(ban_count),0) FROM ip_bans")->fetchColumn());
|
|
$bans = $pdo->query("SELECT ip, banned_until, ban_count, failures, authed_successfully, connection_attempts FROM ip_bans ORDER BY banned_until DESC LIMIT 100")->fetchAll();
|
|
foreach ($bans as &$b) {
|
|
$b['status'] = intval($b['banned_until']) > $now ? 'banned' : 'expired';
|
|
$b['banned_until'] = date('Y-m-d H:i:s', intval($b['banned_until']));
|
|
}
|
|
} catch (PDOException $e) {}
|
|
|
|
json_response(['total' => $total, 'active' => $active, 'issued' => $issued, 'bans' => $bans]);
|