This commit is contained in:
yawnt 2013-09-16 22:02:14 +02:00
commit 38e6d7cd54
5 changed files with 91 additions and 2 deletions

View File

@ -45,6 +45,43 @@ You can easily add a `pass` (stages) into both the pipelines (XXX: ADD API).
In addition, every stage emits a corresponding event so introspection during the process is always available.
#### Setup a basic stand-alone proxy server
var http = require('http'),
caronte = require('caronte');
//
// Create your proxy server
//
caronte.createProxyServer({target:'http://localhost:9000'}).listen(8000);
//
// Create your target server
//
http.createServer(function (req, res) {
res.writeHead(200, { 'Content-Type': 'text/plain' });
res.write('request successfully proxied!' + '\n' + JSON.stringify(req.headers, true, 2));
res.end();
}).listen(9000);
#### Setup a stand-alone proxy server with custom server logic
``` js
var http = require('http'),
caronte = require('caronte');
//
// Create a proxy server with custom application logic
//
var proxy = caronte.createProxyServer({});
var server = require('http').createServer(function(req, res) {
proxy.web(req, res, { target: 'http://127.0.0.1:5060' });
});
console.log("listening on port 5050")
server.listen(5050);
```
### Contributing and Issues
* Search on Google/Github
@ -53,6 +90,17 @@ In addition, every stage emits a corresponding event so introspection during the
* Commit to your local branch (which must be different from `master`)
* Submit your Pull Request (be sure to include tests and update documentation)
### Options
`caronte.createProxyServer` supports the following options:
* **target**: url string to be parsed with the url module
* **forward**: url string to be parsed with the url module
* **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
```

15
examples/stand-alone.js Normal file
View File

@ -0,0 +1,15 @@
var http = require('http'),
caronte = require('caronte');
//
// Create your proxy server
//
caronte.createProxyServer({target:'http://localhost:9000'}).listen(8000);
//
// Create your target server
//
http.createServer(function (req, res) {
res.writeHead(200, { 'Content-Type': 'text/plain' });
res.write('request successfully proxied!' + '\n' + JSON.stringify(req.headers, true, 2));
res.end();
}).listen(9000);

View File

@ -1,4 +1,6 @@
var caronte = exports,
extend = require('util')._extend,
parse_url = require('url').parse,
web = require('./passes/web-incoming'),
ws = require('./passes/ws-incoming');
@ -41,7 +43,11 @@ function createRightProxy(type) {
!(args[cntr] instanceof Buffer) &&
args[cntr] !== res
) {
options = args[cntr];
//Copy global options
options = extend({}, options);
//Overwrite with request options
extend(options, args[cntr]);
cntr--;
}
@ -51,7 +57,12 @@ function createRightProxy(type) {
options.ee.emit(ev + 'begin', req, res);
['target', 'forward'].forEach(
function(e) {
if (typeof options[e] === 'string')
options[e] = parse_url(options[e]);
});
passes.some(function(pass) {
var evnt = ev + pass.name.toLowerCase() + ':';

View File

@ -102,6 +102,14 @@ function stream(req, res, options) {
common.setupOutgoing(options.ssl || {}, options, req)
);
proxyReq.on('error', function(err){
var ev = 'caronte:outgoing:web:';
if (options.ee.listeners(ev + 'error').length == 0){
throw err;
}
options.ee.emit(ev + 'error', err, req, res);
});
req.pipe(proxyReq);
proxyReq.on('response', function(proxyRes) {

View File

@ -78,6 +78,13 @@ function stream(req, socket, options, head) {
var proxyReq = (~['https:', 'wss:'].indexOf(options.target.protocol) ? https : http).request(
common.setupOutgoing(options.ssl || {}, options, req)
);
proxyReq.on('error', function(err){
var ev = 'caronte:outgoing:ws:';
if (options.ee.listeners(ev + 'error').length == 0){
throw err;
}
options.ee.emit(ev + 'error', err, req, res);
});
proxyReq.on('upgrade', function(proxyRes, proxySocket, proxyHead) {
common.setupSocket(proxySocket);