From 2bd9c8eddcca78bc30817e33f1388d17fd0231b6 Mon Sep 17 00:00:00 2001 From: Jason Fischer Date: Sat, 8 Oct 2022 13:52:07 -0700 Subject: [PATCH] Cache console version check result --- src/widgets/unifi/component.jsx | 10 +++++-- src/widgets/unifi/proxy.js | 52 ++++++++++++++++----------------- src/widgets/unifi/widget.js | 2 +- 3 files changed, 33 insertions(+), 31 deletions(-) diff --git a/src/widgets/unifi/component.jsx b/src/widgets/unifi/component.jsx index 82b33941..0ab862bd 100644 --- a/src/widgets/unifi/component.jsx +++ b/src/widgets/unifi/component.jsx @@ -45,12 +45,16 @@ export default function Component({ service }) { } }; + const uptime = `${t("common.number", { value: data.uptime / 86400, maximumFractionDigits: 1 })} ${t("unifi.days")}`; + const lanUsers = `${t("common.number", { value: data.lan.users })} ${t("unifi.users")}`; + const wanUsers = `${t("common.number", { value: data.wlan.users })} ${t("unifi.users")}`; + return ( - + - - + + ); } diff --git a/src/widgets/unifi/proxy.js b/src/widgets/unifi/proxy.js index 91446051..181f43d7 100644 --- a/src/widgets/unifi/proxy.js +++ b/src/widgets/unifi/proxy.js @@ -1,3 +1,5 @@ +import cache from "memory-cache"; + import { formatApiCall } from "utils/proxy/api-helpers"; import { httpProxy } from "utils/proxy/http"; import { addCookieToJar, setCookieHeader } from "utils/proxy/cookie-jar"; @@ -6,7 +8,10 @@ import getServiceWidget from "utils/config/service-helpers"; import createLogger from "utils/logger"; import widgets from "widgets/widgets"; -const logger = createLogger("unifiProxyHandler"); +const udmpPrefix = "/proxy/network"; +const proxyName = "unifiProxyHandler"; +const prefixCacheKey = `${proxyName}__prefix`; +const logger = createLogger(proxyName); async function getWidget(req) { const { group, service, type } = req.query; @@ -38,21 +43,18 @@ async function getWidget(req) { } async function login(widget) { - let endpoint = widget.udmp ? "auth/login" : "login"; - widget.prefix = ""; // never use prefix for login - const api = widgets?.[widget.type]?.api; - const loginUrl = new URL(formatApiCall(api, { endpoint, ...widget })); + let loginUrl = `${widget.url}/api`; + if (widget.prefix === udmpPrefix) { + loginUrl += "/auth" + } + loginUrl += "/login"; const loginBody = { username: widget.username, password: widget.password, remember: true }; - - const headers = { - "Content-Type": "application/json" - }; - + const headers = { "Content-Type": "application/json" }; const [status, contentType, data, responseHeaders] = await httpProxy(loginUrl, { method: "POST", body: JSON.stringify(loginBody), - headers + headers, }); return [status, contentType, data, responseHeaders]; } @@ -67,24 +69,21 @@ export default async function unifiProxyHandler(req, res) { if (!api) { return res.status(403).json({ error: "Service does not support API calls" }); } - - // determine if udm-pro from base url - let [status, contentType, data, responseHeaders] = await httpProxy(`https://${widget.host}`); - if (responseHeaders['x-csrf-token']) { - widget.udmp = true - } - if (!widget.port) { - widget.port = 8443; - if (widget.udmp) { - widget.port = 443 + let [status, contentType, data, responseHeaders] = []; + let prefix = cache.get(prefixCacheKey); + if (prefix === null) { + // auto detect if we're talking to a UDM Pro, and cache the result so that we + // don't make two requests each time data from Unifi is required + [status, contentType, data, responseHeaders] = await httpProxy(widget.url); + prefix = ""; + if (responseHeaders["x-csrf-token"]) { + prefix = udmpPrefix; } + cache.put(prefixCacheKey, prefix); } - widget.prefix = ""; - if (widget.udmp) { - widget.prefix = "/proxy/network" - } + widget.prefix = prefix; const { endpoint } = req.query; const url = new URL(formatApiCall(api, { endpoint, ...widget })); @@ -110,11 +109,10 @@ export default async function unifiProxyHandler(req, res) { addCookieToJar(url, responseHeaders); setCookieHeader(url, params); - logger.debug("Retrying Unifi reqeust after login."); + logger.debug("Retrying Unifi request after login."); [status, contentType, data, responseHeaders] = await httpProxy(url, params); } - if (status !== 200) { logger.error("HTTP %d getting data from Unifi endpoint %s. Data: %s", status, url.href, data); } diff --git a/src/widgets/unifi/widget.js b/src/widgets/unifi/widget.js index f4b1366f..928ebd76 100644 --- a/src/widgets/unifi/widget.js +++ b/src/widgets/unifi/widget.js @@ -1,7 +1,7 @@ import unifiProxyHandler from "./proxy"; const widget = { - api: "https://{host}:{port}{prefix}/api/{endpoint}", + api: "{url}{prefix}/api/{endpoint}", proxyHandler: unifiProxyHandler, mappings: {