/
ws.js
274 lines (221 loc) · 6.08 KB
/
ws.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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
'use strict';
var stream = require('stream'),
utils = require('simples/utils/utils');
var usize = 65536;
var WsParser = function (limit, client) {
// Call stream.Writable in this context
stream.Writable.call(this);
// Prepare the parser members
this.buffer = Buffer(0);
this.client = client;
this.frame = null;
this.index = 0;
this.limit = limit;
this.message = Buffer(0);
this.state = 0;
};
// WS parser factory function
WsParser.create = function (limit, client) {
return new WsParser(limit, client);
};
// Inherit from stream.Writable
WsParser.prototype = Object.create(stream.Writable.prototype, {
constructor: {
value: WsParser
}
});
// Write method implementation
WsParser.prototype._write = function (chunk, encoding, callback) {
var length = this.buffer.length + chunk.length;
// Buffer received data
if (this.buffer.length) {
this.buffer = Buffer.concat([this.buffer, chunk], length);
} else {
this.index = 0;
this.buffer = chunk;
}
// Act depending on the current state
if (this.state === 0) {
this.getHeader();
} else if (this.state === 1) {
this.getExtendedLength();
} else if (this.state === 2) {
this.getMask();
} else if (this.state === 3) {
this.getData();
}
// End the current write
callback();
};
// Get frame data
WsParser.prototype.getData = function () {
var chunk = null,
frame = this.frame,
length = 0,
offset = this.index + frame.length - frame.data.length,
that = this;
// Prepare a new chunk of data
if (offset > this.buffer.length) {
chunk = this.buffer.slice(this.index);
} else {
chunk = this.buffer.slice(this.index, offset);
}
// Prepare the length of the received frame data
length = frame.data.length + chunk.length;
// Limit the received data
if (this.limit && length > this.limit) {
this.state = -1;
this.emit('error', Error('Too long data received'));
} else {
// Update the frame data
frame.data = Buffer.concat([frame.data, chunk], length);
// Cut the chunk from the parser buffer
this.buffer = this.buffer.slice(this.index + chunk.length);
// Reset the parsing index
this.index = 0;
// Check if frame data is ready
if (frame.length === frame.data.length) {
if (this.client) {
// Reset parser state and emit the frame
this.state = 0;
this.emit('frame', frame);
// Check if there is more to parse
if (this.buffer.length) {
setImmediate(function () {
that.getHeader();
});
}
} else {
this.state = 4;
this.unmaskData();
}
}
}
};
// Get 16bit or 32bit(not 64bit!) extended length
WsParser.prototype.getExtendedLength = function () {
var rest = this.buffer.length - this.index;
// Check if there is enough data to read the extended length
if (this.frame.length === 126 && rest >= 2) {
// Set the extended length
this.frame.length = this.buffer.readUInt16BE(this.index);
// Move the index for the next step
this.index += 2;
// Continue to the next state
if (this.client) {
this.state = 3;
this.getData();
} else {
this.state = 2;
this.getMask();
}
} else if (this.frame.length === 127 && rest >= 8) {
// Read only 32bit length
if (this.buffer.readUInt32BE(this.index)) {
this.state = -1;
this.emit('error', Error('Too long frame payload length'));
} else {
// Set the extended length
this.frame.length = this.buffer.readUInt32BE(this.index + 4);
// Move the index for the next step
this.index += 8;
// Continue to the next state
if (this.client) {
this.state = 3;
this.getData();
} else {
this.state = 2;
this.getMask();
}
}
}
};
// Get the frame header
WsParser.prototype.getHeader = function () {
var error = '',
frame = null;
// Check if there is enough data to read the header
if (this.buffer.length - this.index >= 2) {
// Prepare frame object
frame = this.frame = {
data: Buffer(0),
fin: (this.buffer[this.index] & 128) === 128,
length: this.buffer[this.index + 1] & 127,
masked: (this.buffer[this.index + 1] & 128) === 128,
opcode: this.buffer[this.index] & 15
};
// Check for errors
if (this.buffer[this.index] & 112) {
error = 'Extensions are not supported';
} else if ((frame.opcode & 7) > 2) {
error = 'Unknown frame type';
} else if (frame.opcode > 7 && (frame.length > 125 || !frame.fin)) {
error = 'Invalid control frame';
} else if (frame.opcode === 9 && !this.client) {
error = 'Ping frame received';
} else if (frame.opcode === 10 && this.client) {
error = 'Pong frame received';
} else if (!frame.masked && !this.client) {
error = 'Unmasked frame received';
} else if (frame.masked && this.client) {
error = 'Masked frame received';
}
// Move the index for the next step
this.index += 2;
// Continue to the next state
if (error) {
this.state = -1;
this.emit('error', Error(error));
} else if (frame.length > 125) {
this.state = 1;
this.getExtendedLength();
} else if (this.client) {
this.state = 3;
this.getData();
} else {
this.state = 2;
this.getMask();
}
}
};
// Get the masking key
WsParser.prototype.getMask = function () {
// Check if there is enough data to read the masking key
if (this.buffer.length - this.index >= 4) {
// Get 4 bytes for the masking key
this.frame.mask = this.buffer.slice(this.index, this.index + 4);
// Move the index for the next step
this.index += 4;
// Continue to the next state
this.state = 3;
this.getData();
}
};
// Apply the masking key on the frame data
WsParser.prototype.unmaskData = function () {
var frame = this.frame,
index = 0,
that = this;
// Unmask frame data using 64KB chunks
function unmask() {
if (frame.length - index > usize) {
utils.xor(frame.data.slice(index, index + usize), frame.mask);
index += usize;
setImmediate(unmask);
} else {
// Unmask the last chunk of data
utils.xor(frame.data.slice(index), frame.mask);
that.state = 0;
that.emit('frame', frame);
// Check if there is more data to parse
if (that.buffer.length) {
setImmediate(function () {
that.getHeader();
});
}
}
}
// Start unmasking frame data
unmask();
};
module.exports = WsParser;