From 3240b7a169b8d51ab83138342f2ee69662993181 Mon Sep 17 00:00:00 2001 From: theshaun Date: Sat, 13 May 2023 17:20:57 +1000 Subject: [PATCH 1/8] Add support for pfSense API --- src/widgets/pfsenseapi/component.jsx | 71 ++++++++++++++++++++++++++++ src/widgets/pfsenseapi/widget.js | 24 ++++++++++ 2 files changed, 95 insertions(+) create mode 100644 src/widgets/pfsenseapi/component.jsx create mode 100644 src/widgets/pfsenseapi/widget.js diff --git a/src/widgets/pfsenseapi/component.jsx b/src/widgets/pfsenseapi/component.jsx new file mode 100644 index 00000000..00e0719e --- /dev/null +++ b/src/widgets/pfsenseapi/component.jsx @@ -0,0 +1,71 @@ +import { useTranslation } from "next-i18next"; + +import Container from "components/services/widget/container"; +import Block from "components/services/widget/block"; +import useWidgetAPI from "utils/proxy/use-widget-api"; +import { sys } from "typescript"; + +export default function Component({ service }) { + const { t } = useTranslation(); + + const { widget } = service; + + const { data: systemData, error: systemError } = useWidgetAPI(widget, "system"); + const { data: interfaceData, error: interfaceError } = useWidgetAPI(widget, "interface"); + + const showDiskUsage = widget.fields?.includes('disk'); + const showWanIP = widget.fields?.includes('wanIP'); + + if (systemError || interfaceError) { + const finalError = systemError ?? interfaceError; + return ; + } + + if (!systemData || !interfaceData) { + return ( + + + + {showDiskUsage && } + + + {showWanIP && } + + ); + } + + + const loadAvg = systemData.data.load_avg[0]; + const memory = systemData.data.mem_usage; + const tempC = systemData.data.temp_c + const disk = systemData.data.disk_usage + + const wanState = interfaceData.data.filter(l => l.hwif === widget.wan)[0].status + const wanStatus = (wanState == "up") ? + {t("pfsenseapi.up")}: + {t("pfsenseapi.down")}; + const wanIP = interfaceData.data.filter(l => l.hwif === widget.wan)[0].ipaddr + + return ( + + + + {showDiskUsage && } + + + {showWanIP && } + + ); +} diff --git a/src/widgets/pfsenseapi/widget.js b/src/widgets/pfsenseapi/widget.js new file mode 100644 index 00000000..88aa44fd --- /dev/null +++ b/src/widgets/pfsenseapi/widget.js @@ -0,0 +1,24 @@ + +import genericProxyHandler from "utils/proxy/handlers/generic"; + +const widget = { + api: "{url}/api/v1/{endpoint}", + proxyHandler: genericProxyHandler, + + mappings: { + system: { + endpoint: "status/system", + validate: [ + "data" + ] + }, + interface: { + endpoint: "status/interface", + validate: [ + "data" + ] + } + }, +}; + +export default widget; From 1ca240725697ce81f3c89daccaf4f00895c532f1 Mon Sep 17 00:00:00 2001 From: theshaun Date: Sat, 13 May 2023 17:23:51 +1000 Subject: [PATCH 2/8] Fix linting issues --- src/widgets/pfsenseapi/component.jsx | 21 +++++++++++++++++---- 1 file changed, 17 insertions(+), 4 deletions(-) diff --git a/src/widgets/pfsenseapi/component.jsx b/src/widgets/pfsenseapi/component.jsx index 00e0719e..34594ccd 100644 --- a/src/widgets/pfsenseapi/component.jsx +++ b/src/widgets/pfsenseapi/component.jsx @@ -3,7 +3,6 @@ import { useTranslation } from "next-i18next"; import Container from "components/services/widget/container"; import Block from "components/services/widget/block"; import useWidgetAPI from "utils/proxy/use-widget-api"; -import { sys } from "typescript"; export default function Component({ service }) { const { t } = useTranslation(); @@ -41,7 +40,7 @@ export default function Component({ service }) { const disk = systemData.data.disk_usage const wanState = interfaceData.data.filter(l => l.hwif === widget.wan)[0].status - const wanStatus = (wanState == "up") ? + const wanStatus = (wanState === "up") ? {t("pfsenseapi.up")}: {t("pfsenseapi.down")}; const wanIP = interfaceData.data.filter(l => l.hwif === widget.wan)[0].ipaddr @@ -56,7 +55,14 @@ export default function Component({ service }) { label="pfsenseapi.memory" value={t("common.percent", { value: (memory * 100).toFixed(2) })} /> - {showDiskUsage && } + { + showDiskUsage + && + + } - {showWanIP && } + { + showWanIP + && + + } ); } From 668779ede1688325bccc6f59811ca7562c6728b4 Mon Sep 17 00:00:00 2001 From: theshaun Date: Sat, 13 May 2023 17:24:25 +1000 Subject: [PATCH 3/8] remove a line --- src/widgets/pfsenseapi/component.jsx | 1 - 1 file changed, 1 deletion(-) diff --git a/src/widgets/pfsenseapi/component.jsx b/src/widgets/pfsenseapi/component.jsx index 34594ccd..ad3de80b 100644 --- a/src/widgets/pfsenseapi/component.jsx +++ b/src/widgets/pfsenseapi/component.jsx @@ -33,7 +33,6 @@ export default function Component({ service }) { ); } - const loadAvg = systemData.data.load_avg[0]; const memory = systemData.data.mem_usage; const tempC = systemData.data.temp_c From 865206802c829694cdc302c66477c0a903c96811 Mon Sep 17 00:00:00 2001 From: theshaun Date: Sat, 13 May 2023 17:44:13 +1000 Subject: [PATCH 4/8] rename cpu to load in default block --- src/widgets/pfsenseapi/component.jsx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/widgets/pfsenseapi/component.jsx b/src/widgets/pfsenseapi/component.jsx index ad3de80b..74c3adcf 100644 --- a/src/widgets/pfsenseapi/component.jsx +++ b/src/widgets/pfsenseapi/component.jsx @@ -23,7 +23,7 @@ export default function Component({ service }) { if (!systemData || !interfaceData) { return ( - + {showDiskUsage && } From db2481cf0e9d350897136e207171bac407f795a9 Mon Sep 17 00:00:00 2001 From: theshaun Date: Sat, 13 May 2023 18:04:12 +1000 Subject: [PATCH 5/8] Re-order container blocks to ensure defaults show --- src/widgets/pfsenseapi/component.jsx | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/src/widgets/pfsenseapi/component.jsx b/src/widgets/pfsenseapi/component.jsx index 74c3adcf..4c8b3d67 100644 --- a/src/widgets/pfsenseapi/component.jsx +++ b/src/widgets/pfsenseapi/component.jsx @@ -25,10 +25,10 @@ export default function Component({ service }) { - {showDiskUsage && } {showWanIP && } + {showDiskUsage && } ); } @@ -54,14 +54,6 @@ export default function Component({ service }) { label="pfsenseapi.memory" value={t("common.percent", { value: (memory * 100).toFixed(2) })} /> - { - showDiskUsage - && - - } } + { + showDiskUsage + && + + } ); } From 2ca46b595b5cf9945ec612ae03eb51c4581f343a Mon Sep 17 00:00:00 2001 From: theshaun Date: Fri, 19 May 2023 10:41:04 +1000 Subject: [PATCH 6/8] Fix issue with key expiring and not updating correctly --- src/widgets/qnap/proxy.js | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/src/widgets/qnap/proxy.js b/src/widgets/qnap/proxy.js index 3da4c20a..489f24ac 100644 --- a/src/widgets/qnap/proxy.js +++ b/src/widgets/qnap/proxy.js @@ -57,8 +57,17 @@ async function apiCall(widget, endpoint, service) { return { status, contentType, data: null, responseHeaders }; } - const dataDecoded = xml2json(data.toString(), { compact: true }); - return { status, contentType, data: JSON.parse(dataDecoded.toString()), responseHeaders }; + let dataDecoded = JSON.parse(xml2json(data.toString(), { compact: true }).toString()); + + if (dataDecoded.QDocRoot.authPassed._cdata === '0') { + logger.error("QNAP API rejected the request, attempting to obtain new session token"); + key = await login(widget, service); + apiUrl = new URL(formatApiCall(`${endpoint}&sid=${key}`, widget)); + [status, contentType, data, responseHeaders] = await httpProxy(apiUrl); + dataDecoded = JSON.parse(xml2json(data.toString(), { compact: true }).toString()); + } + + return { status, contentType, data: dataDecoded, responseHeaders }; } export default async function qnapProxyHandler(req, res) { From d402be2b78e14490521a7940a4c22ccf80bd0d83 Mon Sep 17 00:00:00 2001 From: theshaun Date: Fri, 19 May 2023 10:46:36 +1000 Subject: [PATCH 7/8] clean up --- src/widgets/pfsenseapi/component.jsx | 83 ---------------------------- src/widgets/pfsenseapi/widget.js | 24 -------- 2 files changed, 107 deletions(-) delete mode 100644 src/widgets/pfsenseapi/component.jsx delete mode 100644 src/widgets/pfsenseapi/widget.js diff --git a/src/widgets/pfsenseapi/component.jsx b/src/widgets/pfsenseapi/component.jsx deleted file mode 100644 index 4c8b3d67..00000000 --- a/src/widgets/pfsenseapi/component.jsx +++ /dev/null @@ -1,83 +0,0 @@ -import { useTranslation } from "next-i18next"; - -import Container from "components/services/widget/container"; -import Block from "components/services/widget/block"; -import useWidgetAPI from "utils/proxy/use-widget-api"; - -export default function Component({ service }) { - const { t } = useTranslation(); - - const { widget } = service; - - const { data: systemData, error: systemError } = useWidgetAPI(widget, "system"); - const { data: interfaceData, error: interfaceError } = useWidgetAPI(widget, "interface"); - - const showDiskUsage = widget.fields?.includes('disk'); - const showWanIP = widget.fields?.includes('wanIP'); - - if (systemError || interfaceError) { - const finalError = systemError ?? interfaceError; - return ; - } - - if (!systemData || !interfaceData) { - return ( - - - - - - {showWanIP && } - {showDiskUsage && } - - ); - } - - const loadAvg = systemData.data.load_avg[0]; - const memory = systemData.data.mem_usage; - const tempC = systemData.data.temp_c - const disk = systemData.data.disk_usage - - const wanState = interfaceData.data.filter(l => l.hwif === widget.wan)[0].status - const wanStatus = (wanState === "up") ? - {t("pfsenseapi.up")}: - {t("pfsenseapi.down")}; - const wanIP = interfaceData.data.filter(l => l.hwif === widget.wan)[0].ipaddr - - return ( - - - - - - { - showWanIP - && - - } - { - showDiskUsage - && - - } - - ); -} diff --git a/src/widgets/pfsenseapi/widget.js b/src/widgets/pfsenseapi/widget.js deleted file mode 100644 index 88aa44fd..00000000 --- a/src/widgets/pfsenseapi/widget.js +++ /dev/null @@ -1,24 +0,0 @@ - -import genericProxyHandler from "utils/proxy/handlers/generic"; - -const widget = { - api: "{url}/api/v1/{endpoint}", - proxyHandler: genericProxyHandler, - - mappings: { - system: { - endpoint: "status/system", - validate: [ - "data" - ] - }, - interface: { - endpoint: "status/interface", - validate: [ - "data" - ] - } - }, -}; - -export default widget; From a9d3873579000193c4118323c0e5654b5f9ae201 Mon Sep 17 00:00:00 2001 From: shamoon <4887959+shamoon@users.noreply.github.com> Date: Fri, 19 May 2023 12:43:24 -0700 Subject: [PATCH 8/8] QNAP widget make sure re-auth status = 200 --- src/widgets/qnap/proxy.js | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/src/widgets/qnap/proxy.js b/src/widgets/qnap/proxy.js index 489f24ac..a4d7376d 100644 --- a/src/widgets/qnap/proxy.js +++ b/src/widgets/qnap/proxy.js @@ -64,6 +64,12 @@ async function apiCall(widget, endpoint, service) { key = await login(widget, service); apiUrl = new URL(formatApiCall(`${endpoint}&sid=${key}`, widget)); [status, contentType, data, responseHeaders] = await httpProxy(apiUrl); + + if (status !== 200) { + logger.error("Error getting data from QNAP: %s status %d. Data: %s", apiUrl, status, data); + return { status, contentType, data: null, responseHeaders }; + } + dataDecoded = JSON.parse(xml2json(data.toString(), { compact: true }).toString()); }