Skip to content

Commit 4ee96dd

Browse files
committed
Merge branch 'agent' into caronte
2 parents 69f126b + 1c7ace2 commit 4ee96dd

File tree

4 files changed

+76
-9
lines changed

4 files changed

+76
-9
lines changed

examples/https.js

+55
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
var http = require('http')
2+
, https = require('https')
3+
, caronte = require('caronte')
4+
;
5+
//
6+
// Create your proxy server
7+
//
8+
var options = {target:'https://google.com',
9+
agent: new https.Agent({rejectUnauthorized:false}),
10+
};
11+
12+
var proxyServer = caronte.createProxyServer(options);
13+
14+
proxyServer.ee.on('*:error', function(err, req, res){
15+
res.end('There was an error proxying your request');
16+
});
17+
18+
console.log("Proxy server listening on port 8000");
19+
proxyServer.listen(8000);
20+
21+
22+
//
23+
// Create your proxy server
24+
//
25+
var options2 = {target:'https://google.com',
26+
headers: {'host':'google.com'},
27+
};
28+
29+
var proxyServer2 = caronte.createProxyServer(options2);
30+
31+
proxyServer2.ee.on('*:error', function(err, req, res){
32+
res.end('There was an error proxying your request');
33+
});
34+
35+
console.log("Proxy server 2 listening on port 8001");
36+
proxyServer2.listen(8001);
37+
38+
//
39+
// Create your proxy server
40+
//
41+
var options3 = {target:'https://google.com',
42+
xfwd:true};
43+
44+
var proxyServer3 = caronte.createProxyServer(options3);
45+
46+
proxyServer3.ee.on('*:error', function(err, req, res){
47+
res.end('There was an error proxying your request');
48+
});
49+
50+
console.log("Proxy server 3 listening on port 8002");
51+
proxyServer3.listen(8002);
52+
53+
54+
55+

examples/stand-alone.js

+2
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,13 @@ var http = require('http'),
33
//
44
// Create your proxy server
55
//
6+
console.log("Proxy server listening on port 8000");
67
caronte.createProxyServer({target:'http://localhost:9000'}).listen(8000);
78

89
//
910
// Create your target server
1011
//
12+
console.log("Web server listening on port 9000");
1113
http.createServer(function (req, res) {
1214
res.writeHead(200, { 'Content-Type': 'text/plain' });
1315
res.write('request successfully proxied!' + '\n' + JSON.stringify(req.headers, true, 2));

lib/caronte.js

+1-8
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ proxy.createProxyServer = function createProxyServer(options) {
2828
" { ",
2929
" target : <url string to be parsed with the url module> ",
3030
" forward: <url string to be parsed with the url module> ",
31+
" agent : <object to be passed to http(s).request(...)> ",
3132
" ssl : <object to be passed to https.createServer()> ",
3233
" ws : <true/false, if you want to proxy websockets> ",
3334
" xfwd : <true/false, adds x-forward headers> ",
@@ -41,14 +42,6 @@ proxy.createProxyServer = function createProxyServer(options) {
4142
].join("\n"));
4243
}
4344

44-
['target', 'forward'].forEach(function(key) {
45-
if(!options[key]) return;
46-
options[key] = url.parse(options[key]);
47-
48-
options[key].maxSockets = options.maxSock;
49-
options[key].agent = options.agent || false // new (options.ssl ? https.Agent : http.Agent)(options[key].maxSockets || 100);
50-
});
51-
5245
options.ee = new events.EventEmitter2({ wildcard: true, delimiter: ':' });
5346

5447
return {

lib/caronte/common.js

+18-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
1-
var common = exports;
1+
var common = exports
2+
, http = require('http')
3+
, https = require('https')
4+
, extend = require('util')._extend
5+
;
26

37
/**
48
* Copies the right headers from `options` and `req` to
@@ -32,6 +36,19 @@ common.setupOutgoing = function(outgoing, options, req, forward) {
3236
function(e) { outgoing[e] = req[e]; }
3337
);
3438

39+
if (options.headers){
40+
extend(outgoing.headers, options.headers);
41+
}
42+
43+
if (options.agent){
44+
outgoing.agent = options.agent;
45+
}
46+
47+
if (!outgoing.agent){
48+
var Agent = (~['https:', 'wss:'].indexOf(options[forward || 'target'].protocol) ? https.Agent : http.Agent);
49+
outgoing.agent = new Agent(options.maxSock || 100);
50+
}
51+
3552
outgoing.path = req.url;
3653

3754
return outgoing;

0 commit comments

Comments
 (0)