mirror of
https://github.com/ajnart/homarr.git
synced 2026-02-26 16:30:57 +01:00
feat(pi-hole): support no-auth (#4480)
This commit is contained in:
@@ -145,7 +145,7 @@ export const integrationDefs = {
|
||||
},
|
||||
piHole: {
|
||||
name: "Pi-hole",
|
||||
secretKinds: [["apiKey"]],
|
||||
secretKinds: [["apiKey"], []],
|
||||
iconUrl: "https://cdn.jsdelivr.net/gh/homarr-labs/dashboard-icons@master/svg/pi-hole.svg",
|
||||
category: ["dnsHole"],
|
||||
documentationUrl: createDocumentationLink("/docs/integrations/pi-hole"),
|
||||
|
||||
@@ -17,7 +17,7 @@ import { dnsBlockingGetSchema, sessionResponseSchema, statsSummaryGetSchema } fr
|
||||
const localLogger = logger.child({ module: "PiHoleIntegrationV6" });
|
||||
|
||||
export class PiHoleIntegrationV6 extends Integration implements DnsHoleSummaryIntegration {
|
||||
private readonly sessionStore: SessionStore<string>;
|
||||
private readonly sessionStore: SessionStore<{ sid: string | null }>;
|
||||
|
||||
constructor(integration: IntegrationInput) {
|
||||
super(integration);
|
||||
@@ -28,7 +28,7 @@ export class PiHoleIntegrationV6 extends Integration implements DnsHoleSummaryIn
|
||||
const response = await this.withAuthAsync(async (sessionId) => {
|
||||
return await fetchWithTrustedCertificatesAsync(this.url("/api/dns/blocking"), {
|
||||
headers: {
|
||||
sid: sessionId,
|
||||
sid: sessionId ?? undefined,
|
||||
},
|
||||
});
|
||||
});
|
||||
@@ -46,7 +46,7 @@ export class PiHoleIntegrationV6 extends Integration implements DnsHoleSummaryIn
|
||||
const response = await this.withAuthAsync(async (sessionId) => {
|
||||
return fetchWithTrustedCertificatesAsync(this.url("/api/stats/summary"), {
|
||||
headers: {
|
||||
sid: sessionId,
|
||||
sid: sessionId ?? undefined,
|
||||
},
|
||||
});
|
||||
});
|
||||
@@ -85,7 +85,7 @@ export class PiHoleIntegrationV6 extends Integration implements DnsHoleSummaryIn
|
||||
const response = await this.withAuthAsync(async (sessionId) => {
|
||||
return await fetchWithTrustedCertificatesAsync(this.url("/api/dns/blocking"), {
|
||||
headers: {
|
||||
sid: sessionId,
|
||||
sid: sessionId ?? undefined,
|
||||
},
|
||||
body: JSON.stringify({ blocking: true }),
|
||||
method: "POST",
|
||||
@@ -101,7 +101,7 @@ export class PiHoleIntegrationV6 extends Integration implements DnsHoleSummaryIn
|
||||
const response = await this.withAuthAsync(async (sessionId) => {
|
||||
return await fetchWithTrustedCertificatesAsync(this.url("/api/dns/blocking"), {
|
||||
headers: {
|
||||
sid: sessionId,
|
||||
sid: sessionId ?? undefined,
|
||||
},
|
||||
body: JSON.stringify({ blocking: false, timer: duration }),
|
||||
method: "POST",
|
||||
@@ -118,12 +118,16 @@ export class PiHoleIntegrationV6 extends Integration implements DnsHoleSummaryIn
|
||||
* @param callback
|
||||
* @returns
|
||||
*/
|
||||
private async withAuthAsync(callback: (sessionId: string) => Promise<UndiciResponse>) {
|
||||
private async withAuthAsync(callback: (sessionId: string | null) => Promise<UndiciResponse>) {
|
||||
if (!super.hasSecretValue("apiKey")) {
|
||||
return await callback(null);
|
||||
}
|
||||
|
||||
const storedSession = await this.sessionStore.getAsync();
|
||||
|
||||
if (storedSession) {
|
||||
localLogger.debug("Using stored session for request", { integrationId: this.integration.id });
|
||||
const response = await callback(storedSession);
|
||||
const response = await callback(storedSession.sid);
|
||||
if (response.status !== 401) {
|
||||
return response;
|
||||
}
|
||||
@@ -132,7 +136,7 @@ export class PiHoleIntegrationV6 extends Integration implements DnsHoleSummaryIn
|
||||
}
|
||||
|
||||
const sessionId = await this.getSessionAsync();
|
||||
await this.sessionStore.setAsync(sessionId);
|
||||
await this.sessionStore.setAsync({ sid: sessionId });
|
||||
const response = await callback(sessionId);
|
||||
return response;
|
||||
}
|
||||
@@ -141,11 +145,13 @@ export class PiHoleIntegrationV6 extends Integration implements DnsHoleSummaryIn
|
||||
* Get a session id from the Pi-hole server
|
||||
* @returns The session id
|
||||
*/
|
||||
private async getSessionAsync(fetchAsync: typeof undiciFetch = fetchWithTrustedCertificatesAsync): Promise<string> {
|
||||
const apiKey = super.getSecretValue("apiKey");
|
||||
private async getSessionAsync(
|
||||
fetchAsync: typeof undiciFetch = fetchWithTrustedCertificatesAsync,
|
||||
): Promise<string | null> {
|
||||
const apiKey = super.hasSecretValue("apiKey") ? super.getSecretValue("apiKey") : null;
|
||||
const response = await fetchAsync(this.url("/api/auth"), {
|
||||
method: "POST",
|
||||
body: JSON.stringify({ password: apiKey }),
|
||||
body: JSON.stringify({ password: apiKey ?? "" }),
|
||||
headers: {
|
||||
"User-Agent": "Homarr",
|
||||
},
|
||||
@@ -156,8 +162,13 @@ export class PiHoleIntegrationV6 extends Integration implements DnsHoleSummaryIn
|
||||
const data = await response.json();
|
||||
const result = await sessionResponseSchema.parseAsync(data);
|
||||
|
||||
if (!result.session.sid) {
|
||||
throw new ResponseError({ status: 401, url: response.url });
|
||||
if (!result.session.valid) {
|
||||
throw new ResponseError(
|
||||
{ status: 401, url: response.url },
|
||||
{
|
||||
cause: result.session.message ? new Error(result.session.message) : undefined,
|
||||
},
|
||||
);
|
||||
}
|
||||
|
||||
localLogger.info("Received session id successfully", { integrationId: this.integration.id });
|
||||
@@ -170,9 +181,14 @@ export class PiHoleIntegrationV6 extends Integration implements DnsHoleSummaryIn
|
||||
* @param sessionId The session id to remove
|
||||
*/
|
||||
private async clearSessionAsync(
|
||||
sessionId: string,
|
||||
sessionId: string | null,
|
||||
fetchAsync: typeof undiciFetch = fetchWithTrustedCertificatesAsync,
|
||||
) {
|
||||
if (!sessionId) {
|
||||
localLogger.debug("No session id to clear");
|
||||
return;
|
||||
}
|
||||
|
||||
const response = await fetchAsync(this.url("/api/auth"), {
|
||||
method: "DELETE",
|
||||
headers: {
|
||||
|
||||
@@ -2,6 +2,7 @@ import { z } from "zod/v4";
|
||||
|
||||
export const sessionResponseSchema = z.object({
|
||||
session: z.object({
|
||||
valid: z.boolean(),
|
||||
sid: z.string().nullable(),
|
||||
message: z.string().nullable(),
|
||||
}),
|
||||
|
||||
Reference in New Issue
Block a user