Merge branch 'benphelps:main' into main
This commit is contained in:
commit
e6c368e629
@ -420,10 +420,10 @@
|
|||||||
"series": "Serie"
|
"series": "Serie"
|
||||||
},
|
},
|
||||||
"octoprint": {
|
"octoprint": {
|
||||||
"temp_bed": "Bed temp",
|
"temp_bed": "temperatura de la plataforma",
|
||||||
"printer_state": "Status",
|
"printer_state": "Status",
|
||||||
"temp_tool": "Tool temp",
|
"temp_tool": "Herramienta de temperatura",
|
||||||
"job_completion": "Completion"
|
"job_completion": "Finalización"
|
||||||
},
|
},
|
||||||
"cloudflared": {
|
"cloudflared": {
|
||||||
"origin_ip": "IP de origen",
|
"origin_ip": "IP de origen",
|
||||||
@ -482,9 +482,9 @@
|
|||||||
"alertstriggered": "Alertas activadas"
|
"alertstriggered": "Alertas activadas"
|
||||||
},
|
},
|
||||||
"nextcloud": {
|
"nextcloud": {
|
||||||
"cpuload": "Cpu Load",
|
"cpuload": "Carga de la CPU",
|
||||||
"memoryusage": "Memory Usage",
|
"memoryusage": "Uso de la memoria",
|
||||||
"freespace": "Free Space",
|
"freespace": "Espacio libre",
|
||||||
"activeusers": "Active Users"
|
"activeusers": "Usuarios activos"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -482,9 +482,9 @@
|
|||||||
"alertstriggered": "Alertes déclenchées"
|
"alertstriggered": "Alertes déclenchées"
|
||||||
},
|
},
|
||||||
"nextcloud": {
|
"nextcloud": {
|
||||||
"freespace": "Free Space",
|
"freespace": "Espace Libre",
|
||||||
"activeusers": "Active Users",
|
"activeusers": "Utilisateurs Actifs",
|
||||||
"cpuload": "Cpu Load",
|
"cpuload": "Charge Cpu",
|
||||||
"memoryusage": "Memory Usage"
|
"memoryusage": "Utilisation Mémoire"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -42,12 +42,15 @@ async function getWidget(req) {
|
|||||||
return widget;
|
return widget;
|
||||||
}
|
}
|
||||||
|
|
||||||
async function login(widget) {
|
async function login(widget, csrfToken) {
|
||||||
const endpoint = (widget.prefix === udmpPrefix) ? "auth/login" : "login";
|
const endpoint = (widget.prefix === udmpPrefix) ? "auth/login" : "login";
|
||||||
const api = widgets?.[widget.type]?.api?.replace("{prefix}", ""); // no prefix for login url
|
const api = widgets?.[widget.type]?.api?.replace("{prefix}", ""); // no prefix for login url
|
||||||
const loginUrl = new URL(formatApiCall(api, { endpoint, ...widget }));
|
const loginUrl = new URL(formatApiCall(api, { endpoint, ...widget }));
|
||||||
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" };
|
||||||
|
if (csrfToken) {
|
||||||
|
headers["X-CSRF-TOKEN"] = csrfToken;
|
||||||
|
}
|
||||||
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),
|
||||||
@ -70,6 +73,7 @@ export default async function unifiProxyHandler(req, res) {
|
|||||||
|
|
||||||
let [status, contentType, data, responseHeaders] = [];
|
let [status, contentType, data, responseHeaders] = [];
|
||||||
let prefix = cache.get(`${prefixCacheKey}.${service}`);
|
let prefix = cache.get(`${prefixCacheKey}.${service}`);
|
||||||
|
let csrfToken;
|
||||||
if (prefix === null) {
|
if (prefix === null) {
|
||||||
// auto detect if we're talking to a UDM Pro, and cache the result so that we
|
// 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
|
// don't make two requests each time data from Unifi is required
|
||||||
@ -77,12 +81,12 @@ export default async function unifiProxyHandler(req, res) {
|
|||||||
prefix = "";
|
prefix = "";
|
||||||
if (responseHeaders?.["x-csrf-token"]) {
|
if (responseHeaders?.["x-csrf-token"]) {
|
||||||
prefix = udmpPrefix;
|
prefix = udmpPrefix;
|
||||||
|
csrfToken = responseHeaders["x-csrf-token"];
|
||||||
}
|
}
|
||||||
cache.put(`${prefixCacheKey}.${service}`, prefix);
|
cache.put(`${prefixCacheKey}.${service}`, prefix);
|
||||||
}
|
}
|
||||||
|
|
||||||
widget.prefix = prefix;
|
widget.prefix = prefix;
|
||||||
|
|
||||||
const { endpoint } = req.query;
|
const { endpoint } = req.query;
|
||||||
const url = new URL(formatApiCall(api, { endpoint, ...widget }));
|
const url = new URL(formatApiCall(api, { endpoint, ...widget }));
|
||||||
const params = { method: "GET", headers: {} };
|
const params = { method: "GET", headers: {} };
|
||||||
@ -92,7 +96,10 @@ export default async function unifiProxyHandler(req, res) {
|
|||||||
|
|
||||||
if (status === 401) {
|
if (status === 401) {
|
||||||
logger.debug("Unifi isn't logged in or rejected the reqeust, attempting login.");
|
logger.debug("Unifi isn't logged in or rejected the reqeust, attempting login.");
|
||||||
[status, contentType, data, responseHeaders] = await login(widget);
|
if (responseHeaders?.["x-csrf-token"]) {
|
||||||
|
csrfToken = responseHeaders["x-csrf-token"];
|
||||||
|
}
|
||||||
|
[status, contentType, data, responseHeaders] = await login(widget, csrfToken);
|
||||||
|
|
||||||
if (status !== 200) {
|
if (status !== 200) {
|
||||||
logger.error("HTTP %d logging in to Unifi. Data: %s", status, data);
|
logger.error("HTTP %d logging in to Unifi. Data: %s", status, data);
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user