Skip to content

Instantly share code, notes, and snippets.

@bhurlow
Created May 7, 2013 19:56
Show Gist options
  • Save bhurlow/5535607 to your computer and use it in GitHub Desktop.
Save bhurlow/5535607 to your computer and use it in GitHub Desktop.
proxy tunnel
var http = require('http');
var net = require('net');
var url = require('url');
var proxy = http.createServer();
// proxy an HTTP request
proxy.on('request', function(req, res){
var uri = url.parse(req.url);
var httpMessage = req.method + ' ' + uri.path + ' HTTP/'+ req.httpVersion + '\r\n';
for(var header in req.headers){
httpMessage += header + ': ' + req.headers[header] + '\r\n';
}
httpMessage += '\r\n';
req.on('data', function(data){
httpMessage += data;
});
req.on('end', function(data){
httpMessage += data;
var client = net.connect(uri.port || 80, uri.hostname, function(){
client.write(httpMessage);
client.pipe(req.connection);
req.connection.pipe(client);
});
});
});
// proxy an HTTPS request
proxy.on('connect', function(req, socket, head) {
var uri = req.url.split(':')
var tunnel = net.connect({
port: uri[1],
host: uri[0]
}, function() {
socket.write('HTTP/1.1 200 Connection Established\r\n\r\n');
tunnel.write(head);
tunnel.pipe(socket);
socket.pipe(tunnel);
});
});
proxy.listen(8080);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment