Cache console version check result

This commit is contained in:
Jason Fischer 2022-10-08 13:52:07 -07:00
parent 1249ecaa68
commit 2bd9c8eddc
No known key found for this signature in database
3 changed files with 33 additions and 31 deletions

View File

@ -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 ( return (
<Container service={service}> <Container service={service}>
<Block label="unifi.uptime" value={t("common.number", { value: data.uptime / 86400, maximumFractionDigits: 1 }) + " " + t("unifi.days")} /> <Block label="unifi.uptime" value={ uptime } />
<Block label="unifi.wan" value={ data.up ? t("unifi.up") : t("unifi.down") } /> <Block label="unifi.wan" value={ data.up ? t("unifi.up") : t("unifi.down") } />
<Block label="unifi.lan" value={t("common.number", { value: data.lan.users }) + " " + t("unifi.users")} /> <Block label="unifi.lan" value={ lanUsers } />
<Block label="unifi.wlan" value={t("common.number", { value: data.wlan.users }) + " " + t("unifi.users")} /> <Block label="unifi.wlan" value={ wanUsers } />
</Container> </Container>
); );
} }

View File

@ -1,3 +1,5 @@
import cache from "memory-cache";
import { formatApiCall } from "utils/proxy/api-helpers"; import { formatApiCall } from "utils/proxy/api-helpers";
import { httpProxy } from "utils/proxy/http"; import { httpProxy } from "utils/proxy/http";
import { addCookieToJar, setCookieHeader } from "utils/proxy/cookie-jar"; 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 createLogger from "utils/logger";
import widgets from "widgets/widgets"; 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) { async function getWidget(req) {
const { group, service, type } = req.query; const { group, service, type } = req.query;
@ -38,21 +43,18 @@ async function getWidget(req) {
} }
async function login(widget) { async function login(widget) {
let endpoint = widget.udmp ? "auth/login" : "login"; let loginUrl = `${widget.url}/api`;
widget.prefix = ""; // never use prefix for login if (widget.prefix === udmpPrefix) {
const api = widgets?.[widget.type]?.api; loginUrl += "/auth"
const loginUrl = new URL(formatApiCall(api, { endpoint, ...widget })); }
loginUrl += "/login";
const loginBody = { username: widget.username, password: widget.password, remember: true }; 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, { const [status, contentType, data, responseHeaders] = await httpProxy(loginUrl, {
method: "POST", method: "POST",
body: JSON.stringify(loginBody), body: JSON.stringify(loginBody),
headers headers,
}); });
return [status, contentType, data, responseHeaders]; return [status, contentType, data, responseHeaders];
} }
@ -68,23 +70,20 @@ export default async function unifiProxyHandler(req, res) {
return res.status(403).json({ error: "Service does not support API calls" }); return res.status(403).json({ error: "Service does not support API calls" });
} }
// determine if udm-pro from base url let [status, contentType, data, responseHeaders] = [];
let [status, contentType, data, responseHeaders] = await httpProxy(`https://${widget.host}`); let prefix = cache.get(prefixCacheKey);
if (responseHeaders['x-csrf-token']) { if (prefix === null) {
widget.udmp = true // 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);
} }
if (!widget.port) { widget.prefix = prefix;
widget.port = 8443;
if (widget.udmp) {
widget.port = 443
}
}
widget.prefix = "";
if (widget.udmp) {
widget.prefix = "/proxy/network"
}
const { endpoint } = req.query; const { endpoint } = req.query;
const url = new URL(formatApiCall(api, { endpoint, ...widget })); const url = new URL(formatApiCall(api, { endpoint, ...widget }));
@ -110,11 +109,10 @@ export default async function unifiProxyHandler(req, res) {
addCookieToJar(url, responseHeaders); addCookieToJar(url, responseHeaders);
setCookieHeader(url, params); 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); [status, contentType, data, responseHeaders] = await httpProxy(url, params);
} }
if (status !== 200) { if (status !== 200) {
logger.error("HTTP %d getting data from Unifi endpoint %s. Data: %s", status, url.href, data); logger.error("HTTP %d getting data from Unifi endpoint %s. Data: %s", status, url.href, data);
} }

View File

@ -1,7 +1,7 @@
import unifiProxyHandler from "./proxy"; import unifiProxyHandler from "./proxy";
const widget = { const widget = {
api: "https://{host}:{port}{prefix}/api/{endpoint}", api: "{url}{prefix}/api/{endpoint}",
proxyHandler: unifiProxyHandler, proxyHandler: unifiProxyHandler,
mappings: { mappings: {