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
This commit is contained in:
@@ -832,19 +832,50 @@ class Firewall extends Component
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Proxmox firewall log endpoint returns text/plain format
|
|
||||||
// Each log entry is on a new line
|
|
||||||
$body = $response->body();
|
$body = $response->body();
|
||||||
$rawLogs = [];
|
$rawLogs = [];
|
||||||
|
$contentType = strtolower($response->header('Content-Type', ''));
|
||||||
|
|
||||||
if (! empty($body)) {
|
if (! empty($body)) {
|
||||||
// Split by newlines (handle both \n and \r\n)
|
if (str_contains($contentType, 'application/json')) {
|
||||||
$lines = preg_split('/\r\n|\r|\n/', $body);
|
// When Accept header allows JSON, Proxmox returns structured data
|
||||||
$rawLogs = array_filter($lines, static function ($line) {
|
$jsonData = $response->json();
|
||||||
return ! empty(trim($line));
|
$entries = [];
|
||||||
});
|
|
||||||
// Re-index array after filtering
|
if (is_array($jsonData)) {
|
||||||
$rawLogs = array_values($rawLogs);
|
$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', [
|
\Log::debug('Firewall logs fetched from Proxmox API', [
|
||||||
|
|||||||
Reference in New Issue
Block a user