1 | var httpProxy = module.exports,
|
---|
2 | extend = require('util')._extend,
|
---|
3 | parse_url = require('url').parse,
|
---|
4 | EE3 = require('eventemitter3'),
|
---|
5 | http = require('http'),
|
---|
6 | https = require('https'),
|
---|
7 | web = require('./passes/web-incoming'),
|
---|
8 | ws = require('./passes/ws-incoming');
|
---|
9 |
|
---|
10 | httpProxy.Server = ProxyServer;
|
---|
11 |
|
---|
12 | /**
|
---|
13 | * Returns a function that creates the loader for
|
---|
14 | * either `ws` or `web`'s passes.
|
---|
15 | *
|
---|
16 | * Examples:
|
---|
17 | *
|
---|
18 | * httpProxy.createRightProxy('ws')
|
---|
19 | * // => [Function]
|
---|
20 | *
|
---|
21 | * @param {String} Type Either 'ws' or 'web'
|
---|
22 | *
|
---|
23 | * @return {Function} Loader Function that when called returns an iterator for the right passes
|
---|
24 | *
|
---|
25 | * @api private
|
---|
26 | */
|
---|
27 |
|
---|
28 | function createRightProxy(type) {
|
---|
29 |
|
---|
30 | return function(options) {
|
---|
31 | return function(req, res /*, [head], [opts] */) {
|
---|
32 | var passes = (type === 'ws') ? this.wsPasses : this.webPasses,
|
---|
33 | args = [].slice.call(arguments),
|
---|
34 | cntr = args.length - 1,
|
---|
35 | head, cbl;
|
---|
36 |
|
---|
37 | /* optional args parse begin */
|
---|
38 | if(typeof args[cntr] === 'function') {
|
---|
39 | cbl = args[cntr];
|
---|
40 |
|
---|
41 | cntr--;
|
---|
42 | }
|
---|
43 |
|
---|
44 | var requestOptions = options;
|
---|
45 | if(
|
---|
46 | !(args[cntr] instanceof Buffer) &&
|
---|
47 | args[cntr] !== res
|
---|
48 | ) {
|
---|
49 | //Copy global options
|
---|
50 | requestOptions = extend({}, options);
|
---|
51 | //Overwrite with request options
|
---|
52 | extend(requestOptions, args[cntr]);
|
---|
53 |
|
---|
54 | cntr--;
|
---|
55 | }
|
---|
56 |
|
---|
57 | if(args[cntr] instanceof Buffer) {
|
---|
58 | head = args[cntr];
|
---|
59 | }
|
---|
60 |
|
---|
61 | /* optional args parse end */
|
---|
62 |
|
---|
63 | ['target', 'forward'].forEach(function(e) {
|
---|
64 | if (typeof requestOptions[e] === 'string')
|
---|
65 | requestOptions[e] = parse_url(requestOptions[e]);
|
---|
66 | });
|
---|
67 |
|
---|
68 | if (!requestOptions.target && !requestOptions.forward) {
|
---|
69 | return this.emit('error', new Error('Must provide a proper URL as target'));
|
---|
70 | }
|
---|
71 |
|
---|
72 | for(var i=0; i < passes.length; i++) {
|
---|
73 | /**
|
---|
74 | * Call of passes functions
|
---|
75 | * pass(req, res, options, head)
|
---|
76 | *
|
---|
77 | * In WebSockets case the `res` variable
|
---|
78 | * refer to the connection socket
|
---|
79 | * pass(req, socket, options, head)
|
---|
80 | */
|
---|
81 | if(passes[i](req, res, requestOptions, head, this, cbl)) { // passes can return a truthy value to halt the loop
|
---|
82 | break;
|
---|
83 | }
|
---|
84 | }
|
---|
85 | };
|
---|
86 | };
|
---|
87 | }
|
---|
88 | httpProxy.createRightProxy = createRightProxy;
|
---|
89 |
|
---|
90 | function ProxyServer(options) {
|
---|
91 | EE3.call(this);
|
---|
92 |
|
---|
93 | options = options || {};
|
---|
94 | options.prependPath = options.prependPath === false ? false : true;
|
---|
95 |
|
---|
96 | this.web = this.proxyRequest = createRightProxy('web')(options);
|
---|
97 | this.ws = this.proxyWebsocketRequest = createRightProxy('ws')(options);
|
---|
98 | this.options = options;
|
---|
99 |
|
---|
100 | this.webPasses = Object.keys(web).map(function(pass) {
|
---|
101 | return web[pass];
|
---|
102 | });
|
---|
103 |
|
---|
104 | this.wsPasses = Object.keys(ws).map(function(pass) {
|
---|
105 | return ws[pass];
|
---|
106 | });
|
---|
107 |
|
---|
108 | this.on('error', this.onError, this);
|
---|
109 |
|
---|
110 | }
|
---|
111 |
|
---|
112 | require('util').inherits(ProxyServer, EE3);
|
---|
113 |
|
---|
114 | ProxyServer.prototype.onError = function (err) {
|
---|
115 | //
|
---|
116 | // Remark: Replicate node core behavior using EE3
|
---|
117 | // so we force people to handle their own errors
|
---|
118 | //
|
---|
119 | if(this.listeners('error').length === 1) {
|
---|
120 | throw err;
|
---|
121 | }
|
---|
122 | };
|
---|
123 |
|
---|
124 | ProxyServer.prototype.listen = function(port, hostname) {
|
---|
125 | var self = this,
|
---|
126 | closure = function(req, res) { self.web(req, res); };
|
---|
127 |
|
---|
128 | this._server = this.options.ssl ?
|
---|
129 | https.createServer(this.options.ssl, closure) :
|
---|
130 | http.createServer(closure);
|
---|
131 |
|
---|
132 | if(this.options.ws) {
|
---|
133 | this._server.on('upgrade', function(req, socket, head) { self.ws(req, socket, head); });
|
---|
134 | }
|
---|
135 |
|
---|
136 | this._server.listen(port, hostname);
|
---|
137 |
|
---|
138 | return this;
|
---|
139 | };
|
---|
140 |
|
---|
141 | ProxyServer.prototype.close = function(callback) {
|
---|
142 | var self = this;
|
---|
143 | if (this._server) {
|
---|
144 | this._server.close(done);
|
---|
145 | }
|
---|
146 |
|
---|
147 | // Wrap callback to nullify server after all open connections are closed.
|
---|
148 | function done() {
|
---|
149 | self._server = null;
|
---|
150 | if (callback) {
|
---|
151 | callback.apply(null, arguments);
|
---|
152 | }
|
---|
153 | };
|
---|
154 | };
|
---|
155 |
|
---|
156 | ProxyServer.prototype.before = function(type, passName, callback) {
|
---|
157 | if (type !== 'ws' && type !== 'web') {
|
---|
158 | throw new Error('type must be `web` or `ws`');
|
---|
159 | }
|
---|
160 | var passes = (type === 'ws') ? this.wsPasses : this.webPasses,
|
---|
161 | i = false;
|
---|
162 |
|
---|
163 | passes.forEach(function(v, idx) {
|
---|
164 | if(v.name === passName) i = idx;
|
---|
165 | })
|
---|
166 |
|
---|
167 | if(i === false) throw new Error('No such pass');
|
---|
168 |
|
---|
169 | passes.splice(i, 0, callback);
|
---|
170 | };
|
---|
171 | ProxyServer.prototype.after = function(type, passName, callback) {
|
---|
172 | if (type !== 'ws' && type !== 'web') {
|
---|
173 | throw new Error('type must be `web` or `ws`');
|
---|
174 | }
|
---|
175 | var passes = (type === 'ws') ? this.wsPasses : this.webPasses,
|
---|
176 | i = false;
|
---|
177 |
|
---|
178 | passes.forEach(function(v, idx) {
|
---|
179 | if(v.name === passName) i = idx;
|
---|
180 | })
|
---|
181 |
|
---|
182 | if(i === false) throw new Error('No such pass');
|
---|
183 |
|
---|
184 | passes.splice(i++, 0, callback);
|
---|
185 | };
|
---|