78 lines
2.0 KiB
PHP
78 lines
2.0 KiB
PHP
<?php
|
|
|
|
namespace App\Livewire\Ui\Security;
|
|
|
|
use Livewire\Attributes\On;
|
|
use Livewire\Component;
|
|
use Illuminate\Support\Str;
|
|
|
|
class Fail2banBanlist extends Component
|
|
{
|
|
public array $banned = [];
|
|
public string $jail = 'mailwolt-blacklist'; // Dein dedizierter Jail
|
|
|
|
#[On('f2b:refresh')]
|
|
public function refreshList(): void
|
|
{
|
|
$this->loadBannedIps();
|
|
}
|
|
|
|
public function mount(): void
|
|
{
|
|
$this->loadBannedIps();
|
|
}
|
|
|
|
private function loadBannedIps(): void
|
|
{
|
|
$output = @shell_exec(sprintf('sudo -n /usr/bin/fail2ban-client status %s 2>&1', escapeshellarg($this->jail)));
|
|
|
|
if (!$output) {
|
|
$this->banned = [];
|
|
return;
|
|
}
|
|
|
|
// Beispielausgabe:
|
|
// Status for the jail: mailwolt-blacklist
|
|
// |- Filter
|
|
// | |- Currently failed: 0
|
|
// | `- Total failed: 0
|
|
// `- Actions
|
|
// |- Currently banned: 2
|
|
// | `- IP list: 203.0.113.45 198.51.100.22
|
|
// `- Total banned: 2
|
|
|
|
if (preg_match('/IP list:\s*(.+)$/mi', $output, $m)) {
|
|
$ips = preg_split('/\s+/', trim($m[1]));
|
|
$this->banned = array_values(array_filter($ips, fn($ip) => filter_var($ip, FILTER_VALIDATE_IP)));
|
|
} else {
|
|
$this->banned = [];
|
|
}
|
|
}
|
|
|
|
public function unban(string $ip): void
|
|
{
|
|
if (!filter_var($ip, FILTER_VALIDATE_IP)) {
|
|
return;
|
|
}
|
|
|
|
$ipEsc = escapeshellarg($ip);
|
|
$cmd = sprintf('sudo -n /usr/bin/fail2ban-client set %s unbanip %s 2>&1', escapeshellarg($this->jail), $ipEsc);
|
|
@shell_exec($cmd);
|
|
|
|
$this->dispatch('toast',
|
|
type: 'info',
|
|
badge: 'Fail2Ban',
|
|
title: 'IP entbannt',
|
|
text: "Die IP {$ip} wurde erfolgreich entbannt.",
|
|
duration: 6000,
|
|
);
|
|
|
|
$this->refreshList();
|
|
}
|
|
|
|
public function render()
|
|
{
|
|
return view('livewire.ui.security.fail2ban-banlist');
|
|
}
|
|
}
|