From fca007d7f2f1eb7e0d3b19b580d1165a5705c23b Mon Sep 17 00:00:00 2001 From: Thorben Grove Date: Mon, 27 Nov 2023 20:29:33 +0100 Subject: [PATCH] :zap: improve fitzbox proxy perfomance - only request the endpoints required by the field configuration - use http instead of https, as it is significantly faster --- public/locales/de/common.json | 2 +- public/locales/en/common.json | 2 +- src/widgets/fritzbox/component.jsx | 4 ++- src/widgets/fritzbox/proxy.js | 43 +++++++++++++++++++----------- 4 files changed, 32 insertions(+), 19 deletions(-) diff --git a/public/locales/de/common.json b/public/locales/de/common.json index fcd45f39..411c563a 100644 --- a/public/locales/de/common.json +++ b/public/locales/de/common.json @@ -127,7 +127,7 @@ "connectionStatusUnconfigured": "Unkonfiguriert", "connectionStatusConnecting": "Verbinde", "connectionStatusAuthenticating": "Authenifiziere", - "connectionStatusPendingDisconnect": "Anstehende Trennung", + "connectionStatusPendingDisconnect": "Ausstehende Trennung", "connectionStatusDisconnecting": "Trenne", "connectionStatusDisconnected": "Getrennt", "connectionStatusConnected": "Verbunden", diff --git a/public/locales/en/common.json b/public/locales/en/common.json index ad94e3aa..8a3b851c 100644 --- a/public/locales/en/common.json +++ b/public/locales/en/common.json @@ -127,7 +127,7 @@ "connectionStatusUnconfigured": "Unconfigured", "connectionStatusConnecting": "Connecting", "connectionStatusAuthenticating": "Authenticating", - "connectionStatusPendingDisconnect": "PendingDisconnect", + "connectionStatusPendingDisconnect": "Pending Disconnect", "connectionStatusDisconnecting": "Disconnecting", "connectionStatusDisconnected": "Disconnected", "connectionStatusConnected": "Connected", diff --git a/src/widgets/fritzbox/component.jsx b/src/widgets/fritzbox/component.jsx index 6e8bf11f..7c32f617 100644 --- a/src/widgets/fritzbox/component.jsx +++ b/src/widgets/fritzbox/component.jsx @@ -4,6 +4,8 @@ import Container from "components/services/widget/container"; import Block from "components/services/widget/block"; import useWidgetAPI from "utils/proxy/use-widget-api"; +export const fritzboxDefaultFields = ["connectionStatus", "uptime", "maxDown", "maxUp"]; + const formatUptime = (timestamp) => { const hours = Math.floor(timestamp / 3600); const minutes = Math.floor((timestamp % 3600) / 60); @@ -27,7 +29,7 @@ export default function Component({ service }) { // Default fields if (!widget.fields?.length > 0) { - widget.fields = ["connectionStatus", "uptime", "maxDown", "maxUp"]; + widget.fields = fritzboxDefaultFields; } const MAX_ALLOWED_FIELDS = 4; // Limits max number of displayed fields diff --git a/src/widgets/fritzbox/proxy.js b/src/widgets/fritzbox/proxy.js index e4f0fefd..b9c35eed 100644 --- a/src/widgets/fritzbox/proxy.js +++ b/src/widgets/fritzbox/proxy.js @@ -1,5 +1,7 @@ import { xml2json } from "xml-js"; +import { fritzboxDefaultFields } from "./component"; + import { httpProxy } from "utils/proxy/http"; import getServiceWidget from "utils/config/service-helpers"; import createLogger from "utils/logger"; @@ -46,36 +48,45 @@ async function requestEndpoint(apiBaseUrl, service, action) { export default async function fritzboxProxyHandler(req, res) { const { group, service } = req.query; const serviceWidget = await getServiceWidget(group, service); + if (!serviceWidget) { res.status(500).json({ error: "Service widget not found" }); return; } + if (!serviceWidget.url) { res.status(500).json({ error: "Service widget url not configured" }); return; } const serviceWidgetUrl = new URL(serviceWidget.url); - const port = serviceWidgetUrl.protocol === "https:" ? 49443 : 49000; - const apiBaseUrl = `${serviceWidgetUrl.protocol}//${serviceWidgetUrl.hostname}:${port}`; + const apiBaseUrl = `http://${serviceWidgetUrl.hostname}:49000`; + + if (!serviceWidget.fields?.length > 0) { + serviceWidget.fields = fritzboxDefaultFields; + } + const requestStatusInfo = ["connectionStatus", "uptime"].some((field) => serviceWidget.fields.includes(field)); + const requestLinkProperties = ["maxDown", "maxUp"].some((field) => serviceWidget.fields.includes(field)); + const requestAddonInfos = ["down", "up", "received", "sent"].some((field) => serviceWidget.fields.includes(field)); + const requestExternalIPAddress = ["externalIPAddress"].some((field) => serviceWidget.fields.includes(field)); await Promise.all([ - requestEndpoint(apiBaseUrl, "WANIPConnection", "GetStatusInfo"), - requestEndpoint(apiBaseUrl, "WANIPConnection", "GetExternalIPAddress"), - requestEndpoint(apiBaseUrl, "WANCommonInterfaceConfig", "GetCommonLinkProperties"), - requestEndpoint(apiBaseUrl, "WANCommonInterfaceConfig", "GetAddonInfos"), + requestStatusInfo ? requestEndpoint(apiBaseUrl, "WANIPConnection", "GetStatusInfo") : null, + requestLinkProperties ? requestEndpoint(apiBaseUrl, "WANCommonInterfaceConfig", "GetCommonLinkProperties") : null, + requestAddonInfos ? requestEndpoint(apiBaseUrl, "WANCommonInterfaceConfig", "GetAddonInfos") : null, + requestExternalIPAddress ? requestEndpoint(apiBaseUrl, "WANIPConnection", "GetExternalIPAddress") : null, ]) - .then(([statusInfo, externalIPAddress, linkProperties, addonInfos]) => { + .then(([statusInfo, linkProperties, addonInfos, externalIPAddress]) => { res.status(200).json({ - connectionStatus: statusInfo.NewConnectionStatus, - uptime: statusInfo.NewUptime, - maxDown: linkProperties.NewLayer1DownstreamMaxBitRate, - maxUp: linkProperties.NewLayer1UpstreamMaxBitRate, - down: addonInfos.NewByteReceiveRate, - up: addonInfos.NewByteSendRate, - received: addonInfos.NewX_AVM_DE_TotalBytesReceived64, - sent: addonInfos.NewX_AVM_DE_TotalBytesSent64, - externalIPAddress: externalIPAddress.NewExternalIPAddress, + connectionStatus: statusInfo?.NewConnectionStatus || "Unconfigured", + uptime: statusInfo?.NewUptime || 0, + maxDown: linkProperties?.NewLayer1DownstreamMaxBitRate || 0, + maxUp: linkProperties?.NewLayer1UpstreamMaxBitRate || 0, + down: addonInfos?.NewByteReceiveRate || 0, + up: addonInfos?.NewByteSendRate || 0, + received: addonInfos?.NewX_AVM_DE_TotalBytesReceived64 || 0, + sent: addonInfos?.NewX_AVM_DE_TotalBytesSent64 || 0, + externalIPAddress: externalIPAddress?.NewExternalIPAddress || null, }); }) .catch((error) => {