Created
January 17, 2023 13:00
-
-
Save mdzzohrabi/5cfa3548b16c8248c2363e80ad95c7e5 to your computer and use it in GitHub Desktop.
Port Forwarding
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// @ts-check | |
/** | |
* Forward local port to an external port through a local network proxy | |
* | |
* @author Masoud Zohrabi <[email protected]> | |
*/ | |
let net = require('net'); | |
let invariant = (expr, error) => { if (!expr) throw Error(error); }; | |
let log = console.log.bind(console); | |
function main() { | |
let [nodePath, scriptPath, ...args] = process.argv; | |
/** @type {{[name: string]: any}} */ | |
let Options = {}; | |
// Parse cli arguments | |
args.forEach(arg => { | |
let sub = 0; | |
if (arg.startsWith('--')) sub = 2; | |
else if (arg.startsWith('-')) sub = 1; | |
let [name, value] = arg.substring(sub).split('='); | |
Options[name] = value || true; | |
}); | |
let { proxy, dest, port } = { proxy: null, dest: null, port: 9090, ...Options }; | |
if (!dest || !port) { | |
return log(`Usage: node forward --port=9090 --proxy=127.0.0.0:8000 --dest=192.168.1.1:1234`); | |
} | |
let [destHost, destPort = 80] = String(dest).split(':'); | |
invariant(destHost && destPort, `Invalid destination address (e.g: 127.0.0.0:9090)`); | |
let [proxyHost, proxyPort] = proxy ? String(proxy).split(':') : []; | |
if (proxy) | |
invariant(proxyHost && proxyPort, `Invalid proxy address (e.g: 127.0.0.0:9090)`); | |
// Start forwarding server | |
net.createServer(async client => { | |
/** @type {net.Socket} */ | |
let proxy; | |
let terminateSession = (err) => { | |
if (err) log('Close client connection because of exception:', err); | |
if (!client.destroyed) client.end(); | |
if (proxy && !proxy.destroyed) proxy.end(); | |
} | |
// Client connected | |
client.on('error', terminateSession); | |
try { | |
// Create tunnel to server | |
proxy = proxyHost ? await createConnectionThroughProxy(proxyHost, Number(proxyPort), destHost, Number(destPort)) : await createConnection(destHost, destPort); | |
proxy.on('error', terminateSession); | |
client.pipe(proxy); | |
proxy.pipe(client); | |
} catch (err) { | |
terminateSession(err); | |
} | |
}).listen(Number(port), () => { | |
log(`Listen to ${port} for new connection`) | |
}); | |
} | |
function createConnection(remoteHost, remotePort, request = '') { | |
return new Promise((resolve, reject) => { | |
let connection = net.createConnection(remotePort, remoteHost, () => { | |
resolve(connection); | |
}); | |
connection.on('error', reject); | |
}); | |
} | |
/** | |
* Connect to a target through proxy | |
* @param {string} proxyHost | |
* @param {number} proxyPort | |
* @param {string} remoteHost | |
* @param {number} remotePort | |
* @param {any} request | |
* @returns {Promise<net.Socket>} | |
*/ | |
function createConnectionThroughProxy(proxyHost, proxyPort, remoteHost, remotePort, request = '') { | |
return new Promise((resolve, reject) => { | |
let connection = net.createConnection(proxyPort, proxyHost, () => { | |
connection.write(`CONNECT ${remoteHost}:${remotePort} HTTP/1.1\n\n${request}`); | |
connection.once('data', buffer => { | |
if (buffer.toString().includes('Connection established')) resolve(connection); | |
else reject(buffer.toString()); | |
}); | |
}); | |
connection.on('error', reject); | |
}); | |
} | |
main(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment