Cache console version check result

pull/341/head
Jason Fischer 2 years ago
parent 1249ecaa68
commit 2bd9c8eddc
No known key found for this signature in database

@ -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 (
<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.lan" value={t("common.number", { value: data.lan.users }) + " " + t("unifi.users")} />
<Block label="unifi.wlan" value={t("common.number", { value: data.wlan.users }) + " " + t("unifi.users")} />
<Block label="unifi.lan" value={ lanUsers } />
<Block label="unifi.wlan" value={ wanUsers } />
</Container>
);
}

@ -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);
}

@ -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: {

Loading…
Cancel
Save