From 13741a823f1c1c884d4a37e597e4b188598b0e25 Mon Sep 17 00:00:00 2001 From: srossross Date: Tue, 17 Sep 2013 11:03:23 -0700 Subject: [PATCH 1/4] ENH: updated https and agent option Removed logic from createProxyServer and put it into setupOutgoing. Conflicts: lib/caronte.js --- lib/caronte.js | 9 +-------- lib/caronte/common.js | 14 +++++++++++++- 2 files changed, 14 insertions(+), 9 deletions(-) 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..a272858 100644 --- a/lib/caronte/common.js +++ b/lib/caronte/common.js @@ -1,4 +1,7 @@ -var common = exports; +var common = exports + , http = require('http') + , https = require('https') + ; /** * Copies the right headers from `options` and `req` to @@ -32,6 +35,15 @@ common.setupOutgoing = function(outgoing, options, req, forward) { function(e) { outgoing[e] = req[e]; } ); + 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; From 427d8d85369b0cd1d38afa0dd0f28ac98fa16001 Mon Sep 17 00:00:00 2001 From: srossross Date: Tue, 17 Sep 2013 11:44:09 -0700 Subject: [PATCH 2/4] ENH: added new https example, needs to be simplified before merge updated existing example with log output --- examples/https.js | 54 +++++++++++++++++++++++++++++++++++++++++ examples/stand-alone.js | 2 ++ 2 files changed, 56 insertions(+) create mode 100644 examples/https.js diff --git a/examples/https.js b/examples/https.js new file mode 100644 index 0000000..cab2206 --- /dev/null +++ b/examples/https.js @@ -0,0 +1,54 @@ +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'}; + +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)); From 7d840d35151be1aac612798754af47368594781d Mon Sep 17 00:00:00 2001 From: srossross Date: Tue, 17 Sep 2013 11:45:41 -0700 Subject: [PATCH 3/4] ENH: added 'headers' to available options, to add or overwrite existing headers --- lib/caronte/common.js | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/lib/caronte/common.js b/lib/caronte/common.js index a272858..47ada1b 100644 --- a/lib/caronte/common.js +++ b/lib/caronte/common.js @@ -1,6 +1,7 @@ -var common = exports +var common = exports , http = require('http') , https = require('https') + , extend = require('util')._extend ; /** @@ -35,6 +36,10 @@ 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; } From 1c7ace26c5a36fb63497f1ab67793c5b75495063 Mon Sep 17 00:00:00 2001 From: srossross Date: Tue, 17 Sep 2013 11:50:04 -0700 Subject: [PATCH 4/4] ENH: updated example --- examples/https.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/examples/https.js b/examples/https.js index cab2206..8e3410d 100644 --- a/examples/https.js +++ b/examples/https.js @@ -38,7 +38,8 @@ proxyServer2.listen(8001); // // Create your proxy server // -var options3 = {target:'https://google.com'}; +var options3 = {target:'https://google.com', + xfwd:true}; var proxyServer3 = caronte.createProxyServer(options3);