homebridge-melcloud-control 4.0.0-beta.395 → 4.0.0-beta.396
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/melcloud.js +2 -2
- package/src/melcloudhometoken.js +33 -134
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.396",
|
|
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/melcloud.js
CHANGED
|
@@ -277,7 +277,7 @@ class MelCloud extends EventEmitter {
|
|
|
277
277
|
}
|
|
278
278
|
}
|
|
279
279
|
|
|
280
|
-
async
|
|
280
|
+
async connectToMelCloudHome() {
|
|
281
281
|
try {
|
|
282
282
|
const melCloudHomeToken = new MelCloudHomeToken({
|
|
283
283
|
user: this.user,
|
|
@@ -302,7 +302,7 @@ class MelCloud extends EventEmitter {
|
|
|
302
302
|
}
|
|
303
303
|
}
|
|
304
304
|
|
|
305
|
-
async
|
|
305
|
+
async connectToMelCloudHome1(refresh = false) {
|
|
306
306
|
if (this.logDebug) this.emit('debug', `Connecting to MELCloud Home`);
|
|
307
307
|
|
|
308
308
|
let browser;
|
package/src/melcloudhometoken.js
CHANGED
|
@@ -1,16 +1,16 @@
|
|
|
1
|
-
import axios from 'axios';
|
|
2
1
|
import crypto from 'crypto';
|
|
3
2
|
import { wrapper } from 'axios-cookiejar-support';
|
|
4
3
|
import { CookieJar } from 'tough-cookie';
|
|
5
|
-
import
|
|
4
|
+
import axios from 'axios';
|
|
6
5
|
import EventEmitter from 'events';
|
|
6
|
+
import puppeteer from 'puppeteer';
|
|
7
7
|
|
|
8
|
-
const MOBILE_USER_AGENT = 'MonitorAndControl.App.Mobile/35 CFNetwork/3860.100.1 Darwin/25.0.0';
|
|
9
8
|
const CLIENT_ID = 'homemobile';
|
|
10
9
|
const REDIRECT_URI = 'melcloudhome://';
|
|
11
10
|
const SCOPE = 'openid profile email offline_access IdentityServerApi';
|
|
12
11
|
const TOKEN_ENDPOINT = 'https://auth.melcloudhome.com/connect/token';
|
|
13
12
|
const AUTHORIZE_ENDPOINT = 'https://auth.melcloudhome.com/connect/authorize';
|
|
13
|
+
const MOBILE_USER_AGENT = 'MonitorAndControl.App.Mobile/35 CFNetwork/3860.100.1 Darwin/25.0.0';
|
|
14
14
|
|
|
15
15
|
class MelCloudHomeToken extends EventEmitter {
|
|
16
16
|
constructor(config) {
|
|
@@ -38,7 +38,6 @@ class MelCloudHomeToken extends EventEmitter {
|
|
|
38
38
|
async buildAuthorizeUrl() {
|
|
39
39
|
const pkce = this.generatePKCE();
|
|
40
40
|
const state = this.generateState();
|
|
41
|
-
|
|
42
41
|
const authUrl = new URL(AUTHORIZE_ENDPOINT);
|
|
43
42
|
authUrl.searchParams.set('client_id', CLIENT_ID);
|
|
44
43
|
authUrl.searchParams.set('redirect_uri', REDIRECT_URI);
|
|
@@ -47,153 +46,54 @@ class MelCloudHomeToken extends EventEmitter {
|
|
|
47
46
|
authUrl.searchParams.set('code_challenge', pkce.challenge);
|
|
48
47
|
authUrl.searchParams.set('code_challenge_method', 'S256');
|
|
49
48
|
authUrl.searchParams.set('state', state);
|
|
50
|
-
|
|
51
49
|
return { url: authUrl.toString(), codeVerifier: pkce.verifier };
|
|
52
50
|
}
|
|
53
51
|
|
|
54
52
|
async loginToMelCloudHome(authUrl) {
|
|
55
53
|
try {
|
|
56
|
-
|
|
57
|
-
const
|
|
58
|
-
const
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
54
|
+
this.emit('warn', `Opening Puppeteer for auth URL`);
|
|
55
|
+
const browser = await puppeteer.launch({ headless: true });
|
|
56
|
+
const page = await browser.newPage();
|
|
57
|
+
await page.setUserAgent(MOBILE_USER_AGENT);
|
|
58
|
+
|
|
59
|
+
await page.goto(authUrl, { waitUntil: 'networkidle0' });
|
|
60
|
+
|
|
61
|
+
// Wpisz login i hasło
|
|
62
|
+
await page.type('input[name="username"]', this.user);
|
|
63
|
+
await page.type('input[name="password"]', this.passwd);
|
|
64
|
+
|
|
65
|
+
// Kliknij przycisk login
|
|
66
|
+
await Promise.all([
|
|
67
|
+
page.click('input[type="submit"]'),
|
|
68
|
+
page.waitForNavigation({ waitUntil: 'networkidle0' }).catch(() => { }) // czasami redirect jest JS, więc catch
|
|
69
|
+
]);
|
|
70
|
+
|
|
71
|
+
// Spróbuj pobrać kod z formularza form_post
|
|
72
|
+
const code = await page.evaluate(() => {
|
|
73
|
+
const form = document.querySelector('form[action^="melcloudhome://"]');
|
|
74
|
+
if (form) {
|
|
75
|
+
const input = form.querySelector('input[name="code"]');
|
|
76
|
+
return input ? input.value : null;
|
|
77
|
+
}
|
|
63
78
|
return null;
|
|
64
|
-
}
|
|
65
|
-
|
|
66
|
-
const formData = new URLSearchParams({
|
|
67
|
-
_csrf: csrf,
|
|
68
|
-
username: this.user,
|
|
69
|
-
password: this.passwd
|
|
70
79
|
});
|
|
71
80
|
|
|
72
|
-
|
|
73
|
-
headers: {
|
|
74
|
-
'User-Agent': MOBILE_USER_AGENT,
|
|
75
|
-
'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
|
|
76
|
-
'Accept-Language': 'en-US,en;q=0.9',
|
|
77
|
-
'Content-Type': 'application/x-www-form-urlencoded',
|
|
78
|
-
'Content-Length': formData.toString().length,
|
|
79
|
-
'Cookie': cookies.join('; '),
|
|
80
|
-
'Origin': 'https://live-melcloudhome.auth.eu-west-1.amazoncognito.com',
|
|
81
|
-
'Referer': authUrl
|
|
82
|
-
},
|
|
83
|
-
maxRedirects: 0,
|
|
84
|
-
validateStatus: status => [200, 302, 400].includes(status)
|
|
85
|
-
});
|
|
81
|
+
await browser.close();
|
|
86
82
|
|
|
87
|
-
if (
|
|
88
|
-
this.emit('warn', `
|
|
83
|
+
if (code) {
|
|
84
|
+
this.emit('warn', `Authorization code obtained from form_post: ${code}`);
|
|
85
|
+
return code;
|
|
86
|
+
} else {
|
|
87
|
+
this.emit('warn', `Authorization code not found in Puppeteer`);
|
|
89
88
|
return null;
|
|
90
89
|
}
|
|
91
90
|
|
|
92
|
-
// Extract authorization code
|
|
93
|
-
const code = await this.extractCodeFromResponse(
|
|
94
|
-
response.data,
|
|
95
|
-
response.headers,
|
|
96
|
-
async (url) => {
|
|
97
|
-
const r = await this.client.get(url, {
|
|
98
|
-
maxRedirects: 0,
|
|
99
|
-
validateStatus: status => [200, 302, 303].includes(status)
|
|
100
|
-
});
|
|
101
|
-
return this.extractCodeFromResponse(r.data, r.headers, async u => this.extractCodeFromResponse(r.data, r.headers, u));
|
|
102
|
-
}
|
|
103
|
-
);
|
|
104
|
-
|
|
105
|
-
if (code) this.emit('warn', `Authorization code obtained: ${code}`);
|
|
106
|
-
return code || null;
|
|
107
|
-
|
|
108
91
|
} catch (err) {
|
|
109
92
|
this.emit('warn', `loginToMelCloudHome error: ${err}`);
|
|
110
93
|
return null;
|
|
111
94
|
}
|
|
112
95
|
}
|
|
113
96
|
|
|
114
|
-
async extractCodeFromResponse(data, headers, followRedirect) {
|
|
115
|
-
return new Promise(async (resolve, reject) => {
|
|
116
|
-
try {
|
|
117
|
-
const locationHeader = headers['location'] || headers['Location'];
|
|
118
|
-
|
|
119
|
-
// 1️⃣ Location header
|
|
120
|
-
if (locationHeader && locationHeader.startsWith('melcloudhome://')) {
|
|
121
|
-
const match = locationHeader.match(/[?&]code=([^&]+)/);
|
|
122
|
-
if (match) {
|
|
123
|
-
this.emit('warn', `Found code in Location header: ${match[1]}`);
|
|
124
|
-
resolve(match[1]);
|
|
125
|
-
return;
|
|
126
|
-
}
|
|
127
|
-
}
|
|
128
|
-
|
|
129
|
-
// 2️⃣ form_post HTML
|
|
130
|
-
const formCodeMatch = data.match(/name="code"\s+value="([^"]+)"/);
|
|
131
|
-
const formStateMatch = data.match(/name="state"\s+value="([^"]+)"/);
|
|
132
|
-
const formActionMatch = data.match(/action="([^"]+)"/);
|
|
133
|
-
|
|
134
|
-
if (formCodeMatch && formStateMatch && formActionMatch) {
|
|
135
|
-
this.emit('warn', 'Found form_post, submitting...');
|
|
136
|
-
try {
|
|
137
|
-
const code = await this.submitFormPost(formActionMatch[1], formCodeMatch[1], formStateMatch[1]);
|
|
138
|
-
this.emit('warn', `submitFormPost returned code: ${code}`);
|
|
139
|
-
resolve(code);
|
|
140
|
-
return;
|
|
141
|
-
} catch (err) {
|
|
142
|
-
this.emit('warn', `submitFormPost failed: ${err}`);
|
|
143
|
-
reject(err);
|
|
144
|
-
return;
|
|
145
|
-
}
|
|
146
|
-
}
|
|
147
|
-
|
|
148
|
-
// 3️⃣ JS redirect in body
|
|
149
|
-
const bodyCodeMatch = data.match(/melcloudhome:\/\/[^"'\s]*[?&]code=([^&"'\s]+)/);
|
|
150
|
-
if (bodyCodeMatch) {
|
|
151
|
-
this.emit('warn', `Found code in body: ${bodyCodeMatch[1]}`);
|
|
152
|
-
resolve(bodyCodeMatch[1]);
|
|
153
|
-
return;
|
|
154
|
-
}
|
|
155
|
-
|
|
156
|
-
// 4️⃣ Follow redirect
|
|
157
|
-
if (locationHeader && ['301', '302', '303'].includes(headers['status'] || '')) {
|
|
158
|
-
this.emit('warn', `Following redirect to ${locationHeader}`);
|
|
159
|
-
try {
|
|
160
|
-
const code = await followRedirect(locationHeader);
|
|
161
|
-
resolve(code);
|
|
162
|
-
return;
|
|
163
|
-
} catch (err) {
|
|
164
|
-
this.emit('warn', `Follow redirect failed: ${err}`);
|
|
165
|
-
reject(err);
|
|
166
|
-
return;
|
|
167
|
-
}
|
|
168
|
-
}
|
|
169
|
-
|
|
170
|
-
this.emit('warn', 'Authorization code not found in response');
|
|
171
|
-
reject(new Error('Authorization code not found'));
|
|
172
|
-
} catch (err) {
|
|
173
|
-
this.emit('warn', `extractCodeFromResponse error: ${err}`);
|
|
174
|
-
reject(err);
|
|
175
|
-
}
|
|
176
|
-
});
|
|
177
|
-
}
|
|
178
|
-
|
|
179
|
-
async submitFormPost(actionUrl, code, state) {
|
|
180
|
-
const formData = new URLSearchParams({ code, state });
|
|
181
|
-
this.emit('warn', `Submitting form_post to ${actionUrl}`);
|
|
182
|
-
const res = await this.client.post(actionUrl, formData.toString(), {
|
|
183
|
-
headers: { 'Content-Type': 'application/x-www-form-urlencoded' },
|
|
184
|
-
maxRedirects: 0,
|
|
185
|
-
validateStatus: status => [200, 302, 303].includes(status)
|
|
186
|
-
});
|
|
187
|
-
|
|
188
|
-
const location = res.headers['location'];
|
|
189
|
-
if (location) {
|
|
190
|
-
const match = location.match(/[?&]code=([^&]+)/);
|
|
191
|
-
if (match) return match[1];
|
|
192
|
-
}
|
|
193
|
-
|
|
194
|
-
this.emit('warn', 'Code not found after form_post submission');
|
|
195
|
-
throw new Error('Code not found after form_post submission');
|
|
196
|
-
}
|
|
197
97
|
|
|
198
98
|
async getTokens(code, codeVerifier) {
|
|
199
99
|
const tokenData = new URLSearchParams({
|
|
@@ -215,7 +115,6 @@ class MelCloudHomeToken extends EventEmitter {
|
|
|
215
115
|
const tokens = tokenResponse.data;
|
|
216
116
|
this.emit('warn', `Token obtained: ${JSON.stringify(tokens)}`);
|
|
217
117
|
return tokens;
|
|
218
|
-
|
|
219
118
|
} catch (err) {
|
|
220
119
|
throw new Error(`Failed to obtain OAuth token: ${err}`);
|
|
221
120
|
}
|