Remove unnecessary proxy

This commit is contained in:
shamoon 2024-08-09 14:06:20 -07:00
parent a5bb3fe60f
commit 2645e78fa7
3 changed files with 12 additions and 86 deletions

View File

@ -35,9 +35,16 @@ export default async function credentialedProxyHandler(req, res, map) {
} else if (widget.type === "gotify") { } else if (widget.type === "gotify") {
headers["X-gotify-Key"] = `${widget.key}`; headers["X-gotify-Key"] = `${widget.key}`;
} else if ( } else if (
["authentik", "cloudflared", "ghostfolio", "mealie", "tailscale", "tandoor", "pterodactyl"].includes( [
widget.type, "authentik",
) "cloudflared",
"ghostfolio",
"linkwarden",
"mealie",
"tailscale",
"tandoor",
"pterodactyl",
].includes(widget.type)
) { ) {
headers.Authorization = `Bearer ${widget.key}`; headers.Authorization = `Bearer ${widget.key}`;
} else if (widget.type === "truenas") { } else if (widget.type === "truenas") {

View File

@ -1,81 +0,0 @@
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";
const proxyName = "linkwardenProxyHandler";
const logger = createLogger(proxyName);
const CONTENT_TYPE_JSON = "application/json";
const AUTHORIZATION = "Authorization";
const BEARER = "Bearer";
const ERROR_INVALID_SERVICE = "Invalid proxy service type";
const ERROR_MISSING_TOKEN = "Missing widget token";
async function retrieveFromAPI(url, token) {
const headers = {
"Content-Type": CONTENT_TYPE_JSON,
[AUTHORIZATION]: `${BEARER} ${token}`,
};
const [status, , data] = await httpProxy(url, { headers });
if (status === 401) {
const errorResponse = JSON.parse(Buffer.from(data).toString());
throw new Error(`Unauthorized: ${errorResponse.response}`);
}
if (status !== 200) {
throw new Error(`Error getting data from Linkwarden: ${status}. Data: ${data.toString()}`);
}
return JSON.parse(Buffer.from(data).toString());
}
export default async function linkwardenProxyHandler(req, res) {
const { group, service, endpoint, query } = req.query;
if (!group || !service) {
logger.debug("Invalid or missing service '%s' or group '%s'", service, group);
return res.status(400).json({ error: ERROR_INVALID_SERVICE });
}
const widget = await getServiceWidget(group, service);
if (!widget) {
logger.debug("Invalid or missing widget for service '%s' in group '%s'", service, group);
return res.status(400).json({ error: ERROR_INVALID_SERVICE });
}
if (!widget.token) {
logger.debug("Invalid or missing token for service '%s' in group '%s'", service, group);
return res.status(400).json({ error: ERROR_MISSING_TOKEN });
}
const apiURL = "{url}/api/v1/{endpoint}";
try {
const url = new URL(formatApiCall(apiURL, { endpoint, ...widget }));
// Parse the query JSON if it exists
if (query) {
try {
const parsedQuery = JSON.parse(query);
Object.entries(parsedQuery).forEach(([key, value]) => {
url.searchParams.append(key, value);
});
} catch (error) {
logger.error("Error parsing query JSON:", error);
}
}
logger.info(`Constructed API URL: ${url.toString()}`);
const data = await retrieveFromAPI(url, widget.token);
return res.status(200).json(data);
} catch (e) {
logger.error(e.message);
return res.status(500).json({ error: { message: e.message } });
}
}

View File

@ -1,8 +1,8 @@
import linkwardenProxyHandler from "./proxy"; import credentialedProxyHandler from "utils/proxy/handlers/credentialed";
const widget = { const widget = {
api: "{url}/api/v1/{endpoint}", api: "{url}/api/v1/{endpoint}",
proxyHandler: linkwardenProxyHandler, proxyHandler: credentialedProxyHandler,
mappings: { mappings: {
collections: { collections: {