From 13741a823f1c1c884d4a37e597e4b188598b0e25 Mon Sep 17 00:00:00 2001 From: srossross Date: Tue, 17 Sep 2013 11:03:23 -0700 Subject: [PATCH 01/10] 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 02/10] 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 03/10] 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 04/10] 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); From f566a42e511f4a6a8f3620f64e05df209e61b64f Mon Sep 17 00:00:00 2001 From: srossross Date: Tue, 17 Sep 2013 14:51:56 -0700 Subject: [PATCH 05/10] ENH: updated examples --- examples/https-secure.js | 15 +++++++++++ examples/https.js | 55 ++++------------------------------------ 2 files changed, 20 insertions(+), 50 deletions(-) create mode 100644 examples/https-secure.js diff --git a/examples/https-secure.js b/examples/https-secure.js new file mode 100644 index 0000000..b6d7bb7 --- /dev/null +++ b/examples/https-secure.js @@ -0,0 +1,15 @@ +var caronte = require('caronte'), + https = require('https'); +/* + * Create your proxy server pointing to a secure domain + * Enable ssl validation + */ +var options = {target : 'https://google.com', + agent : https.globalAgent, + headers: {host: 'google.com'} + }; + +var proxyServer = caronte.createProxyServer(options); +console.log("Proxy server listening on port 8000"); +proxyServer.listen(8000); + diff --git a/examples/https.js b/examples/https.js index 8e3410d..b64e3cf 100644 --- a/examples/https.js +++ b/examples/https.js @@ -1,55 +1,10 @@ -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 caronte = require('caronte'); +/* + * Create your proxy server pointing to a secure domain + */ +var options = {target:'https://google.com'}; 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); - - - - From 12cda561afe534427a5f84da9d7e0beb64a8ecbc Mon Sep 17 00:00:00 2001 From: srossross Date: Tue, 17 Sep 2013 14:52:53 -0700 Subject: [PATCH 06/10] ENH: updated agent options in `common.setupOutgoing` --- lib/caronte.js | 4 +--- lib/caronte/common.js | 21 +++++---------------- 2 files changed, 6 insertions(+), 19 deletions(-) diff --git a/lib/caronte.js b/lib/caronte.js index ed0280c..337fac9 100644 --- a/lib/caronte.js +++ b/lib/caronte.js @@ -28,12 +28,10 @@ proxy.createProxyServer = function createProxyServer(options) { " { ", " target : ", " forward: ", - " agent : ", + " agent : ", " ssl : ", " ws : ", " xfwd : ", - " maxSock: ", - " agent : ", " } ", " ", "NOTE: `options.ws` and `options.ssl` are optional. ", diff --git a/lib/caronte/common.js b/lib/caronte/common.js index 47ada1b..e84f36c 100644 --- a/lib/caronte/common.js +++ b/lib/caronte/common.js @@ -1,8 +1,5 @@ -var common = exports - , http = require('http') - , https = require('https') - , extend = require('util')._extend - ; +var common = exports, + extend = require('util')._extend; /** * Copies the right headers from `options` and `req` to @@ -28,7 +25,7 @@ common.setupOutgoing = function(outgoing, options, req, forward) { outgoing.port = options[forward || 'target'].port || (~['https:', 'wss:'].indexOf(options[forward || 'target'].protocol) ? 443 : 80); - ['host', 'hostname', 'socketPath', 'agent'].forEach( + ['host', 'hostname', 'socketPath'].forEach( function(e) { outgoing[e] = options[forward || 'target'][e]; } ); @@ -39,16 +36,8 @@ common.setupOutgoing = function(outgoing, options, req, forward) { 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.agent = options.agent || false; outgoing.path = req.url; return outgoing; From 1b5fb1d8fc21421b8383919d93e4149b586b211b Mon Sep 17 00:00:00 2001 From: srossross Date: Tue, 17 Sep 2013 15:00:28 -0700 Subject: [PATCH 07/10] DOC: updated readme with options --- README.md | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index f58bbba..c429613 100644 --- a/README.md +++ b/README.md @@ -105,10 +105,14 @@ server.listen(5050); * **target**: url string to be parsed with the url module * **forward**: url string to be parsed with the url module + * **agent**: object to be passed to http(s).request (see Node's [https agent][0] and [http agent][1] agent objects) + +If you are using the `proxyServer.listen` method, the following options are also applicable: + * **ssl**: object to be passed to https.createServer() * **ws**: true/false, if you want to proxy websockets * **xfwd**: true/false, adds x-forward headers - * **maxSock**: maximum number of sockets + ### Test @@ -144,3 +148,5 @@ Logo created by [Diego Pasquali](http://dribbble.com/diegopq) >OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN >THE SOFTWARE. +[0]: http://nodejs.org/api/https.html#https_class_https_agent +[1]: http://nodejs.org/api/http.html#http_class_http_agent From a350fadea6bace293131581487f8c66948009449 Mon Sep 17 00:00:00 2001 From: srossross Date: Tue, 17 Sep 2013 15:06:22 -0700 Subject: [PATCH 08/10] FIX: tests. still need to add more tests tho --- test/lib-caronte-common-test.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/test/lib-caronte-common-test.js b/test/lib-caronte-common-test.js index 1523cff..88536e2 100644 --- a/test/lib-caronte-common-test.js +++ b/test/lib-caronte-common-test.js @@ -7,12 +7,12 @@ describe('lib/caronte/common.js', function () { var outgoing = {}; common.setupOutgoing(outgoing, { + agent : '?', target: { host : 'hey', hostname : 'how', socketPath: 'are', port : 'you', - agent : '?' } }, { @@ -35,12 +35,12 @@ describe('lib/caronte/common.js', function () { it('set the port according to the protocol', function () { var outgoing = {}; common.setupOutgoing(outgoing, - { + { + agent : '?', target: { host : 'how', hostname : 'are', socketPath: 'you', - agent : '?', protocol: 'https:' } }, From 39b0c46a6967fda5329760ad93a8ec01bc4a6f14 Mon Sep 17 00:00:00 2001 From: srossross Date: Tue, 17 Sep 2013 15:29:48 -0700 Subject: [PATCH 09/10] TEST: added agent and header tests --- test/lib-caronte-common-test.js | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/test/lib-caronte-common-test.js b/test/lib-caronte-common-test.js index 88536e2..ac79867 100644 --- a/test/lib-caronte-common-test.js +++ b/test/lib-caronte-common-test.js @@ -13,12 +13,13 @@ describe('lib/caronte/common.js', function () { hostname : 'how', socketPath: 'are', port : 'you', - } + }, + headers: {'fizz': 'bang', 'overwritten':true}, }, { method : 'i', url : 'am', - headers : 'proxy' + headers : {'pro':'xy','overwritten':false} }); expect(outgoing.host).to.eql('hey'); @@ -29,7 +30,16 @@ describe('lib/caronte/common.js', function () { expect(outgoing.method).to.eql('i'); expect(outgoing.path).to.eql('am'); - expect(outgoing.headers).to.eql('proxy') + + expect(outgoing.headers.pro).to.eql('xy'); + expect(outgoing.headers.fizz).to.eql('bang'); + expect(outgoing.headers.overwritten).to.eql(true); + }); + + it('should set the agent to false if none is given', function () { + var outgoing = {}; + common.setupOutgoing(outgoing, {target: {},}, {}); + expect(outgoing.agent).to.eql(false); }); it('set the port according to the protocol', function () { From 7ad5c0f993294c9e2e7650e15fbc62d11a2cb062 Mon Sep 17 00:00:00 2001 From: srossross Date: Wed, 18 Sep 2013 09:07:56 -0700 Subject: [PATCH 10/10] DOC: updated readme @yawnt I think it is good to go. If you have any other tests in mind let me know. --- README.md | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index c429613..b53985a 100644 --- a/README.md +++ b/README.md @@ -105,7 +105,7 @@ server.listen(5050); * **target**: url string to be parsed with the url module * **forward**: url string to be parsed with the url module - * **agent**: object to be passed to http(s).request (see Node's [https agent][0] and [http agent][1] agent objects) + * **agent**: object to be passed to http(s).request (see Node's [https agent](http://nodejs.org/api/https.html#https_class_https_agent) and [http agent](http://nodejs.org/api/http.html#http_class_http_agent) objects) If you are using the `proxyServer.listen` method, the following options are also applicable: @@ -148,5 +148,4 @@ Logo created by [Diego Pasquali](http://dribbble.com/diegopq) >OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN >THE SOFTWARE. -[0]: http://nodejs.org/api/https.html#https_class_https_agent -[1]: http://nodejs.org/api/http.html#http_class_http_agent +