homebridge-xbox-tv 2.9.7-beta.8 → 2.9.7

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "displayName": "Xbox TV",
3
3
  "name": "homebridge-xbox-tv",
4
- "version": "2.9.7-beta.8",
4
+ "version": "2.9.7",
5
5
  "description": "Homebridge plugin (https://github.com/homebridge/homebridge) to control Xbox game consoles.",
6
6
  "license": "MIT",
7
7
  "author": "grzegorz914",
@@ -7,7 +7,6 @@ class MESSAGE {
7
7
  constructor(type, packetData = false) {
8
8
  this.packetType = type;
9
9
  this.packetData = packetData;
10
- this.packetDecoded = false;
11
10
  this.channelId = Buffer.from('\x00\x00\x00\x00\x00\x00\x00\x00');
12
11
 
13
12
  const Type = {
@@ -415,9 +414,9 @@ class MESSAGE {
415
414
  }
416
415
 
417
416
  const iv = xboxlocalapi.crypto.getIv();
418
- const key = xboxlocalapi.crypto.encrypt(header.toBuffer().slice(0, 16), iv);
419
- const encryptionKey = xboxlocalapi.crypto.getEncryptionKey();
420
- const encryptedPayload = xboxlocalapi.crypto.encrypt(payload.toBuffer(), encryptionKey, key);
417
+ const key = xboxlocalapi.crypto.getKey();
418
+ let encryptedPayload = xboxlocalapi.crypto.encrypt(header.toBuffer().slice(0, 16), iv);
419
+ encryptedPayload = xboxlocalapi.crypto.encrypt(payload.toBuffer(), key, encryptedPayload);
421
420
 
422
421
  let packet = Buffer.concat([
423
422
  header.toBuffer(),
@@ -455,7 +454,6 @@ class MESSAGE {
455
454
  if (packet.protectedPayload) {
456
455
  const iv = xboxlocalapi.crypto.getIv();
457
456
  const key = xboxlocalapi.crypto.encrypt(this.packetData.slice(0, 16), iv);
458
-
459
457
  let decryptedPayload = xboxlocalapi.crypto.decrypt(packet.protectedPayload, key);
460
458
  packet.decryptedPayload = new PacketStructure(decryptedPayload).toBuffer();
461
459
  decryptedPayload = new PacketStructure(decryptedPayload);
@@ -469,10 +467,8 @@ class MESSAGE {
469
467
  };
470
468
  };
471
469
 
472
- this.packetType = packet.type;
473
- this.packetDecoded = packet;
474
-
475
- return this;
470
+ const data = { packetType: packet.type, packetDecoded: packet };
471
+ return data;
476
472
  };
477
473
  };
478
474
  module.exports = MESSAGE;
@@ -3,10 +3,11 @@ const JsRsaSign = require('jsrsasign');
3
3
  const Crypto = require('crypto');
4
4
  const EOL = require('os').EOL;
5
5
  const EC = require('elliptic').ec;
6
+ const bufferIv = Buffer.from('\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00');
6
7
 
7
8
  class SGCRYPTO {
8
9
  constructor() {
9
- this.encryptionKey = false;
10
+ this.key = false;
10
11
  this.iv = false;
11
12
  this.hashKey = false;
12
13
 
@@ -48,7 +49,7 @@ class SGCRYPTO {
48
49
  const shaSecretHex = shaSecret.toString('hex');
49
50
  const secret = Buffer.from(shaSecretHex, 'hex');
50
51
 
51
- this.encryptionKey = secret.subarray(0, 16);
52
+ this.key = secret.subarray(0, 16);
52
53
  this.iv = secret.subarray(16, 32);
53
54
  this.hashKey = secret.subarray(32);
54
55
 
@@ -64,69 +65,54 @@ class SGCRYPTO {
64
65
  });
65
66
  };
66
67
 
67
- getEncryptionKey() {
68
- return this.encryptionKey;
68
+ getKey() {
69
+ return this.key;
69
70
  };
70
71
 
71
72
  getIv() {
72
73
  return this.iv;
73
74
  };
74
75
 
75
- encrypt(data, key = false, iv = false) {
76
+ encrypt(data, key, iv) {
76
77
  data = Buffer.from(data);
77
-
78
- if (!key) {
79
- key = this.getEncryptionKey();
80
- }
81
-
82
- if (!iv) {
83
- iv = Buffer.from('\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00');
84
- }
78
+ key = !key ? this.key : key;
79
+ iv = !iv ? bufferIv : iv;
85
80
 
86
81
  const cipher = Crypto.createCipheriv('aes-128-cbc', key, iv);
87
82
  cipher.setAutoPadding(false);
88
83
 
89
84
  let encryptedPayload = cipher.update(data, 'binary', 'binary');
90
85
  encryptedPayload += cipher.final('binary');
91
- return Buffer.from(encryptedPayload, 'binary');
86
+ encryptedPayload = Buffer.from(encryptedPayload, 'binary');
87
+ return encryptedPayload;
92
88
  }
93
89
 
94
- decrypt(data, iv, key = false) {
95
- if (!key) {
96
- key = this.getEncryptionKey();
97
- }
98
-
99
- if (!iv) {
100
- iv = Buffer.from('\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00');
101
- }
90
+ decrypt(data, iv, key) {
91
+ key = !key ? this.key : key;
92
+ iv = !iv ? bufferIv : iv;
102
93
 
103
- const paddedData = this.addPadding(data);
104
94
  const cipher = Crypto.createDecipheriv('aes-128-cbc', key, iv);
105
95
  cipher.setAutoPadding(false);
106
96
 
107
97
  let decryptedPayload = cipher.update(data, 'binary', 'binary');
108
98
  decryptedPayload += cipher.final('binary');
109
- return this.removePadding(Buffer.from(decryptedPayload, 'binary'));
99
+ decryptedPayload = this.removePadding(Buffer.from(decryptedPayload, 'binary'));
100
+ return decryptedPayload;
110
101
  };
111
102
 
112
103
  sign(data) {
113
104
  const hashHmac = Crypto.createHmac('sha256', this.hashKey);
114
105
  hashHmac.update(data, 'binary', 'binary');
115
- const protectedPayloadHash = hashHmac.digest('binary');
116
- return Buffer.from(protectedPayloadHash, 'binary');
106
+ let protectedPayloadHash = hashHmac.digest('binary');
107
+ protectedPayloadHash = Buffer.from(protectedPayloadHash, 'binary');
108
+ return protectedPayloadHash;
117
109
  };
118
110
 
119
111
  removePadding(payload) {
120
112
  const payloadBuffer = Buffer.from(payload.slice(-1));
121
113
  const payloadLength = payloadBuffer.readUInt8(0);
122
114
 
123
- if (payloadLength > 0 && payloadLength < 16) {
124
- return Buffer.from(payload.slice(0, payload.length - payloadLength));
125
- }
126
- return payload;
127
- };
128
-
129
- addPadding(payload) {
115
+ payload = payloadLength > 0 && payloadLength < 16 ? Buffer.from(payload.slice(0, payload.length - payloadLength)) : payload;
130
116
  return payload;
131
117
  };
132
118
  }
@@ -6,7 +6,6 @@ class SIMPLE {
6
6
  this.packetType = type;
7
7
  this.packetFormat = type;
8
8
  this.packetData = packetData;
9
- this.packetDecoded = false;
10
9
  this.structureProtected = false;
11
10
 
12
11
  const Type = {
@@ -154,7 +153,7 @@ class SIMPLE {
154
153
  }
155
154
  }
156
155
  this.protectedPayloadLengthReal = this.protectedPayload.toBuffer().length;
157
- const encryptionKey = xboxlocalapi.crypto.getEncryptionKey();
156
+ const encryptionKey = xboxlocalapi.crypto.getKey();
158
157
  const encryptedPayload = xboxlocalapi.crypto.encrypt(this.protectedPayload.toBuffer(), encryptionKey, this.structure.iv.value);
159
158
  payload.writeBytes(encryptedPayload);
160
159
  break;
@@ -195,9 +194,9 @@ class SIMPLE {
195
194
  };
196
195
  };
197
196
  const iv = xboxlocalapi.crypto.getIv();
198
- const encryptedPayload = xboxlocalapi.crypto.encrypt(payload.toBuffer(), iv);
199
- const encryptedPayloadStructure = new PacketStructure(encryptedPayload)
200
- packet = encryptedPayloadStructure.toBuffer();
197
+ let encryptedPayload = xboxlocalapi.crypto.encrypt(payload.toBuffer(), iv);
198
+ encryptedPayload = new PacketStructure(encryptedPayload)
199
+ packet = encryptedPayload.toBuffer();
201
200
  break;
202
201
  default:
203
202
  packet = payload.toBuffer();
@@ -259,11 +258,15 @@ class SIMPLE {
259
258
  this.set(name, packet[name]);
260
259
  }
261
260
 
261
+ // Lets decrypt the data when the payload is encrypted
262
262
  if (packet.protectedPayload !== undefined) {
263
263
  packet.protectedPayload = packet.protectedPayload.slice(0, -32);
264
264
  packet.signature = packet.protectedPayload.slice(-32);
265
265
 
266
- let decryptedPayload = xboxlocalapi.crypto.decrypt(packet.protectedPayload, packet.iv).slice(0, packet.protectedPayloadLength);
266
+ const iv = packet.iv;
267
+ const protectedPayloadLength = packet.protectedPayloadLength;
268
+ let decryptedPayload = xboxlocalapi.crypto.decrypt(packet.protectedPayload, iv);
269
+ decryptedPayload = decryptedPayload.slice(0, protectedPayloadLength);
267
270
  decryptedPayload = new PacketStructure(decryptedPayload);
268
271
 
269
272
  const protectedStructure = Packet[`${packetFormat}Protected`];
@@ -275,10 +278,9 @@ class SIMPLE {
275
278
  }
276
279
  }
277
280
 
278
- this.packetType = (packet.type === 'dd02') ? 'powerOn' : this.packetType;
279
- this.packetDecoded = packet;
280
-
281
- return this;
281
+ const packetType = (packet.type === 'dd02') ? 'powerOn' : this.packetType;
282
+ const data = { packetType: packetType, packetDecoded: packet };
283
+ return data;
282
284
  }
283
285
  };
284
286
  module.exports = SIMPLE;