From a841f2bc7e09e9345ebbc69d7f6bf91996cebd43 Mon Sep 17 00:00:00 2001 From: "Joseph.Rawlings" Date: Tue, 2 Dec 2025 07:48:06 -0500 Subject: [PATCH] refactor: enhance log processing in Proxmox Firewall component to support both JSON and text/plain formats, improving log entry parsing and ensuring robust handling of structured data --- .../Servers/Proxmox/Livewire/Firewall.php | 49 +++++++++++++++---- 1 file changed, 40 insertions(+), 9 deletions(-) diff --git a/extensions/Servers/Proxmox/Livewire/Firewall.php b/extensions/Servers/Proxmox/Livewire/Firewall.php index 9cd3f6475..308a86fa5 100644 --- a/extensions/Servers/Proxmox/Livewire/Firewall.php +++ b/extensions/Servers/Proxmox/Livewire/Firewall.php @@ -832,19 +832,50 @@ class Firewall extends Component return; } - // Proxmox firewall log endpoint returns text/plain format - // Each log entry is on a new line $body = $response->body(); $rawLogs = []; + $contentType = strtolower($response->header('Content-Type', '')); if (! empty($body)) { - // Split by newlines (handle both \n and \r\n) - $lines = preg_split('/\r\n|\r|\n/', $body); - $rawLogs = array_filter($lines, static function ($line) { - return ! empty(trim($line)); - }); - // Re-index array after filtering - $rawLogs = array_values($rawLogs); + if (str_contains($contentType, 'application/json')) { + // When Accept header allows JSON, Proxmox returns structured data + $jsonData = $response->json(); + $entries = []; + + if (is_array($jsonData)) { + $entries = $jsonData['data'] ?? $jsonData; + } + + if (is_array($entries)) { + foreach ($entries as $entry) { + $logText = null; + + if (is_array($entry)) { + $logText = $entry['log'] + ?? $entry['text'] + ?? $entry['t'] + ?? $entry['n'] + ?? null; + } elseif (is_string($entry)) { + $logText = $entry; + } + + if (is_string($logText) && trim($logText) !== '') { + $rawLogs[] = trim($logText); + } + } + } + } else { + // Proxmox firewall log endpoint may also return text/plain + // Each log entry is on a new line (handle both \n and \r\n) + $lines = preg_split('/\r\n|\r|\n/', $body); + foreach ($lines as $line) { + $line = trim((string) $line); + if ($line !== '') { + $rawLogs[] = $line; + } + } + } } \Log::debug('Firewall logs fetched from Proxmox API', [