feat(pihole): add support for v6 (#2448)

* feat(pihole): add support for v6

* fix: add session-store to keep using same session for pi-hole requests

* chore: address pull request feedback

* fix: import issue

* fix: other import errors
This commit is contained in:
Meier Lukas
2025-03-04 21:17:35 +01:00
committed by GitHub
parent e88b29dc9d
commit 3804d530ec
33 changed files with 624 additions and 98 deletions

View File

@@ -0,0 +1,22 @@
import { removeTrailingSlash } from "@homarr/common";
import type { IntegrationInput } from "../base/integration";
import { PiHoleIntegrationV5 } from "./v5/pi-hole-integration-v5";
import { PiHoleIntegrationV6 } from "./v6/pi-hole-integration-v6";
export const createPiHoleIntegrationAsync = async (input: IntegrationInput) => {
const baseUrl = removeTrailingSlash(input.url);
const url = new URL(`${baseUrl}/api/info/version`);
const response = await fetch(url);
/**
* In pi-hole 5 the api was at /admin/api.php, in pi-hole 6 it was moved to /api
* For the /api/info/version endpoint, the response is 404 in pi-hole 5
* and 401 in pi-hole 6
*/
if (response.status === 404) {
return new PiHoleIntegrationV5(input);
}
return new PiHoleIntegrationV6(input);
};

View File

@@ -1,12 +1,12 @@
import { fetchWithTrustedCertificatesAsync } from "@homarr/certificates/server";
import { Integration } from "../base/integration";
import { IntegrationTestConnectionError } from "../base/test-connection-error";
import type { DnsHoleSummaryIntegration } from "../interfaces/dns-hole-summary/dns-hole-summary-integration";
import type { DnsHoleSummary } from "../interfaces/dns-hole-summary/dns-hole-summary-types";
import { summaryResponseSchema } from "./pi-hole-types";
import { Integration } from "../../base/integration";
import { IntegrationTestConnectionError } from "../../base/test-connection-error";
import type { DnsHoleSummaryIntegration } from "../../interfaces/dns-hole-summary/dns-hole-summary-integration";
import type { DnsHoleSummary } from "../../interfaces/dns-hole-summary/dns-hole-summary-types";
import { summaryResponseSchema } from "./pi-hole-schemas-v5";
export class PiHoleIntegration extends Integration implements DnsHoleSummaryIntegration {
export class PiHoleIntegrationV5 extends Integration implements DnsHoleSummaryIntegration {
public async getSummaryAsync(): Promise<DnsHoleSummary> {
const apiKey = super.getSecretValue("apiKey");
const response = await fetchWithTrustedCertificatesAsync(this.url("/admin/api.php?summaryRaw", { auth: apiKey }));

View File

@@ -7,7 +7,3 @@ export const summaryResponseSchema = z.object({
dns_queries_today: z.number(),
ads_percentage_today: z.number(),
});
export const controlsInputSchema = z.object({
duration: z.number().optional(),
});

View File

@@ -0,0 +1,204 @@
import type { Response as UndiciResponse } from "undici";
import type { z } from "zod";
import { fetchWithTrustedCertificatesAsync } from "@homarr/certificates/server";
import { extractErrorMessage } from "@homarr/common";
import { logger } from "@homarr/log";
import { IntegrationResponseError, ParseError, ResponseError } from "../../base/error";
import type { IntegrationInput } from "../../base/integration";
import { Integration } from "../../base/integration";
import type { SessionStore } from "../../base/session-store";
import { createSessionStore } from "../../base/session-store";
import { IntegrationTestConnectionError } from "../../base/test-connection-error";
import type { DnsHoleSummaryIntegration } from "../../interfaces/dns-hole-summary/dns-hole-summary-integration";
import type { DnsHoleSummary } from "../../types";
import { dnsBlockingGetSchema, sessionResponseSchema, statsSummaryGetSchema } from "./pi-hole-schemas-v6";
const localLogger = logger.child({ module: "PiHoleIntegrationV6" });
export class PiHoleIntegrationV6 extends Integration implements DnsHoleSummaryIntegration {
private readonly sessionStore: SessionStore;
constructor(integration: IntegrationInput) {
super(integration);
this.sessionStore = createSessionStore(integration);
}
public async getDnsBlockingStatusAsync(): Promise<z.infer<typeof dnsBlockingGetSchema>> {
const response = await this.withAuthAsync(async (sessionId) => {
return await fetchWithTrustedCertificatesAsync(this.url("/api/dns/blocking"), {
headers: {
sid: sessionId,
},
});
});
if (!response.ok) {
throw new IntegrationResponseError(this.integration, response, await response.json());
}
const result = dnsBlockingGetSchema.safeParse(await response.json());
if (!result.success) {
throw new ParseError("DNS blocking status", result.error, await response.json());
}
return result.data;
}
private async getStatsSummaryAsync(): Promise<z.infer<typeof statsSummaryGetSchema>> {
const response = await this.withAuthAsync(async (sessionId) => {
return await fetchWithTrustedCertificatesAsync(this.url("/api/stats/summary"), {
headers: {
sid: sessionId,
},
});
});
if (!response.ok) {
throw new IntegrationResponseError(this.integration, response, await response.json());
}
const data = await response.json();
const result = statsSummaryGetSchema.safeParse(data);
if (!result.success) {
throw new ParseError("stats summary", result.error, data);
}
return result.data;
}
public async getSummaryAsync(): Promise<DnsHoleSummary> {
const dnsStatsSummary = await this.getStatsSummaryAsync();
const dnsBlockingStatus = await this.getDnsBlockingStatusAsync();
return {
status: dnsBlockingStatus.blocking,
adsBlockedToday: dnsStatsSummary.queries.blocked,
adsBlockedTodayPercentage: dnsStatsSummary.queries.percent_blocked,
domainsBeingBlocked: dnsStatsSummary.gravity.domains_being_blocked,
dnsQueriesToday: dnsStatsSummary.queries.total,
};
}
public async testConnectionAsync(): Promise<void> {
try {
const sessionId = await this.getSessionAsync();
await this.clearSessionAsync(sessionId);
} catch (error: unknown) {
if (error instanceof ParseError) {
throw new IntegrationTestConnectionError("invalidJson");
}
if (error instanceof ResponseError && error.statusCode === 401) {
throw new IntegrationTestConnectionError("invalidCredentials");
}
throw new IntegrationTestConnectionError("commonError", extractErrorMessage(error));
}
}
public async enableAsync(): Promise<void> {
const response = await this.withAuthAsync(async (sessionId) => {
return await fetchWithTrustedCertificatesAsync(this.url("/api/dns/blocking"), {
headers: {
sid: sessionId,
},
body: JSON.stringify({ blocking: true }),
method: "POST",
});
});
if (!response.ok) {
throw new IntegrationResponseError(this.integration, response, await response.json());
}
}
public async disableAsync(duration?: number): Promise<void> {
const response = await this.withAuthAsync(async (sessionId) => {
return await fetchWithTrustedCertificatesAsync(this.url("/api/dns/blocking"), {
headers: {
sid: sessionId,
},
body: JSON.stringify({ blocking: false, timer: duration }),
method: "POST",
});
});
if (!response.ok) {
throw new IntegrationResponseError(this.integration, response, await response.json());
}
}
/**
* Run the callback with the current session id
* @param callback
* @returns
*/
private async withAuthAsync(callback: (sessionId: string) => Promise<UndiciResponse>) {
const storedSession = await this.sessionStore.getAsync();
if (storedSession) {
localLogger.debug("Using stored session for request", { integrationId: this.integration.id });
const response = await callback(storedSession);
if (response.status !== 401) {
return response;
}
localLogger.info("Session expired, getting new session", { integrationId: this.integration.id });
}
const sessionId = await this.getSessionAsync();
await this.sessionStore.setAsync(sessionId);
const response = await callback(sessionId);
return response;
}
/**
* Get a session id from the Pi-hole server
* @returns The session id
*/
private async getSessionAsync(): Promise<string> {
const apiKey = super.getSecretValue("apiKey");
const response = await fetchWithTrustedCertificatesAsync(this.url("/api/auth"), {
method: "POST",
body: JSON.stringify({ password: apiKey }),
headers: {
"User-Agent": "Homarr",
},
});
const data = await response.json();
const result = sessionResponseSchema.safeParse(data);
if (!result.success) {
throw new ParseError("session response", result.error, data);
}
if (!result.data.session.sid) {
throw new IntegrationResponseError(this.integration, response, data);
}
localLogger.info("Received session id successfully", { integrationId: this.integration.id });
return result.data.session.sid;
}
/**
* Remove the session from the Pi-hole server
* @param sessionId The session id to remove
*/
private async clearSessionAsync(sessionId: string) {
const response = await fetchWithTrustedCertificatesAsync(this.url("/api/auth"), {
method: "DELETE",
headers: {
sid: sessionId,
},
});
if (!response.ok) {
localLogger.warn("Failed to clear session", { statusCode: response.status, content: await response.text() });
}
logger.debug("Cleared session successfully");
}
}

View File

@@ -0,0 +1,28 @@
import { z } from "zod";
export const sessionResponseSchema = z.object({
session: z.object({
sid: z.string().nullable(),
message: z.string().nullable(),
}),
});
export const dnsBlockingGetSchema = z.object({
blocking: z.enum(["enabled", "disabled", "failed", "unknown"]).transform((value) => {
if (value === "failed") return undefined;
if (value === "unknown") return undefined;
return value;
}),
timer: z.number().nullable(),
});
export const statsSummaryGetSchema = z.object({
queries: z.object({
total: z.number(),
blocked: z.number(),
percent_blocked: z.number(),
}),
gravity: z.object({
domains_being_blocked: z.number(),
}),
});