2025-03-30 21:40:03 -07:00
|
|
|
import { getSettings } from "utils/config/config";
|
2022-09-26 15:25:10 +03:00
|
|
|
import getServiceWidget from "utils/config/service-helpers";
|
2025-03-30 21:40:03 -07:00
|
|
|
import createLogger from "utils/logger";
|
2023-02-15 14:46:31 -08:00
|
|
|
import { formatApiCall, sanitizeErrorURL } from "utils/proxy/api-helpers";
|
2022-09-26 12:04:37 +03:00
|
|
|
import { httpProxy } from "utils/proxy/http";
|
2025-03-30 21:40:03 -07:00
|
|
|
import validateWidgetData from "utils/proxy/validate-widget-data";
|
2022-09-25 19:43:47 +03:00
|
|
|
import widgets from "widgets/widgets";
|
2022-09-04 21:58:42 +03:00
|
|
|
|
2022-09-25 17:42:16 -07:00
|
|
|
const logger = createLogger("credentialedProxyHandler");
|
|
|
|
|
2022-12-16 22:32:28 -08:00
|
|
|
export default async function credentialedProxyHandler(req, res, map) {
|
2024-11-27 02:33:40 -08:00
|
|
|
const { group, service, endpoint, index } = req.query;
|
2022-09-04 21:58:42 +03:00
|
|
|
|
|
|
|
if (group && service) {
|
2024-11-27 02:33:40 -08:00
|
|
|
const widget = await getServiceWidget(group, service, index);
|
2022-09-04 21:58:42 +03:00
|
|
|
|
2022-09-25 19:43:47 +03:00
|
|
|
if (!widgets?.[widget.type]?.api) {
|
|
|
|
return res.status(403).json({ error: "Service does not support API calls" });
|
|
|
|
}
|
|
|
|
|
2022-09-04 21:58:42 +03:00
|
|
|
if (widget) {
|
2022-09-25 19:43:47 +03:00
|
|
|
const url = new URL(formatApiCall(widgets[widget.type].api, { endpoint, ...widget }));
|
2022-09-12 10:59:56 +03:00
|
|
|
|
|
|
|
const headers = {
|
|
|
|
"Content-Type": "application/json",
|
|
|
|
};
|
|
|
|
|
2024-07-01 17:16:10 -07:00
|
|
|
if (widget.type === "stocks") {
|
|
|
|
const { providers } = getSettings();
|
|
|
|
if (widget.provider === "finnhub" && providers?.finnhub) {
|
|
|
|
headers["X-Finnhub-Token"] = `${providers?.finnhub}`;
|
|
|
|
}
|
|
|
|
} else if (widget.type === "coinmarketcap") {
|
2022-09-12 10:59:56 +03:00
|
|
|
headers["X-CMC_PRO_API_KEY"] = `${widget.key}`;
|
2022-09-12 12:39:04 +03:00
|
|
|
} else if (widget.type === "gotify") {
|
2022-09-12 10:06:47 +01:00
|
|
|
headers["X-gotify-Key"] = `${widget.key}`;
|
2023-10-17 23:26:55 -07:00
|
|
|
} else if (
|
2024-08-09 22:19:06 +01:00
|
|
|
[
|
2024-11-19 22:59:52 +01:00
|
|
|
"argocd",
|
2024-08-09 22:19:06 +01:00
|
|
|
"authentik",
|
|
|
|
"cloudflared",
|
|
|
|
"ghostfolio",
|
2024-11-05 20:02:33 +03:00
|
|
|
"headscale",
|
2025-03-12 09:46:01 -04:00
|
|
|
"hoarder",
|
2025-04-05 23:54:48 -07:00
|
|
|
"karakeep",
|
2024-08-09 22:19:06 +01:00
|
|
|
"linkwarden",
|
|
|
|
"mealie",
|
2024-10-12 15:30:45 -07:00
|
|
|
"netalertx",
|
2024-08-09 22:19:06 +01:00
|
|
|
"tailscale",
|
|
|
|
"tandoor",
|
|
|
|
"pterodactyl",
|
2024-10-12 10:53:02 -04:00
|
|
|
"vikunja",
|
2025-02-02 11:40:21 +08:00
|
|
|
"firefly",
|
2024-08-09 22:19:06 +01:00
|
|
|
].includes(widget.type)
|
2023-10-17 23:26:55 -07:00
|
|
|
) {
|
|
|
|
headers.Authorization = `Bearer ${widget.key}`;
|
2024-02-15 23:38:55 -06:00
|
|
|
} else if (widget.type === "truenas") {
|
|
|
|
if (widget.key) {
|
|
|
|
headers.Authorization = `Bearer ${widget.key}`;
|
|
|
|
} else {
|
|
|
|
headers.Authorization = `Basic ${Buffer.from(`${widget.username}:${widget.password}`).toString("base64")}`;
|
|
|
|
}
|
2022-10-04 21:46:48 -07:00
|
|
|
} else if (widget.type === "proxmox") {
|
|
|
|
headers.Authorization = `PVEAPIToken=${widget.username}=${widget.password}`;
|
2023-01-29 00:32:47 +00:00
|
|
|
} else if (widget.type === "proxmoxbackupserver") {
|
|
|
|
delete headers["Content-Type"];
|
|
|
|
headers.Authorization = `PBSAPIToken=${widget.username}:${widget.password}`;
|
2022-10-26 12:56:43 +02:00
|
|
|
} else if (widget.type === "autobrr") {
|
|
|
|
headers["X-API-Token"] = `${widget.key}`;
|
2022-10-31 15:23:34 +02:00
|
|
|
} else if (widget.type === "tubearchivist") {
|
|
|
|
headers.Authorization = `Token ${widget.key}`;
|
2022-12-16 22:33:15 -08:00
|
|
|
} else if (widget.type === "miniflux") {
|
|
|
|
headers["X-Auth-Token"] = `${widget.key}`;
|
2023-03-20 01:15:40 +01:00
|
|
|
} else if (widget.type === "nextcloud") {
|
2023-03-19 23:03:12 -07:00
|
|
|
if (widget.key) {
|
2023-03-20 01:15:40 +01:00
|
|
|
headers["NC-Token"] = `${widget.key}`;
|
|
|
|
} else {
|
|
|
|
headers.Authorization = `Basic ${Buffer.from(`${widget.username}:${widget.password}`).toString("base64")}`;
|
|
|
|
}
|
2023-06-05 16:23:37 +02:00
|
|
|
} else if (widget.type === "paperlessngx") {
|
|
|
|
if (widget.key) {
|
|
|
|
headers.Authorization = `Token ${widget.key}`;
|
|
|
|
} else {
|
|
|
|
headers.Authorization = `Basic ${Buffer.from(`${widget.username}:${widget.password}`).toString("base64")}`;
|
|
|
|
}
|
2023-10-17 23:26:55 -07:00
|
|
|
} else if (widget.type === "azuredevops") {
|
2023-07-19 22:59:27 +03:00
|
|
|
headers.Authorization = `Basic ${Buffer.from(`$:${widget.key}`).toString("base64")}`;
|
2023-08-01 03:54:19 +03:00
|
|
|
} else if (widget.type === "glances") {
|
|
|
|
headers.Authorization = `Basic ${Buffer.from(`${widget.username}:${widget.password}`).toString("base64")}`;
|
2024-02-20 02:54:28 +01:00
|
|
|
} else if (widget.type === "plantit") {
|
|
|
|
headers.Key = `${widget.key}`;
|
2024-06-23 21:00:04 +02:00
|
|
|
} else if (widget.type === "myspeed") {
|
|
|
|
headers.Password = `${widget.password}`;
|
2024-08-13 16:40:42 +02:00
|
|
|
} else if (widget.type === "esphome") {
|
2024-12-17 17:35:15 +13:00
|
|
|
if (widget.username && widget.password) {
|
|
|
|
headers.Authorization = `Basic ${Buffer.from(`${widget.username}:${widget.password}`).toString("base64")}`;
|
|
|
|
} else if (widget.key) {
|
2024-08-13 16:40:42 +02:00
|
|
|
headers.Cookie = `authenticated=${widget.key}`;
|
|
|
|
}
|
2024-09-09 11:40:11 -07:00
|
|
|
} else if (widget.type === "wgeasy") {
|
|
|
|
headers.Authorization = widget.password;
|
2024-11-22 16:07:17 +01:00
|
|
|
} else if (widget.type === "gitlab") {
|
|
|
|
headers["PRIVATE-TOKEN"] = widget.key;
|
2025-02-02 19:11:08 -08:00
|
|
|
} else if (widget.type === "speedtest") {
|
|
|
|
if (widget.key) {
|
|
|
|
// v1 does not require a key
|
|
|
|
headers.Authorization = `Bearer ${widget.key}`;
|
|
|
|
}
|
2022-09-12 10:59:56 +03:00
|
|
|
} else {
|
|
|
|
headers["X-API-Key"] = `${widget.key}`;
|
|
|
|
}
|
|
|
|
|
2022-09-04 21:58:42 +03:00
|
|
|
const [status, contentType, data] = await httpProxy(url, {
|
2022-09-11 14:30:28 +03:00
|
|
|
method: req.method,
|
2022-09-04 21:58:42 +03:00
|
|
|
withCredentials: true,
|
|
|
|
credentials: "include",
|
2022-09-12 10:59:56 +03:00
|
|
|
headers,
|
2022-09-04 21:58:42 +03:00
|
|
|
});
|
|
|
|
|
2022-12-16 22:32:28 -08:00
|
|
|
let resultData = data;
|
|
|
|
|
2023-04-11 11:05:30 -07:00
|
|
|
if (resultData.error?.url) {
|
|
|
|
resultData.error.url = sanitizeErrorURL(url);
|
|
|
|
}
|
|
|
|
|
2022-09-11 14:30:14 +03:00
|
|
|
if (status === 204 || status === 304) {
|
|
|
|
return res.status(status).end();
|
|
|
|
}
|
|
|
|
|
2022-09-25 17:42:16 -07:00
|
|
|
if (status >= 400) {
|
2022-11-19 00:12:05 -08:00
|
|
|
logger.error("HTTP Error %d calling %s", status, url.toString());
|
2022-09-25 17:42:16 -07:00
|
|
|
}
|
2023-10-17 23:26:55 -07:00
|
|
|
|
2023-04-11 11:05:30 -07:00
|
|
|
if (status === 200) {
|
|
|
|
if (!validateWidgetData(widget, endpoint, resultData)) {
|
2023-10-17 23:26:55 -07:00
|
|
|
return res
|
|
|
|
.status(500)
|
|
|
|
.json({ error: { message: "Invalid data", url: sanitizeErrorURL(url), data: resultData } });
|
2023-02-15 14:46:31 -08:00
|
|
|
}
|
2023-04-11 11:05:30 -07:00
|
|
|
if (map) resultData = map(resultData);
|
2022-12-16 22:32:28 -08:00
|
|
|
}
|
|
|
|
|
2022-09-05 10:08:02 +03:00
|
|
|
if (contentType) res.setHeader("Content-Type", contentType);
|
2022-12-16 22:32:28 -08:00
|
|
|
return res.status(status).send(resultData);
|
2022-09-04 21:58:42 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-25 17:42:16 -07:00
|
|
|
logger.debug("Invalid or missing proxy service type '%s' in group '%s'", service, group);
|
2022-09-04 21:58:42 +03:00
|
|
|
return res.status(400).json({ error: "Invalid proxy service type" });
|
|
|
|
}
|