homebridge-melcloud-control 4.0.0-beta.367 → 4.0.0-beta.369
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 +1 -1
- package/src/melcloudhometoken.js +22 -29
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"displayName": "MELCloud Control",
|
|
3
3
|
"name": "homebridge-melcloud-control",
|
|
4
|
-
"version": "4.0.0-beta.
|
|
4
|
+
"version": "4.0.0-beta.369",
|
|
5
5
|
"description": "Homebridge plugin to control Mitsubishi Air Conditioner, Heat Pump and Energy Recovery Ventilation.",
|
|
6
6
|
"license": "MIT",
|
|
7
7
|
"author": "grzegorz914",
|
package/src/melcloudhometoken.js
CHANGED
|
@@ -4,7 +4,6 @@ import { wrapper } from 'axios-cookiejar-support';
|
|
|
4
4
|
import { CookieJar } from 'tough-cookie';
|
|
5
5
|
import { JSDOM } from 'jsdom';
|
|
6
6
|
import EventEmitter from 'events';
|
|
7
|
-
import { ApiUrls, ApiUrlsHome } from './constants.js';
|
|
8
7
|
|
|
9
8
|
const MOBILE_USER_AGENT = 'MonitorAndControl.App.Mobile/35 CFNetwork/3860.100.1 Darwin/25.0.0';
|
|
10
9
|
const CLIENT_ID = 'homemobile';
|
|
@@ -22,8 +21,7 @@ class MelCloudHomeToken extends EventEmitter {
|
|
|
22
21
|
this.logError = config.logError;
|
|
23
22
|
|
|
24
23
|
const jar = new CookieJar();
|
|
25
|
-
this.
|
|
26
|
-
this.client.defaults.headers['User-Agent'] = MOBILE_USER_AGENT;
|
|
24
|
+
this.axiosInstance = wrapper(axios.create({ jar, withCredentials: true }));
|
|
27
25
|
}
|
|
28
26
|
|
|
29
27
|
generatePKCE() {
|
|
@@ -37,7 +35,11 @@ class MelCloudHomeToken extends EventEmitter {
|
|
|
37
35
|
}
|
|
38
36
|
|
|
39
37
|
async getLoginPage(authUrl) {
|
|
40
|
-
const res = await this.
|
|
38
|
+
const res = await this.axiosInstance({
|
|
39
|
+
method: 'GET',
|
|
40
|
+
baseURL: authUrl,
|
|
41
|
+
maxRedirects: 10
|
|
42
|
+
});
|
|
41
43
|
return { url: res.request.res.responseUrl };
|
|
42
44
|
}
|
|
43
45
|
|
|
@@ -94,11 +96,15 @@ class MelCloudHomeToken extends EventEmitter {
|
|
|
94
96
|
code_verifier: codeVerifier,
|
|
95
97
|
});
|
|
96
98
|
|
|
97
|
-
const tokenResponse = await this.
|
|
99
|
+
const tokenResponse = await this.axiosInstance({
|
|
100
|
+
method: 'POST',
|
|
101
|
+
baseURL: TOKEN_ENDPOINT,
|
|
98
102
|
headers: {
|
|
99
103
|
'Content-Type': 'application/x-www-form-urlencoded',
|
|
104
|
+
'Content-Length': tokenData.toString().length,
|
|
100
105
|
'Authorization': 'Basic aG9tZW1vYmlsZTo=',
|
|
101
106
|
},
|
|
107
|
+
data: tokenData.toString()
|
|
102
108
|
});
|
|
103
109
|
|
|
104
110
|
const tokens = tokenResponse.data;
|
|
@@ -112,8 +118,11 @@ class MelCloudHomeToken extends EventEmitter {
|
|
|
112
118
|
async loginToMelCloudHome(url) {
|
|
113
119
|
try {
|
|
114
120
|
// Step 1: Pobierz stronę logowania, żeby uzyskać sesję + token _csrf
|
|
115
|
-
const getResp = await this.
|
|
121
|
+
const getResp = await this.axiosInstance({
|
|
122
|
+
method: 'GET',
|
|
123
|
+
baseURL: url,
|
|
116
124
|
headers: {
|
|
125
|
+
'User-Agent': MOBILE_USER_AGENT,
|
|
117
126
|
'Accept': 'text/html',
|
|
118
127
|
},
|
|
119
128
|
withCredentials: true,
|
|
@@ -137,8 +146,11 @@ class MelCloudHomeToken extends EventEmitter {
|
|
|
137
146
|
if (this.logWarn) this.emit('warn', `Redirect URL: ${url}`);
|
|
138
147
|
|
|
139
148
|
// Step 3: Wyślij POST z danymi logowania
|
|
140
|
-
const response = await this.
|
|
149
|
+
const response = await this.axiosInstance({
|
|
150
|
+
method: 'POST',
|
|
151
|
+
baseURL: url,
|
|
141
152
|
headers: {
|
|
153
|
+
'User-Agent': MOBILE_USER_AGENT,
|
|
142
154
|
'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
|
|
143
155
|
'Accept-Language': 'en-US,en;q=0.9',
|
|
144
156
|
'Content-Type': 'application/x-www-form-urlencoded',
|
|
@@ -147,34 +159,17 @@ class MelCloudHomeToken extends EventEmitter {
|
|
|
147
159
|
'Origin': 'https://live-melcloudhome.auth.eu-west-1.amazoncognito.com',
|
|
148
160
|
'Referer': url,
|
|
149
161
|
},
|
|
162
|
+
data: formData.toString(),
|
|
150
163
|
maxRedirects: 0,
|
|
151
164
|
validateStatus: status => [200, 302, 400].includes(status),
|
|
152
165
|
});
|
|
153
166
|
|
|
154
|
-
|
|
167
|
+
if (response.status === 400) if (this.logWarn) this.emit('warn', `Login failed (bad request or invalid credentials: ${response.data}`);
|
|
155
168
|
if (response.status === 302 && response.headers.location) {
|
|
156
169
|
// Login success — mamy redirect z "code="
|
|
157
170
|
const redirectUrl = response.headers.location;
|
|
158
171
|
if (this.logWarn) this.emit('warn', `Redirect URL: ${redirectUrl}`);
|
|
159
|
-
|
|
160
|
-
const listDevicesData = await this.client.get(ApiUrlsHome.GetUserContext, {
|
|
161
|
-
baseURL: ApiUrlsHome.BaseURL,
|
|
162
|
-
timeout: 25000,
|
|
163
|
-
headers: {
|
|
164
|
-
'Accept': '*/*',
|
|
165
|
-
'Accept-Language': 'en-US,en;q=0.9',
|
|
166
|
-
'User-Agent': 'homebridge-melcloud-control/4.0.0',
|
|
167
|
-
'DNT': '1',
|
|
168
|
-
'Origin': 'https://melcloudhome.com',
|
|
169
|
-
'Referer': 'https://melcloudhome.com/dashboard',
|
|
170
|
-
'Sec-Fetch-Dest': 'empty',
|
|
171
|
-
'Sec-Fetch-Mode': 'cors',
|
|
172
|
-
'Sec-Fetch-Site': 'same-origin',
|
|
173
|
-
'X-CSRF': '1'
|
|
174
|
-
}
|
|
175
|
-
});
|
|
176
|
-
const buildingsList = listDevicesData.data.buildings;
|
|
177
|
-
if (this.logDebug) this.emit('debug', `Buildings: ${JSON.stringify(buildingsList, null, 2)}`);
|
|
172
|
+
if (this.logWarn) this.emit('warn', `Response headers: ${response.headers}`);
|
|
178
173
|
|
|
179
174
|
const match = redirectUrl.match(/[?&]code=([^&]+)/);
|
|
180
175
|
if (match) {
|
|
@@ -187,8 +182,6 @@ class MelCloudHomeToken extends EventEmitter {
|
|
|
187
182
|
}
|
|
188
183
|
}
|
|
189
184
|
|
|
190
|
-
if (response.status === 400) if (this.logWarn) this.emit('warn', `Login failed (bad request or invalid credentials: ${response.data}`);
|
|
191
|
-
|
|
192
185
|
return null;
|
|
193
186
|
} catch (error) {
|
|
194
187
|
throw new Error(`Login to MELCloud Home error: ${error}`);
|