Created
May 12, 2020 16:58
-
-
Save dvdbng/621043d7b60713208bafb1f06191884e to your computer and use it in GitHub Desktop.
TCP forwarding proxy over ssh
This file contains hidden or 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
const net = require('net'); | |
const child_process = require('child_process'); | |
function makeProxy(port, jump, target) { | |
const server = net.createServer(socket => { | |
const subprocess = child_process.spawn( | |
'ssh', | |
[jump, '-W', target], | |
{ stdio: ['pipe', 'pipe', 'inherit'] } | |
); | |
socket.pipe(subprocess.stdin); | |
subprocess.stdout.pipe(socket); | |
socket.on('close', _ => subprocess.kill()); | |
socket.on('error', _ => subprocess.kill()); | |
subprocess.on('close', _ => socket.end()); | |
}); | |
server.listen(port, '127.0.0.1'); | |
} | |
makeProxy(5433, 'ssh_user@ssh_host.com', 'db_host:5432'); | |
makeProxy(5434, 'ssh_user_2@ssh_host_2.com', 'db_host_2:5432'); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment