homebridge-melcloud-control 4.0.0-beta.354 → 4.0.0-beta.356

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "displayName": "MELCloud Control",
3
3
  "name": "homebridge-melcloud-control",
4
- "version": "4.0.0-beta.354",
4
+ "version": "4.0.0-beta.356",
5
5
  "description": "Homebridge plugin to control Mitsubishi Air Conditioner, Heat Pump and Energy Recovery Ventilation.",
6
6
  "license": "MIT",
7
7
  "author": "grzegorz914",
@@ -111,81 +111,54 @@ class MelCloudHomeToken extends EventEmitter {
111
111
 
112
112
  async loginToMelCloudHome(url) {
113
113
  try {
114
- // Step 1: Pobierz stronę logowania, żeby uzyskać sesję + token _csrf
115
- const getResp = await axios.get(url, {
116
- headers: {
117
- 'User-Agent': MOBILE_USER_AGENT,
118
- 'Accept': 'text/html',
119
- },
120
- withCredentials: true,
114
+
115
+ const jar = new CookieJar();
116
+ const client = wrapper(axios.create({ jar, withCredentials: true }));
117
+ client.defaults.headers['User-Agent'] = MOBILE_USER_AGENT;
118
+
119
+ // --- Krok 1: GET strony logowania ---
120
+ const getResp = await client.get(url, {
121
+ headers: { 'User-Agent': MOBILE_USER_AGENT }
121
122
  });
122
123
 
123
- const cookies = getResp.headers['set-cookie'] || [];
124
124
  const dom = new JSDOM(getResp.data);
125
- const csrf = dom.window.document.querySelector('input[name="_csrf"]')?.value;
126
- if (!csrf) {
127
- if (this.logWarn) this.emit('warn', `CSRF token not found`);
128
- return null;
129
- }
125
+ const csrf = dom.window.document.querySelector('input[name="_csrf"]')?.getAttribute('value');
126
+ if (!csrf) throw new Error('CSRF token not found');
130
127
 
131
- // Step 2: Przygotuj dane logowania
128
+ // --- Krok 2: POST login ---
132
129
  const formData = new URLSearchParams({
133
130
  _csrf: csrf,
134
131
  username: this.user,
135
- password: this.passwd,
136
- });
132
+ password: this.passwd
133
+ }).toString();
137
134
 
138
- if (this.logWarn) this.emit('warn', `Redirect URL: ${url}`);
139
-
140
- // Step 3: Wyślij POST z danymi logowania
141
- const response = await axios.post(url, formData.toString(), {
135
+ const postResp = await client.post(url, formData, {
142
136
  headers: {
143
137
  'User-Agent': MOBILE_USER_AGENT,
144
- 'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
145
- 'Accept-Language': 'en-US,en;q=0.9',
146
138
  'Content-Type': 'application/x-www-form-urlencoded',
147
- 'Content-Length': formData.toString().length,
148
- 'Cookie': cookies,
149
139
  'Origin': 'https://live-melcloudhome.auth.eu-west-1.amazoncognito.com',
150
140
  'Referer': url,
151
141
  },
152
142
  maxRedirects: 0,
153
- validateStatus: status => [200, 302, 400].includes(status),
143
+ validateStatus: status => [200, 302, 400].includes(status)
154
144
  });
155
145
 
146
+ if (postResp.status === 400) throw new Error('Login failed');
156
147
 
157
- if (response.status === 302 && response.headers.location) {
158
- // Login success mamy redirect z "code="
159
- const redirectUrl = response.headers.location;
160
- if (this.logWarn) this.emit('warn', `Redirect URL: ${redirectUrl}`);
161
-
162
- const getResp = await axios.get('https://melcloudhome.com/dashboard', {
163
- headers: {
164
- 'User-Agent': MOBILE_USER_AGENT,
165
- 'Accept': 'text/html',
166
- },
167
- withCredentials: true,
168
- });
169
-
170
- // --- Krok 3: GET dashboard po zalogowaniu ---
171
- const dashboardResp = await axios.get('https://melcloudhome.com/dashboard', {
172
- headers: { 'User-Agent': MOBILE_USER_AGENT }
173
- });
174
-
175
- // --- Krok 4: Pobierz cookies z dashboard ---
176
- const dashboardCookies = await jar.getCookies('https://melcloudhome.com/dashboard');
177
- // Możemy zwrócić jako string do użycia w nagłówku:
178
- const cookieHeader = dashboardCookies.map(c => `${c.key}=${c.value}`).join('; ');
148
+ // --- Krok 3: GET dashboard po zalogowaniu ---
149
+ const dashboardResp = await client.get('https://melcloudhome.com/dashboard', {
150
+ headers: { 'User-Agent': MOBILE_USER_AGENT }
151
+ });
179
152
 
180
- const cookies = dashboardCookies.map(c => `${c.key}=${c.value}`);
153
+ // --- Krok 4: Pobierz cookies z dashboard ---
154
+ const dashboardCookies = await jar.getCookies('https://melcloudhome.com/dashboard');
155
+ // Możemy zwrócić jako string do użycia w nagłówku:
156
+ const cookieHeader = dashboardCookies.map(c => `${c.key}=${c.value}`).join('; ');
181
157
 
182
- if (this.logWarn) this.emit('warn', `Response: ${dashboardResp.data}, ${dashboardResp.headers}, ${cookies}`);
183
- return cookies
184
- }
158
+ this.emit('debug', `Cookies ${cookieHeader}`);
185
159
 
186
- if (response.status === 400) if (this.logWarn) this.emit('warn', `Login failed (bad request or invalid credentials: ${response.data}`);
160
+ return dashboardCookies.map(c => `${c.key}=${c.value}`);
187
161
 
188
- return null;
189
162
  } catch (error) {
190
163
  throw new Error(`Login to MELCloud Home error: ${error}`);
191
164
  }