Skip to content

Instantly share code, notes, and snippets.

@skhavari
Forked from tonygambone/https_forward_proxy.js
Last active August 29, 2015 14:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save skhavari/0bb83f0f0e6946335918 to your computer and use it in GitHub Desktop.
Save skhavari/0bb83f0f0e6946335918 to your computer and use it in GitHub Desktop.
// HTTP forward proxy server that can also proxy HTTPS requests
// using the CONNECT method
// requires https://github.com/nodejitsu/node-http-proxy
var httpProxy = require('http-proxy'),
url = require('url'),
net = require('net'),
http = require('http');
process.on('uncaughtException', logError);
function truncate(str) {
var maxLength = 64;
return (str.length >= maxLength ? str.substring(0,maxLength) + '...' : str);
}
function logRequest(req) {
console.log(req.method + ' ' + truncate(req.url));
for (var i in req.headers)
console.log(' * ' + i + ': ' + truncate(req.headers[i]));
}
function logError(e) {
console.warn('*** ' + e);
}
// this proxy will handle regular HTTP requests
var regularProxy = new httpProxy.RoutingProxy();
// standard HTTP server that will pass requests
// to the proxy
var server = http.createServer(function (req, res) {
logRequest(req);
uri = url.parse(req.url);
regularProxy.proxyRequest(req, res, {
host: uri.hostname,
port: uri.port || 80
});
});
// support HTTP CONNECT to tunnel SSL connections
// see here: http://en.wikipedia.org/wiki/HTTP_tunnel#HTTP_CONNECT_tunneling
server.on('upgrade', function(req, socket, head) {
logRequest(req);
// URL is in the form 'hostname:port'
var parts = req.url.split(':', 2);
// open a TCP connection to the remote host
var conn = net.connect(parts[1], parts[0], function() {
// respond to the client that the connection was made
socket.write("HTTP/1.1 200 OK\r\n\r\n");
// create a tunnel between the two hosts
socket.pipe(conn);
conn.pipe(socket);
});
});
server.listen(3333);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment