isomorfeus-transport 1.0.0.zeta24 → 2.0.0.rc3
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/LICENSE +21 -21
- data/README.md +27 -36
- data/lib/isomorfeus/transport/client_processor.rb +35 -35
- data/lib/isomorfeus/transport/config.rb +182 -166
- data/lib/isomorfeus/transport/hamster_session_store.rb +96 -0
- data/lib/isomorfeus/transport/handler/authentication_handler.rb +70 -70
- data/lib/isomorfeus/transport/imports.rb +9 -0
- data/lib/isomorfeus/transport/middlewares.rb +13 -13
- data/lib/isomorfeus/transport/rack_middleware.rb +59 -55
- data/lib/isomorfeus/transport/request_agent.rb +34 -34
- data/lib/isomorfeus/transport/response_agent.rb +23 -23
- data/lib/isomorfeus/transport/server_processor.rb +129 -129
- data/lib/isomorfeus/transport/server_socket_processor.rb +54 -54
- data/lib/isomorfeus/transport/ssr_login.rb +28 -28
- data/lib/isomorfeus/transport/version.rb +5 -5
- data/lib/isomorfeus/transport/{websocket.rb → websocket_client.rb} +123 -123
- data/lib/isomorfeus/transport.rb +200 -196
- data/lib/isomorfeus-transport.rb +70 -61
- data/lib/lucid_authentication/mixin.rb +122 -122
- data/lib/lucid_channel/base.rb +8 -9
- data/lib/lucid_channel/mixin.rb +105 -105
- data/lib/lucid_handler/base.rb +8 -9
- data/lib/lucid_handler/mixin.rb +27 -27
- data/node_modules/.package-lock.json +27 -0
- data/node_modules/ws/LICENSE +19 -0
- data/node_modules/ws/README.md +496 -0
- data/node_modules/ws/browser.js +8 -0
- data/node_modules/ws/index.js +13 -0
- data/node_modules/ws/lib/buffer-util.js +126 -0
- data/node_modules/ws/lib/constants.js +12 -0
- data/node_modules/ws/lib/event-target.js +266 -0
- data/node_modules/ws/lib/extension.js +203 -0
- data/node_modules/ws/lib/limiter.js +55 -0
- data/node_modules/ws/lib/permessage-deflate.js +511 -0
- data/node_modules/ws/lib/receiver.js +612 -0
- data/node_modules/ws/lib/sender.js +414 -0
- data/node_modules/ws/lib/stream.js +180 -0
- data/node_modules/ws/lib/subprotocol.js +62 -0
- data/node_modules/ws/lib/validation.js +124 -0
- data/node_modules/ws/lib/websocket-server.js +485 -0
- data/node_modules/ws/lib/websocket.js +1144 -0
- data/node_modules/ws/package.json +61 -0
- data/node_modules/ws/wrapper.mjs +8 -0
- data/package.json +6 -0
- metadata +76 -54
- data/lib/isomorfeus/transport/dbm_session_store.rb +0 -51
@@ -0,0 +1,124 @@
|
|
1
|
+
'use strict';
|
2
|
+
|
3
|
+
//
|
4
|
+
// Allowed token characters:
|
5
|
+
//
|
6
|
+
// '!', '#', '$', '%', '&', ''', '*', '+', '-',
|
7
|
+
// '.', 0-9, A-Z, '^', '_', '`', a-z, '|', '~'
|
8
|
+
//
|
9
|
+
// tokenChars[32] === 0 // ' '
|
10
|
+
// tokenChars[33] === 1 // '!'
|
11
|
+
// tokenChars[34] === 0 // '"'
|
12
|
+
// ...
|
13
|
+
//
|
14
|
+
// prettier-ignore
|
15
|
+
const tokenChars = [
|
16
|
+
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 0 - 15
|
17
|
+
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 16 - 31
|
18
|
+
0, 1, 0, 1, 1, 1, 1, 1, 0, 0, 1, 1, 0, 1, 1, 0, // 32 - 47
|
19
|
+
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, // 48 - 63
|
20
|
+
0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // 64 - 79
|
21
|
+
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 1, 1, // 80 - 95
|
22
|
+
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // 96 - 111
|
23
|
+
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 0, 1, 0 // 112 - 127
|
24
|
+
];
|
25
|
+
|
26
|
+
/**
|
27
|
+
* Checks if a status code is allowed in a close frame.
|
28
|
+
*
|
29
|
+
* @param {Number} code The status code
|
30
|
+
* @return {Boolean} `true` if the status code is valid, else `false`
|
31
|
+
* @public
|
32
|
+
*/
|
33
|
+
function isValidStatusCode(code) {
|
34
|
+
return (
|
35
|
+
(code >= 1000 &&
|
36
|
+
code <= 1014 &&
|
37
|
+
code !== 1004 &&
|
38
|
+
code !== 1005 &&
|
39
|
+
code !== 1006) ||
|
40
|
+
(code >= 3000 && code <= 4999)
|
41
|
+
);
|
42
|
+
}
|
43
|
+
|
44
|
+
/**
|
45
|
+
* Checks if a given buffer contains only correct UTF-8.
|
46
|
+
* Ported from https://www.cl.cam.ac.uk/%7Emgk25/ucs/utf8_check.c by
|
47
|
+
* Markus Kuhn.
|
48
|
+
*
|
49
|
+
* @param {Buffer} buf The buffer to check
|
50
|
+
* @return {Boolean} `true` if `buf` contains only correct UTF-8, else `false`
|
51
|
+
* @public
|
52
|
+
*/
|
53
|
+
function _isValidUTF8(buf) {
|
54
|
+
const len = buf.length;
|
55
|
+
let i = 0;
|
56
|
+
|
57
|
+
while (i < len) {
|
58
|
+
if ((buf[i] & 0x80) === 0) {
|
59
|
+
// 0xxxxxxx
|
60
|
+
i++;
|
61
|
+
} else if ((buf[i] & 0xe0) === 0xc0) {
|
62
|
+
// 110xxxxx 10xxxxxx
|
63
|
+
if (
|
64
|
+
i + 1 === len ||
|
65
|
+
(buf[i + 1] & 0xc0) !== 0x80 ||
|
66
|
+
(buf[i] & 0xfe) === 0xc0 // Overlong
|
67
|
+
) {
|
68
|
+
return false;
|
69
|
+
}
|
70
|
+
|
71
|
+
i += 2;
|
72
|
+
} else if ((buf[i] & 0xf0) === 0xe0) {
|
73
|
+
// 1110xxxx 10xxxxxx 10xxxxxx
|
74
|
+
if (
|
75
|
+
i + 2 >= len ||
|
76
|
+
(buf[i + 1] & 0xc0) !== 0x80 ||
|
77
|
+
(buf[i + 2] & 0xc0) !== 0x80 ||
|
78
|
+
(buf[i] === 0xe0 && (buf[i + 1] & 0xe0) === 0x80) || // Overlong
|
79
|
+
(buf[i] === 0xed && (buf[i + 1] & 0xe0) === 0xa0) // Surrogate (U+D800 - U+DFFF)
|
80
|
+
) {
|
81
|
+
return false;
|
82
|
+
}
|
83
|
+
|
84
|
+
i += 3;
|
85
|
+
} else if ((buf[i] & 0xf8) === 0xf0) {
|
86
|
+
// 11110xxx 10xxxxxx 10xxxxxx 10xxxxxx
|
87
|
+
if (
|
88
|
+
i + 3 >= len ||
|
89
|
+
(buf[i + 1] & 0xc0) !== 0x80 ||
|
90
|
+
(buf[i + 2] & 0xc0) !== 0x80 ||
|
91
|
+
(buf[i + 3] & 0xc0) !== 0x80 ||
|
92
|
+
(buf[i] === 0xf0 && (buf[i + 1] & 0xf0) === 0x80) || // Overlong
|
93
|
+
(buf[i] === 0xf4 && buf[i + 1] > 0x8f) ||
|
94
|
+
buf[i] > 0xf4 // > U+10FFFF
|
95
|
+
) {
|
96
|
+
return false;
|
97
|
+
}
|
98
|
+
|
99
|
+
i += 4;
|
100
|
+
} else {
|
101
|
+
return false;
|
102
|
+
}
|
103
|
+
}
|
104
|
+
|
105
|
+
return true;
|
106
|
+
}
|
107
|
+
|
108
|
+
try {
|
109
|
+
const isValidUTF8 = require('utf-8-validate');
|
110
|
+
|
111
|
+
module.exports = {
|
112
|
+
isValidStatusCode,
|
113
|
+
isValidUTF8(buf) {
|
114
|
+
return buf.length < 150 ? _isValidUTF8(buf) : isValidUTF8(buf);
|
115
|
+
},
|
116
|
+
tokenChars
|
117
|
+
};
|
118
|
+
} catch (e) /* istanbul ignore next */ {
|
119
|
+
module.exports = {
|
120
|
+
isValidStatusCode,
|
121
|
+
isValidUTF8: _isValidUTF8,
|
122
|
+
tokenChars
|
123
|
+
};
|
124
|
+
}
|
@@ -0,0 +1,485 @@
|
|
1
|
+
/* eslint no-unused-vars: ["error", { "varsIgnorePattern": "^net|tls|https$" }] */
|
2
|
+
|
3
|
+
'use strict';
|
4
|
+
|
5
|
+
const EventEmitter = require('events');
|
6
|
+
const http = require('http');
|
7
|
+
const https = require('https');
|
8
|
+
const net = require('net');
|
9
|
+
const tls = require('tls');
|
10
|
+
const { createHash } = require('crypto');
|
11
|
+
|
12
|
+
const extension = require('./extension');
|
13
|
+
const PerMessageDeflate = require('./permessage-deflate');
|
14
|
+
const subprotocol = require('./subprotocol');
|
15
|
+
const WebSocket = require('./websocket');
|
16
|
+
const { GUID, kWebSocket } = require('./constants');
|
17
|
+
|
18
|
+
const keyRegex = /^[+/0-9A-Za-z]{22}==$/;
|
19
|
+
|
20
|
+
const RUNNING = 0;
|
21
|
+
const CLOSING = 1;
|
22
|
+
const CLOSED = 2;
|
23
|
+
|
24
|
+
/**
|
25
|
+
* Class representing a WebSocket server.
|
26
|
+
*
|
27
|
+
* @extends EventEmitter
|
28
|
+
*/
|
29
|
+
class WebSocketServer extends EventEmitter {
|
30
|
+
/**
|
31
|
+
* Create a `WebSocketServer` instance.
|
32
|
+
*
|
33
|
+
* @param {Object} options Configuration options
|
34
|
+
* @param {Number} [options.backlog=511] The maximum length of the queue of
|
35
|
+
* pending connections
|
36
|
+
* @param {Boolean} [options.clientTracking=true] Specifies whether or not to
|
37
|
+
* track clients
|
38
|
+
* @param {Function} [options.handleProtocols] A hook to handle protocols
|
39
|
+
* @param {String} [options.host] The hostname where to bind the server
|
40
|
+
* @param {Number} [options.maxPayload=104857600] The maximum allowed message
|
41
|
+
* size
|
42
|
+
* @param {Boolean} [options.noServer=false] Enable no server mode
|
43
|
+
* @param {String} [options.path] Accept only connections matching this path
|
44
|
+
* @param {(Boolean|Object)} [options.perMessageDeflate=false] Enable/disable
|
45
|
+
* permessage-deflate
|
46
|
+
* @param {Number} [options.port] The port where to bind the server
|
47
|
+
* @param {(http.Server|https.Server)} [options.server] A pre-created HTTP/S
|
48
|
+
* server to use
|
49
|
+
* @param {Boolean} [options.skipUTF8Validation=false] Specifies whether or
|
50
|
+
* not to skip UTF-8 validation for text and close messages
|
51
|
+
* @param {Function} [options.verifyClient] A hook to reject connections
|
52
|
+
* @param {Function} [callback] A listener for the `listening` event
|
53
|
+
*/
|
54
|
+
constructor(options, callback) {
|
55
|
+
super();
|
56
|
+
|
57
|
+
options = {
|
58
|
+
maxPayload: 100 * 1024 * 1024,
|
59
|
+
skipUTF8Validation: false,
|
60
|
+
perMessageDeflate: false,
|
61
|
+
handleProtocols: null,
|
62
|
+
clientTracking: true,
|
63
|
+
verifyClient: null,
|
64
|
+
noServer: false,
|
65
|
+
backlog: null, // use default (511 as implemented in net.js)
|
66
|
+
server: null,
|
67
|
+
host: null,
|
68
|
+
path: null,
|
69
|
+
port: null,
|
70
|
+
...options
|
71
|
+
};
|
72
|
+
|
73
|
+
if (
|
74
|
+
(options.port == null && !options.server && !options.noServer) ||
|
75
|
+
(options.port != null && (options.server || options.noServer)) ||
|
76
|
+
(options.server && options.noServer)
|
77
|
+
) {
|
78
|
+
throw new TypeError(
|
79
|
+
'One and only one of the "port", "server", or "noServer" options ' +
|
80
|
+
'must be specified'
|
81
|
+
);
|
82
|
+
}
|
83
|
+
|
84
|
+
if (options.port != null) {
|
85
|
+
this._server = http.createServer((req, res) => {
|
86
|
+
const body = http.STATUS_CODES[426];
|
87
|
+
|
88
|
+
res.writeHead(426, {
|
89
|
+
'Content-Length': body.length,
|
90
|
+
'Content-Type': 'text/plain'
|
91
|
+
});
|
92
|
+
res.end(body);
|
93
|
+
});
|
94
|
+
this._server.listen(
|
95
|
+
options.port,
|
96
|
+
options.host,
|
97
|
+
options.backlog,
|
98
|
+
callback
|
99
|
+
);
|
100
|
+
} else if (options.server) {
|
101
|
+
this._server = options.server;
|
102
|
+
}
|
103
|
+
|
104
|
+
if (this._server) {
|
105
|
+
const emitConnection = this.emit.bind(this, 'connection');
|
106
|
+
|
107
|
+
this._removeListeners = addListeners(this._server, {
|
108
|
+
listening: this.emit.bind(this, 'listening'),
|
109
|
+
error: this.emit.bind(this, 'error'),
|
110
|
+
upgrade: (req, socket, head) => {
|
111
|
+
this.handleUpgrade(req, socket, head, emitConnection);
|
112
|
+
}
|
113
|
+
});
|
114
|
+
}
|
115
|
+
|
116
|
+
if (options.perMessageDeflate === true) options.perMessageDeflate = {};
|
117
|
+
if (options.clientTracking) {
|
118
|
+
this.clients = new Set();
|
119
|
+
this._shouldEmitClose = false;
|
120
|
+
}
|
121
|
+
|
122
|
+
this.options = options;
|
123
|
+
this._state = RUNNING;
|
124
|
+
}
|
125
|
+
|
126
|
+
/**
|
127
|
+
* Returns the bound address, the address family name, and port of the server
|
128
|
+
* as reported by the operating system if listening on an IP socket.
|
129
|
+
* If the server is listening on a pipe or UNIX domain socket, the name is
|
130
|
+
* returned as a string.
|
131
|
+
*
|
132
|
+
* @return {(Object|String|null)} The address of the server
|
133
|
+
* @public
|
134
|
+
*/
|
135
|
+
address() {
|
136
|
+
if (this.options.noServer) {
|
137
|
+
throw new Error('The server is operating in "noServer" mode');
|
138
|
+
}
|
139
|
+
|
140
|
+
if (!this._server) return null;
|
141
|
+
return this._server.address();
|
142
|
+
}
|
143
|
+
|
144
|
+
/**
|
145
|
+
* Stop the server from accepting new connections and emit the `'close'` event
|
146
|
+
* when all existing connections are closed.
|
147
|
+
*
|
148
|
+
* @param {Function} [cb] A one-time listener for the `'close'` event
|
149
|
+
* @public
|
150
|
+
*/
|
151
|
+
close(cb) {
|
152
|
+
if (this._state === CLOSED) {
|
153
|
+
if (cb) {
|
154
|
+
this.once('close', () => {
|
155
|
+
cb(new Error('The server is not running'));
|
156
|
+
});
|
157
|
+
}
|
158
|
+
|
159
|
+
process.nextTick(emitClose, this);
|
160
|
+
return;
|
161
|
+
}
|
162
|
+
|
163
|
+
if (cb) this.once('close', cb);
|
164
|
+
|
165
|
+
if (this._state === CLOSING) return;
|
166
|
+
this._state = CLOSING;
|
167
|
+
|
168
|
+
if (this.options.noServer || this.options.server) {
|
169
|
+
if (this._server) {
|
170
|
+
this._removeListeners();
|
171
|
+
this._removeListeners = this._server = null;
|
172
|
+
}
|
173
|
+
|
174
|
+
if (this.clients) {
|
175
|
+
if (!this.clients.size) {
|
176
|
+
process.nextTick(emitClose, this);
|
177
|
+
} else {
|
178
|
+
this._shouldEmitClose = true;
|
179
|
+
}
|
180
|
+
} else {
|
181
|
+
process.nextTick(emitClose, this);
|
182
|
+
}
|
183
|
+
} else {
|
184
|
+
const server = this._server;
|
185
|
+
|
186
|
+
this._removeListeners();
|
187
|
+
this._removeListeners = this._server = null;
|
188
|
+
|
189
|
+
//
|
190
|
+
// The HTTP/S server was created internally. Close it, and rely on its
|
191
|
+
// `'close'` event.
|
192
|
+
//
|
193
|
+
server.close(() => {
|
194
|
+
emitClose(this);
|
195
|
+
});
|
196
|
+
}
|
197
|
+
}
|
198
|
+
|
199
|
+
/**
|
200
|
+
* See if a given request should be handled by this server instance.
|
201
|
+
*
|
202
|
+
* @param {http.IncomingMessage} req Request object to inspect
|
203
|
+
* @return {Boolean} `true` if the request is valid, else `false`
|
204
|
+
* @public
|
205
|
+
*/
|
206
|
+
shouldHandle(req) {
|
207
|
+
if (this.options.path) {
|
208
|
+
const index = req.url.indexOf('?');
|
209
|
+
const pathname = index !== -1 ? req.url.slice(0, index) : req.url;
|
210
|
+
|
211
|
+
if (pathname !== this.options.path) return false;
|
212
|
+
}
|
213
|
+
|
214
|
+
return true;
|
215
|
+
}
|
216
|
+
|
217
|
+
/**
|
218
|
+
* Handle a HTTP Upgrade request.
|
219
|
+
*
|
220
|
+
* @param {http.IncomingMessage} req The request object
|
221
|
+
* @param {(net.Socket|tls.Socket)} socket The network socket between the
|
222
|
+
* server and client
|
223
|
+
* @param {Buffer} head The first packet of the upgraded stream
|
224
|
+
* @param {Function} cb Callback
|
225
|
+
* @public
|
226
|
+
*/
|
227
|
+
handleUpgrade(req, socket, head, cb) {
|
228
|
+
socket.on('error', socketOnError);
|
229
|
+
|
230
|
+
const key =
|
231
|
+
req.headers['sec-websocket-key'] !== undefined
|
232
|
+
? req.headers['sec-websocket-key']
|
233
|
+
: false;
|
234
|
+
const version = +req.headers['sec-websocket-version'];
|
235
|
+
|
236
|
+
if (
|
237
|
+
req.method !== 'GET' ||
|
238
|
+
req.headers.upgrade.toLowerCase() !== 'websocket' ||
|
239
|
+
!key ||
|
240
|
+
!keyRegex.test(key) ||
|
241
|
+
(version !== 8 && version !== 13) ||
|
242
|
+
!this.shouldHandle(req)
|
243
|
+
) {
|
244
|
+
return abortHandshake(socket, 400);
|
245
|
+
}
|
246
|
+
|
247
|
+
const secWebSocketProtocol = req.headers['sec-websocket-protocol'];
|
248
|
+
let protocols = new Set();
|
249
|
+
|
250
|
+
if (secWebSocketProtocol !== undefined) {
|
251
|
+
try {
|
252
|
+
protocols = subprotocol.parse(secWebSocketProtocol);
|
253
|
+
} catch (err) {
|
254
|
+
return abortHandshake(socket, 400);
|
255
|
+
}
|
256
|
+
}
|
257
|
+
|
258
|
+
const secWebSocketExtensions = req.headers['sec-websocket-extensions'];
|
259
|
+
const extensions = {};
|
260
|
+
|
261
|
+
if (
|
262
|
+
this.options.perMessageDeflate &&
|
263
|
+
secWebSocketExtensions !== undefined
|
264
|
+
) {
|
265
|
+
const perMessageDeflate = new PerMessageDeflate(
|
266
|
+
this.options.perMessageDeflate,
|
267
|
+
true,
|
268
|
+
this.options.maxPayload
|
269
|
+
);
|
270
|
+
|
271
|
+
try {
|
272
|
+
const offers = extension.parse(secWebSocketExtensions);
|
273
|
+
|
274
|
+
if (offers[PerMessageDeflate.extensionName]) {
|
275
|
+
perMessageDeflate.accept(offers[PerMessageDeflate.extensionName]);
|
276
|
+
extensions[PerMessageDeflate.extensionName] = perMessageDeflate;
|
277
|
+
}
|
278
|
+
} catch (err) {
|
279
|
+
return abortHandshake(socket, 400);
|
280
|
+
}
|
281
|
+
}
|
282
|
+
|
283
|
+
//
|
284
|
+
// Optionally call external client verification handler.
|
285
|
+
//
|
286
|
+
if (this.options.verifyClient) {
|
287
|
+
const info = {
|
288
|
+
origin:
|
289
|
+
req.headers[`${version === 8 ? 'sec-websocket-origin' : 'origin'}`],
|
290
|
+
secure: !!(req.socket.authorized || req.socket.encrypted),
|
291
|
+
req
|
292
|
+
};
|
293
|
+
|
294
|
+
if (this.options.verifyClient.length === 2) {
|
295
|
+
this.options.verifyClient(info, (verified, code, message, headers) => {
|
296
|
+
if (!verified) {
|
297
|
+
return abortHandshake(socket, code || 401, message, headers);
|
298
|
+
}
|
299
|
+
|
300
|
+
this.completeUpgrade(
|
301
|
+
extensions,
|
302
|
+
key,
|
303
|
+
protocols,
|
304
|
+
req,
|
305
|
+
socket,
|
306
|
+
head,
|
307
|
+
cb
|
308
|
+
);
|
309
|
+
});
|
310
|
+
return;
|
311
|
+
}
|
312
|
+
|
313
|
+
if (!this.options.verifyClient(info)) return abortHandshake(socket, 401);
|
314
|
+
}
|
315
|
+
|
316
|
+
this.completeUpgrade(extensions, key, protocols, req, socket, head, cb);
|
317
|
+
}
|
318
|
+
|
319
|
+
/**
|
320
|
+
* Upgrade the connection to WebSocket.
|
321
|
+
*
|
322
|
+
* @param {Object} extensions The accepted extensions
|
323
|
+
* @param {String} key The value of the `Sec-WebSocket-Key` header
|
324
|
+
* @param {Set} protocols The subprotocols
|
325
|
+
* @param {http.IncomingMessage} req The request object
|
326
|
+
* @param {(net.Socket|tls.Socket)} socket The network socket between the
|
327
|
+
* server and client
|
328
|
+
* @param {Buffer} head The first packet of the upgraded stream
|
329
|
+
* @param {Function} cb Callback
|
330
|
+
* @throws {Error} If called more than once with the same socket
|
331
|
+
* @private
|
332
|
+
*/
|
333
|
+
completeUpgrade(extensions, key, protocols, req, socket, head, cb) {
|
334
|
+
//
|
335
|
+
// Destroy the socket if the client has already sent a FIN packet.
|
336
|
+
//
|
337
|
+
if (!socket.readable || !socket.writable) return socket.destroy();
|
338
|
+
|
339
|
+
if (socket[kWebSocket]) {
|
340
|
+
throw new Error(
|
341
|
+
'server.handleUpgrade() was called more than once with the same ' +
|
342
|
+
'socket, possibly due to a misconfiguration'
|
343
|
+
);
|
344
|
+
}
|
345
|
+
|
346
|
+
if (this._state > RUNNING) return abortHandshake(socket, 503);
|
347
|
+
|
348
|
+
const digest = createHash('sha1')
|
349
|
+
.update(key + GUID)
|
350
|
+
.digest('base64');
|
351
|
+
|
352
|
+
const headers = [
|
353
|
+
'HTTP/1.1 101 Switching Protocols',
|
354
|
+
'Upgrade: websocket',
|
355
|
+
'Connection: Upgrade',
|
356
|
+
`Sec-WebSocket-Accept: ${digest}`
|
357
|
+
];
|
358
|
+
|
359
|
+
const ws = new WebSocket(null);
|
360
|
+
|
361
|
+
if (protocols.size) {
|
362
|
+
//
|
363
|
+
// Optionally call external protocol selection handler.
|
364
|
+
//
|
365
|
+
const protocol = this.options.handleProtocols
|
366
|
+
? this.options.handleProtocols(protocols, req)
|
367
|
+
: protocols.values().next().value;
|
368
|
+
|
369
|
+
if (protocol) {
|
370
|
+
headers.push(`Sec-WebSocket-Protocol: ${protocol}`);
|
371
|
+
ws._protocol = protocol;
|
372
|
+
}
|
373
|
+
}
|
374
|
+
|
375
|
+
if (extensions[PerMessageDeflate.extensionName]) {
|
376
|
+
const params = extensions[PerMessageDeflate.extensionName].params;
|
377
|
+
const value = extension.format({
|
378
|
+
[PerMessageDeflate.extensionName]: [params]
|
379
|
+
});
|
380
|
+
headers.push(`Sec-WebSocket-Extensions: ${value}`);
|
381
|
+
ws._extensions = extensions;
|
382
|
+
}
|
383
|
+
|
384
|
+
//
|
385
|
+
// Allow external modification/inspection of handshake headers.
|
386
|
+
//
|
387
|
+
this.emit('headers', headers, req);
|
388
|
+
|
389
|
+
socket.write(headers.concat('\r\n').join('\r\n'));
|
390
|
+
socket.removeListener('error', socketOnError);
|
391
|
+
|
392
|
+
ws.setSocket(socket, head, {
|
393
|
+
maxPayload: this.options.maxPayload,
|
394
|
+
skipUTF8Validation: this.options.skipUTF8Validation
|
395
|
+
});
|
396
|
+
|
397
|
+
if (this.clients) {
|
398
|
+
this.clients.add(ws);
|
399
|
+
ws.on('close', () => {
|
400
|
+
this.clients.delete(ws);
|
401
|
+
|
402
|
+
if (this._shouldEmitClose && !this.clients.size) {
|
403
|
+
process.nextTick(emitClose, this);
|
404
|
+
}
|
405
|
+
});
|
406
|
+
}
|
407
|
+
|
408
|
+
cb(ws, req);
|
409
|
+
}
|
410
|
+
}
|
411
|
+
|
412
|
+
module.exports = WebSocketServer;
|
413
|
+
|
414
|
+
/**
|
415
|
+
* Add event listeners on an `EventEmitter` using a map of <event, listener>
|
416
|
+
* pairs.
|
417
|
+
*
|
418
|
+
* @param {EventEmitter} server The event emitter
|
419
|
+
* @param {Object.<String, Function>} map The listeners to add
|
420
|
+
* @return {Function} A function that will remove the added listeners when
|
421
|
+
* called
|
422
|
+
* @private
|
423
|
+
*/
|
424
|
+
function addListeners(server, map) {
|
425
|
+
for (const event of Object.keys(map)) server.on(event, map[event]);
|
426
|
+
|
427
|
+
return function removeListeners() {
|
428
|
+
for (const event of Object.keys(map)) {
|
429
|
+
server.removeListener(event, map[event]);
|
430
|
+
}
|
431
|
+
};
|
432
|
+
}
|
433
|
+
|
434
|
+
/**
|
435
|
+
* Emit a `'close'` event on an `EventEmitter`.
|
436
|
+
*
|
437
|
+
* @param {EventEmitter} server The event emitter
|
438
|
+
* @private
|
439
|
+
*/
|
440
|
+
function emitClose(server) {
|
441
|
+
server._state = CLOSED;
|
442
|
+
server.emit('close');
|
443
|
+
}
|
444
|
+
|
445
|
+
/**
|
446
|
+
* Handle premature socket errors.
|
447
|
+
*
|
448
|
+
* @private
|
449
|
+
*/
|
450
|
+
function socketOnError() {
|
451
|
+
this.destroy();
|
452
|
+
}
|
453
|
+
|
454
|
+
/**
|
455
|
+
* Close the connection when preconditions are not fulfilled.
|
456
|
+
*
|
457
|
+
* @param {(net.Socket|tls.Socket)} socket The socket of the upgrade request
|
458
|
+
* @param {Number} code The HTTP response status code
|
459
|
+
* @param {String} [message] The HTTP response body
|
460
|
+
* @param {Object} [headers] Additional HTTP response headers
|
461
|
+
* @private
|
462
|
+
*/
|
463
|
+
function abortHandshake(socket, code, message, headers) {
|
464
|
+
if (socket.writable) {
|
465
|
+
message = message || http.STATUS_CODES[code];
|
466
|
+
headers = {
|
467
|
+
Connection: 'close',
|
468
|
+
'Content-Type': 'text/html',
|
469
|
+
'Content-Length': Buffer.byteLength(message),
|
470
|
+
...headers
|
471
|
+
};
|
472
|
+
|
473
|
+
socket.write(
|
474
|
+
`HTTP/1.1 ${code} ${http.STATUS_CODES[code]}\r\n` +
|
475
|
+
Object.keys(headers)
|
476
|
+
.map((h) => `${h}: ${headers[h]}`)
|
477
|
+
.join('\r\n') +
|
478
|
+
'\r\n\r\n' +
|
479
|
+
message
|
480
|
+
);
|
481
|
+
}
|
482
|
+
|
483
|
+
socket.removeListener('error', socketOnError);
|
484
|
+
socket.destroy();
|
485
|
+
}
|