From 989cc7de585cc6d7cfd6bf8aed2ad6b4ab27a1fe Mon Sep 17 00:00:00 2001 From: Meier Lukas Date: Fri, 21 Mar 2025 20:13:44 +0100 Subject: [PATCH] Revert "Update PiHole authentication. (PiHole v6)" (#2287) --- src/tools/server/sdk/pihole/piHole.ts | 47 +++++++++------------------ 1 file changed, 15 insertions(+), 32 deletions(-) diff --git a/src/tools/server/sdk/pihole/piHole.ts b/src/tools/server/sdk/pihole/piHole.ts index a2267bcbd..52c55867f 100644 --- a/src/tools/server/sdk/pihole/piHole.ts +++ b/src/tools/server/sdk/pihole/piHole.ts @@ -12,43 +12,26 @@ export class PiHoleClient { this.baseHostName = trimStringEnding(hostname, ['/admin/index.php', '/admin', '/']); } - async getSummary() { - const authResponse = await fetch(`${this.baseHostName}/api/auth`, { - method: "POST", - headers: { "Content-Type": "application/json" }, - body: JSON.stringify({ password: this.apiToken }) - }); + async getSummary() { + const response = await fetch( + new URL(`${this.baseHostName}/admin/api.php?summaryRaw&auth=${this.apiToken}`) + ); - if (!authResponse.ok) { - throw new Error(`Authentication failed: ${authResponse.status}`); - } + if (response.status !== 200) { + throw new Error(`Status code does not indicate success: ${response.status}`); + } - const authJson = await authResponse.json(); - if (!authJson.session?.valid || !authJson.session.sid) { - throw new Error(`Invalid session response: ${JSON.stringify(authJson)}`); - } + const json = await response.json(); - const sessionId = authJson.session.sid; - - const response = await fetch( - new URL(`${this.baseHostName}/api.php?summaryRaw&auth=${sessionId}`) + if (Array.isArray(json)) { + throw new Error( + `Response does not indicate success. Authentication is most likely invalid: ${json}` ); + } - if (response.status !== 200) { - throw new Error(`Failed to fetch summary: ${response.status}`); - } - - const json = await response.json(); - - if (Array.isArray(json)) { - throw new Error( - `Response does not indicate success. Authentication might be invalid: ${json}` - ); - } - return json as PiHoleApiSummaryResponse; + return json as PiHoleApiSummaryResponse; } - async enable() { const response = await this.sendStatusChangeRequest('enable'); return response.status === 'enabled'; @@ -65,8 +48,8 @@ export class PiHoleClient { ): Promise { const response = await fetch( duration !== 0 - ? `${this.baseHostName}/api?${action}=${duration}&auth=${this.apiToken}` - : `${this.baseHostName}/api?${action}&auth=${this.apiToken}` + ? `${this.baseHostName}/admin/api.php?${action}=${duration}&auth=${this.apiToken}` + : `${this.baseHostName}/admin/api.php?${action}&auth=${this.apiToken}` ); if (response.status !== 200) {