forked from sequoiar/bouncy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
111 lines (92 loc) · 3.03 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
var http = require('http');
var ServerResponse = http.ServerResponse;
var parsley = require('parsley');
var BufferedStream = require('morestreams').BufferedStream;
var insertHeaders = require('./lib/insert_headers');
var parseArgs = require('./lib/parse_args');
var net = require('net');
var tls = require('tls');
var bouncy = module.exports = function (opts, cb) {
if (typeof opts === 'function') {
cb = opts;
opts = {};
}
if (opts && opts.key && opts.cert) {
return tls.createServer(opts, handler.bind(null, cb));
}
else {
return net.createServer(handler.bind(null, cb));
}
};
var handler = bouncy.handler = function (cb, c) {
parsley(c, function (req) {
c.setMaxListeners(0);
var stream = new BufferedStream;
stream.pause();
function onData (buf) {
stream.write(buf);
}
req.socket.on('close', function() {
stream.end();
});
req.on('rawHead', onData);
req.on('rawBody', onData);
req.on('rawEnd', function () {
req.removeListener('rawHead', onData);
req.removeListener('rawBody', onData);
});
function onHeaders () {
req.removeListener('error', onError);
var bounce = makeBounce(stream, c, req);
cb(req, bounce);
}
req.on('headers', onHeaders);
function onError (err) {
req.removeListener('headers', onHeaders);
var bounce = makeBounce(stream, c, req);
cb(req, bounce);
req.emit('error', err);
}
req.once('error', onError);
});
};
function makeBounce (bs, client, req) {
var bounce = function (stream, opts) {
if (!stream || !stream.write) {
opts = parseArgs(arguments);
stream = opts.stream;
}
if (!opts) opts = {};
if (!opts.headers) opts.headers = {};
if (!('x-forwarded-for' in opts.headers)) {
opts.headers['x-forwarded-for'] = client.remoteAddress;
}
if (!('x-forwarded-port' in opts.headers)) {
var m = (req.headers.host || '').match(/:(\d+)/);
opts.headers['x-forwarded-port'] = m && m[1] || 80;
}
if (!('x-forwarded-proto' in opts.headers)) {
opts.headers['x-forwarded-proto'] =
client.encrypted ? 'https' : 'http';
}
insertHeaders(bs.chunks, opts.headers);
if (stream.writable && client.writable) {
bs.pipe(stream);
stream.pipe(client);
}
else {
opts.emitter.emit('drop', client);
}
return stream;
};
bounce.respond = function () {
var res = new ServerResponse(req);
res.assignSocket(client);
res.on('finish', function () {
res.detachSocket(client);
client.destroySoon();
});
return res;
};
return bounce;
}