Skip to content

Commit

Permalink
Enhancement: improve fritzbox proxy perfomance (gethomepage#2429)
Browse files Browse the repository at this point in the history
---------

Co-authored-by: Thorben Grove <[email protected]>
Co-authored-by: shamoon <[email protected]>
  • Loading branch information
3 people authored Dec 5, 2023
1 parent 77ed445 commit 0f3fc77
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 18 deletions.
4 changes: 2 additions & 2 deletions docs/widgets/services/fritzbox.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,12 @@ Home Network > Network > Network Settings > Access Settings in the Home Network
[x] Transmit status information over UPnP
```

You don't need to provide any credentials.
Credentials are not needed and, as such, you may want to consider using `http` instead of `https` as those requests are significantly faster.

Allowed fields (limited to a max of 4): `["connectionStatus", "upTime", "maxDown", "maxUp", "down", "up", "received", "sent", "externalIPAddress"]`.

```yaml
widget:
type: fritzbox
url: https://192.168.178.1
url: http://192.168.178.1
```
2 changes: 1 addition & 1 deletion public/locales/en/common.json
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@
"connectionStatusUnconfigured": "Unconfigured",
"connectionStatusConnecting": "Connecting",
"connectionStatusAuthenticating": "Authenticating",
"connectionStatusPendingDisconnect": "PendingDisconnect",
"connectionStatusPendingDisconnect": "Pending Disconnect",
"connectionStatusDisconnecting": "Disconnecting",
"connectionStatusDisconnected": "Disconnected",
"connectionStatusConnected": "Connected",
Expand Down
4 changes: 3 additions & 1 deletion src/widgets/fritzbox/component.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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
Expand Down
40 changes: 26 additions & 14 deletions src/widgets/fritzbox/proxy.js
Original file line number Diff line number Diff line change
@@ -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";
Expand Down Expand Up @@ -46,10 +48,12 @@ 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;
Expand All @@ -59,23 +63,31 @@ export default async function fritzboxProxyHandler(req, res) {
const port = serviceWidgetUrl.protocol === "https:" ? 49443 : 49000;
const apiBaseUrl = `${serviceWidgetUrl.protocol}//${serviceWidgetUrl.hostname}:${port}`;

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) => {
Expand Down

0 comments on commit 0f3fc77

Please sign in to comment.