-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserversocket.js
More file actions
388 lines (335 loc) · 8.73 KB
/
Copy pathserversocket.js
File metadata and controls
388 lines (335 loc) · 8.73 KB
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
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
let Url = require('url')
let Crypto = require('crypto')
let Http = require('http')
function WebSocket(request, socket, header){
if(arguments.length === 1){
this.initClient(request)
}
if(arguments.length === 3){
this.initServer(request, socket, header)
}
}
module.exports = WebSocket
var proto = WebSocket.prototype
proto.onMessage = function(){
}
//proto.onOpen = function(){
//}
proto.onClose = function(){
}
proto.onError = function(){
}
proto.initClient = function(clientUrl){
let url = this.clientUrl = Url.parse(clientUrl)
let socketHost = url.hostname+':'+client
let socketKey = new Buffer('13-'+Date.now()).toString('base64')
let sha1 = Crypto.creatHash('sha1')
sha1.update(socketKey+'258EAFA5-E914-47DA-95CA-C5AB0DC85B11')
let expectHeader = sha1.digest('base64')
let httpOpts = {
port:url.port,
host:url.hostname,
path:url.path,
headers:{
'connection':'Upgrade',
'upgrade':'websocket',
'pragma':'no-cache',
'host':socketHost,
'origin':'http://'+socketHost,
'sec-websocket-version':13,
'sec-websocket-key':socketKey
}
}
let request = http.request(httpOpts)
request.on('error', function(e){
console.log("Node Websocket Client error "+e)
this.onError(e)
})
request.on('response', function(){
console.error("Unexpected response")
})
request.on('upgrade', function(response, socket, header){
if(response.headers['sec-websocket-accept'] !== expectHeader){
console.log("Node Websocket Client error, unexpected accept ")
if(this.onError) this.onError("unexpected accept")
}
this.socket = socket
this.initialize()
this.onOpen()
})
request.end()
}
proto.initServer = function(request, socket){
let version = request.headers['sec-websocket-version']
if(version != 13){
console.log("Node Websocket incompatible socket version header (need 13)")
return socket.destroy()
}
this.socket = socket
let socketKey = request.headers['sec-websocket-key']
let sha1 = Crypto.createHash('sha1')
sha1.update(socketKey + "258EAFA5-E914-47DA-95CA-C5AB0DC85B11")
let serverAck = 'HTTP/1.1 101 Switching Protocols\r\nUpgrade: websocket\r\nConnection: Upgrade\r\nSec-WebSocket-Accept: '+
sha1.digest('base64')+'\r\n\r\n'
socket.write(serverAck)
this.initialize()
let pingFrame = new Buffer(2)
pingFrame[0] = 9|128
pingFrame[1] = 0
this.pingInterval = setInterval(function(){
if(!this.socket) clearInterval(this.pingInterval)
else this.socket.write(pingFrame)
}.bind(this), 10000)
if(this.onOpen){
setImmediate(_=>{
this.onOpen()
})
}
}
proto.initialize = function(){
this.maxBuffer = 10000000
this.headerBuf = new Buffer(14)
this.outputBuf = new Buffer(10000000)
this.parseState = this.parseOpcode
this.bytesExpected = 1
this.bytesWritten = 0
this.bytesRead = 0
this.partialString = ''
this.inputBuf = null
this.maskOffset = 0
this.maskCount = 0
this.bytesPayload = 0
this.readyState = 1
this.socket.on('data', function(data){
this.inputBuf = data
this.bytesRead = 0
while(this.parseState());
}.bind(this))
this.socket.on('close', function(){
this.close()
}.bind(this))
}
proto.close = function(){
if(!this.socket) return
this.onClose()
if(this.pingInterval) clearInterval(this.pingInterval)
this.socket.destroy()
this.readyState = 3
this.socket = undefined
}
proto.error = function(t){
this.onError(t)
this.close()
}
proto.send = function(data, partial, continuation, masking){
if(!this.socket) return
let head
let buf = new Buffer(data)
let buflen = buf.length
if(buflen < 126){
head = new Buffer(2)
head[1] = buflen
}
else if(buflen < 65536){
head = new Buffer(4)
head[1] = 126
head.writeUInt16BE(buflen, 2)
}
else{
head = new Buffer(10)
head[1] = 127
head[2] = head[3] = head[4] = head[5] = 0
head.writeUInt32BE(buflen, 6)
}
let type = 1
if(data instanceof Buffer || data instanceof ArrayBuffer) type = 2
if(continuation) type = 0
head[0] = (partial?0:128) | type
this.socket.write(head)
if(masking){
head[1] |= 128
let mask = new Buffer(4)
mask[0] = 0x7f
mask[1] = 0x7f
mask[2] = 0x7f
mask[3] = 0x7f
this.socket.write(mask)
for(var i = 0; i < buflen; i++){
buf[i] ^= mask[i&3]
}
}
this.socket.write(buf)
}
proto.parseOpcode = function(){
if(this.parseHead()) return false
let frame = this.headerBuf[0] & 128
let type = this.headerBuf[0] & 15
if(type <= 2){
this.isPartial = !frame
this.isBinary = type ===2 || type === 0 && this.lastType === 2
this.bytesExpected = 1
this.parseState = this.parseLen1
if(type) this.lastType = 2
return true
}
if(type == 8) return this.close()
if(type == 9){
this.bytesExpected = 1
this.parseState = this.parsePing
return true
}
if(type == 10){
this.bytesExpected = 1
this.parseState = this.parsePong
return true
}
return this.error("Opcode not supported "+type)
}
proto.parseHead = function(){
let exp = this.bytesExpected
while(this.bytesExpected > 0 && this.bytesRead < this.inputBuf.length && this.bytesWritten < this.headerBuf.length){
this.headerBuf[this.bytesWritten++] = this.inputBuf[this.bytesRead++]
this.bytesExpected--
}
if(this.bytesWritten > this.headerBuf.length) return this.error("Unexpected data in header")
return this.bytesExpected != 0
}
proto.parseData = function(){
if(this.isMasked){
while(this.bytesExpected > 0 && this.bytesRead < this.inputBuf.length){
this.outputBuf[this.bytesWritten++] = this.inputBuf[this.bytesRead++] ^ this.headerBuf[this.maskOffset+(this.maskCount++&3)]
this.bytesExpected--
}
}
else{
if(this.bytesWritten > this.outputBuf.length) return this.error("Output buffer overflow")
while(this.bytesExpected > 0 && this.bytesRead < this.inputBuf.length){
this.outputBuf[this.bytesWritten++] = this.inputBuf[this.bytesRead++]
this.bytesExpected--
}
}
if(this.bytesExpected) return false
if(this.isBinary){ // we received a binary message
if(this.isPartial){
console.log("IMPLEMENT BINARY")
}
else{
console.log("IMPLEMENT BINARY")
}
}
else{
let msg = this.outputBuf.toString('utf8',0,this.bytesWritten)
if(!this.isPartial){
this.onMessage({
data:this.partialString + msg
})
this.partialString = ''
}
else this.partialString += msg
}
this.bytesWritten = 0
this.bytesExpected = 1
this.parseState = this.parseOpcode
return true
}
proto.parseMask = function(){
if(this.parseHead()) return false
if(!this.bytesPayload){
this.bytesExpected = 1
this.bytesWritten = 0
this.parseState = this.parseOpcode
return true
}
this.maskOffset = this.bytesWritten -4
this.bytesWritten = 0
this.maskCount = 0
this.bytesExpected = this.bytesPayload
if(this.bytesPayload > this.maxBuffer) return this.error("Socket buffer size too large")
if(this.bytesPayload > this.outputBuf.length) this.outputBuf = new Buffer(this.bytesPayload)
this.parseState = this.parseData
return true
}
proto.parseLen8 = function(){
if(this.parseHead()) return false
this.bytesPayload = this.headerBuf.readUInt32BE(this.bytesWritten - 4)
if(this.isMasked){
this.bytesExpected = 4
this.parseState = this.parseMask
}
else{
this.bytesExpected = this.bytesPayload
this.parseState = this.parseData
this.bytesWritten = 0
}
return true
}
proto.parseLen2 = function(){
if(this.parseHead()) return false
this.bytesPayload = this.headerBuf.readUInt16BE(this.bytesWritten - 2)
if(this.isMasked){
this.bytesExpected = 4
this.parseState = this.parseMask
}
else{
this.bytesExpected = this.bytesPayload
this.parseState = this.parseData
this.bytesWritten = 0
}
return true
}
proto.parseLen1 = function(){
if(this.parseHead()) return false
this.isMasked = this.headerBuf[this.bytesWritten-1]&128
let type = this.headerBuf[this.bytesWritten-1]&127
if(type < 126){
this.bytesPayload = type
if(!this.isMasked){
this.bytesExpected = this.bytesPayload
this.parseState = this.parseData
this.bytesWritten = 0
}
else{
this.bytesExpected = 4
this.parseState = this.parseMask
}
}
else if(type == 126){
this.bytesExpected = 2
this.parseState = this.parseLen2
}
else if(type == 127){
this.bytesExpected = 8
this.parseState = this.parseLen8
}
return true
}
let pongFrame = new Buffer(2)
pongFrame[0] = 10|128
pongFrame[1] = 0
proto.parsePing = function(){
if(this.parseHead()) return false
if(this.headerBuf[this.bytesWritten - 1] & 128){
this.bytesExpected = 4
this.bytesPayload = 0
this.parseState = this.parseMask
return true
}
this.bytesExpected = 1
this.bytesWritten = 0
this.parseState = this.parseOpcode
this.socket.write(pongFrame)
return true
}
proto.parsePong = function(){
if(this.parseHead()) return false
if(this.headerBuf[this.bytesWritten - 1] & 128){
this.bytesExpected = 4
this.bytesPayload = 0
this.parseState = this.parseMask
return true
}
this.bytesExpected = 1
this.bytesWritten = 0
this.parseState = this.parseOpcode
return true
}