utils cleanup, initial static generation

This commit is contained in:
Ben Phelps 2022-09-26 12:04:37 +03:00
parent ec8700f3e9
commit e1a3a82f75
86 changed files with 279 additions and 261 deletions

View file

@ -0,0 +1,55 @@
export function formatApiCall(url, args) {
const find = /\{.*?\}/g;
const replace = (match) => {
const key = match.replace(/\{|\}/g, "");
return args[key];
};
return url.replace(find, replace);
}
function getURLSearchParams(widget, endpoint) {
const params = new URLSearchParams({
type: widget.type,
group: widget.service_group,
service: widget.service_name,
endpoint,
});
return params;
}
export function formatProxyUrlWithSegments(widget, endpoint, segments) {
const params = getURLSearchParams(widget, endpoint);
if (segments) {
params.append("segments", JSON.stringify(segments));
}
return `/api/services/proxy?${params.toString()}`;
}
export function formatProxyUrl(widget, endpoint, queryParams) {
const params = getURLSearchParams(widget, endpoint);
if (queryParams) {
params.append("query", JSON.stringify(queryParams));
}
return `/api/services/proxy?${params.toString()}`;
}
export function asJson(data) {
if (data?.length > 0) {
const json = JSON.parse(data.toString());
return json;
}
return data;
}
export function jsonArrayTransform(data, transform) {
const json = asJson(data);
if (json instanceof Array) {
return transform(json);
}
return json;
}
export function jsonArrayFilter(data, filter) {
return jsonArrayTransform(data, (items) => items.filter(filter));
}

View file

@ -0,0 +1,13 @@
import cache from "memory-cache";
export default async function cachedFetch(url, duration) {
const cached = cache.get(url);
if (cached) {
return cached;
}
const data = await fetch(url).then((res) => res.json());
cache.put(url, data, duration * 1000 * 60);
return data;
}

View file

@ -0,0 +1,34 @@
/* eslint-disable no-param-reassign */
import { Cookie, CookieJar } from 'tough-cookie';
const cookieJar = new CookieJar();
export function setCookieHeader(url, params) {
// add cookie header, if we have one in the jar
const existingCookie = cookieJar.getCookieStringSync(url.toString());
if (existingCookie) {
params.headers = params.headers ?? {};
params.headers.Cookie = existingCookie;
}
}
export function addCookieToJar(url, headers) {
let cookieHeader = headers['set-cookie'];
if (headers instanceof Headers) {
cookieHeader = headers.get('set-cookie');
}
if (!cookieHeader || cookieHeader.length === 0) return;
let cookies = null;
if (cookieHeader instanceof Array) {
cookies = cookieHeader.map(Cookie.parse);
}
else {
cookies = [Cookie.parse(cookieHeader)];
}
for (let i = 0; i < cookies.length; i += 1) {
cookieJar.setCookieSync(cookies[i], url.toString());
}
}

View file

@ -0,0 +1,58 @@
import getServiceWidget from "utils/service-helpers";
import { formatApiCall } from "utils/proxy/api-helpers";
import { httpProxy } from "utils/proxy/http";
import createLogger from "utils/logger";
import widgets from "widgets/widgets";
const logger = createLogger("credentialedProxyHandler");
export default async function credentialedProxyHandler(req, res) {
const { group, service, endpoint } = req.query;
if (group && service) {
const widget = await getServiceWidget(group, service);
if (!widgets?.[widget.type]?.api) {
return res.status(403).json({ error: "Service does not support API calls" });
}
if (widget) {
const url = new URL(formatApiCall(widgets[widget.type].api, { endpoint, ...widget }));
const headers = {
"Content-Type": "application/json",
};
if (widget.type === "coinmarketcap") {
headers["X-CMC_PRO_API_KEY"] = `${widget.key}`;
} else if (widget.type === "gotify") {
headers["X-gotify-Key"] = `${widget.key}`;
} else if (widget.type === "authentik") {
headers.Authorization = `Bearer ${widget.key}`;
} else {
headers["X-API-Key"] = `${widget.key}`;
}
const [status, contentType, data] = await httpProxy(url, {
method: req.method,
withCredentials: true,
credentials: "include",
headers,
});
if (status === 204 || status === 304) {
return res.status(status).end();
}
if (status >= 400) {
logger.debug("HTTP Error %d calling %s//%s%s...", status, url.protocol, url.hostname, url.pathname);
}
if (contentType) res.setHeader("Content-Type", contentType);
return res.status(status).send(data);
}
}
logger.debug("Invalid or missing proxy service type '%s' in group '%s'", service, group);
return res.status(400).json({ error: "Invalid proxy service type" });
}

View file

@ -0,0 +1,55 @@
import getServiceWidget from "utils/service-helpers";
import { formatApiCall } from "utils/proxy/api-helpers";
import { httpProxy } from "utils/proxy/http";
import createLogger from "utils/logger";
import widgets from "widgets/widgets";
const logger = createLogger("genericProxyHandler");
export default async function genericProxyHandler(req, res, map) {
const { group, service, endpoint } = req.query;
if (group && service) {
const widget = await getServiceWidget(group, service);
if (!widgets?.[widget.type]?.api) {
return res.status(403).json({ error: "Service does not support API calls" });
}
if (widget) {
const url = new URL(formatApiCall(widgets[widget.type].api, { endpoint, ...widget }));
let headers;
if (widget.username && widget.password) {
headers = {
Authorization: `Basic ${Buffer.from(`${widget.username}:${widget.password}`).toString("base64")}`,
};
}
const [status, contentType, data] = await httpProxy(url, {
method: req.method,
headers,
});
let resultData = data;
if (status === 200 && map) {
resultData = map(data);
}
if (contentType) res.setHeader("Content-Type", contentType);
if (status === 204 || status === 304) {
return res.status(status).end();
}
if (status >= 400) {
logger.debug("HTTP Error %d calling %s//%s%s...", status, url.protocol, url.hostname, url.pathname);
}
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" });
}

87
src/utils/proxy/http.js Normal file
View file

@ -0,0 +1,87 @@
/* eslint-disable prefer-promise-reject-errors */
/* eslint-disable no-param-reassign */
import { http, https } from "follow-redirects";
import { addCookieToJar, setCookieHeader } from "./cookie-jar";
function addCookieHandler(url, params) {
setCookieHeader(url, params);
// handle cookies during redirects
params.beforeRedirect = (options, responseInfo) => {
addCookieToJar(options.href, responseInfo.headers);
setCookieHeader(options.href, options);
};
}
export function httpsRequest(url, params) {
return new Promise((resolve, reject) => {
addCookieHandler(url, params);
const request = https.request(url, params, (response) => {
const data = [];
response.on("data", (chunk) => {
data.push(chunk);
});
response.on("end", () => {
addCookieToJar(url, response.headers);
resolve([response.statusCode, response.headers["content-type"], Buffer.concat(data), response.headers]);
});
});
request.on("error", (error) => {
reject([500, error]);
});
if (params.body) {
request.write(params.body);
}
request.end();
});
}
export function httpRequest(url, params) {
return new Promise((resolve, reject) => {
addCookieHandler(url, params);
const request = http.request(url, params, (response) => {
const data = [];
response.on("data", (chunk) => {
data.push(chunk);
});
response.on("end", () => {
addCookieToJar(url, response.headers);
resolve([response.statusCode, response.headers["content-type"], Buffer.concat(data), response.headers]);
});
});
request.on("error", (error) => {
reject([500, error]);
});
if (params.body) {
request.write(params.body);
}
request.end();
});
}
export function httpProxy(url, params = {}) {
const constructedUrl = new URL(url);
if (constructedUrl.protocol === "https:") {
const httpsAgent = new https.Agent({
rejectUnauthorized: false,
});
return httpsRequest(constructedUrl, {
agent: httpsAgent,
...params,
});
}
return httpRequest(constructedUrl, params);
}