-
Notifications
You must be signed in to change notification settings - Fork 35
/
protocol-micloud.js
294 lines (252 loc) · 7.99 KB
/
protocol-micloud.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
const crypto = require('crypto');
const fetch = require('node-fetch');
const randomstring = require('randomstring');
const querystring = require('querystring');
const debug = require('debug')('mihome:micloud');
class MiCloudProtocol {
constructor() {
this.username = null;
this.password = null;
this.ssecurity = null;
this.userId = null;
this.serviceToken = null;
this.REQUEST_TIMEOUT = 5000;
this.AGENT_ID = randomstring.generate({
length: 13,
charset: 'ABCDEF',
});
this.USERAGENT = `Android-7.1.1-1.0.0-ONEPLUS A3010-136-${
this.AGENT_ID
} APP/xiaomi.smarthome APPV/62830`;
this.CLIENT_ID = randomstring.generate({
length: 6,
charset: 'alphabetic',
capitalization: 'uppercase',
});
this.countries = ['ru', 'us', 'tw', 'sg', 'cn', 'de'];
this.locale = 'en';
}
get isLoggedIn() {
return !!this.serviceToken;
}
async login(username, password) {
debug(`Login with username ${username}`);
if (this.isLoggedIn) {
throw new Error(`You're logged in with username ${this.username}. Pls logout first`);
}
const { sign } = await this._loginStep1();
const { ssecurity, userId, location } = await this._loginStep2(username, password, sign);
const { serviceToken } = await this._loginStep3(sign.indexOf('http') === -1 ? location : sign);
this.username = username;
this.password = password;
this.ssecurity = ssecurity;
this.userId = userId;
this.serviceToken = serviceToken;
}
logout() {
if (!this.isLoggedIn) {
throw new Error('You aren\'t logged in');
}
debug(`Logout username ${this.username}`);
this.username = null;
this.password = null;
this.ssecurity = null;
this.userId = null;
this.serviceToken = null;
}
async request(path, data, country = 'cn') {
if (!this.isLoggedIn) {
throw new Error('Pls login before make any request');
}
if (!this.countries.includes(country)) {
throw new Error(`The country ${country} is not support, list supported countries is ${this.countries.join(', ')}`);
}
const url = this._getApiUrl(country) + path;
const params = {
data: JSON.stringify(data),
};
const nonce = this._generateNonce();
const signedNonce = this._signedNonce(this.ssecurity, nonce);
const signature = this._generateSignature(path, signedNonce, nonce, params);
const body = {
_nonce: nonce,
data: params.data,
signature,
};
debug(`Request ${url} - ${JSON.stringify(body)}`);
const res = await fetch(url, {
method: 'POST',
timeout: this.REQUEST_TIMEOUT,
headers: {
'User-Agent': this.USERAGENT,
'x-xiaomi-protocal-flag-cli': 'PROTOCAL-HTTP2',
'mishop-client-id': '180100041079',
'Content-Type': 'application/x-www-form-urlencoded',
Cookie: [
'sdkVersion=accountsdk-18.8.15',
`deviceId=${this.CLIENT_ID}`,
`userId=${this.userId}`,
`yetAnotherServiceToken=${this.serviceToken}`,
`serviceToken=${this.serviceToken}`,
`locale=${this.locale}`,
'channel=MI_APP_STORE'].join('; '),
},
body: querystring.stringify(body),
});
if (!res.ok) {
throw new Error(`Request error with status ${res.statusText}`);
}
const json = await res.json();
return json;
}
async getDevices(deviceIds, options = {}) {
const req = deviceIds ? {
dids: deviceIds,
} : {
getVirtualModel: false,
getHuamiDevices: 0,
};
const data = await this.request('/home/device_list', req, options.country);
return data.result.list;
}
async getDevice(deviceId, options = {}) {
const req = {
dids: [String(deviceId)]
};
const data = await this.request('/home/device_list', req, options.country);
return data.result.list[0];
}
async miioCall(deviceId, method, params, options = {}) {
const req = { method, params };
const data = await this.request(`/home/rpc/${deviceId}`, req, options.country);
return data.result;
}
_getApiUrl(country) {
country = country.trim().toLowerCase();
return `https://${country === 'cn' ? '' : `${country}.`}api.io.mi.com/app`;
}
_parseJson(str) {
if (str.indexOf('&&&START&&&') === 0) {
str = str.replace('&&&START&&&', '');
}
return JSON.parse(str);
}
_generateSignature(path, _signedNonce, nonce, params) {
const exps = [];
exps.push(path);
exps.push(_signedNonce);
exps.push(nonce);
const paramKeys = Object.keys(params);
paramKeys.sort();
for (let i = 0, { length } = paramKeys; i < length; i++) {
const key = paramKeys[i];
exps.push(`${key}=${params[key]}`);
}
return crypto
.createHmac('sha256', Buffer.from(_signedNonce, 'base64'))
.update(exps.join('&'))
.digest('base64');
}
_generateNonce() {
const buf = Buffer.allocUnsafe(12);
buf.write(crypto.randomBytes(8).toString('hex'), 0, 'hex');
buf.writeInt32BE(parseInt(Date.now() / 60000, 10), 8);
return buf.toString('base64');
}
_signedNonce(ssecret, nonce) {
const s = Buffer.from(ssecret, 'base64');
const n = Buffer.from(nonce, 'base64');
return crypto.createHash('sha256').update(s).update(n).digest('base64');
}
async _loginStep1() {
const url = 'https://account.xiaomi.com/pass/serviceLogin?sid=xiaomiio&_json=true';
const res = await fetch(url);
const content = await res.text();
const { statusText } = res;
debug(`Login step 1: ${statusText} - ${content}`);
if (!res.ok) {
throw new Error(`Response step 1 error with status ${statusText}`);
}
const data = this._parseJson(content);
if (!data._sign) {
throw new Error('Login step 1 failed');
}
return {
sign: data._sign,
};
}
async _loginStep2(username, password, sign) {
const formData = querystring.stringify({
hash: crypto
.createHash('md5')
.update(password)
.digest('hex')
.toUpperCase(),
_json: 'true',
sid: 'xiaomiio',
callback: 'https://sts.api.io.mi.com/sts',
qs: '%3Fsid%3Dxiaomiio%26_json%3Dtrue',
_sign: sign,
user: username,
});
const url = 'https://account.xiaomi.com/pass/serviceLoginAuth2';
const res = await fetch(url, {
method: 'POST',
body: formData,
headers: {
'User-Agent': this.USERAGENT,
'Content-Type': 'application/x-www-form-urlencoded',
Cookie: [
'sdkVersion=accountsdk-18.8.15',
`deviceId=${this.CLIENT_ID};`
].join('; '),
},
});
const content = await res.text();
const { statusText } = res;
debug(`Login step 2: ${statusText} - ${content}`);
if (!res.ok) {
throw new Error(`Response step 2 error with status ${statusText}`);
}
const { ssecurity, userId, location } = this._parseJson(content);
if (!ssecurity || !userId || !location) {
throw new Error('Login step 2 failed');
}
this.ssecurity = ssecurity; // Buffer.from(data.ssecurity, 'base64').toString('hex');
this.userId = userId;
return {
ssecurity,
userId,
location,
};
}
async _loginStep3(location) {
const url = location;
const res = await fetch(url);
const content = await res.text();
const { statusText } = res;
debug(`Login step 3: ${statusText} - ${content}`);
if (!res.ok) {
throw new Error(`Response step 3 error with status ${statusText}`);
}
const headers = res.headers.raw();
const cookies = headers['set-cookie'];
let serviceToken;
cookies.forEach(cookieStr => {
const cookie = cookieStr.split('; ')[0];
const idx = cookie.indexOf('=');
const key = cookie.substr(0, idx);
const value = cookie.substr(idx + 1, cookie.length).trim();
if (key === 'serviceToken') {
serviceToken = value;
}
});
if (!serviceToken) {
throw new Error('Login step 3 failed');
}
return {
serviceToken,
};
}
}
module.exports = new MiCloudProtocol();