From 0858d33f38cb624022ea164066ac4a67f77a760a Mon Sep 17 00:00:00 2001 From: Lars Gebhardt-Kusche Date: Mon, 27 Apr 2026 01:09:12 +0200 Subject: [PATCH] adasd --- modules/pihole/assets/pihole.js | 3 +- modules/pihole/design.json | 4 +- modules/pihole/pages/api.php | 84 +++++++++++++++++++++++++++++---- 3 files changed, 78 insertions(+), 13 deletions(-) diff --git a/modules/pihole/assets/pihole.js b/modules/pihole/assets/pihole.js index 5f8eb56..833d8c5 100644 --- a/modules/pihole/assets/pihole.js +++ b/modules/pihole/assets/pihole.js @@ -145,7 +145,8 @@ const code = err.http_code ? `HTTP ${err.http_code}` : 'HTTP ?'; const scope = err.scope || 'request'; const msg = err.error || 'error'; - return `${scope}: ${msg} (${code})`; + const hint = err.hint ? `, Hint: ${typeof err.hint === 'string' ? err.hint : JSON.stringify(err.hint)}` : ''; + return `${scope}: ${msg} (${code}${hint})`; }); errorEl.textContent = `API Fehler: ${lines.join(' | ')}`; } else { diff --git a/modules/pihole/design.json b/modules/pihole/design.json index ef5b517..4e1a89e 100644 --- a/modules/pihole/design.json +++ b/modules/pihole/design.json @@ -3,12 +3,10 @@ "title": "Pi-hole", "description": "Pi-hole Monitoring, Listen und Steuerung fuer mehrere Instanzen.", "actions": [ - { "label": "Zur Startseite", "href": "/", "variant": "ghost" }, - { "label": "Instanzen", "href": "/module/pihole/instances", "variant": "secondary" } + { "label": "Zur Startseite", "href": "/", "variant": "ghost" } ], "tabs": [ { "label": "Dashboard", "href": "/module/pihole" }, - { "label": "Instanzen", "href": "/module/pihole/instances" }, { "label": "Listen", "href": "/module/pihole/lists" }, { "label": "Queries", "href": "/module/pihole/queries" } ] diff --git a/modules/pihole/pages/api.php b/modules/pihole/pages/api.php index 4aa710f..76f2d4a 100644 --- a/modules/pihole/pages/api.php +++ b/modules/pihole/pages/api.php @@ -87,6 +87,32 @@ $httpRequest = function (string $method, string $url, array $headers, ?string $b return ['ok' => false, 'error' => 'invalid_json', 'http_code' => $httpCode, 'raw' => $raw, 'url' => $url]; } + if ($httpCode >= 400) { + $apiError = is_array($data['error'] ?? null) ? $data['error'] : []; + return [ + 'ok' => false, + 'error' => (string)($apiError['message'] ?? $apiError['key'] ?? ('HTTP ' . $httpCode)), + 'error_key' => (string)($apiError['key'] ?? ''), + 'hint' => $apiError['hint'] ?? null, + 'http_code' => $httpCode, + 'data' => $data, + 'url' => $url, + ]; + } + + if (isset($data['error'])) { + $apiError = is_array($data['error']) ? $data['error'] : []; + return [ + 'ok' => false, + 'error' => (string)($apiError['message'] ?? $apiError['key'] ?? 'api_error'), + 'error_key' => (string)($apiError['key'] ?? ''), + 'hint' => $apiError['hint'] ?? null, + 'http_code' => $httpCode, + 'data' => $data, + 'url' => $url, + ]; + } + return ['ok' => true, 'data' => $data, 'http_code' => $httpCode, 'url' => $url]; }; @@ -156,7 +182,23 @@ $v6Request = function (array $instance, string $path, string $method, array $pay return $httpRequest($method, $url, $headers, $body, $verify, $timeout); }; -$detectApi = function (array $instance) use ($v6Auth, $v6Request, $v5Request): array { +$v6RequestAny = function (array $instance, array $paths, string $method, array $payload, string $sid) use ($v6Request): array { + $last = ['ok' => false, 'error' => 'no_path', 'http_code' => 0, 'url' => '']; + foreach ($paths as $path) { + $result = $v6Request($instance, (string)$path, $method, $payload, $sid); + if (($result['ok'] ?? false) === true) { + return $result; + } + $last = $result; + $httpCode = (int)($result['http_code'] ?? 0); + if (!in_array($httpCode, [0, 400, 404], true)) { + return $result; + } + } + return $last; +}; + +$detectApi = function (array $instance) use ($v6Auth, $v6RequestAny, $v5Request): array { $sid = ''; $authRes = null; if (!empty($instance['password'])) { @@ -166,7 +208,7 @@ $detectApi = function (array $instance) use ($v6Auth, $v6Request, $v5Request): a } } - $probe = $v6Request($instance, 'stats/summary', 'GET', [], $sid); + $probe = $v6RequestAny($instance, ['stats/summary', 'summary'], 'GET', [], $sid); if ($probe['ok'] || in_array((int)($probe['http_code'] ?? 0), [401, 403], true)) { return ['version' => 6, 'sid' => $sid, 'probe' => $probe, 'auth' => $authRes]; } @@ -306,6 +348,8 @@ if ($action === 'dashboard') { return [ 'scope' => $scope, 'error' => $result['error'] ?? 'error', + 'error_key' => $result['error_key'] ?? '', + 'hint' => $result['hint'] ?? null, 'http_code' => $result['http_code'] ?? 0, 'url' => $result['url'] ?? '', ]; @@ -333,17 +377,22 @@ if ($action === 'dashboard') { $errors[] = $makeError('summary', $summary); } - $blocking = $v6Request($instance, 'dns/blocking', 'GET', [], $sid); + $blocking = $v6RequestAny($instance, ['dns/blocking'], 'GET', [], $sid); if (!$blocking['ok']) { $errors[] = $makeError('blocking', $blocking); } - $queries = $v6Request($instance, 'queries?from=' . $from . '&until=' . $now . '&length=2000', 'GET', [], $sid); + $queries = $v6RequestAny($instance, [ + 'queries', + 'queries/all', + 'queries?from=' . $from . '&until=' . $now, + 'queries/all?from=' . $from . '&until=' . $now, + ], 'GET', [], $sid); if (!$queries['ok']) { $errors[] = $makeError('queries', $queries); } - $upstreams = $v6Request($instance, 'stats/upstreams', 'GET', [], $sid); + $upstreams = $v6RequestAny($instance, ['stats/upstreams', 'upstreams'], 'GET', [], $sid); if (!$upstreams['ok']) { $errors[] = $makeError('upstreams', $upstreams); } @@ -383,7 +432,14 @@ if ($action === 'dashboard') { } if ($queries['ok'] && is_array($queries['data'])) { - $queryList = (array)($queries['data']['queries'] ?? []); + $queryList = []; + if (isset($queries['data']['queries']) && is_array($queries['data']['queries'])) { + $queryList = $queries['data']['queries']; + } elseif (isset($queries['data']['data']) && is_array($queries['data']['data'])) { + $queryList = $queries['data']['data']; + } elseif (array_is_list($queries['data'])) { + $queryList = $queries['data']; + } $topAds = []; $topQueries = []; $sources = []; @@ -418,8 +474,11 @@ if ($action === 'dashboard') { $topQueries[$domain] = ($topQueries[$domain] ?? 0) + 1; } - $statusVal = (string)($entry['status'] ?? ''); - $isBlocked = str_contains($statusVal, 'GRAVITY') || str_contains($statusVal, 'BLOCK'); + $statusVal = strtoupper((string)($entry['status'] ?? '')); + $isBlocked = str_contains($statusVal, 'GRAVITY') + || str_contains($statusVal, 'BLOCK') + || str_contains($statusVal, 'DENY') + || str_contains($statusVal, 'CNAME'); if ($isBlocked && $domain !== '') { $topAds[$domain] = ($topAds[$domain] ?? 0) + 1; $recent[] = ['domain' => $domain, 'instance' => $instance['name']]; @@ -432,7 +491,14 @@ if ($action === 'dashboard') { } if ($upstreams['ok'] && is_array($upstreams['data'])) { - $upList = (array)($upstreams['data']['upstreams'] ?? []); + $upList = []; + if (isset($upstreams['data']['upstreams']) && is_array($upstreams['data']['upstreams'])) { + $upList = $upstreams['data']['upstreams']; + } elseif (isset($upstreams['data']['data']) && is_array($upstreams['data']['data'])) { + $upList = $upstreams['data']['data']; + } elseif (array_is_list($upstreams['data'])) { + $upList = $upstreams['data']; + } $forwardDestData = []; foreach ($upList as $item) { if (!is_array($item)) {