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 */
|
|
|
|
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-08-26 22:33:54 +03:00
|
|
|
export function httpsRequest(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-09-07 16:53:24 +03:00
|
|
|
const request = https.request(url, params, (response) => {
|
|
|
|
const data = [];
|
2022-08-25 20:57:25 +03:00
|
|
|
|
|
|
|
response.on("data", (chunk) => {
|
2022-08-27 12:38:32 +03:00
|
|
|
data.push(chunk);
|
2022-08-25 20:57:25 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
response.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-09-12 19:35:47 -07:00
|
|
|
if (params.body) {
|
|
|
|
request.write(params.body);
|
|
|
|
}
|
|
|
|
|
2022-08-25 20:57:25 +03:00
|
|
|
request.end();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2022-08-26 22:33:54 +03:00
|
|
|
export function httpRequest(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-09-07 16:53:24 +03:00
|
|
|
const request = http.request(url, params, (response) => {
|
|
|
|
const data = [];
|
2022-08-25 20:57:25 +03:00
|
|
|
|
|
|
|
response.on("data", (chunk) => {
|
2022-08-27 12:38:32 +03:00
|
|
|
data.push(chunk);
|
2022-08-25 20:57:25 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
response.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-09-12 19:35:47 -07:00
|
|
|
if (params.body) {
|
|
|
|
request.write(params.body);
|
|
|
|
}
|
|
|
|
|
2022-08-25 20:57:25 +03:00
|
|
|
request.end();
|
|
|
|
});
|
|
|
|
}
|
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) {
|
|
|
|
logger.error("Error calling %s//%s%s...", url.protocol, url.hostname, url.pathname);
|
|
|
|
logger.error(err);
|
|
|
|
return [500, "application/json", { error: "Unexpected error" }, null];
|
2022-09-04 21:58:42 +03:00
|
|
|
}
|
|
|
|
}
|