From 96c0ea66b4de597a3051d874103cdbfe023b71ea Mon Sep 17 00:00:00 2001 From: Larry Date: Sat, 5 Oct 2024 17:53:13 +0200 Subject: [PATCH] Added functionality to add a local address in the services.yaml config that will be used if the homepage is accessed in a local network. --- src/components/services/item.jsx | 42 ++++++++++++++++++++++++++++---- 1 file changed, 37 insertions(+), 5 deletions(-) diff --git a/src/components/services/item.jsx b/src/components/services/item.jsx index a38dfaa3..47477d91 100644 --- a/src/components/services/item.jsx +++ b/src/components/services/item.jsx @@ -1,5 +1,5 @@ import classNames from "classnames"; -import { useContext, useState } from "react"; +import { useContext, useState, useEffect } from "react"; import Status from "./status"; import Widget from "./widget"; @@ -13,12 +13,44 @@ import { SettingsContext } from "utils/contexts/settings"; import ResolvedIcon from "components/resolvedicon"; export default function Item({ service, group, useEqualHeights }) { - const hasLink = service.href && service.href !== "#"; + const hasLink = (service.href && service.href !== "#") || (service.href_local && service.href_local !== "#"); const { settings } = useContext(SettingsContext); const showStats = service.showStats === false ? false : settings.showStats; const statusStyle = service.statusStyle !== undefined ? service.statusStyle : settings.statusStyle; const [statsOpen, setStatsOpen] = useState(service.showStats); const [statsClosing, setStatsClosing] = useState(false); + const [isLocalConnection, setIsLocalConnection] = useState(false) + + useEffect(() => { + try { + if (!service.href_local) { + return; + } + + if (!service.href) { + setIsLocalConnection(true); + return; + } + + const windowHref = window.location.href; + const localRanges = [ + /^10\./, + /^192\.168\./, + /^172\.(1[6-9]|2[0-9]|3[0-1])\./, + /^127\.0\.0\.1$/, + /^::1$/, + /^localhost$/i + ]; + const tldRegex = /^(https?:\/\/)?((([a-zA-Z0-9-]+\.)+([a-zA-Z]{2,}))(\/.*)?)(:[0-9]{1,5})?\/?.*$/;; + + const inLocalRange = localRanges.some((range) => range.test(windowHref)); + const isValidTLD = tldRegex.test(windowHref); + setIsLocalConnection(inLocalRange || !isValidTLD); + } + catch (err) { + console.error("error while deciding weather connection is local or external"); + } + }) // set stats to closed after 300ms const closeStats = () => { @@ -44,7 +76,7 @@ export default function Item({ service, group, useEqualHeights }) { {service.icon && (hasLink ? ( {service.name}

- {service.description} + { service.description }