From 27548fd3dc2f7b8ed9595556b958eeec2f700356 Mon Sep 17 00:00:00 2001 From: shamoon <4887959+shamoon@users.noreply.github.com> Date: Sat, 14 Sep 2024 23:07:51 -0700 Subject: [PATCH] Save this --- src/widgets/pihole/proxy.js | 20 +++++++++++++------- 1 file changed, 13 insertions(+), 7 deletions(-) diff --git a/src/widgets/pihole/proxy.js b/src/widgets/pihole/proxy.js index 724d4943..16d42606 100644 --- a/src/widgets/pihole/proxy.js +++ b/src/widgets/pihole/proxy.js @@ -57,25 +57,31 @@ export default async function piholeProxyHandler(req, res) { } // pihole v6 - if (!cache.get(`${sessionSIDCacheKey}.${service}`)) { + if (!cache.get(`${sessionSIDCacheKey}.${service}`) && widget.key) { await login(widget, service); } const sid = cache.get(`${sessionSIDCacheKey}.${service}`); - if (!sid) { + if (widget.key && !sid) { return res.status(500).json({ error: "Failed to authenticate with Pi-hole" }); } try { logger.debug("Calling Pi-hole API endpoint: %s", endpoint); - + const headers = { + "Content-Type": "application/json", + }; + if (sid) { + headers["X-FTL-SID"] = sid; + } else { + logger.debug("Pi-hole request is unauthenticated"); + } [status, , data] = await httpProxy(formatApiCall(widgets[widget.type].api, { ...widget, endpoint }), { - headers: { - "Content-Type": "application/json", - "X-FTL-SID": sid, - }, + headers, }); + logger.debug("Pi-hole API response: %d", status, Buffer.from(data).toString("utf8")); + if (status !== 200) { logger.error("Error calling Pi-Hole API: %d. Data: %s", status, data); return res.status(status).json({ error: "Pi-Hole API Error", data });