Skip to content

Instantly share code, notes, and snippets.

@matths
Forked from bnoordhuis/http-and-https-proxy.js
Last active August 29, 2015 14:01
Show Gist options
  • Save matths/3c08cbca785628bfc4fe to your computer and use it in GitHub Desktop.
Save matths/3c08cbca785628bfc4fe to your computer and use it in GitHub Desktop.
A node.js proxy listening on port 80 that forwards HTTP or SSH traffic to different ports. Thus SSH might become reachable to a somewhat restrictive hotel or corporate firewall, while HTTP can still be served as well.
var fs = require('fs');
var net = require('net');
var http = require('http');
var sshAddress = '22';
var httpAddress = '8080';
var incoming = '80';
http.createServer(httpConnection).listen(httpAddress);
net.createServer(tcpConnection).listen(incoming);
function tcpConnection(conn) {
conn.once('data', function(buffer) {
var address = (buffer.toString().substr(0,3) == 'SSH') ? sshAddress : httpAddress;
var proxy = net.createConnection(address, function() {
proxy.write(buffer);
conn.pipe(proxy).pipe(conn);
});
});
}
function httpConnection(req, res) {
res.writeHead(200, {'Content-Type': 'text/plain'});
res.end('Hello World\n');
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment