homebridge-melcloud-control 4.0.0-beta.388 → 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 +113 -13
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
|
@@ -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
|
|
@@ -112,19 +201,30 @@ class MelCloudHomeToken extends EventEmitter {
|
|
|
112
201
|
});
|
|
113
202
|
|
|
114
203
|
if (response.status === 400) if (this.logWarn) this.emit('warn', `Login failed (bad request or invalid credentials: ${response.data}`);
|
|
115
|
-
if (response.status === 302
|
|
116
|
-
//
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
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}`);
|
|
128
228
|
return null;
|
|
129
229
|
}
|
|
130
230
|
}
|