diff --git a/examples/https.js b/examples/https.js new file mode 100644 index 0000000..8e3410d --- /dev/null +++ b/examples/https.js @@ -0,0 +1,55 @@ +var http = require('http') + , https = require('https') + , caronte = require('caronte') + ; +// +// Create your proxy server +// +var options = {target:'https://google.com', + agent: new https.Agent({rejectUnauthorized:false}), + }; + +var proxyServer = caronte.createProxyServer(options); + +proxyServer.ee.on('*:error', function(err, req, res){ + res.end('There was an error proxying your request'); +}); + +console.log("Proxy server listening on port 8000"); +proxyServer.listen(8000); + + +// +// Create your proxy server +// +var options2 = {target:'https://google.com', + headers: {'host':'google.com'}, + }; + +var proxyServer2 = caronte.createProxyServer(options2); + +proxyServer2.ee.on('*:error', function(err, req, res){ + res.end('There was an error proxying your request'); +}); + +console.log("Proxy server 2 listening on port 8001"); +proxyServer2.listen(8001); + +// +// Create your proxy server +// +var options3 = {target:'https://google.com', + xfwd:true}; + +var proxyServer3 = caronte.createProxyServer(options3); + +proxyServer3.ee.on('*:error', function(err, req, res){ + res.end('There was an error proxying your request'); +}); + +console.log("Proxy server 3 listening on port 8002"); +proxyServer3.listen(8002); + + + + diff --git a/examples/stand-alone.js b/examples/stand-alone.js index d3848ab..081134e 100644 --- a/examples/stand-alone.js +++ b/examples/stand-alone.js @@ -3,11 +3,13 @@ var http = require('http'), // // Create your proxy server // +console.log("Proxy server listening on port 8000"); caronte.createProxyServer({target:'http://localhost:9000'}).listen(8000); // // Create your target server // +console.log("Web server listening on port 9000"); http.createServer(function (req, res) { res.writeHead(200, { 'Content-Type': 'text/plain' }); res.write('request successfully proxied!' + '\n' + JSON.stringify(req.headers, true, 2)); diff --git a/lib/caronte.js b/lib/caronte.js index 78d7bd4..ed0280c 100644 --- a/lib/caronte.js +++ b/lib/caronte.js @@ -28,6 +28,7 @@ proxy.createProxyServer = function createProxyServer(options) { " { ", " target : ", " forward: ", + " agent : ", " ssl : ", " ws : ", " xfwd : ", @@ -41,14 +42,6 @@ proxy.createProxyServer = function createProxyServer(options) { ].join("\n")); } - ['target', 'forward'].forEach(function(key) { - if(!options[key]) return; - options[key] = url.parse(options[key]); - - options[key].maxSockets = options.maxSock; - options[key].agent = options.agent || false // new (options.ssl ? https.Agent : http.Agent)(options[key].maxSockets || 100); - }); - options.ee = new events.EventEmitter2({ wildcard: true, delimiter: ':' }); return { diff --git a/lib/caronte/common.js b/lib/caronte/common.js index 6b75c6f..47ada1b 100644 --- a/lib/caronte/common.js +++ b/lib/caronte/common.js @@ -1,4 +1,8 @@ -var common = exports; +var common = exports + , http = require('http') + , https = require('https') + , extend = require('util')._extend + ; /** * Copies the right headers from `options` and `req` to @@ -32,6 +36,19 @@ common.setupOutgoing = function(outgoing, options, req, forward) { function(e) { outgoing[e] = req[e]; } ); + if (options.headers){ + extend(outgoing.headers, options.headers); + } + + if (options.agent){ + outgoing.agent = options.agent; + } + + if (!outgoing.agent){ + var Agent = (~['https:', 'wss:'].indexOf(options[forward || 'target'].protocol) ? https.Agent : http.Agent); + outgoing.agent = new Agent(options.maxSock || 100); + } + outgoing.path = req.url; return outgoing;