iobroker.google-sharedlocations2 0.3.1 → 0.3.2

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/README.md CHANGED
@@ -27,6 +27,9 @@ Copyright and trademark of Google are property of Google.
27
27
  Placeholder for the next version (at the beginning of the line):
28
28
  ### **WORK IN PROGRESS**
29
29
  -->
30
+ ### 0.3.2 (2026-02-09)
31
+ * (Garfonso) refresh with browser ignores cookies from adapter
32
+
30
33
  ### 0.3.1 (2026-02-09)
31
34
  * (Garfonso) improved logging during login.
32
35
  * (Garfonso) handle situation where browser logs in with his cookie, so we only need to read the cookie.
package/io-package.json CHANGED
@@ -1,8 +1,21 @@
1
1
  {
2
2
  "common": {
3
3
  "name": "google-sharedlocations2",
4
- "version": "0.3.1",
4
+ "version": "0.3.2",
5
5
  "news": {
6
+ "0.3.2": {
7
+ "en": "refresh with browser ignores cookies from adapter",
8
+ "de": "mit browser aktualisieren ignoriert cookies vom adapter",
9
+ "ru": "обновление с браузером игнорирует файлы cookie от адаптера",
10
+ "pt": "atualizar com o navegador ignora cookies do adaptador",
11
+ "nl": "vernieuwen met browser negeert cookies van adapter",
12
+ "fr": "rafraîchir avec le navigateur ignore les cookies de l'adaptateur",
13
+ "it": "aggiornare con il browser ignora i cookie dall'adattatore",
14
+ "es": "refresco con el navegador ignora las cookies del adaptador",
15
+ "pl": "odświeżenie z przeglądarką ignoruje pliki cookie z adaptera",
16
+ "uk": "оновлення з браузером ігнорує файли cookie з адаптера",
17
+ "zh-cn": "用浏览器刷新忽略适配器的 cookie"
18
+ },
6
19
  "0.3.1": {
7
20
  "en": "improved logging during login.\nhandle situation where browser logs in with his cookie, so we only need to read the cookie.",
8
21
  "de": "verbessertes logging während des logins.\nkorrektes bearbeiten der situation, in der sich browser mit seinem cookie einloggt, wir müssen nur das cookie lesen.",
@@ -80,19 +93,6 @@
80
93
  "pl": "zapobiec logowaniu, jeśli nie jest ustawiona nazwa użytkownika i hasło\nbadania naprawcze",
81
94
  "uk": "заборонити логін, якщо не встановлено ім’я користувача та пароль\nфіксувати тести",
82
95
  "zh-cn": "如果没有设置用户名和密码, 请防止登录\n固定测试"
83
- },
84
- "0.0.2": {
85
- "en": "store password encrypted",
86
- "de": "speicher passwort verschlüsselt",
87
- "ru": "зашифрованный пароль",
88
- "pt": "guardar a senha encriptada",
89
- "nl": "wachtwoord versleuteld opslaan",
90
- "fr": "crypté du mot de passe de stockage",
91
- "it": "negozio password crittografato",
92
- "es": "almacenar contraseña cifrada",
93
- "pl": "przechowywać zaszyfrowane hasło",
94
- "uk": "зашифрований паролем",
95
- "zh-cn": "存储加密密码"
96
96
  }
97
97
  },
98
98
  "titleLang": {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "iobroker.google-sharedlocations2",
3
- "version": "0.3.1",
3
+ "version": "0.3.2",
4
4
  "description": "Share your location with iobroker via google maps.",
5
5
  "author": {
6
6
  "name": "Garfonso",
package/src/lib/Cookie.ts CHANGED
@@ -169,12 +169,15 @@ export class Cookie {
169
169
  }
170
170
  }
171
171
 
172
+ // seems puppeteer sets expires to -1 if not present.
172
173
  this.cookies
173
- .filter(c => c.expires && c.expires < Date.now() / 1000)
174
+ .filter(c => c.expires && c.expires > 0 && c.expires < Date.now() / 1000)
174
175
  .forEach(c =>
175
- this.log.debug(`Cookie ${c.name} expired at ${new Date(c.expires! * 1000).toISOString()}`),
176
+ this.log.debug(
177
+ `Cookie ${c.name} expired at ${new Date(c.expires! * 1000).toISOString()} - ${c.expires}`,
178
+ ),
176
179
  );
177
- this.cookies = this.cookies.filter(c => !c.expires || c.expires > Date.now() / 1000); //remove expired cookies
180
+ this.cookies = this.cookies.filter(c => !c.expires || c.expires < 0 || c.expires > Date.now() / 1000); //remove expired cookies
178
181
 
179
182
  this.log?.debug(`Cookie updated. Length: ${oldLength} -> ${this.cookies.length}`);
180
183
  return this.storeCookie();
@@ -313,7 +316,7 @@ export class Cookie {
313
316
  *
314
317
  * @returns true if refresh was successful
315
318
  */
316
- async refreshCookieWithBrowser(): Promise<boolean> {
319
+ async refreshCookieWithBrowser(withCookies: boolean = false): Promise<boolean> {
317
320
  if (this.browser) {
318
321
  this.log.info('Seems we are already trying to log in. Aborting new login attempt.');
319
322
  return false;
@@ -323,36 +326,52 @@ export class Cookie {
323
326
  const page = await this.startBrowser();
324
327
  if (!page) {
325
328
  this.log.error('Could not start browser for cookie refresh.');
329
+ await this.cleanUp();
326
330
  return false;
327
331
  }
328
332
 
329
- const cookieArray = [...this.cookies];
330
- // somehow we stored wrong cookies... :-/ Try to clean up here.
331
- while (cookieArray.length > 0) {
332
- try {
333
- //await page.setCookie(...cookieArray);
334
- await this.browser!.setCookie(...cookieArray);
335
- break;
336
- } catch (e) {
337
- this.log.error(`Error setting cookies in browser: ${(e as Error).message}, trying again...`);
338
- const cookie = cookieArray.pop(); //remove last cookie and try again, maybe some cookies are not valid for puppeteer or something.
339
- console.log('Removed cookie:', cookie);
333
+ if (withCookies) {
334
+ const cookieArray = [...this.cookies];
335
+ // somehow we stored wrong cookies... :-/ Try to clean up here.
336
+ while (cookieArray.length > 0) {
337
+ try {
338
+ //await page.setCookie(...cookieArray);
339
+ await this.browser!.setCookie(...cookieArray);
340
+ break;
341
+ } catch (e) {
342
+ this.log.error(`Error setting cookies in browser: ${(e as Error).message}, trying again...`);
343
+ const cookie = cookieArray.pop(); //remove last cookie and try again, maybe some cookies are not valid for puppeteer or something.
344
+ console.log('Removed cookie:', cookie);
345
+ }
340
346
  }
341
347
  }
342
348
 
343
349
  try {
344
- await page.goto('https://www.google.com/maps', { waitUntil: 'networkidle2', timeout: 60000 });
350
+ this.log.debug('Loading Google login page to refresh cookie.');
351
+ await page.goto(
352
+ 'https://accounts.google.com/ServiceLogin?hl=de&continue=https://www.google.com/maps&gae=cb-eomtm',
353
+ { waitUntil: 'networkidle2', timeout: 60000 },
354
+ );
355
+ this.log.debug(
356
+ 'Waiting for page to load, currently waiting fixed 5 seconds, because network never gets idle for maps',
357
+ );
345
358
  await new Promise(r => setTimeout(r, 5000));
346
-
347
- await this.sendRequest();
348
- await this.getCookiesFromPage(page);
349
- return true;
359
+ if (!page.url().includes('accounts.google.com')) {
360
+ this.log.debug('Browser logged in, refreshing cookie.');
361
+ await this.getCookiesFromPage(page);
362
+ const results = await this.sendRequest();
363
+ if (results && results.length > 0) {
364
+ await this.cleanUp();
365
+ return true;
366
+ }
367
+ }
350
368
  } catch (e) {
351
369
  this.log.error(
352
370
  `Error during cookie refresh: ${(e as Error).message}, ${e instanceof Error ? e.stack : ''}`,
353
371
  );
354
- return false;
355
372
  }
373
+ await this.cleanUp();
374
+ return false;
356
375
  }
357
376
 
358
377
  /**
@@ -361,11 +380,16 @@ export class Cookie {
361
380
  async loginToGetNewCookies(): Promise<boolean> {
362
381
  let currentStep;
363
382
  try {
364
- if (this.isValid()) {
383
+ // try to refresh cookie from browser session first:
384
+ let result = await this.refreshCookieWithBrowser();
385
+ if (result) {
386
+ this.log.info('Cookie refresh successful, no need to login again.');
387
+ return true;
388
+ } else if (this.isValid()) {
365
389
  this.log.info('Current cookie seems valid, trying refresh.');
366
- await this.refreshCookieWithBrowser();
367
- if (this.isValid()) {
368
- this.log.info('Cookie refresh successful, no need to login again.');
390
+ result = await this.refreshCookieWithBrowser(true);
391
+ if (result) {
392
+ this.log.info('Cookie refresh with existing cookies successful, no need to login again.');
369
393
  return true;
370
394
  }
371
395
  }
@@ -384,6 +408,7 @@ export class Cookie {
384
408
  const page = await this.startBrowser();
385
409
  if (!page) {
386
410
  this.log.error('Could not start browser for login.');
411
+ await this.cleanUp();
387
412
  return false;
388
413
  }
389
414
 
@@ -405,7 +430,13 @@ export class Cookie {
405
430
  if (!page.url().includes('accounts.google.com')) {
406
431
  logDebug('Already logged in, refreshing cookie.');
407
432
  await this.getCookiesFromPage(page);
408
- return true;
433
+ await this.cleanUp();
434
+ const results = await this.sendRequest();
435
+ if (results && results.length > 0) {
436
+ this.log.info('Login successful with existing session, no need to fill in credentials.');
437
+ return true;
438
+ }
439
+ return false;
409
440
  }
410
441
 
411
442
  logDebug('filling in username.');
@@ -432,19 +463,18 @@ export class Cookie {
432
463
  await page.goto('https://www.google.com/maps');
433
464
  logDebug('getting cookies.');
434
465
  await this.getCookiesFromPage(page);
435
- return true;
466
+ await this.cleanUp();
467
+ const results = await this.sendRequest();
468
+ if (results && results.length > 0) {
469
+ this.log.info('Login successful with existing session, no need to fill in credentials.');
470
+ return true;
471
+ }
472
+ return false;
436
473
  } catch (e) {
437
474
  this.log.error(`Error in puppeteer: ${(e as Error).message}`);
438
475
  this.log.error(`The step puppeteer failed was: ${currentStep}`);
439
476
  // try to close browser if open
440
- if (this.browser) {
441
- try {
442
- await this.browser.close();
443
- } catch {
444
- /* ignore */
445
- }
446
- }
447
- this.browser = null;
477
+ await this.cleanUp();
448
478
  }
449
479
  return false;
450
480
  }
@@ -453,9 +483,13 @@ export class Cookie {
453
483
  * Clean up on unload.
454
484
  */
455
485
  async cleanUp(): Promise<void> {
456
- if (this.browser) {
457
- await this.browser.close();
458
- this.browser = null;
486
+ try {
487
+ if (this.browser) {
488
+ await this.browser.close();
489
+ this.browser = null;
490
+ }
491
+ } catch (e) {
492
+ this.log.error(`Error closing browser: ${(e as Error).message} - ${e instanceof Error ? e.stack : ''}`);
459
493
  }
460
494
  }
461
495