forked from xmppo/node-xmpp-bosh
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwebsocket.js
343 lines (294 loc) · 12.2 KB
/
websocket.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
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
// -*- tab-width:4 -*-
/*
* Copyright (c) 2011 Dhruv Matani, Sonny Piers
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*
*/
"use strict";
var http = require('http');
var ltx = require('ltx');
var util = require('util');
var uuid = require('node-uuid');
var dutil = require('./dutil.js');
var us = require('underscore');
var assert = require('assert').ok;
var EventPipe = require('eventpipe').EventPipe;
var path = require('path');
var filename = path.basename(path.normalize(__filename));
var log = require('./log.js').getLogger(filename);
var xmlTextDeclRE = /<\?xml [^\?]+\?>/;
var STREAM_UNOPENED = 1;
var STREAM_OPENED = 2;
var STREAM_CLOSED = 3;
var XML_STREAM_CLOSE = '</stream:stream>';
//
// Important links:
//
// Draft Websocket protocol specification
// http://tools.ietf.org/html/draft-moffitt-xmpp-over-websocket-00
//
// Strophe.js modified for Websocket support
// https://github.com/superfeedr/strophejs/tree/protocol-ed
//
exports.createServer = function(bosh_server, options, webSocket) {
webSocket = webSocket || require('ws');
// Config options
var ping_interval = options.websocket_ping_interval;
// State information for XMPP streams
var sn_state = { };
function WebSocketEventPipe(bosh_server) {
this.bosh_server = bosh_server;
}
util.inherits(WebSocketEventPipe, EventPipe);
dutil.copy(WebSocketEventPipe.prototype, {
stop: function() {
return websocket_server.close();
},
stat_stream_add: function() {
return this.bosh_server.stat_stream_add();
},
stat_stream_terminate: function() {
return this.bosh_server.stat_stream_terminate();
}
});
var wsep = new WebSocketEventPipe(bosh_server);
var websocket_server = new webSocket.Server({
server: bosh_server.server,
// autoAcceptConnections: true,
// subprotocol: 'xmpp'
});
wsep.server = websocket_server;
wsep.on('stream-added', function(sstate) {
var to = sstate.to || '';
var ss_xml = new ltx.Element('stream:stream', {
'xmlns': 'jabber:client',
'xmlns:stream': 'http://etherx.jabber.org/streams',
'version': '1.0',
'xml:lang': 'en',
'from': to
}).toString();
if (sstate.has_open_stream_tag) {
ss_xml = ss_xml.replace('/>', '>');
}
log.trace("%s sending data: %s", sstate.name, ss_xml);
wsep.emit('response', ss_xml, sstate);
});
// Special case for WebSockets due to
// https://github.com/dhruvbird/node-xmpp-bosh/issues/16
wsep.on('stream-restarted', function(sstate, stanza) {
var ss_xml = stanza.toString();
if (sstate.has_open_stream_tag) {
ss_xml = ss_xml.replace('/>', '>');
}
log.trace("%s sending stream:stream tag on stream restart: %s", sstate.name, ss_xml);
wsep.emit('response', ss_xml, sstate);
});
wsep.on('response', function(response, sstate) {
// Send the data back to the client
if (!sstate.terminated && sn_state.hasOwnProperty(sstate.name)) {
try {
sstate.conn.send(response.toString());
} catch (e) {
log.warn(e.stack);
}
}
});
wsep.on('terminate', function(sstate, had_error) {
if (!sn_state.hasOwnProperty(sstate.name)) {
return;
}
if (sstate.terminated) {
log.warn('%s Multiple terminate events received', sstate.name);
return;
}
wsep.emit('response', XML_STREAM_CLOSE, sstate);
sstate.terminated = true;
});
websocket_server.on('connection', function(conn) {
var stream_name = uuid();
// Note: xmpp-proxy.js relies on the session object
// to have a sid attribute and the stream object to
// contain a name attribute. This is done to improve
// readability of the logs, even though it introduces
// coupling. We may choose to get rid of it later.
// Deviation from this behaviour for now might lead to
// a crash or unreadable logs.
var sstate = {
name: stream_name,
stream_state: STREAM_UNOPENED,
conn: conn,
// Compatibility with xmpp-proxy-connector
state: {
sid: "WEBSOCKET"
},
session: {
sid: "WEBSOCKET"
},
has_open_stream_tag: false,
terminated: false,
last_pong: Date.now(),
ping_timer_id: null
};
if (ping_interval) {
sstate.ping_timer_id = setInterval(function () {
if (Date.now() - sstate.last_pong > ping_interval * 1000 * 2) {
log.warn("%s no pong - closing stream", stream_name);
sstate.terminated = true;
// Other end unresponsive: no point in a graceful close
conn.terminate();
// Prevent any further ping attempts (close event may not be
// emitted immediately)
clearInterval(sstate.ping_timer_id);
sstate.ping_timer_id = null;
return;
}
try {
conn.ping();
} catch (e) {
log.warn(e.stack);
}
}, ping_interval * 1000)
}
sn_state[stream_name] = sstate;
conn.on('pong', function() {
sstate.last_pong = Date.now();
});
conn.on('message', function(message) {
// console.log("message:", message);
if (typeof message != 'string') {
log.warn("Only utf-8 supported...");
return;
}
// Check if this is a stream open message
if (message.indexOf('<stream:stream') !== -1) {
// Yes, it is.
// Remove the leading <?xml ... ?> declaration if present.
//
// See
// https://github.com/dhruvbird/node-xmpp-bosh/issues/59
// for more details.
//
message = message.replace(xmlTextDeclRE, '');
// Now, check if it is closed or unclosed
if (message.indexOf('/>') === -1) {
// Unclosed - Close it to continue parsing
message += XML_STREAM_CLOSE;
sstate.has_open_stream_tag = true;
}
} else if (message.indexOf(XML_STREAM_CLOSE) !== -1) {
// Stream close message from a client must appear in a message
// by itself - see draft-moffitt-xmpp-over-websocket-02
if (sstate.stream_state === STREAM_CLOSED) {
log.warn('%s Multiple stream close tags received', stream_name);
return;
}
sstate.stream_state = STREAM_CLOSED;
if (sstate.terminated) {
// We initiated the stream close, so we should close the WS
// Note: Always delete before closing
delete sn_state[sstate.name];
try {
sstate.conn.close();
} catch (e) {
log.warn(e.stack);
}
} else {
// Raise the stream-terminate event on wsep
wsep.emit('stream-terminate', sstate);
wsep.emit('response', XML_STREAM_CLOSE, sstate);
sstate.terminated = true;
}
return;
}
// TODO: Maybe use a SAX based parser instead
message = '<dummy>' + message + '</dummy>';
log.debug("%s - Processing: %s", stream_name, message);
// XML parse the message
var nodes = dutil.xml_parse(message);
if (!nodes) {
log.warn("%s Closing connection due to invalid packet", stream_name);
sstate.terminated = true;
sstate.conn.close();
return;
}
// console.log("xml nodes:", nodes);
nodes = nodes.children;
// The stream start node is special since we trigger a
// stream-add event when we get it.
var ss_node = nodes.filter(function(node) {
return typeof node.is === 'function' && node.is('stream');
});
ss_node = us.first(ss_node);
nodes = nodes.filter(function(node) {
return typeof node.is === 'function' ? !node.is('stream') : true;
});
if (ss_node) {
if (sstate.stream_state === STREAM_UNOPENED) {
// Start a new stream
wsep.stat_stream_add();
sstate.stream_state = STREAM_OPENED;
// console.log("stream start attrs:", ss_node.attrs);
sstate.to = ss_node.attrs.to;
wsep.emit('stream-add', sstate, ss_node.attrs);
} else if (sstate.stream_state === STREAM_OPENED) {
// Restart the current stream
wsep.emit('stream-restart', sstate, ss_node.attrs);
}
}
// console.log("nodes:", nodes);
assert(nodes instanceof Array);
// Process the nodes normally.
wsep.emit('nodes', nodes, sstate);
});
conn.on('close', function() {
log.trace("%s Stream close requested", stream_name);
if (sn_state.hasOwnProperty(stream_name)) {
// Note: Always delete before emitting events
delete sn_state[stream_name];
if (sstate.stream_state !== STREAM_CLOSED) {
// Bad client: did not close the stream first
// Raise the stream-terminate event on wsep
wsep.emit('stream-terminate', sstate);
sstate.terminated = true;
}
}
// This code is run regardless of which end closed the stream
if (sstate.ping_timer_id !== null) {
clearInterval(sstate.ping_timer_id);
sstate.ping_timer_id = null;
}
wsep.stat_stream_terminate();
});
});
websocket_server.on('disconnect', function(conn) {
});
function emit_error(ex) {
// We enforce similar semantics as the rest of the node.js for
// the 'error' event and throw an exception if it is unhandled
if (!wsep.emit('error', ex)) {
throw new Error(ex.toString());
}
}
// Handle the 'error' event on the bosh_server and re-emit it.
// Throw an exception if no one handles the exception we threw
bosh_server.on('error', emit_error);
websocket_server.on('error', emit_error);
return wsep;
};