-
Notifications
You must be signed in to change notification settings - Fork 356
/
Copy pathpacket.js
168 lines (139 loc) · 3.69 KB
/
packet.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
'use strict';
const crypto = require('crypto');
const debug = require('debug')('miio:packet');
class Packet {
constructor(discovery = false) {
this.discovery = discovery;
this.header = Buffer.alloc(2 + 2 + 4 + 4 + 4 + 16);
this.header[0] = 0x21;
this.header[1] = 0x31;
for(let i=4; i<32; i++) {
this.header[i] = 0xff;
}
this._serverStampTime = 0;
this._token = null;
}
handshake() {
this.data = null;
}
handleHandshakeReply() {
if(this._token === null) {
const token = this.checksum;
if(token.toString('hex').match(/^[fF0]+$/)) {
// Device did not return its token so we set our token to null
this._token = null;
} else {
this.token = this.checksum;
}
}
}
get needsHandshake() {
/*
* Handshake if we:
* 1) do not have a token
* 2) it has been longer then 120 seconds since last received message
*/
return ! this._token || (Date.now() - this._serverStampTime) > 120000;
}
get raw() {
if(this.data) {
// Send a command to the device
if(! this._token) {
throw new Error('Token is required to send commands');
}
for(let i=4; i<8; i++) {
this.header[i] = 0x00;
}
// Update the stamp to match server
if(this._serverStampTime) {
const secondsPassed = Math.floor(Date.now() - this._serverStampTime) / 1000;
this.header.writeUInt32BE(this._serverStamp + secondsPassed, 12);
}
// Encrypt the data
let cipher = crypto.createCipheriv('aes-128-cbc', this._tokenKey, this._tokenIV);
let encrypted = Buffer.concat([
cipher.update(this.data),
cipher.final()
]);
// Set the length
this.header.writeUInt16BE(32 + encrypted.length, 2);
// Calculate the checksum
let digest = crypto.createHash('md5')
.update(this.header.slice(0, 16))
.update(this._token)
.update(encrypted)
.digest();
digest.copy(this.header, 16);
debug('->', this.header);
return Buffer.concat([ this.header, encrypted ]);
} else {
// Handshake
this.header.writeUInt16BE(32, 2);
for(let i=4; i<32; i++) {
this.header[i] = 0xff;
}
debug('->', this.header);
return this.header;
}
}
set raw(msg) {
msg.copy(this.header, 0, 0, 32);
debug('<-', this.header);
const stamp = this.stamp;
if(stamp > 0) {
// If the device returned a stamp, store it
this._serverStamp = this.stamp;
this._serverStampTime = Date.now();
}
const encrypted = msg.slice(32);
if(this.discovery) {
// This packet is only intended to be used for discovery
this.data = encrypted.length > 0;
} else {
// Normal packet, decrypt data
if(encrypted.length > 0) {
if(! this._token) {
debug('<- No token set, unable to handle packet');
this.data = null;
return;
}
const digest = crypto.createHash('md5')
.update(this.header.slice(0, 16))
.update(this._token)
.update(encrypted)
.digest();
const checksum = this.checksum;
if(! checksum.equals(digest)) {
debug('<- Invalid packet, checksum was', checksum, 'should be', digest);
this.data = null;
} else {
let decipher = crypto.createDecipheriv('aes-128-cbc', this._tokenKey, this._tokenIV);
this.data = Buffer.concat([
decipher.update(encrypted),
decipher.final()
]);
}
} else {
this.data = null;
}
}
}
get token() {
return this._token;
}
set token(t) {
this._token = Buffer.from(t);
this._tokenKey = crypto.createHash('md5').update(t).digest();
this._tokenIV = crypto.createHash('md5').update(this._tokenKey).update(t).digest();
}
get checksum() {
return this.header.slice(16);
}
get deviceId() {
return this.header.readUInt32BE(8);
}
get stamp() {
return this.header.readUInt32BE(12);
}
}
module.exports = Packet;