94 lines
3.0 KiB
JavaScript
Raw Normal View History

2022-09-26 15:25:10 +03:00
import getServiceWidget from "utils/config/service-helpers";
import createLogger from "utils/logger";
import { formatApiCall, sanitizeErrorURL } from "utils/proxy/api-helpers";
import { httpProxy } from "utils/proxy/http";
import validateWidgetData from "utils/proxy/validate-widget-data";
2022-09-25 19:43:47 +03:00
import widgets from "widgets/widgets";
2022-09-25 19:43:47 +03:00
const logger = createLogger("genericProxyHandler");
2022-09-25 19:43:47 +03:00
export default async function genericProxyHandler(req, res, map) {
const { group, service, endpoint, index } = req.query;
if (group && service) {
const widget = await getServiceWidget(group, service, index);
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" });
}
if (widget) {
// if there are more than one question marks, replace others to &
const url = new URL(
formatApiCall(widgets[widget.type].api, { endpoint, ...widget }).replace(/(?<=\?.*)\?/g, "&"),
);
2022-09-16 14:05:27 +03:00
const headers = req.extraHeaders ?? widget.headers ?? widgets[widget.type].headers ?? {};
2022-09-16 14:05:27 +03:00
if (widget.username && widget.password) {
2023-08-22 21:42:53 -07:00
headers.Authorization = `Basic ${Buffer.from(`${widget.username}:${widget.password}`).toString("base64")}`;
2022-09-16 14:05:27 +03:00
}
const params = {
method: widget.method ?? req.method,
2022-09-16 14:05:27 +03:00
headers,
};
if (req.body) {
params.body = req.body;
} else if (widget.requestBody) {
if (typeof widget.requestBody === "object") {
params.body = JSON.stringify(widget.requestBody);
} else {
params.body = widget.requestBody;
}
}
const [status, contentType, data] = await httpProxy(url, params);
let resultData = data;
2023-04-11 11:05:30 -07:00
if (resultData.error?.url) {
resultData.error.url = sanitizeErrorURL(url);
2022-10-22 22:48:25 -07:00
}
2023-04-11 11:05:30 -07:00
if (status === 200) {
if (!validateWidgetData(widget, endpoint, resultData)) {
return res
.status(status)
.json({ error: { message: "Invalid data", url: sanitizeErrorURL(url), data: resultData } });
2023-04-11 11:05:30 -07:00
}
if (map) resultData = map(resultData);
}
if (contentType) res.setHeader("Content-Type", contentType);
2022-09-11 14:30:14 +03:00
if (status === 204 || status === 304) {
return res.status(status).end();
}
if (status >= 400) {
2023-05-18 00:26:32 -07:00
logger.debug(
2023-05-23 03:37:56 -07:00
"HTTP Error %d calling %s//%s%s%s...",
2023-05-18 00:26:32 -07:00
status,
url.protocol,
url.hostname,
url.port ? `:${url.port}` : "",
url.pathname,
2023-05-18 00:26:32 -07:00
);
return res.status(status).json({
error: {
message: "HTTP Error",
url: sanitizeErrorURL(url),
2025-01-21 07:13:12 -08:00
data: Buffer.isBuffer(resultData) ? Buffer.from(resultData).toString() : resultData,
},
});
}
return res.status(status).send(resultData);
}
}
logger.debug("Invalid or missing proxy service type '%s' in group '%s'", service, group);
return res.status(400).json({ error: "Invalid proxy service type" });
}