mirror of
https://github.com/gethomepage/homepage.git
synced 2026-01-11 11:32:09 +08:00
Minor refactoring
This commit is contained in:
@@ -107,68 +107,64 @@ async function authenticate(ws, widget) {
|
||||
|
||||
export default async function truenasProxyHandler(req, res, map) {
|
||||
const { group, service, endpoint, index } = req.query;
|
||||
if (!group || !service) {
|
||||
logger.debug("Invalid or missing service '%s' or group '%s'", service, group);
|
||||
return res.status(400).json({ error: "Invalid proxy service type" });
|
||||
}
|
||||
|
||||
const widget = await getServiceWidget(group, service, index);
|
||||
|
||||
if (!widget) {
|
||||
logger.debug("Invalid or missing widget for service '%s' in group '%s'", service, group);
|
||||
return res.status(400).json({ error: "Invalid proxy service type" });
|
||||
}
|
||||
|
||||
if (!endpoint) {
|
||||
return res.status(204).end();
|
||||
}
|
||||
|
||||
const version = Number(widget.version ?? 1);
|
||||
if (Number.isNaN(version) || version < 2) {
|
||||
// Use legacy REST proxy for version 1
|
||||
return credentialedProxyHandler(req, res, map);
|
||||
}
|
||||
|
||||
const mappingEntry = Object.values(widgets[widget.type].mappings).find((mapping) => mapping.endpoint === endpoint);
|
||||
const wsMethod = mappingEntry.wsMethod;
|
||||
|
||||
if (!wsMethod) {
|
||||
logger.debug("Missing wsMethod mapping for TrueNAS endpoint %s", endpoint);
|
||||
return res.status(500).json({ error: "Missing wsMethod mapping." });
|
||||
}
|
||||
|
||||
try {
|
||||
const widget = await getServiceWidget(group, service, index);
|
||||
if (!widget || !widgets[widget.type]) {
|
||||
logger.debug("Invalid or missing widget for service '%s' in group '%s'", service, group);
|
||||
return res.status(400).json({ error: "Invalid proxy service type" });
|
||||
}
|
||||
|
||||
if (!endpoint) {
|
||||
return res.status(204).end();
|
||||
}
|
||||
|
||||
const version = Number(widget.version ?? 1);
|
||||
|
||||
if (Number.isNaN(version) || version < 2) {
|
||||
// Use legacy REST proxy for version 1
|
||||
return credentialedProxyHandler(req, res, map);
|
||||
}
|
||||
|
||||
const mappingEntry = Object.values(widgets[widget.type].mappings ?? {}).find(
|
||||
(mapping) => mapping.endpoint === endpoint,
|
||||
);
|
||||
const wsMethod = mappingEntry?.wsMethod;
|
||||
|
||||
if (!wsMethod) {
|
||||
logger.debug("Missing wsMethod mapping for TrueNAS endpoint %s", endpoint);
|
||||
return res.status(500).json({ error: "Missing wsMethod mapping." });
|
||||
}
|
||||
|
||||
let data;
|
||||
const wsUrl = new URL(formatApiCall(widgets[widget.type].wsAPI, { ...widget }));
|
||||
const useSecure = wsUrl.protocol === "https:" || Boolean(widget.key); // API key requires secure connection
|
||||
wsUrl.protocol = useSecure ? "wss:" : "ws:";
|
||||
logger.info("Connecting to TrueNAS websocket at %s", wsUrl);
|
||||
const ws = new WebSocket(wsUrl, { rejectUnauthorized: false });
|
||||
await waitForEvent(ws, () => true, { event: "open", parseJson: false }); // wait for open
|
||||
logger.info("Connected to TrueNAS websocket at %s", wsUrl);
|
||||
try {
|
||||
let data;
|
||||
const wsUrl = new URL(formatApiCall(widgets[widget.type].wsAPI, { ...widget }));
|
||||
const useSecure = wsUrl.protocol === "https:" || Boolean(widget.key); // API key requires secure connection
|
||||
wsUrl.protocol = useSecure ? "wss:" : "ws:";
|
||||
logger.info("Connecting to TrueNAS websocket at %s", wsUrl);
|
||||
const ws = new WebSocket(wsUrl, { rejectUnauthorized: false });
|
||||
|
||||
await waitForEvent(ws, () => true, { event: "open", parseJson: false });
|
||||
logger.info("Connected to TrueNAS websocket at %s", wsUrl);
|
||||
try {
|
||||
await authenticate(ws, widget);
|
||||
data = await sendMethod(ws, wsMethod);
|
||||
} finally {
|
||||
ws.close();
|
||||
}
|
||||
|
||||
if (!validateWidgetData(widget, endpoint, data)) {
|
||||
return res.status(500).json({ error: { message: "Invalid data", url: sanitizeErrorURL(widget.url), data } });
|
||||
}
|
||||
|
||||
if (map) data = map(data);
|
||||
|
||||
return res.status(200).json(data);
|
||||
} catch (err) {
|
||||
if (err?.status) {
|
||||
return res.status(err.status).json({ error: err.message });
|
||||
}
|
||||
logger.warn("Websocket call for TrueNAS failed: %s", err?.message ?? err);
|
||||
return res.status(500).json({ error: err?.message ?? "TrueNAS websocket call failed" });
|
||||
await authenticate(ws, widget);
|
||||
data = await sendMethod(ws, wsMethod);
|
||||
} finally {
|
||||
ws.close();
|
||||
}
|
||||
|
||||
if (!validateWidgetData(widget, endpoint, data)) {
|
||||
return res.status(500).json({ error: { message: "Invalid data", url: sanitizeErrorURL(widget.url), data } });
|
||||
}
|
||||
|
||||
if (map) data = map(data);
|
||||
|
||||
return res.status(200).json(data);
|
||||
} catch (err) {
|
||||
logger.error(err);
|
||||
return res.status(500).json({ error: err?.message ?? "Unexpected TrueNAS error" });
|
||||
if (err?.status) {
|
||||
return res.status(err.status).json({ error: err.message });
|
||||
}
|
||||
logger.warn("Websocket call for TrueNAS failed: %s", err?.message ?? err);
|
||||
return res.status(500).json({ error: err?.message ?? "TrueNAS websocket call failed" });
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user