forked from Atlantis-Software/node-turn
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmessage.js
384 lines (330 loc) · 11.5 KB
/
message.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
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
const crypto = require('crypto');
const Data = require('./data');
const Attribute = require('./attribute');
const CONSTANTS = require('./constants');
const crc = require('crc');
const STATE_WAITING = 0;
const STATE_RESOLVED = 1;
const STATE_REJECTED = 2;
const STATE_DISCARDED = 3;
const STATE_INCOMMING = 4;
/*
* The most significant 2 bits of every STUN message MUST be zeroes
*
*
* 0 1 2 3
* 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
* +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
* |0 0| STUN Message Type | Message Length |
* +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
* | Magic Cookie |
* +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
* | |
* | Transaction ID (96 bits) |
* | |
* +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
*
* STUN Message Type:
*
* 0 1
* 2 3 4 5 6 7 8 9 0 1 2 3 4 5
*
* +--+--+-+-+-+-+-+-+-+-+-+-+-+-+
* |M |M |M|M|M|C|M|M|M|C|M|M|M|M|
* |11|10|9|8|7|1|6|5|4|0|3|2|1|0|
* +--+--+-+-+-+-+-+-+-+-+-+-+-+-+
*/
var message = function(server, transport) {
this.server = server;
this.transport = transport;
this.allocation = null;
this.class = null;
this.method = null;
this.useFingerprint = false;
this.length = 0;
this.raw = Buffer.alloc(0);
this.attributes = [];
this.magicCookie = CONSTANTS.MAGIC_COOKIE;
this.transactionID = null;
this.user = null;
this.hmacInput = null;
this._state = STATE_WAITING;
this.debugLevel = server.debugLevel;
this.debug = server.debug.bind(server);
};
message.prototype.read = function(udpMessage) {
this._state = STATE_INCOMMING;
if (!udpMessage) {
return false;
}
udpMessage = new Data(udpMessage);
var firstBit = udpMessage.readBit(0);
var secondBit = udpMessage.readBit(1);
// The most significant 2nd bits of every TURN message MUST be zeroes.
if (firstBit || secondBit) {
return false;
}
// read class C1 7th bit and C0 11th bit
this.class = udpMessage.readUncontiguous([7, 11]);
// read method from M11 to M0
this.method = udpMessage.readUncontiguous([2, 3, 4, 5, 6, 8, 9, 10, 12, 13, 14, 15]);
var messageLength = udpMessage.readInt16BE(2);
// check message length
if (messageLength + 20 > udpMessage.length) {
throw new Error('invalid STUN message length');
}
this.magicCookie = udpMessage.readUInt32BE(4);
// check magic cookie
if (this.magicCookie != CONSTANTS.MAGIC_COOKIE) {
return false;
}
this.transactionID = udpMessage.toString('hex', 8, 20);
// read attributes
var attributes = udpMessage.slice(20);
while (attributes.length >= 4) {
var attribute = new Attribute(this);
attribute.read(attributes);
if (attribute.type === CONSTANTS.ATTR.MESSAGE_INTEGRITY) {
let hmacInput = udpMessage.slice(0, 20 + this.length);
let username = this.getAttribute('username');
if (!username) {
this.debug('DEBUG', 'no username sent');
attribute.value = false;
} else {
let password = this.server.authentification.credentials[username];
if (!password) {
this.debug('DEBUG', 'invalid user');
attribute.value = false;
} else {
let hmacKey = crypto.createHash('md5').update(username + ':' + this.server.realm + ':' + password).digest();
// update message length to compute hmac
let previousLength = hmacInput.readInt16BE(2);
hmacInput.writeInt16BE(this.length + 24, 2);
let hmac = crypto.createHmac('sha1', hmacKey).update(hmacInput).digest('hex');
// reset message length for fingerprint
hmacInput.writeInt16BE(previousLength, 2);
if (hmac !== attribute.value) {
this.debug('DEBUG', 'invalid message-integrity, should be ' + hmac + ' instead of ' + attribute.value);
attribute.value = false;
}
}
}
}
if (attribute.type === CONSTANTS.ATTR.FINGERPRINT) {
this.useFingerprint = true;
let toHash = udpMessage.slice(0, 20 + this.length);
let fingerprint = Buffer.alloc(4);
fingerprint.writeUIntBE(crc.crc32(toHash), 0, 4);
let xor = Buffer.alloc(4);
xor.writeUIntBE(0x5354554e, 0, 4);
fingerprint.forEach(function(byte, i) {
fingerprint[i] = (parseInt(byte, 10) & 0xff) ^ xor[i];
});
if (fingerprint.readUIntBE(0,4) !== attribute.value) {
// fingerprint is invalid
attribute.value = false;
}
}
this.attributes.push(attribute);
this.length += 4 + attribute.length + attribute.padding;
attributes = attributes.slice(4 + attribute.length + attribute.padding);
}
// all tests passed so this is a valid STUN message
return true;
};
message.prototype.reply = function() {
var replyMsg = new message(this.server, this.transport.revert());
replyMsg.class = this.class;
replyMsg.method = this.method;
replyMsg.magicCookie = this.magicCookie;
replyMsg.transactionID = this.transactionID;
replyMsg.useFingerprint = this.useFingerprint;
return replyMsg;
};
message.prototype.getMethodName = function() {
var self = this;
var method = "unknown-method";
Object.keys(CONSTANTS.METHOD).forEach(function(methodName) {
if (self.method === CONSTANTS.METHOD[methodName]) {
method = methodName.toLowerCase();
}
});
return method;
};
message.prototype.getClassName = function() {
var self = this;
var _class = "unknown-class";
Object.keys(CONSTANTS.CLASS).forEach(function(className) {
if (self.class === CONSTANTS.CLASS[className]) {
_class = className.toLowerCase();
}
});
return _class;
};
message.prototype.setUser = function(user) {
this.user = user;
};
message.prototype.addAttribute = function(name, value) {
if (!name) {
throw new Error('addAttribute require a name as first argument');
}
// skip message-integrity when no user is registered
if (name === 'message-integrity' && !this.user) {
return;
}
var attribute = new Attribute(this, name, value);
this.attributes.push(attribute);
// update length
this.length += 4 + attribute.length + attribute.padding;
};
message.prototype.getAttribute = function(name) {
var attributeName = name.replace(/-/g, '_').toUpperCase();
var type = CONSTANTS.ATTR[attributeName];
var attribute = null;
this.attributes.forEach(function(attr) {
if (attr.type === type) {
attribute = attribute || attr;
}
});
if (attribute && attribute.value !== void 0) {
return attribute.value;
}
return void 0;
};
message.prototype.getAttributes = function(name) {
var attributeName = name.replace(/-/g, '_').toUpperCase();
var type = CONSTANTS.ATTR[attributeName];
var attributes = [];
this.attributes.forEach(function(attr) {
if (attr.type === type) {
attributes.push(attr.value);
}
});
return attributes;
};
message.prototype.toBuffer = function() {
var self = this;
// add fingerprint if it was used
if (this.useFingerprint) {
this.addAttribute('fingerprint');
}
var header = new Data(Buffer.alloc(20));
// write two first bits to 0
header.writeBit(0, 0);
header.writeBit(0, 1);
// write method in header
header.writeUncontiguous(this.method, [2, 3, 4, 5, 6, 8, 9, 10, 12, 13, 14, 15]);
// write success class in header
header.writeUncontiguous(this.class, [7, 11]);
// write message length
header.writeUInt16BE(this.length, 2);
// write Magic Cookie
header.writeUInt32BE(this.magicCookie, 4);
// write transactionID
header.write(this.transactionID, 8, 20, 'hex');
this.raw = header;
this.attributes.forEach(function(attribute) {
self.raw = Buffer.concat([self.raw, attribute.toBuffer()]);
});
return this.raw;
};
message.prototype.resolve = function() {
var self = this;
if (this._state !== STATE_WAITING) {
return;
}
this.class = CONSTANTS.CLASS.SUCCESS;
var msg = this.toBuffer();
this._state = STATE_RESOLVED;
this.transport.socket.send(msg, this.transport.dst.port, this.transport.dst.address, function(err) {
if (err) {
self.debug('FATAL', 'Fatal error while responding to ' + self.transport.dst + ' TransactionID: ' + self.transactionID + '\n' + self);
self.debug('FATAL', err);
return;
}
self.debug('DEBUG', 'Sending ' + self);
});
};
message.prototype.reject = function(code, reason) {
var self = this;
if (this._state !== STATE_WAITING) {
return;
}
this.class = CONSTANTS.CLASS.ERROR;
this.addAttribute('error-code', {
code: code,
reason: reason
});
this._state = STATE_REJECTED;
var msg = this.toBuffer();
this.transport.socket.send(msg, this.transport.dst.port, this.transport.dst.address, function(err) {
if (err) {
self.debug('FATAL', 'Fatal error while responding to ' + self.transport.dst + ' TransactionID: ' + self.transactionID + '\n' + self);
self.debug('FATAL', err);
return;
}
self.debug('DEBUG', 'Sending ' + self);
});
};
message.prototype.discard = function() {
if (this._state !== STATE_WAITING) {
return;
}
this._state = STATE_DISCARDED;
this.debug('DEBUG', 'Discarding ' + this);
};
message.prototype.data = function(data) {
if (this._state !== STATE_WAITING) {
return;
}
var self = this;
this._state = STATE_RESOLVED;
this.class = CONSTANTS.CLASS.INDICATION;
this.method = CONSTANTS.METHOD.DATA;
this.addAttribute('data', data);
// generate a transactionID
crypto.randomBytes(12, function(err, buf) {
if (err) {
self.debug('FATAL', 'Fatal error while generating TransactionID, indicating to ' + self.transport.dst + '\n' + self);
self.debug('FATAL', err);
return;
}
self.transactionID = buf.toString('hex');
var msg = self.toBuffer();
self.transport.socket.send(msg, self.transport.dst.port, self.transport.dst.address, function(err) {
if (err) {
self.debug('FATAL', 'Fatal error while indicating to ' + self.transport.dst + ' TransactionID: ' + self.transactionID + '\n' + self);
self.debug('FATAL', err);
return;
}
self.debug('DEBUG', 'Sending ' + self);
});
});
};
message.prototype.toString = function() {
var self = this;
var str = '';
if (this.debugLevel <= CONSTANTS.DEBUG_LEVEL.DEBUG) {
str = this.transport + ' ' + this.getMethodName() + ' ' + this.getClassName() + ' TransactionID: ' + this.transactionID;
var indent = ' ';
this.attributes.forEach(function(attribute) {
if (Buffer.isBuffer(attribute.value)) {
if (self.debugLevel <= CONSTANTS.DEBUG_LEVEL.TRACE) {
str += '\n' + indent + attribute.name + ': ';
attribute.value.toString('hex').match(/.{1,32}/g).forEach(function(bin) {
str += '\n' + indent + indent + bin;
});
return;
}
str += '\n' + indent + attribute.name + ': [ Binary data length: ' + attribute.value.length + ' ]';
return;
} else if (attribute.name === 'error-code') {
str += '\n' + indent + attribute.name + ": " + attribute.value.code + ' ' + attribute.value.reason;
return;
}
str += '\n' + indent + attribute.name + ": " + attribute.value;
});
}
return str;
};
module.exports = message;