homebridge-melcloud-control 4.0.0-beta.387 → 4.0.0-beta.389
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 +115 -14
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.389",
|
|
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
|
@@ -9,7 +9,7 @@ const MOBILE_USER_AGENT = 'MonitorAndControl.App.Mobile/35 CFNetwork/3860.100.1
|
|
|
9
9
|
const CLIENT_ID = 'homemobile';
|
|
10
10
|
const REDIRECT_URI = 'melcloudhome://';
|
|
11
11
|
const SCOPE = 'openid profile email offline_access IdentityServerApi';
|
|
12
|
-
const TOKEN_ENDPOINT = 'https://
|
|
12
|
+
const TOKEN_ENDPOINT = 'https://auth.melcloudhome.com/connect/token';
|
|
13
13
|
const AUTHORIZE_ENDPOINT = 'https://auth.melcloudhome.com/connect/authorize';
|
|
14
14
|
|
|
15
15
|
class MelCloudHomeToken extends EventEmitter {
|
|
@@ -71,6 +71,95 @@ class MelCloudHomeToken extends EventEmitter {
|
|
|
71
71
|
}
|
|
72
72
|
}
|
|
73
73
|
|
|
74
|
+
async extractCodeFromResponse(data, headers, followRedirect) {
|
|
75
|
+
return new Promise(async (resolve, reject) => {
|
|
76
|
+
try {
|
|
77
|
+
const locationHeader = headers['location'] || headers['Location'];
|
|
78
|
+
|
|
79
|
+
// 1️⃣ Location header
|
|
80
|
+
if (locationHeader && locationHeader.startsWith('melcloudhome://')) {
|
|
81
|
+
const match = locationHeader.match(/[?&]code=([^&]+)/);
|
|
82
|
+
if (match) {
|
|
83
|
+
this.emit('warn', `Found code in Location header: ${match[1]}`);
|
|
84
|
+
resolve(match[1]);
|
|
85
|
+
return;
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
// 2️⃣ form_post HTML
|
|
90
|
+
const formCodeMatch = data.match(/name="code"\s+value="([^"]+)"/);
|
|
91
|
+
const formStateMatch = data.match(/name="state"\s+value="([^"]+)"/);
|
|
92
|
+
const formActionMatch = data.match(/action="([^"]+)"/);
|
|
93
|
+
|
|
94
|
+
if (formCodeMatch && formStateMatch && formActionMatch) {
|
|
95
|
+
this.emit('warn', 'Found form_post response, submitting to callback endpoint...');
|
|
96
|
+
try {
|
|
97
|
+
const code = await this.submitFormPost(formActionMatch[1], formCodeMatch[1], formStateMatch[1]);
|
|
98
|
+
this.emit('warn', `submitFormPost returned code: ${code}`);
|
|
99
|
+
resolve(code);
|
|
100
|
+
return;
|
|
101
|
+
} catch (err) {
|
|
102
|
+
this.emit('warn', `submitFormPost failed: ${err}`);
|
|
103
|
+
reject(err);
|
|
104
|
+
return;
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
// 3️⃣ JS redirect w body
|
|
109
|
+
const bodyCodeMatch = data.match(/melcloudhome:\/\/[^"'\s]*[?&]code=([^&"'\s]+)/);
|
|
110
|
+
if (bodyCodeMatch) {
|
|
111
|
+
this.emit('warn', `Found code in response body: ${bodyCodeMatch[1]}`);
|
|
112
|
+
resolve(bodyCodeMatch[1]);
|
|
113
|
+
return;
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
// 4️⃣ Follow redirect
|
|
117
|
+
if (locationHeader && ['301', '302', '303'].includes(headers['status'] || '')) {
|
|
118
|
+
this.emit('warn', `Following redirect to ${locationHeader}`);
|
|
119
|
+
try {
|
|
120
|
+
const code = await followRedirect(locationHeader);
|
|
121
|
+
resolve(code);
|
|
122
|
+
return;
|
|
123
|
+
} catch (err) {
|
|
124
|
+
this.emit('warn', `Follow redirect failed: ${err}`);
|
|
125
|
+
reject(err);
|
|
126
|
+
return;
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
this.emit('warn', 'Authorization code not found in response');
|
|
131
|
+
reject(new Error('Authorization code not found'));
|
|
132
|
+
} catch (error) {
|
|
133
|
+
this.emit('warn', `extractCodeFromResponse error: ${error}`);
|
|
134
|
+
reject(error);
|
|
135
|
+
}
|
|
136
|
+
});
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
// submitFormPost w JS
|
|
140
|
+
async submitFormPost(actionUrl, code, state) {
|
|
141
|
+
const formData = new URLSearchParams({ code, state });
|
|
142
|
+
this.emit('warn', `Submitting form_post to ${actionUrl}`);
|
|
143
|
+
const res = await this.client.post(actionUrl, formData.toString(), {
|
|
144
|
+
headers: { 'Content-Type': 'application/x-www-form-urlencoded' },
|
|
145
|
+
maxRedirects: 0,
|
|
146
|
+
validateStatus: status => [200, 302, 303].includes(status),
|
|
147
|
+
});
|
|
148
|
+
|
|
149
|
+
const location = res.headers['location'];
|
|
150
|
+
if (location) {
|
|
151
|
+
const match = location.match(/[?&]code=([^&]+)/);
|
|
152
|
+
if (match) {
|
|
153
|
+
this.emit('warn', `Form post redirect returned code: ${match[1]}`);
|
|
154
|
+
return match[1];
|
|
155
|
+
}
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
this.emit('warn', 'Code not found after form_post submission');
|
|
159
|
+
throw new Error('Code not found after form_post submission');
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
|
|
74
163
|
async loginToMelCloudHome(url) {
|
|
75
164
|
try {
|
|
76
165
|
// Step 1: Pobierz stronę logowania, żeby uzyskać sesję + token _csrf
|
|
@@ -98,6 +187,7 @@ class MelCloudHomeToken extends EventEmitter {
|
|
|
98
187
|
// Step 3: Wyślij POST z danymi logowania
|
|
99
188
|
const response = await this.client.post(url, formData.toString(), {
|
|
100
189
|
headers: {
|
|
190
|
+
'User-Agent': MOBILE_USER_AGENT,
|
|
101
191
|
'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
|
|
102
192
|
'Accept-Language': 'en-US,en;q=0.9',
|
|
103
193
|
'Content-Type': 'application/x-www-form-urlencoded',
|
|
@@ -111,19 +201,30 @@ class MelCloudHomeToken extends EventEmitter {
|
|
|
111
201
|
});
|
|
112
202
|
|
|
113
203
|
if (response.status === 400) if (this.logWarn) this.emit('warn', `Login failed (bad request or invalid credentials: ${response.data}`);
|
|
114
|
-
if (response.status === 302
|
|
115
|
-
//
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
204
|
+
if (response.status === 302 || response.status === 200) {
|
|
205
|
+
// Zamiast szukać ręcznie, używamy extractCodeFromResponse
|
|
206
|
+
try {
|
|
207
|
+
const code = await this.extractCodeFromResponse(
|
|
208
|
+
response.data, // treść odpowiedzi (HTML lub body)
|
|
209
|
+
response.headers, // nagłówki
|
|
210
|
+
async (url) => { // followRedirect
|
|
211
|
+
const r = await this.client.get(url, {
|
|
212
|
+
maxRedirects: 0,
|
|
213
|
+
validateStatus: status => [200, 302, 303].includes(status)
|
|
214
|
+
});
|
|
215
|
+
return this.extractCodeFromResponse(r.data, r.headers, async (u) => this.extractCodeFromResponse(r.data, r.headers, followRedirect));
|
|
216
|
+
}
|
|
217
|
+
);
|
|
218
|
+
|
|
219
|
+
if (code) {
|
|
220
|
+
if (this.logWarn) this.emit('warn', `Authorization code obtained: ${code}`);
|
|
221
|
+
return code;
|
|
222
|
+
} else {
|
|
223
|
+
if (this.logWarn) this.emit('warn', 'Authorization code not found in response');
|
|
224
|
+
return null;
|
|
225
|
+
}
|
|
226
|
+
} catch (err) {
|
|
227
|
+
if (this.logWarn) this.emit('warn', `extractCodeFromResponse error: ${err}`);
|
|
127
228
|
return null;
|
|
128
229
|
}
|
|
129
230
|
}
|