Skip to content

Commit 38e6d7c

Browse files
committed
[merge] PR #470
2 parents 469b0d4 + bd106d6 commit 38e6d7c

File tree

5 files changed

+91
-2
lines changed

5 files changed

+91
-2
lines changed

README.md

+48
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,43 @@ You can easily add a `pass` (stages) into both the pipelines (XXX: ADD API).
4545

4646
In addition, every stage emits a corresponding event so introspection during the process is always available.
4747

48+
#### Setup a basic stand-alone proxy server
49+
50+
var http = require('http'),
51+
caronte = require('caronte');
52+
//
53+
// Create your proxy server
54+
//
55+
caronte.createProxyServer({target:'http://localhost:9000'}).listen(8000);
56+
57+
//
58+
// Create your target server
59+
//
60+
http.createServer(function (req, res) {
61+
res.writeHead(200, { 'Content-Type': 'text/plain' });
62+
res.write('request successfully proxied!' + '\n' + JSON.stringify(req.headers, true, 2));
63+
res.end();
64+
}).listen(9000);
65+
66+
#### Setup a stand-alone proxy server with custom server logic
67+
68+
``` js
69+
var http = require('http'),
70+
caronte = require('caronte');
71+
72+
//
73+
// Create a proxy server with custom application logic
74+
//
75+
var proxy = caronte.createProxyServer({});
76+
77+
var server = require('http').createServer(function(req, res) {
78+
proxy.web(req, res, { target: 'http://127.0.0.1:5060' });
79+
});
80+
81+
console.log("listening on port 5050")
82+
server.listen(5050);
83+
```
84+
4885
### Contributing and Issues
4986

5087
* Search on Google/Github
@@ -53,6 +90,17 @@ In addition, every stage emits a corresponding event so introspection during the
5390
* Commit to your local branch (which must be different from `master`)
5491
* Submit your Pull Request (be sure to include tests and update documentation)
5592

93+
### Options
94+
95+
`caronte.createProxyServer` supports the following options:
96+
97+
* **target**: url string to be parsed with the url module
98+
* **forward**: url string to be parsed with the url module
99+
* **ssl**: object to be passed to https.createServer()
100+
* **ws**: true/false, if you want to proxy websockets
101+
* **xfwd**: true/false, adds x-forward headers
102+
* **maxSock**: maximum number of sockets
103+
56104
### Test
57105

58106
```

examples/stand-alone.js

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
var http = require('http'),
2+
caronte = require('caronte');
3+
//
4+
// Create your proxy server
5+
//
6+
caronte.createProxyServer({target:'http://localhost:9000'}).listen(8000);
7+
8+
//
9+
// Create your target server
10+
//
11+
http.createServer(function (req, res) {
12+
res.writeHead(200, { 'Content-Type': 'text/plain' });
13+
res.write('request successfully proxied!' + '\n' + JSON.stringify(req.headers, true, 2));
14+
res.end();
15+
}).listen(9000);

lib/caronte/index.js

+13-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
var caronte = exports,
2+
extend = require('util')._extend,
3+
parse_url = require('url').parse,
24
web = require('./passes/web-incoming'),
35
ws = require('./passes/ws-incoming');
46

@@ -41,7 +43,11 @@ function createRightProxy(type) {
4143
!(args[cntr] instanceof Buffer) &&
4244
args[cntr] !== res
4345
) {
44-
options = args[cntr];
46+
//Copy global options
47+
options = extend({}, options);
48+
//Overwrite with request options
49+
extend(options, args[cntr]);
50+
4551
cntr--;
4652
}
4753

@@ -51,7 +57,12 @@ function createRightProxy(type) {
5157

5258
options.ee.emit(ev + 'begin', req, res);
5359

54-
60+
['target', 'forward'].forEach(
61+
function(e) {
62+
if (typeof options[e] === 'string')
63+
options[e] = parse_url(options[e]);
64+
});
65+
5566
passes.some(function(pass) {
5667
var evnt = ev + pass.name.toLowerCase() + ':';
5768

lib/caronte/passes/web-incoming.js

+8
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,14 @@ function stream(req, res, options) {
102102
common.setupOutgoing(options.ssl || {}, options, req)
103103
);
104104

105+
proxyReq.on('error', function(err){
106+
var ev = 'caronte:outgoing:web:';
107+
if (options.ee.listeners(ev + 'error').length == 0){
108+
throw err;
109+
}
110+
options.ee.emit(ev + 'error', err, req, res);
111+
});
112+
105113
req.pipe(proxyReq);
106114

107115
proxyReq.on('response', function(proxyRes) {

lib/caronte/passes/ws-incoming.js

+7
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,13 @@ function stream(req, socket, options, head) {
7878
var proxyReq = (~['https:', 'wss:'].indexOf(options.target.protocol) ? https : http).request(
7979
common.setupOutgoing(options.ssl || {}, options, req)
8080
);
81+
proxyReq.on('error', function(err){
82+
var ev = 'caronte:outgoing:ws:';
83+
if (options.ee.listeners(ev + 'error').length == 0){
84+
throw err;
85+
}
86+
options.ee.emit(ev + 'error', err, req, res);
87+
});
8188

8289
proxyReq.on('upgrade', function(proxyRes, proxySocket, proxyHead) {
8390
common.setupSocket(proxySocket);

0 commit comments

Comments
 (0)