mirror of
https://github.com/AikidoSec/safe-chain.git
synced 2026-05-26 12:10:49 +00:00
Using port from req url when creating proxy request instead of hardcoded port 443
This commit is contained in:
parent
53c59e35e9
commit
bd19f477f7
1 changed files with 11 additions and 8 deletions
|
|
@ -15,7 +15,7 @@ import { gunzipSync, gzipSync } from "zlib";
|
||||||
*/
|
*/
|
||||||
export function mitmConnect(req, clientSocket, interceptor) {
|
export function mitmConnect(req, clientSocket, interceptor) {
|
||||||
ui.writeVerbose(`Safe-chain: Set up MITM tunnel for ${req.url}`);
|
ui.writeVerbose(`Safe-chain: Set up MITM tunnel for ${req.url}`);
|
||||||
const { hostname } = new URL(`http://${req.url}`);
|
const { hostname, port } = new URL(`http://${req.url}`);
|
||||||
|
|
||||||
clientSocket.on("error", (err) => {
|
clientSocket.on("error", (err) => {
|
||||||
ui.writeVerbose(
|
ui.writeVerbose(
|
||||||
|
|
@ -26,7 +26,7 @@ export function mitmConnect(req, clientSocket, interceptor) {
|
||||||
// Not subscribing to 'close' event will cause node to throw and crash.
|
// Not subscribing to 'close' event will cause node to throw and crash.
|
||||||
});
|
});
|
||||||
|
|
||||||
const server = createHttpsServer(hostname, interceptor);
|
const server = createHttpsServer(hostname, port, interceptor);
|
||||||
|
|
||||||
server.on("error", (err) => {
|
server.on("error", (err) => {
|
||||||
ui.writeError(`Safe-chain: HTTPS server error: ${err.message}`);
|
ui.writeError(`Safe-chain: HTTPS server error: ${err.message}`);
|
||||||
|
|
@ -46,10 +46,11 @@ export function mitmConnect(req, clientSocket, interceptor) {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param {string} hostname
|
* @param {string} hostname
|
||||||
|
* @param {string} port
|
||||||
* @param {Interceptor} interceptor
|
* @param {Interceptor} interceptor
|
||||||
* @returns {import("https").Server}
|
* @returns {import("https").Server}
|
||||||
*/
|
*/
|
||||||
function createHttpsServer(hostname, interceptor) {
|
function createHttpsServer(hostname, port, interceptor) {
|
||||||
const cert = generateCertForHost(hostname);
|
const cert = generateCertForHost(hostname);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
@ -80,7 +81,7 @@ function createHttpsServer(hostname, interceptor) {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Collect request body
|
// Collect request body
|
||||||
forwardRequest(req, hostname, res, requestInterceptor);
|
forwardRequest(req, hostname, port, res, requestInterceptor);
|
||||||
}
|
}
|
||||||
|
|
||||||
const server = https.createServer(
|
const server = https.createServer(
|
||||||
|
|
@ -109,11 +110,12 @@ function getRequestPathAndQuery(url) {
|
||||||
/**
|
/**
|
||||||
* @param {import("http").IncomingMessage} req
|
* @param {import("http").IncomingMessage} req
|
||||||
* @param {string} hostname
|
* @param {string} hostname
|
||||||
|
* @param {string} port
|
||||||
* @param {import("http").ServerResponse} res
|
* @param {import("http").ServerResponse} res
|
||||||
* @param {import("./interceptors/interceptorBuilder.js").RequestInterceptionHandler} requestHandler
|
* @param {import("./interceptors/interceptorBuilder.js").RequestInterceptionHandler} requestHandler
|
||||||
*/
|
*/
|
||||||
function forwardRequest(req, hostname, res, requestHandler) {
|
function forwardRequest(req, hostname, port, res, requestHandler) {
|
||||||
const proxyReq = createProxyRequest(hostname, req, res, requestHandler);
|
const proxyReq = createProxyRequest(hostname, port, req, res, requestHandler);
|
||||||
|
|
||||||
proxyReq.on("error", (err) => {
|
proxyReq.on("error", (err) => {
|
||||||
ui.writeVerbose(
|
ui.writeVerbose(
|
||||||
|
|
@ -144,13 +146,14 @@ function forwardRequest(req, hostname, res, requestHandler) {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param {string} hostname
|
* @param {string} hostname
|
||||||
|
* @param {string} port
|
||||||
* @param {import("http").IncomingMessage} req
|
* @param {import("http").IncomingMessage} req
|
||||||
* @param {import("http").ServerResponse} res
|
* @param {import("http").ServerResponse} res
|
||||||
* @param {import("./interceptors/interceptorBuilder.js").RequestInterceptionHandler} requestHandler
|
* @param {import("./interceptors/interceptorBuilder.js").RequestInterceptionHandler} requestHandler
|
||||||
*
|
*
|
||||||
* @returns {import("http").ClientRequest}
|
* @returns {import("http").ClientRequest}
|
||||||
*/
|
*/
|
||||||
function createProxyRequest(hostname, req, res, requestHandler) {
|
function createProxyRequest(hostname, port, req, res, requestHandler) {
|
||||||
/** @type {NodeJS.Dict<string | string[]> | undefined} */
|
/** @type {NodeJS.Dict<string | string[]> | undefined} */
|
||||||
let headers = { ...req.headers };
|
let headers = { ...req.headers };
|
||||||
// Remove the host header from the incoming request before forwarding.
|
// Remove the host header from the incoming request before forwarding.
|
||||||
|
|
@ -163,7 +166,7 @@ function createProxyRequest(hostname, req, res, requestHandler) {
|
||||||
/** @type {import("http").RequestOptions} */
|
/** @type {import("http").RequestOptions} */
|
||||||
const options = {
|
const options = {
|
||||||
hostname: hostname,
|
hostname: hostname,
|
||||||
port: 443,
|
port: port,
|
||||||
path: req.url,
|
path: req.url,
|
||||||
method: req.method,
|
method: req.method,
|
||||||
headers: { ...headers },
|
headers: { ...headers },
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue