Revert "Update PiHole authentication. (PiHole v6)" (#2287)

This commit is contained in:
Meier Lukas
2025-03-21 20:13:44 +01:00
committed by GitHub
parent 677224f8de
commit 989cc7de58

View File

@@ -12,43 +12,26 @@ export class PiHoleClient {
this.baseHostName = trimStringEnding(hostname, ['/admin/index.php', '/admin', '/']); this.baseHostName = trimStringEnding(hostname, ['/admin/index.php', '/admin', '/']);
} }
async getSummary() { async getSummary() {
const authResponse = await fetch(`${this.baseHostName}/api/auth`, { const response = await fetch(
method: "POST", new URL(`${this.baseHostName}/admin/api.php?summaryRaw&auth=${this.apiToken}`)
headers: { "Content-Type": "application/json" }, );
body: JSON.stringify({ password: this.apiToken })
});
if (!authResponse.ok) { if (response.status !== 200) {
throw new Error(`Authentication failed: ${authResponse.status}`); throw new Error(`Status code does not indicate success: ${response.status}`);
} }
const authJson = await authResponse.json(); const json = await response.json();
if (!authJson.session?.valid || !authJson.session.sid) {
throw new Error(`Invalid session response: ${JSON.stringify(authJson)}`);
}
const sessionId = authJson.session.sid; if (Array.isArray(json)) {
throw new Error(
const response = await fetch( `Response does not indicate success. Authentication is most likely invalid: ${json}`
new URL(`${this.baseHostName}/api.php?summaryRaw&auth=${sessionId}`)
); );
}
if (response.status !== 200) { return json as PiHoleApiSummaryResponse;
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;
} }
async enable() { async enable() {
const response = await this.sendStatusChangeRequest('enable'); const response = await this.sendStatusChangeRequest('enable');
return response.status === 'enabled'; return response.status === 'enabled';
@@ -65,8 +48,8 @@ export class PiHoleClient {
): Promise<PiHoleApiStatusChangeResponse> { ): Promise<PiHoleApiStatusChangeResponse> {
const response = await fetch( const response = await fetch(
duration !== 0 duration !== 0
? `${this.baseHostName}/api?${action}=${duration}&auth=${this.apiToken}` ? `${this.baseHostName}/admin/api.php?${action}=${duration}&auth=${this.apiToken}`
: `${this.baseHostName}/api?${action}&auth=${this.apiToken}` : `${this.baseHostName}/admin/api.php?${action}&auth=${this.apiToken}`
); );
if (response.status !== 200) { if (response.status !== 200) {