2022-10-27 23:28:21 +02:00
|
|
|
import { httpProxy } from "utils/proxy/http";
|
|
|
|
import { formatApiCall } from "utils/proxy/api-helpers";
|
|
|
|
import getServiceWidget from "utils/config/service-helpers";
|
|
|
|
import createLogger from "utils/logger";
|
|
|
|
import widgets from "widgets/widgets";
|
|
|
|
|
|
|
|
const proxyName = "watchtowerProxyHandler";
|
|
|
|
const logger = createLogger(proxyName);
|
|
|
|
|
|
|
|
export default async function watchtowerProxyHandler(req, res) {
|
2024-11-27 02:33:40 -08:00
|
|
|
const { group, service, endpoint, index } = req.query;
|
2022-10-27 23:28:21 +02:00
|
|
|
|
|
|
|
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" });
|
|
|
|
}
|
|
|
|
|
2024-11-27 02:33:40 -08:00
|
|
|
const widget = await getServiceWidget(group, service, index);
|
2022-10-27 23:28:21 +02:00
|
|
|
|
|
|
|
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" });
|
|
|
|
}
|
|
|
|
|
2023-10-17 23:26:55 -07:00
|
|
|
const url = new URL(formatApiCall(widgets[widget.type].api, { endpoint, ...widget }));
|
|
|
|
|
2022-10-27 23:28:21 +02:00
|
|
|
const [status, contentType, data] = await httpProxy(url, {
|
2022-10-28 00:58:59 -07:00
|
|
|
method: "GET",
|
|
|
|
headers: {
|
2023-10-17 23:26:55 -07:00
|
|
|
Authorization: `Bearer ${widget.key}`,
|
|
|
|
},
|
2022-10-27 23:28:21 +02:00
|
|
|
});
|
|
|
|
|
2022-10-28 00:58:59 -07:00
|
|
|
if (status !== 200 || !data) {
|
|
|
|
logger.error("Error getting data from WatchTower: %d. Data: %s", status, data);
|
2023-10-17 23:26:55 -07:00
|
|
|
return res.status(status).json({ error: { message: `HTTP Error ${status}`, url, data } });
|
2022-10-28 00:58:59 -07:00
|
|
|
}
|
|
|
|
|
2023-10-17 23:26:55 -07:00
|
|
|
const cleanData = data
|
|
|
|
.toString()
|
|
|
|
.split("\n")
|
|
|
|
.filter((s) => s.startsWith("watchtower"));
|
|
|
|
const jsonRes = {};
|
|
|
|
|
|
|
|
cleanData
|
|
|
|
.map((e) => e.split(" "))
|
|
|
|
.forEach((strArray) => {
|
|
|
|
const [key, value] = strArray;
|
|
|
|
jsonRes[key] = value;
|
|
|
|
});
|
2022-10-27 23:28:21 +02:00
|
|
|
|
|
|
|
if (contentType) res.setHeader("Content-Type", contentType);
|
|
|
|
return res.status(status).send(jsonRes);
|
|
|
|
}
|