Enhancement: forward cookies from request (#1804)

pull/1936/head
James Waters 1 year ago committed by GitHub
parent 63f952509e
commit d4edd432d8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -3,6 +3,7 @@ import { performance } from "perf_hooks";
import { getServiceItem } from "utils/config/service-helpers";
import createLogger from "utils/logger";
import { httpProxy } from "utils/proxy/http";
import {importCookieHeader} from "utils/proxy/cookie-jar";
const logger = createLogger("ping");
@ -25,20 +26,24 @@ export default async function handler(req, res) {
});
}
if (req.headers.cookie) {
importCookieHeader(pingURL, req.headers.cookie)
}
try {
let startTime = performance.now();
let [status] = await httpProxy(pingURL, {
method: "HEAD"
});
let endTime = performance.now();
if (status > 403) {
// try one more time as a GET in case HEAD is rejected for whatever reason
startTime = performance.now();
[status] = await httpProxy(pingURL);
endTime = performance.now();
}
return res.status(200).json({
status,
latency: endTime - startTime

@ -37,3 +37,27 @@ export function addCookieToJar(url, headers) {
cookieJar.setCookieSync(cookies[i], url.toString(), { ignoreError: true });
}
}
export function importCookieHeader(url, cookieHeader) {
const cookies = cookieHeader.split(';');
for (let i = 0; i < cookies.length; i += 1) {
const [key, value] = cookies[i].trim().split('=');
// If there's an existing cookie with a matching key for this url,
// we want to update it. Otherwise, we add a new cookie
let existingCookie;
try {
existingCookie = cookieJar.getCookiesSync(url).find(existing => existing.key === key);
} catch (e) {
console.debug(`Failed to get cookies for ${url}: ${e}`)
}
if (existingCookie) {
existingCookie.value = value;
} else {
cookieJar.setCookieSync(new Cookie({
key, value
}), url.toString(), { ignoreError: true });
}
}
}

@ -4,6 +4,7 @@ import validateWidgetData from "utils/proxy/validate-widget-data";
import { httpProxy } from "utils/proxy/http";
import createLogger from "utils/logger";
import widgets from "widgets/widgets";
import {importCookieHeader} from "utils/proxy/cookie-jar";
const logger = createLogger("genericProxyHandler");
@ -34,14 +35,18 @@ export default async function genericProxyHandler(req, res, map) {
params.body = req.body;
}
if (req.headers.cookie) {
importCookieHeader(url, req.headers.cookie)
}
const [status, contentType, data] = await httpProxy(url, params);
let resultData = data;
if (resultData.error?.url) {
resultData.error.url = sanitizeErrorURL(url);
}
if (status === 200) {
if (!validateWidgetData(widget, endpoint, resultData)) {
return res.status(status).json({error: {message: "Invalid data", url: sanitizeErrorURL(url), data: resultData}});

Loading…
Cancel
Save