2022-09-07 16:53:24 +03:00
|
|
|
/* eslint-disable prefer-promise-reject-errors */
|
2022-09-14 10:46:52 -07:00
|
|
|
/* eslint-disable no-param-reassign */
|
2023-06-05 21:43:49 -04:00
|
|
|
import { createUnzip } from "node:zlib";
|
|
|
|
|
2022-09-14 10:46:52 -07:00
|
|
|
import { http, https } from "follow-redirects";
|
|
|
|
|
2022-09-26 12:04:37 +03:00
|
|
|
import { addCookieToJar, setCookieHeader } from "./cookie-jar";
|
2022-09-14 11:08:36 -07:00
|
|
|
|
2022-09-28 09:55:18 -07:00
|
|
|
import createLogger from "utils/logger";
|
|
|
|
|
|
|
|
const logger = createLogger("httpProxy");
|
|
|
|
|
2022-09-14 11:08:36 -07:00
|
|
|
function addCookieHandler(url, params) {
|
|
|
|
setCookieHeader(url, params);
|
2022-09-14 10:46:52 -07:00
|
|
|
|
|
|
|
// handle cookies during redirects
|
|
|
|
params.beforeRedirect = (options, responseInfo) => {
|
2022-09-16 19:11:57 -07:00
|
|
|
addCookieToJar(options.href, responseInfo.headers);
|
2022-09-14 11:08:36 -07:00
|
|
|
setCookieHeader(options.href, options);
|
2022-09-14 10:46:52 -07:00
|
|
|
};
|
|
|
|
}
|
2022-08-25 20:57:25 +03:00
|
|
|
|
2022-11-24 12:26:22 -08:00
|
|
|
function handleRequest(requestor, url, params) {
|
2022-09-07 16:53:24 +03:00
|
|
|
return new Promise((resolve, reject) => {
|
2022-09-14 10:46:52 -07:00
|
|
|
addCookieHandler(url, params);
|
2022-11-24 12:26:22 -08:00
|
|
|
if (params?.body) {
|
|
|
|
params.headers = params.headers ?? {};
|
|
|
|
params.headers['content-length'] = Buffer.byteLength(params.body);
|
|
|
|
}
|
|
|
|
|
|
|
|
const request = requestor.request(url, params, (response) => {
|
2022-09-07 16:53:24 +03:00
|
|
|
const data = [];
|
2023-06-05 21:43:49 -04:00
|
|
|
const contentEncoding = response.headers['content-encoding']?.trim().toLowerCase();
|
|
|
|
|
|
|
|
let responseContent = response;
|
|
|
|
if (contentEncoding === 'gzip' || contentEncoding === 'deflate') {
|
|
|
|
responseContent = createUnzip();
|
2023-06-13 19:21:12 -07:00
|
|
|
// zlib errors
|
|
|
|
responseContent.on("error", (e) => {
|
|
|
|
logger.error(e);
|
|
|
|
responseContent = response; // fallback
|
|
|
|
});
|
2023-06-05 21:43:49 -04:00
|
|
|
response.pipe(responseContent);
|
|
|
|
}
|
2022-08-25 20:57:25 +03:00
|
|
|
|
2023-06-05 21:43:49 -04:00
|
|
|
responseContent.on("data", (chunk) => {
|
2022-08-27 12:38:32 +03:00
|
|
|
data.push(chunk);
|
2022-08-25 20:57:25 +03:00
|
|
|
});
|
|
|
|
|
2023-06-05 21:43:49 -04:00
|
|
|
responseContent.on("end", () => {
|
2022-09-16 19:11:57 -07:00
|
|
|
addCookieToJar(url, response.headers);
|
2022-09-12 19:35:47 -07:00
|
|
|
resolve([response.statusCode, response.headers["content-type"], Buffer.concat(data), response.headers]);
|
2022-08-25 20:57:25 +03:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
request.on("error", (error) => {
|
|
|
|
reject([500, error]);
|
|
|
|
});
|
|
|
|
|
2022-11-24 12:26:22 -08:00
|
|
|
if (params?.body) {
|
2022-09-12 19:35:47 -07:00
|
|
|
request.write(params.body);
|
|
|
|
}
|
|
|
|
|
2022-08-25 20:57:25 +03:00
|
|
|
request.end();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2022-11-24 12:26:22 -08:00
|
|
|
export function httpsRequest(url, params) {
|
|
|
|
return handleRequest(https, url, params);
|
|
|
|
}
|
2022-09-12 19:35:47 -07:00
|
|
|
|
2022-11-24 12:26:22 -08:00
|
|
|
export function httpRequest(url, params) {
|
|
|
|
return handleRequest(http, url, params);
|
2022-08-25 20:57:25 +03:00
|
|
|
}
|
2022-09-04 21:58:42 +03:00
|
|
|
|
2022-09-28 09:55:18 -07:00
|
|
|
export async function httpProxy(url, params = {}) {
|
2022-09-04 21:58:42 +03:00
|
|
|
const constructedUrl = new URL(url);
|
|
|
|
|
2022-09-28 09:55:18 -07:00
|
|
|
let request = null;
|
2022-09-04 21:58:42 +03:00
|
|
|
if (constructedUrl.protocol === "https:") {
|
|
|
|
const httpsAgent = new https.Agent({
|
|
|
|
rejectUnauthorized: false,
|
|
|
|
});
|
|
|
|
|
2022-09-28 09:55:18 -07:00
|
|
|
request = httpsRequest(constructedUrl, {
|
2022-09-04 21:58:42 +03:00
|
|
|
agent: httpsAgent,
|
|
|
|
...params,
|
|
|
|
});
|
2022-09-28 09:55:18 -07:00
|
|
|
} else {
|
|
|
|
request = httpRequest(constructedUrl, params);
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
|
|
|
const [status, contentType, data, responseHeaders] = await request;
|
|
|
|
return [status, contentType, data, responseHeaders];
|
|
|
|
}
|
|
|
|
catch (err) {
|
2023-05-18 00:26:32 -07:00
|
|
|
logger.error(
|
2023-05-23 03:37:56 -07:00
|
|
|
"Error calling %s//%s%s%s...",
|
2023-05-18 00:26:32 -07:00
|
|
|
constructedUrl.protocol,
|
|
|
|
constructedUrl.hostname,
|
2023-05-23 03:37:56 -07:00
|
|
|
constructedUrl.port ? `:${constructedUrl.port}` : '',
|
2023-05-18 00:26:32 -07:00
|
|
|
constructedUrl.pathname
|
|
|
|
);
|
2022-09-28 09:55:18 -07:00
|
|
|
logger.error(err);
|
2022-10-22 22:48:25 -07:00
|
|
|
return [500, "application/json", { error: {message: err?.message ?? "Unknown error", url, rawError: err} }, null];
|
2022-09-04 21:58:42 +03:00
|
|
|
}
|
|
|
|
}
|