diff --git a/app/Livewire/Ui/Security/Fail2BanCard.php b/app/Livewire/Ui/Security/Fail2BanCard.php index a3c2045..5860c60 100644 --- a/app/Livewire/Ui/Security/Fail2BanCard.php +++ b/app/Livewire/Ui/Security/Fail2BanCard.php @@ -7,10 +7,9 @@ use Livewire\Component; class Fail2BanCard extends Component { - public bool $available = true; // fail2ban-client vorhanden? - public bool $permDenied = false; // Socket/Root-Rechte fehlen? - public int $activeBans = 0; // Summe gebannter IPs - /** @var array */ + public bool $available = true; + public bool $permDenied = false; + public int $activeBans = 0; public array $jails = []; public function mount(): void @@ -28,12 +27,13 @@ class Fail2BanCard extends Component $this->load(true); } - // Optional: öffnet später dein Detail-Modal/Tab public function openDetails(string $jail): void { - $this->dispatch('openModal', component: 'ui.security.modal.fail2-ban-jail-modal', arguments: ['jail' => $jail]); + // KORREKTER DISPATCH für wire-elements/modal + $this->dispatch('openModal', 'ui.security.modal.fail2ban-jail-modal', ['jail' => $jail]); } - /* ---------------- intern ---------------- */ + + /* ------------------- intern ------------------- */ protected function load(bool $force = false): void { @@ -46,7 +46,6 @@ class Fail2BanCard extends Component return; } - // Rechtecheck [$ok, $raw] = $this->f2b('ping'); if (!$ok && stripos($raw, 'permission denied') !== false) { $this->available = true; @@ -56,18 +55,16 @@ class Fail2BanCard extends Component return; } - // Jails laden [, $status] = $this->f2b('status'); $jailsLn = $this->firstMatch('/Jail list:\s*(.+)$/mi', $status); $jails = $jailsLn ? array_filter(array_map('trim', preg_split('/\s*,\s*/', $jailsLn))) : []; $rows = []; $sum = 0; - foreach ($jails as $j) { [, $s] = $this->f2b('status ' . escapeshellarg($j)); $banned = (int)($this->firstMatch('/Currently banned:\s+(\d+)/i', $s) ?: 0); - $bantime = $this->getBantime($j); // Sek.; -1 = permanent + $bantime = $this->getBantime($j); $rows[] = ['name' => $j, 'banned' => $banned, 'bantime' => $bantime]; $sum += $banned; } @@ -78,15 +75,12 @@ class Fail2BanCard extends Component $this->jails = $rows; } - /** sudo + fail2ban-client ausführen; [ok, output] */ private function f2b(string $args): array { $sudo = '/usr/bin/sudo'; $f2b = '/usr/bin/fail2ban-client'; $out = (string)@shell_exec("timeout 2 $sudo -n $f2b $args 2>&1"); - $ok = stripos($out, 'Status') !== false - || stripos($out, 'Jail list') !== false - || stripos($out, 'pong') !== false; + $ok = str_contains($out, 'Status') || str_contains($out, 'Jail list') || str_contains($out, 'pong'); return [$ok, $out]; } @@ -95,7 +89,7 @@ class Fail2BanCard extends Component [, $out] = $this->f2b('get ' . escapeshellarg($jail) . ' bantime'); $val = trim($out); if (preg_match('/-?\d+/', $val, $m)) return (int)$m[0]; - return 600; // defensiver Default + return 600; } private function firstMatch(string $pattern, string $haystack): ?string @@ -105,6 +99,110 @@ class Fail2BanCard extends Component } +//namespace App\Livewire\Ui\Security; +// +//use Livewire\Component; +// +//class Fail2BanCard extends Component +//{ +// public bool $available = true; // fail2ban-client vorhanden? +// public bool $permDenied = false; // Socket/Root-Rechte fehlen? +// public int $activeBans = 0; // Summe gebannter IPs +// /** @var array */ +// public array $jails = []; +// +// public function mount(): void +// { +// $this->load(); +// } +// +// public function render() +// { +// return view('livewire.ui.security.fail2-ban-card'); +// } +// +// public function refresh(): void +// { +// $this->load(true); +// } +// +// // Optional: öffnet später dein Detail-Modal/Tab +// public function openDetails(string $jail): void +// { +// $this->dispatch('openModal', 'ui.security.modal.fail2-ban-jail-modal', ['jail' => $jail]); +// } +// /* ---------------- intern ---------------- */ +// +// protected function load(bool $force = false): void +// { +// $bin = trim((string)@shell_exec('command -v fail2ban-client 2>/dev/null')) ?: ''; +// if ($bin === '') { +// $this->available = false; +// $this->permDenied = false; +// $this->activeBans = 0; +// $this->jails = []; +// return; +// } +// +// // Rechtecheck +// [$ok, $raw] = $this->f2b('ping'); +// if (!$ok && stripos($raw, 'permission denied') !== false) { +// $this->available = true; +// $this->permDenied = true; +// $this->activeBans = 0; +// $this->jails = []; +// return; +// } +// +// // Jails laden +// [, $status] = $this->f2b('status'); +// $jailsLn = $this->firstMatch('/Jail list:\s*(.+)$/mi', $status); +// $jails = $jailsLn ? array_filter(array_map('trim', preg_split('/\s*,\s*/', $jailsLn))) : []; +// +// $rows = []; +// $sum = 0; +// +// foreach ($jails as $j) { +// [, $s] = $this->f2b('status ' . escapeshellarg($j)); +// $banned = (int)($this->firstMatch('/Currently banned:\s+(\d+)/i', $s) ?: 0); +// $bantime = $this->getBantime($j); // Sek.; -1 = permanent +// $rows[] = ['name' => $j, 'banned' => $banned, 'bantime' => $bantime]; +// $sum += $banned; +// } +// +// $this->available = true; +// $this->permDenied = false; +// $this->activeBans = $sum; +// $this->jails = $rows; +// } +// +// /** sudo + fail2ban-client ausführen; [ok, output] */ +// private function f2b(string $args): array +// { +// $sudo = '/usr/bin/sudo'; +// $f2b = '/usr/bin/fail2ban-client'; +// $out = (string)@shell_exec("timeout 2 $sudo -n $f2b $args 2>&1"); +// $ok = stripos($out, 'Status') !== false +// || stripos($out, 'Jail list') !== false +// || stripos($out, 'pong') !== false; +// return [$ok, $out]; +// } +// +// private function getBantime(string $jail): int +// { +// [, $out] = $this->f2b('get ' . escapeshellarg($jail) . ' bantime'); +// $val = trim($out); +// if (preg_match('/-?\d+/', $val, $m)) return (int)$m[0]; +// return 600; // defensiver Default +// } +// +// private function firstMatch(string $pattern, string $haystack): ?string +// { +// return preg_match($pattern, $haystack, $m) ? trim($m[1]) : null; +// } +//} + + //namespace App\Livewire\Ui\Security; // //use Livewire\Component; diff --git a/resources/views/livewire/ui/security/fail2-ban-card.blade.php b/resources/views/livewire/ui/security/fail2-ban-card.blade.php index c8a3f22..9d5daca 100644 --- a/resources/views/livewire/ui/security/fail2-ban-card.blade.php +++ b/resources/views/livewire/ui/security/fail2-ban-card.blade.php @@ -30,14 +30,22 @@
{{ $j['name'] }}
-
+ + Bannzeit: + @if($j['bantime'] === -1) + permanent + @else + {{ $j['bantime'] }}s + @endif + {{ $j['banned'] }} gebannt - {{-- Optional: Details öffnen (Tab/Modal) --}} - @@ -79,6 +87,68 @@ {{-- @endif--}} {{--
--}} +{{-- @if(!$available)--}} +{{--
fail2ban-client wurde nicht gefunden.
--}} +{{-- @elseif($permDenied)--}} +{{--
--}} +{{-- Keine Berechtigung auf /var/run/fail2ban/fail2ban.sock.--}} +{{-- Sudo-Regel prüfen.--}} +{{--
--}} +{{-- @else--}} +{{--
--}} +{{-- @forelse($jails as $j)--}} +{{--
--}} +{{--
--}} +{{--
{{ $j['name'] }}
--}} + +{{--
--}} +{{-- --}} +{{-- {{ $j['banned'] }} gebannt--}} +{{-- --}} +{{-- --}}{{-- Optional: Details öffnen (Tab/Modal) --}} +{{-- --}} +{{--
--}} +{{--
--}} +{{--
--}} +{{-- @empty--}} +{{--
Keine Jails gefunden.
--}} +{{-- @endforelse--}} +{{--
--}} + +{{--
--}} +{{-- --}} +{{--
--}} +{{-- @endif--}} +{{--
--}} + +{{--
--}} +{{--
--}} +{{--
--}} +{{-- --}} +{{-- Fail2Ban--}} +{{--
--}} + +{{-- @if($available)--}} +{{-- --}} +{{-- {{ $activeBans }} aktuell--}} +{{-- --}} +{{-- @else--}} +{{-- --}} +{{-- nicht installiert--}} +{{-- --}} +{{-- @endif--}} +{{--
--}} + {{-- @if(!$available)--}} {{--
fail2ban-client wurde nicht gefunden.
--}} {{-- @else--}}