diff --git a/README.md b/README.md index 5ef7f38..bc64e42 100644 --- a/README.md +++ b/README.md @@ -107,10 +107,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](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: + * **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 @@ -146,3 +150,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. + 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 new file mode 100644 index 0000000..b64e3cf --- /dev/null +++ b/examples/https.js @@ -0,0 +1,10 @@ +var caronte = require('caronte'); +/* + * Create your proxy server pointing to a secure domain + */ +var options = {target:'https://google.com'}; + +var proxyServer = caronte.createProxyServer(options); +console.log("Proxy server listening on port 8000"); +proxyServer.listen(8000); + 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..337fac9 100644 --- a/lib/caronte.js +++ b/lib/caronte.js @@ -28,11 +28,10 @@ proxy.createProxyServer = function createProxyServer(options) { " { ", " target : ", " forward: ", + " agent : ", " ssl : ", " ws : ", " xfwd : ", - " maxSock: ", - " agent : ", " } ", " ", "NOTE: `options.ws` and `options.ssl` are optional. ", @@ -41,14 +40,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 2b9d436..88a20c1 100644 --- a/lib/caronte/common.js +++ b/lib/caronte/common.js @@ -1,4 +1,5 @@ -var common = exports; +var common = exports, + extend = require('util')._extend; /** * Copies the right headers from `options` and `req` to @@ -24,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]; } ); @@ -32,6 +33,11 @@ common.setupOutgoing = function(outgoing, options, req, forward) { function(e) { outgoing[e] = req[e]; } ); + if (options.headers){ + extend(outgoing.headers, options.headers); + } + + outgoing.agent = options.agent || false; outgoing.path = req.url; return outgoing; diff --git a/test/lib-caronte-common-test.js b/test/lib-caronte-common-test.js index 1523cff..ac79867 100644 --- a/test/lib-caronte-common-test.js +++ b/test/lib-caronte-common-test.js @@ -7,18 +7,19 @@ describe('lib/caronte/common.js', function () { var outgoing = {}; common.setupOutgoing(outgoing, { + agent : '?', target: { host : 'hey', hostname : 'how', socketPath: 'are', port : 'you', - agent : '?' - } + }, + headers: {'fizz': 'bang', 'overwritten':true}, }, { method : 'i', url : 'am', - headers : 'proxy' + headers : {'pro':'xy','overwritten':false} }); expect(outgoing.host).to.eql('hey'); @@ -29,18 +30,27 @@ 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 () { var outgoing = {}; common.setupOutgoing(outgoing, - { + { + agent : '?', target: { host : 'how', hostname : 'are', socketPath: 'you', - agent : '?', protocol: 'https:' } },