2022-09-26 15:25:10 +03:00
|
|
|
import getServiceWidget from "utils/config/service-helpers";
|
2022-09-26 12:04:37 +03:00
|
|
|
import { formatApiCall } from "utils/proxy/api-helpers";
|
|
|
|
import { httpProxy } from "utils/proxy/http";
|
2022-09-25 17:42:16 -07:00
|
|
|
import createLogger from "utils/logger";
|
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-09-04 21:58:42 +03:00
|
|
|
export default async function credentialedProxyHandler(req, res) {
|
|
|
|
const { group, service, endpoint } = req.query;
|
|
|
|
|
|
|
|
if (group && service) {
|
|
|
|
const widget = await getServiceWidget(group, service);
|
|
|
|
|
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",
|
|
|
|
};
|
|
|
|
|
|
|
|
if (widget.type === "coinmarketcap") {
|
|
|
|
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}`;
|
2022-09-25 17:42:16 -07:00
|
|
|
} else if (widget.type === "authentik") {
|
|
|
|
headers.Authorization = `Bearer ${widget.key}`;
|
2022-10-04 21:46:48 -07:00
|
|
|
} else if (widget.type === "proxmox") {
|
|
|
|
headers.Authorization = `PVEAPIToken=${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-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-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) {
|
|
|
|
logger.debug("HTTP Error %d calling %s//%s%s...", status, url.protocol, url.hostname, url.pathname);
|
|
|
|
}
|
|
|
|
|
2022-09-05 10:08:02 +03:00
|
|
|
if (contentType) res.setHeader("Content-Type", contentType);
|
2022-09-04 21:58:42 +03:00
|
|
|
return res.status(status).send(data);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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" });
|
|
|
|
}
|