forked from soef/alexa-remote
-
Notifications
You must be signed in to change notification settings - Fork 25
/
Copy pathalexa-wsmqtt.js
executable file
·862 lines (767 loc) · 37.9 KB
/
alexa-wsmqtt.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
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
const WebSocket = require('ws');
const EventEmitter = require('events');
const crypto = require('crypto');
class AlexaWsMqtt extends EventEmitter {
constructor(options, cookie, macDms) {
super();
this._options = options;
this.stop = false;
let serialArr = null;
this.cookie = cookie;
this.macDms = macDms;
if (cookie) serialArr = cookie.match(/ubid-[a-z]+=([^;]+);/);
if (!serialArr || !serialArr[1]) {
this._options.logger && this._options.logger('Alexa-Remote WS-MQTT: Cookie incomplete : ' + JSON.stringify(serialArr));
return undefined;
}
this.accountSerial = serialArr[1];
this.websocket = null;
this.pingPongInterval = null;
this.errorRetryCounter = 0;
this.reconnectTimeout = null;
this.pongTimeout = null;
this.initTimeout = null;
this.connectionActive = false;
this.messageId = Math.floor(1E9 * Math.random());
}
isConnected() {
return this.connectionActive;
}
connectType1() {
const urlTime = Date.now();
let amazonPage = '.' + this._options.amazonPage;
if (amazonPage === '.amazon.com') amazonPage = '-js.amazon.com'; // Special Handling for US!
const url = `wss://dp-gw-na${amazonPage}/?x-amz-device-type=ALEGCNGL9K0HM&x-amz-device-serial=${this.accountSerial}-${urlTime}`;
this.websocket = new WebSocket(url, [],
{
'perMessageDeflate': true,
'protocolVersion': 13,
'timeout': 5000,
'headers': {
'Connection': 'keep-alive, Upgrade',
'Upgrade': 'websocket',
'Host': 'dp-gw-na.' + this._options.amazonPage,
'Origin': 'https://alexa.' + this._options.amazonPage,
'Pragma': 'no-cache',
'Cache-Control': 'no-cache',
//'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
//'Accept-Language': 'de,en-US;q=0.7,en;q=0.3',
//'Sec-WebSocket-Key': 'aV/ud2q+G4pTtOhlt/Amww==',
//'Sec-WebSocket-Extensions': 'permessage-deflate', // 'x-webkit-deflate-frame',
//'User-Agent': 'Mozilla/5.0 (iPhone; CPU iPhone OS 11_4_1 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Mobile/15G77 PitanguiBridge/2.2.219248.0-[HARDWARE=iPhone10_4][SOFTWARE=11.4.1]',
'Cookie': this.cookie,
}
}
);
return url;
}
/**
* @param method The http request method (GET, POST, DELETE, ...).
* @param path The requested http url path and query.
* @param body The http message body.
* @return {string}
*/
createRequestSignature(method, path, body) {
/*
date = datetime.utcnow().isoformat("T") + "Z"
body = body.decode("utf-8")
data = f"{method}\n{path}\n{date}\n{body}\n{adp_token}"
key = rsa.PrivateKey.load_pkcs1(private_key.encode("utf-8"))
cipher = rsa.pkcs1.sign(data.encode(), key, "SHA-256")
signed_encoded = base64.b64encode(cipher)
signature = f"{signed_encoded.decode()}:{date}"
return {
"x-adp-token": adp_token,
"x-adp-alg": "SHA256withRSA:1.0",
"x-adp-signature": signature
}
<base64>:2021-10-08T23:49:33Z
*/
const now = new Date().toISOString();
const sign = crypto.createSign('SHA256');
sign.write(method + '\n');
sign.write(path + '\n');
sign.write(now + '\n');
sign.write(body.toString('utf-8') + '\n');
sign.write(this.macDms.adp_token);
sign.end();
const privateKey = '-----BEGIN PRIVATE KEY-----\n' + this.macDms.device_private_key + '\n-----END PRIVATE KEY-----';
return `${sign.sign(privateKey, 'base64')}:${now}`;
}
connectType2() {
let amazonPage = '.' + this._options.amazonPage;
if (amazonPage === '.amazon.com') amazonPage = '-js.amazon.com'; // Special Handling for US!
const url = `wss://dp-gw-na${amazonPage}/tcomm/`;
this.websocket = new WebSocket(url, [],
{
'perMessageDeflate': true,
'protocolVersion': 13,
'timeout': 5000,
'headers': {
'Connection': 'keep-alive, Upgrade',
'Upgrade': 'websocket',
'Host': 'dp-gw-na.' + this._options.amazonPage,
'Origin': 'https://alexa.' + this._options.amazonPage,
'Pragma': 'no-cache',
'Cache-Control': 'no-cache',
//'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
//'Accept-Language': 'de,en-US;q=0.7,en;q=0.3',
//'User-Agent': 'Mozilla/5.0 (iPhone; CPU iPhone OS 11_4_1 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Mobile/15G77 PitanguiBridge/2.2.219248.0-[HARDWARE=iPhone10_4][SOFTWARE=11.4.1]',
'Cookie': this.cookie,
'x-dp-comm-tuning': 'A:F;A:H',
'x-dp-reason': 'ClientInitiated;1',
'x-dp-tcomm-purpose': 'Regular',
//'x-dp-deviceVersion': 'motorola/osprey_reteu_2gb/osprey_u2:6.0.1/MPI24.107-55/33:user/release-keys',
//'x-dp-networkType': 'WIFI',
//'x-dp-tcomm-versionCode': '894920010',
//'x-dp-oui': 'dca632',
'x-dp-obfuscatedBssid': '-2019514039',
'x-dp-tcomm-versionName': '2.2.443692.0',
'x-adp-signature': this.createRequestSignature('GET', '/tcomm/', ''),
'x-adp-token': this.macDms.adp_token,
'x-adp-alg': 'SHA256WithRSA:1.0',
}
}
);
return url;
}
sendWs(data) {
return new Promise(resolve => {
if (!this.websocket) {
resolve();
return;
}
this.websocket.send(data, () => resolve());
});
}
wait(delay) {
return new Promise(resolve => {
setTimeout(() => resolve(), delay);
});
}
connect() {
let url;
try {
if (this._options.usePushConnectType !== undefined) {
this._options.logger && this._options.logger(`Alexa-Remote WS-MQTT: Try to initialize push connection with type ${this._options.usePushConnectType}`);
if (this._options.usePushConnectType === 1) {
url = this.connectType1();
} else if (this._options.usePushConnectType === 2) {
url = this.connectType2();
} else {
throw new Error(`Invalid push connect type ${this._options.usePushConnectType} specified`);
}
}
else if (!this.macDms || !this.macDms.adp_token || !this.macDms.device_private_key) {
this._options.logger && this._options.logger('Alexa-Remote WS-MQTT: Try to initialize old style push connection because macDms data missing');
url = this.connectType1();
} else {
url = this.connectType2();
}
}
catch (err) {
this._options.logger && this._options.logger('Alexa-Remote WS-MQTT: Error on Init ' + err.message);
this._options.logger && this._options.logger(err.stack);
this.emit('error', err);
return;
}
let msgCounter = 0;
this.initTimeout && clearTimeout(this.initTimeout);
const onWebsocketClose = (code, reason) => {
if (reason) {
reason = reason.toString();
}
this.websocket = null;
this.connectionActive = false;
this._options.logger && this._options.logger('Alexa-Remote WS-MQTT: Close: ' + code + ': ' + reason);
if (this.initTimeout) {
clearTimeout(this.initTimeout);
this.initTimeout = null;
}
if (this.pingPongInterval) {
clearInterval(this.pingPongInterval);
this.pingPongInterval = null;
}
if (this.pongTimeout) {
clearTimeout(this.pongTimeout);
this.pongTimeout = null;
}
if (code === 4001 && reason.startsWith('before - Could not find any')) { // code = 40001, reason = "before - Could not find any vali"
this._options.logger && this._options.logger('Alexa-Remote WS-MQTT: Cookie invalid!');
this.emit('disconnect', false, 'Cookie invalid');
return;
}
if (this.stop) return;
if (this.errorRetryCounter > 100) {
this.emit('disconnect', false, 'Too many failed retries. Check cookie and data');
return;
} else {
this.errorRetryCounter++;
}
const retryDelay = Math.min(60, (this.errorRetryCounter * 5) + 5);
this._options.logger && this._options.logger('Alexa-Remote WS-MQTT: Retry Connection in ' + retryDelay + 's');
this.emit('disconnect', true, 'Retry Connection in ' + retryDelay + 's');
this.reconnectTimeout && clearTimeout(this.reconnectTimeout);
this.reconnectTimeout = setTimeout(() => {
this.reconnectTimeout = null;
this.connect();
}, retryDelay * 1000);
};
this.initTimeout = setTimeout(() => {
this._options.logger && this._options.logger('Alexa-Remote WS-MQTT: Initialization not done within 30s');
try {
this.websocket && this.websocket.close();
} catch (err) {
//just make sure
}
if (this.websocket || !this.reconnectTimeout) { // seems no close was emmitted so far?!
onWebsocketClose();
}
}, 30000);
this.websocket.on('open', () => {
if (! this.websocket) return;
if (this.stop) {
this.websocket.close();
return;
}
this._options.logger && this._options.logger('Alexa-Remote WS-MQTT: Open: ' + url);
this.connectionActive = false;
if (!this.macDms) {
// tell Tuning Service that we support "A:H" protocol = AlphaPrococol
const msg = Buffer.from('0x99d4f71a 0x0000001d A:HTUNE');
//console.log('SEND: ' + msg.toString('ascii'));
this._options.logger && this._options.logger('Alexa-Remote WS-MQTT: A:H Initialization Msg 1 sent');
if (this.websocket.readyState !== 1 /* OPEN */) return;
this.websocket.send(msg);
}
});
this.websocket.on('close', onWebsocketClose);
this.websocket.on('error', (error) => {
this._options.logger && this._options.logger('Alexa-Remote WS-MQTT: Error: ' + error);
this.emit('error', error);
this.websocket && this.websocket.terminate();
});
this.websocket.on('unexpected-response', () => {
this._options.logger && this._options.logger('Alexa-Remote WS-MQTT: Unexpected Response');
});
this.websocket.on('message', async (data) => {
if (!this.websocket || this.websocket.readyState !== 1 /* OPEN */) {
return;
}
this._options.logger && this._options.logger('Alexa-Remote WS-MQTT: Incoming RAW message: ' + data.toString('hex'));
const message = this.parseIncomingMessage(data);
this._options.logger && this._options.logger('Alexa-Remote WS-MQTT: Incoming message: ' + JSON.stringify(message));
if (msgCounter === 0) { // initialization
if (message.content.protocolName) {
this.protocolName = message.content.protocolName;
if (this.protocolName !== 'A:H' && this.protocolName !== 'A:F') {
this._options.logger && this._options.logger('Alexa-Remote WS-MQTT: Server requests unknown protocol: ' + this.protocolName);
}
}
else {
this._options.logger && this._options.logger('Alexa-Remote WS-MQTT: Unexpected Response: ' + JSON.stringify(message));
this.protocolName = this.macDms ? 'A:F' : 'A:H';
}
this._options.logger && this._options.logger('Alexa-Remote WS-MQTT: Detected protocol ' + this.protocolName);
let msg;
if (this.protocolName === 'A:F') { // A:F
msg = Buffer.from('0xfe88bc52 0x0000009c {"protocolName":"A:F","parameters":{"AlphaProtocolHandler.receiveWindowSize":"16","AlphaProtocolHandler.maxFragmentSize":"16000"}}TUNE');
await this.sendWs(msg);
this._options.logger && this._options.logger('Alexa-Remote WS-MQTT: A:F Initialization Msg 2 sent: ' + msg.toString('hex'));
await this.wait(50);
msg = this.encodeGWRegisterAF();
this._options.logger && this._options.logger('Alexa-Remote WS-MQTT: A:F Initialization Msg 3 (Register Connection) sent: ' + msg.toString('hex'));
//console.log('SEND: ' + msg.toString('ascii'));
await this.sendWs(msg);
msgCounter++;
} else { // A:H
msg = Buffer.from('0xa6f6a951 0x0000009c {"protocolName":"A:H","parameters":{"AlphaProtocolHandler.receiveWindowSize":"16","AlphaProtocolHandler.maxFragmentSize":"16000"}}TUNE');
//console.log('SEND: ' + msg.toString('ascii'));
await this.sendWs(msg);
await this.wait(50);
//msg = new Buffer('MSG 0x00000361 0x0e414e45 f 0x00000001 0xd7c62f29 0x0000009b INI 0x00000003 1.0 0x00000024 ff1c4525-c036-4942-bf6c-a098755ac82f 0x00000164d106ce6b END FABE');
msg = this.encodeGWHandshake();
//console.log('SEND: ' + msg.toString('ascii'));
await this.sendWs(msg);
this._options.logger && this._options.logger('Alexa-Remote WS-MQTT: A-H Initialization Msg 2+3 sent');
}
}
if (msgCounter === 1) {
if (this.protocolName === 'A:H') {
//let msg = new Buffer('MSG 0x00000362 0x0e414e46 f 0x00000001 0xf904b9f5 0x00000109 GWM MSG 0x0000b479 0x0000003b urn:tcomm-endpoint:device:deviceType:0:deviceSerialNumber:0 0x00000041 urn:tcomm-endpoint:service:serviceName:DeeWebsiteMessagingService {"command":"REGISTER_CONNECTION"}FABE');
const msg = this.encodeGWRegisterAH();
this._options.logger && this._options.logger('Alexa-Remote WS-MQTT: Initialization Msg 4 (Register Connection) sent');
//console.log('SEND: ' + msg.toString('ascii'));
await this.sendWs(msg);
}
setTimeout(() => {
if (!this.websocket || this.websocket.readyState !== 1 /* OPEN */) {
return;
}
//msg = new Buffer('4D53472030783030303030303635203078306534313465343720662030783030303030303031203078626332666262356620307830303030303036322050494E00000000D1098D8CD1098D8C000000070052006500670075006C0061007246414245', 'hex'); // "MSG 0x00000065 0x0e414e47 f 0x00000001 0xbc2fbb5f 0x00000062 PIN" + 30 + "FABE"
const msg = this.encodePing();
this._options.logger && this._options.logger('Alexa-Remote WS-MQTT: Send First Ping');
//console.log('SEND: ' + msg.toString('hex'));
this.websocket.send(msg);
this.pingPongInterval = setInterval(() => {
if (!this.websocket) return;
//let msg = new Buffer('4D53472030783030303030303635203078306534313465343720662030783030303030303031203078626332666262356620307830303030303036322050494E00000000D1098D8CD1098D8C000000070052006500670075006C0061007246414245', 'hex'); // "MSG 0x00000065 0x0e414e47 f 0x00000001 0xbc2fbb5f 0x00000062 PIN" + 30 + "FABE"
const msg = this.encodePing();
this._options.logger && this._options.logger('Alexa-Remote WS-MQTT: Send Ping');
//console.log('SEND: ' + msg.toString('hex'));
this.websocket.send(msg);
this.pongTimeout = setTimeout(() => {
this.pongTimeout = null;
this._options.logger && this._options.logger('Alexa-Remote WS-MQTT: No Pong received after 30s');
this.websocket && this.websocket.close();
}, 30000);
}, 180000);
}, 100);
}
msgCounter++;
if (msgCounter < 3 || !this.pingPongInterval) return;
const incomingMsg = data.toString('ascii');
//if (incomingMsg.includes('PON') && incomingMsg.includes('\u0000R\u0000e\u0000g\u0000u\u0000l\u0000a\u0000r')) {
if (message.service === 'FABE' && message.content && message.content.messageType === 'PON' && message.content.payloadData && message.content.payloadData.includes('\u0000R\u0000e\u0000g\u0000u\u0000l\u0000a\u0000r')) {
this._options.logger && this._options.logger('Alexa-Remote WS-MQTT: Received Pong');
if (this.initTimeout) {
clearTimeout(this.initTimeout);
this.initTimeout = null;
this._options.logger && this._options.logger('Alexa-Remote WS-MQTT: Initialization completed');
this.emit('connect');
}
if (this.pongTimeout) {
clearTimeout(this.pongTimeout);
this.pongTimeout = null;
}
this.connectionActive = true;
return;
}
else if (message.content && message.content.payload) {
const command = message.content.payload.command;
const payload = message.content.payload.payload;
this._options.logger && this._options.logger('Alexa-Remote WS-MQTT: Command ' + command + ': ' + JSON.stringify(payload, null, 4));
this.emit('command', command, payload);
return;
}
this._options.logger && this._options.logger('Alexa-Remote WS-MQTT: Unknown Data (' + msgCounter + '): ' + incomingMsg);
this.emit('unknown', incomingMsg);
});
}
encodeNumber(val, byteLen) {
if (!byteLen) byteLen = 8;
let str = val.toString(16);
while (str.length < byteLen) str = '0' + str;
return '0x' +str;
}
generateUUID() {
const a = [];
for (let b = 0; 36 > b; b++) {
const c = 'rrrrrrrr-rrrr-4rrr-srrr-rrrrrrrrrrrr'.charAt(b);
if ('r' === c || 's' === c) {
let d = Math.floor(16 * Math.random());
's' === c && (d = d & 3 | 8);
a.push(d.toString(16));
}
else a.push(c);
}
return a.join('');
}
encodeGWHandshake() {
//pubrelBuf = new Buffer('MSG 0x00000361 0x0e414e45 f 0x00000001 0xd7c62f29 0x0000009b INI 0x00000003 1.0 0x00000024 ff1c4525-c036-4942-bf6c-a098755ac82f 0x00000164d106ce6b END FABE');
this.messageId++;
let msg = 'MSG 0x00000361 '; // Message-type and Channel = GW_HANDSHAKE_CHANNEL;
msg += this.encodeNumber(this.messageId) + ' f 0x00000001 ';
const idx1 = msg.length;
msg += '0x00000000 '; // Checksum!
const idx2 = msg.length;
msg += '0x0000009b '; // length content
msg += 'INI 0x00000003 1.0 0x00000024 '; // content part 1
msg += this.generateUUID();
msg += ' ';
msg += this.encodeNumber(Date.now(), 16);
msg += ' END FABE';
const completeBuffer = Buffer.from(msg, 'ascii');
const checksum = this.computeChecksum(completeBuffer, idx1, idx2);
const checksumBuf = Buffer.from(this.encodeNumber(checksum));
checksumBuf.copy(completeBuffer, 39);
return completeBuffer;
}
encodeGWRegisterAH() {
//pubrelBuf = new Buffer('MSG 0x00000362 0x0e414e46 f 0x00000001 0xf904b9f5 0x00000109 GWM MSG 0x0000b479 0x0000003b urn:tcomm-endpoint:device:deviceType:0:deviceSerialNumber:0 0x00000041 urn:tcomm-endpoint:service:serviceName:DeeWebsiteMessagingService {"command":"REGISTER_CONNECTION"}FABE');
this.messageId++;
let msg = 'MSG 0x00000362 '; // Message-type and Channel = GW_CHANNEL;
msg += this.encodeNumber(this.messageId) + ' f 0x00000001 ';
const idx1 = msg.length;
msg += '0x00000000 '; // Checksum!
const idx2 = msg.length;
msg += '0x00000109 '; // length content
msg += 'GWM MSG 0x0000b479 0x0000003b urn:tcomm-endpoint:device:deviceType:0:deviceSerialNumber:0 0x00000041 urn:tcomm-endpoint:service:serviceName:DeeWebsiteMessagingService {"command":"REGISTER_CONNECTION"}FABE';
const completeBuffer = Buffer.from(msg, 'ascii');
const checksum = this.computeChecksum(completeBuffer, idx1, idx2);
const checksumBuf = Buffer.from(this.encodeNumber(checksum));
checksumBuf.copy(completeBuffer, 39);
return completeBuffer;
}
encodeGWRegisterAF() {
this.messageId++;
const completeBuffer = Buffer.alloc(0xe4);
completeBuffer.write('MSG', 0,'ascii');
completeBuffer.writeUInt32BE(0x00000362, 3); // Message-type and Channel = GW_CHANNEL;
completeBuffer.writeUInt32BE(this.messageId, 7);
completeBuffer.write('f', 11, 'ascii');
completeBuffer.writeUInt32BE(0x00000001, 12);
completeBuffer.writeUInt32BE(0x00000000, 16); // Checksum!
completeBuffer.writeUInt32BE(0x000000e4, 20); // length content
completeBuffer.write('GWM MSG 0x0000b479 0x0000003b urn:tcomm-endpoint:device:deviceType:0:deviceSerialNumber:0 0x00000041 urn:tcomm-endpoint:service:serviceName:DeeWebsiteMessagingService {"command":"REGISTER_CONNECTION"}FABE', 24, 'ascii');
const checksum = this.computeChecksum(completeBuffer, 16, 20);
completeBuffer.writeUInt32BE(checksum, 16); // Checksum!
return completeBuffer;
}
encodePing() {
if (this.protocolName === 'A:H') {
return this.encodePingAH();
} else {
return this.encodePingAF();
}
}
encodePingAH() {
function encode(a, b, c, h) {
a = new Uint8Array(a, c, h);
for (c = 0; c < h; c++) a[c] = b >> 8 * (h - 1 - c) & 255;
}
this.messageId++;
let msg = 'MSG 0x00000065 '; // Message-type and Channel = CHANNEL_FOR_HEARTBEAT;
msg += this.encodeNumber(this.messageId) + ' f 0x00000001 ';
const idx1 = msg.length;
msg += '0x00000000 '; // Checksum!
const idx2 = msg.length;
msg+= '0x00000062 '; // length content
const completeBuffer = Buffer.alloc(0x62, 0);
const startBuffer = Buffer.from(msg, 'ascii');
startBuffer.copy(completeBuffer);
const header = 'PIN';
const payload = 'Regular'; // g = h.length
const n = new ArrayBuffer(header.length + 4 + 8 + 4 + 2 * payload.length);
let idx = 0;
let u = new Uint8Array(n, idx, header.length);
const l = 0;
const e = Date.now();
for (let q = 0; q < header.length; q++) u[q] = header.charCodeAt(q);
idx += header.length;
encode(n, l, idx, 4);
idx += 4;
encode(n, e, idx, 8);
idx += 8;
encode(n, payload.length, idx, 4);
idx += 4;
u = new Uint8Array(n, idx, payload.length * 2);
let q;
for (q = 0; q < payload.length; q++) {
u[q * 2] = 0;
u[q * 2 + 1] = payload.charCodeAt(q);
}
const buf = Buffer.from(n);
buf.copy(completeBuffer, msg.length);
const buf2End = Buffer.from('FABE', 'ascii');
buf2End.copy(completeBuffer, msg.length + buf.length);
const checksum = this.computeChecksum(completeBuffer, idx1, idx2);
const checksumBuf = Buffer.from(this.encodeNumber(checksum));
checksumBuf.copy(completeBuffer, 39);
return completeBuffer;
}
encodePingAF() {
function encode(a, b, c, h) {
a = new Uint8Array(a, c, h);
for (c = 0; c < h; c++) a[c] = b >> 8 * (h - 1 - c) & 255;
}
this.messageId++;
const completeBuffer = Buffer.alloc(0x3d);
completeBuffer.write('MSG', 0,'ascii');
completeBuffer.writeUInt32BE(0x00000065, 3); // Message-type and Channel = CHANNEL_FOR_HEARTBEAT;
completeBuffer.writeUInt32BE(this.messageId, 7);
completeBuffer.write('f', 11, 'ascii');
completeBuffer.writeUInt32BE(0x00000001, 12);
completeBuffer.writeUInt32BE(0x00000000, 16); // Checksum!
completeBuffer.writeUInt32BE(0x0000003d, 20); // length content
const header = 'PIN';
const payload = 'Regular'; // g = h.length
const n = new ArrayBuffer(header.length + 4 + 8 + 4 + 2 * payload.length);
let idx = 0;
let u = new Uint8Array(n, idx, header.length);
const l = 0;
const e = Date.now();
for (let q = 0; q < header.length; q++) u[q] = header.charCodeAt(q);
idx += header.length;
encode(n, l, idx, 4);
idx += 4;
encode(n, e, idx, 8);
idx += 8;
encode(n, payload.length, idx, 4);
idx += 4;
u = new Uint8Array(n, idx, payload.length * 2);
let q;
for (q = 0; q < payload.length; q++) {
u[q * 2] = 0;
u[q * 2 + 1] = payload.charCodeAt(q);
}
const buf = Buffer.from(n);
buf.copy(completeBuffer, 24);
completeBuffer.write('FABE', buf.length + 24, 'ascii');
const checksum = this.computeChecksum(completeBuffer, 16, 20);
completeBuffer.writeUInt32BE(checksum, 16); // Checksum!
return completeBuffer;
}
computeChecksum(a, f, k) {
function b(a, b) {
for (a = c(a); 0 != b && 0 != a;) a = Math.floor(a / 2), b--;
return a;
}
function c(a) {
0 > a && (a = 4294967295 + a + 1);
return a;
}
if (k < f) throw 'Invalid checksum exclusion window!';
a = new Uint8Array(a);
let h = 0, l = 0;
for (let e = 0; e < a.length; e++) e != f ? (l += c(a[e] << ((e & 3 ^ 3) << 3)), h += b(l, 32), l = c(l & 4294967295)) : e = k - 1;
for (; h;) l += h, h = b(l, 32), l &= 4294967295;
return c(l);
}
parseIncomingMessage(data) {
if (this.protocolName === 'A:H') {
return this.parseIncomingMessageAH(data);
} else {
return this.parseIncomingMessageAF(data);
}
}
parseIncomingMessageAH(data) {
function readHex(index, length) {
let str = data.toString('ascii', index, index + length);
if (str.startsWith('0x')) str = str.substr(2);
return parseInt(str, 16);
}
function readString(index, length) {
return data.toString('ascii', index, index + length);
}
let idx = 0;
const message = {};
message.service = readString(data.length - 4, 4);
if (message.service === 'TUNE') {
message.checksum = readHex(idx, 10);
idx += 11; // 10 + delimiter;
const contentLength = readHex(idx, 10);
idx += 11; // 10 + delimiter;
message.content = readString(idx, contentLength - 4 - idx);
if (message.content.startsWith('{') && message.content.endsWith('}')) {
try {
message.content = JSON.parse(message.content);
}
catch (e) {
// ignore
}
}
}
else if (message.service === 'FABE') {
message.messageType = readString(idx, 3);
idx += 4;
message.channel = readHex(idx, 10);
idx += 11; // 10 + delimiter;
message.messageId = readHex(idx, 10);
idx += 11; // 10 + delimiter;
message.moreFlag = readString(idx, 1);
idx += 2; // 1 + delimiter;
message.seq = readHex(idx, 10);
idx += 11; // 10 + delimiter;
message.checksum = readHex(idx, 10);
idx += 11; // 10 + delimiter;
//const contentLength = readHex(idx, 10);
idx += 11; // 10 + delimiter;
message.content = {};
message.content.messageType = readString(idx, 3);
idx += 4;
if (message.channel === 0x361) { // GW_HANDSHAKE_CHANNEL
if (message.content.messageType === 'ACK') {
let length = readHex(idx, 10);
idx += 11; // 10 + delimiter;
message.content.protocolVersion = readString(idx, length);
idx += length + 1;
length = readHex(idx, 10);
idx += 11; // 10 + delimiter;
message.content.connectionUUID = readString(idx, length);
idx += length + 1;
message.content.established = readHex(idx, 10);
idx += 11; // 10 + delimiter;
message.content.timestampINI = readHex(idx, 18);
idx += 19; // 18 + delimiter;
message.content.timestampACK = readHex(idx, 18);
idx += 19; // 18 + delimiter;
}
}
else if (message.channel === 0x362) { // GW_CHANNEL
if (message.content.messageType === 'GWM') {
message.content.subMessageType = readString(idx, 3);
idx += 4;
message.content.channel = readHex(idx, 10);
idx += 11; // 10 + delimiter;
if (message.content.channel === 0xb479) { // DEE_WEBSITE_MESSAGING
let length = readHex(idx, 10);
idx += 11; // 10 + delimiter;
message.content.destinationIdentityUrn = readString(idx, length);
idx += length + 1;
length = readHex(idx, 10);
idx += 11; // 10 + delimiter;
let idData = readString(idx, length);
idx += length + 1;
idData = idData.split(' ');
message.content.deviceIdentityUrn = idData[0];
message.content.payload = idData[1];
if (!message.content.payload) {
message.content.payload = readString(idx, data.length - 4 - idx);
}
if (message.content.payload.startsWith('{') && message.content.payload.endsWith('}')) {
try {
message.content.payload = JSON.parse(message.content.payload);
if (message.content.payload && message.content.payload.payload && typeof message.content.payload.payload === 'string') {
message.content.payload.payload = JSON.parse(message.content.payload.payload);
}
}
catch (e) {
// Ignore
}
}
}
}
}
else if (message.channel === 0x65) { // CHANNEL_FOR_HEARTBEAT
idx -= 1; // no delimiter!
message.content.payloadData = data.slice(idx, data.length - 4);
}
}
//console.log(JSON.stringify(message, null, 4));
return message;
}
parseIncomingMessageAF(data) {
function readHex(index, length) {
let str = data.toString('ascii', index, index + length);
if (str.startsWith('0x')) str = str.substr(2);
return parseInt(str, 16);
}
function readString(index, length) {
return data.toString('ascii', index, index + length);
}
let idx = 0;
const message = {};
message.service = readString(data.length - 4, 4);
//this._options.logger && this._options.logger('Alexa-Remote WS-MQTT: Incoming message Service: ' + message.service);
if (message.service === 'TUNE') {
message.checksum = readHex(idx, 10);
idx += 11; // 10 + delimiter;
const contentLength = readHex(idx, 10);
idx += 11; // 10 + delimiter;
message.content = readString(idx, contentLength - 4 - idx);
if (message.content.startsWith('{') && message.content.endsWith('}')) {
try {
message.content = JSON.parse(message.content);
}
catch (e) {
// Ignore
}
}
}
else if (message.service === 'FABE') {
message.messageType = readString(0, 3);
message.channel = data.readUInt32BE(3);
message.messageId = data.readUInt32BE(7);
message.moreFlag = data.readUInt32BE(11);
message.seq = data.readUInt32BE(12);
message.checksum = data.readUInt32BE(16);
//const contentLength = data.readUInt32BE(20);
message.content = {};
message.content.messageType = readString(24, 3);
idx = 28;
//this._options.logger && this._options.logger('Alexa-Remote WS-MQTT: Incoming message Service Channel: ' + message.channel);
if (message.channel === 0x361) { // GW_HANDSHAKE_CHANNEL
if (message.content.messageType === 'ACK') {
let length = readHex(idx, 10);
idx += 11; // 10 + delimiter;
message.content.protocolVersion = readString(idx, length);
idx += length + 1;
length = readHex(idx, 10);
idx += 11; // 10 + delimiter;
message.content.connectionUUID = readString(idx, length);
idx += length + 1;
message.content.established = readHex(idx, 10);
idx += 11; // 10 + delimiter;
message.content.timestampINI = readHex(idx, 18);
idx += 19; // 18 + delimiter;
message.content.timestampACK = readHex(idx, 18);
idx += 19; // 18 + delimiter;
}
}
else if (message.channel === 0x362) { // GW_CHANNEL
if (message.content.messageType === 'GWM') {
message.content.subMessageType = readString(idx, 3);
idx += 4;
message.content.channel = readHex(idx, 10);
idx += 11; // 10 + delimiter;
if (message.content.channel === 0xb479) { // DEE_WEBSITE_MESSAGING
let length = readHex(idx, 10);
idx += 11; // 10 + delimiter;
message.content.destinationIdentityUrn = readString(idx, length);
idx += length + 1;
length = readHex(idx, 10);
idx += 11; // 10 + delimiter;
let idData = readString(idx, length);
idx += length + 1;
idData = idData.split(' ');
message.content.deviceIdentityUrn = idData[0];
message.content.payload = idData[1];
if (!message.content.payload) {
message.content.payload = readString(idx, data.length - 4 - idx);
}
if (message.content.payload.startsWith('{') && message.content.payload.endsWith('}')) {
try {
message.content.payload = JSON.parse(message.content.payload);
if (message.content.payload && message.content.payload.payload && typeof message.content.payload.payload === 'string') {
message.content.payload.payload = JSON.parse(message.content.payload.payload);
}
}
catch (e) {
// Ignore
}
}
}
}
}
else if (message.channel === 0x65) { // CHANNEL_FOR_HEARTBEAT
message.content.payloadData = data.slice(idx, data.length - 4);
}
}
//console.log(JSON.stringify(message, null, 4));
//this._options.logger && this._options.logger('Alexa-Remote WS-MQTT: Parsed Message: ' + JSON.stringify(message));
return message;
}
disconnect() {
if (this.reconnectTimeout) {
clearTimeout(this.reconnectTimeout);
this.reconnectTimeout = null;
}
if (this.pollingTimeout) {
clearTimeout(this.pollingTimeout);
this.pollingTimeout = null;
}
if (this.initTimeout) {
clearTimeout(this.initTimeout);
this.initTimeout = null;
}
this.stop = true;
if (!this.websocket) return;
try {
this.websocket.close();
} catch (e) {
this.connectionActive && this._options.logger && this._options.logger('Alexa-Remote WS-MQTT: Disconnect error: ' + e.message);
}
}
}
module.exports = AlexaWsMqtt;