gologin 2.0.14 → 2.0.16

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,6 +1,6 @@
1
1
  {
2
2
  "name": "gologin",
3
- "version": "2.0.14",
3
+ "version": "2.0.16",
4
4
  "description": "A high-level API to control Orbita browser over GoLogin API",
5
5
  "main": "./src/gologin.js",
6
6
  "repository": {
@@ -1,5 +1,9 @@
1
1
  import { open } from 'sqlite';
2
2
  import sqlite3 from 'sqlite3';
3
+ import { promises as fsPromises } from 'fs';
4
+ import { join } from 'path';
5
+
6
+ const { access } = fsPromises;
3
7
  const { Database, OPEN_READONLY } = sqlite3;
4
8
 
5
9
  const MAX_SQLITE_VARIABLES = 76;
@@ -171,3 +175,15 @@ export const chunk = (arr, chunkSize = 1, cache = []) => {
171
175
 
172
176
  return cache;
173
177
  }
178
+
179
+ export const getCookiesFilePath = async (profileId, tmpdir) => {
180
+ const baseCookiesFilePath = join(tmpdir, `gologin_profile_${profileId}`, 'Default', 'Cookies');
181
+ const bypassCookiesFilePath = join(tmpdir, `gologin_profile_${profileId}`, 'Default', 'Network', 'Cookies');
182
+
183
+ return access(baseCookiesFilePath)
184
+ .then(() => baseCookiesFilePath)
185
+ .catch(() => access(bypassCookiesFilePath)
186
+ .then(() => bypassCookiesFilePath)
187
+ .catch(() => baseCookiesFilePath)
188
+ );
189
+ }
package/src/gologin.js CHANGED
@@ -1,5 +1,5 @@
1
1
  import { execFile, spawn } from 'child_process';
2
- import debug from 'debug';
2
+ import debugDefault from 'debug';
3
3
  import decompress from 'decompress';
4
4
  import decompressUnzip from 'decompress-unzip';
5
5
  import { existsSync, mkdirSync, promises as _promises } from 'fs';
@@ -17,7 +17,12 @@ import BrowserChecker from './browser/browser-checker.js';
17
17
  import {
18
18
  composeFonts, downloadCookies, setExtPathsAndRemoveDeleted, setOriginalExtPaths, uploadCookies,
19
19
  } from './browser/browser-user-data-manager.js';
20
- import { getChunckedInsertValues, getDB, loadCookiesFromFile } from './cookies/cookies-manager.js';
20
+ import {
21
+ getChunckedInsertValues,
22
+ getDB,
23
+ loadCookiesFromFile,
24
+ getCookiesFilePath,
25
+ } from './cookies/cookies-manager.js';
21
26
  import ExtensionsManager from './extensions/extensions-manager.js';
22
27
  import { archiveProfile } from './profile/profile-archiver.js';
23
28
  import { checkAutoLang } from './utils/browser.js';
@@ -31,6 +36,7 @@ const OS_PLATFORM = process.platform;
31
36
 
32
37
  // process.env["NODE_TLS_REJECT_UNAUTHORIZED"] = 0;
33
38
 
39
+ const debug = debugDefault('gologin');
34
40
  const delay = (time) => new Promise((resolve) => setTimeout(resolve, time));
35
41
 
36
42
  export class GoLogin {
@@ -53,11 +59,16 @@ export class GoLogin {
53
59
  this.waitWebsocket = false;
54
60
  }
55
61
 
62
+ this.isNewCloudBrowser = true;
63
+ if (options.isNewCloudBrowser === false) {
64
+ this.isNewCloudBrowser = false;
65
+ }
66
+
56
67
  this.tmpdir = tmpdir();
57
68
  this.autoUpdateBrowser = !!options.autoUpdateBrowser;
58
69
  this.browserChecker = new BrowserChecker(options.skipOrbitaHashChecking);
59
70
  this.uploadCookiesToServer = options.uploadCookiesToServer || false;
60
- this.writeCookesFromServer = options.writeCookesFromServer;
71
+ this.writeCookiesFromServer = options.writeCookiesFromServer;
61
72
  this.remote_debugging_port = options.remote_debugging_port || 0;
62
73
  this.timezone = options.timezone;
63
74
  this.extensionPathsToInstall = [];
@@ -71,7 +82,6 @@ export class GoLogin {
71
82
  }
72
83
  }
73
84
 
74
- this.cookiesFilePath = join(this.tmpdir, `gologin_profile_${this.profile_id}`, 'Default', 'Network', 'Cookies');
75
85
  this.profile_zip_path = join(this.tmpdir, `gologin_${this.profile_id}.zip`);
76
86
  this.bookmarksFilePath = join(this.tmpdir, `gologin_profile_${this.profile_id}`, 'Default', 'Bookmarks');
77
87
  debug('INIT GOLOGIN', this.profile_id);
@@ -83,7 +93,7 @@ export class GoLogin {
83
93
 
84
94
  async setProfileId(profile_id) {
85
95
  this.profile_id = profile_id;
86
- this.cookiesFilePath = join(this.tmpdir, `gologin_profile_${this.profile_id}`, 'Default', 'Network', 'Cookies');
96
+ this.cookiesFilePath = await getCookiesFilePath(profile_id, this.tmpdir);
87
97
  this.profile_zip_path = join(this.tmpdir, `gologin_${this.profile_id}.zip`);
88
98
  }
89
99
 
@@ -582,8 +592,9 @@ export class GoLogin {
582
592
  debug(`Writing profile for screenWidth ${profilePath}`, JSON.stringify(gologin));
583
593
  gologin.screenWidth = this.resolution.width;
584
594
  gologin.screenHeight = this.resolution.height;
585
- debug('writeCookesFromServer', this.writeCookesFromServer);
586
- if (this.writeCookesFromServer) {
595
+ debug('writeCookiesFromServer', this.writeCookiesFromServer);
596
+ this.cookiesFilePath = await getCookiesFilePath(this.profile_id, this.tmpdir);
597
+ if (this.writeCookiesFromServer) {
587
598
  await this.writeCookiesToFile();
588
599
  }
589
600
 
@@ -1207,7 +1218,7 @@ export class GoLogin {
1207
1218
  });
1208
1219
 
1209
1220
  debug('update profile', profile);
1210
- const response = await requests.put(`https://api.gologin.com/browser/${options.id}`,{
1221
+ const response = await requests.put(`${API_URL}/browser/${options.id}`,{
1211
1222
  json: profile,
1212
1223
  headers: {
1213
1224
  'Authorization': `Bearer ${this.access_token}`,
@@ -1280,26 +1291,29 @@ export class GoLogin {
1280
1291
 
1281
1292
  async writeCookiesToFile() {
1282
1293
  const cookies = await this.getCookies(this.profile_id);
1283
- if (!cookies.length) {
1284
- return;
1285
- }
1286
-
1287
1294
  const resultCookies = cookies.map((el) => ({ ...el, value: Buffer.from(el.value) }));
1288
1295
 
1289
1296
  let db;
1290
1297
  try {
1291
1298
  db = await getDB(this.cookiesFilePath, false);
1292
- const chunckInsertValues = getChunckedInsertValues(resultCookies);
1293
-
1294
- for (const [query, queryParams] of chunckInsertValues) {
1299
+ if (resultCookies.length) {
1300
+ const chunckInsertValues = getChunckedInsertValues(resultCookies);
1301
+
1302
+ for (const [query, queryParams] of chunckInsertValues) {
1303
+ const insertStmt = await db.prepare(query);
1304
+ await insertStmt.run(queryParams);
1305
+ await insertStmt.finalize();
1306
+ }
1307
+ } else {
1308
+ const query = 'delete from cookies';
1295
1309
  const insertStmt = await db.prepare(query);
1296
- await insertStmt.run(queryParams);
1310
+ await insertStmt.run();
1297
1311
  await insertStmt.finalize();
1298
1312
  }
1299
1313
  } catch (error) {
1300
1314
  console.log(error.message);
1301
1315
  } finally {
1302
- await db && db.close();
1316
+ db && await db.close();
1303
1317
  }
1304
1318
  }
1305
1319
 
@@ -1365,9 +1379,9 @@ export class GoLogin {
1365
1379
  await this.stopAndCommit(opts, true);
1366
1380
  }
1367
1381
 
1368
- async waitDebuggingUrl(delay_ms, try_count=0) {
1382
+ async waitDebuggingUrl(delay_ms, try_count=0, remoteOrbitaUrl) {
1369
1383
  await delay(delay_ms);
1370
- const url = `https://${this.profile_id}.orbita.gologin.com/json/version`;
1384
+ const url = `${remoteOrbitaUrl}/json/version`;
1371
1385
  console.log('try_count=', try_count, 'url=', url);
1372
1386
  const response = await requests.get(url);
1373
1387
  let wsUrl = '';
@@ -1382,13 +1396,14 @@ export class GoLogin {
1382
1396
  wsUrl = parsedBody.webSocketDebuggerUrl;
1383
1397
  } catch (e) {
1384
1398
  if (try_count < 3) {
1385
- return this.waitDebuggingUrl(delay_ms, try_count+1);
1399
+ return this.waitDebuggingUrl(delay_ms, try_count + 1, remoteOrbitaUrl);
1386
1400
  }
1387
1401
 
1388
1402
  return { 'status': 'failure', wsUrl, 'message': 'Check proxy settings', 'profile_id': this.profile_id };
1389
1403
  }
1390
1404
 
1391
- wsUrl = wsUrl.replace('ws://', 'wss://').replace('127.0.0.1', `${this.profile_id}.orbita.gologin.com`);
1405
+ const remoteOrbitaUrlWithoutProtocol = remoteOrbitaUrl.replace('https://', '');
1406
+ wsUrl = wsUrl.replace('ws://', 'wss://').replace('127.0.0.1', remoteOrbitaUrlWithoutProtocol);
1392
1407
 
1393
1408
  return wsUrl;
1394
1409
  }
@@ -1405,10 +1420,9 @@ export class GoLogin {
1405
1420
  // if (profileResponse.body === 'ok') {
1406
1421
  const profile = await this.getProfile();
1407
1422
 
1408
- const profileResponse = await requests.post(`https://api.gologin.com/browser/${this.profile_id}/web`, {
1409
- headers: {
1410
- 'Authorization': `Bearer ${this.access_token}`,
1411
- },
1423
+ const profileResponse = await requests.post(`${API_URL}/browser/${this.profile_id}/web`, {
1424
+ headers: { 'Authorization': `Bearer ${this.access_token}` },
1425
+ json: { isNewCloudBrowser: this.isNewCloudBrowser },
1412
1426
  });
1413
1427
 
1414
1428
  debug('profileResponse', profileResponse.statusCode, profileResponse.body);
@@ -1417,6 +1431,15 @@ export class GoLogin {
1417
1431
  throw new Error('invalid token');
1418
1432
  }
1419
1433
 
1434
+ let remoteOrbitaUrl = `https://${this.profile_id}.orbita.gologin.com`;
1435
+ if (this.isNewCloudBrowser) {
1436
+ if (!profileResponse.body.remoteOrbitaUrl) {
1437
+ throw new Error('Couldn\' start the remote browser');
1438
+ }
1439
+
1440
+ remoteOrbitaUrl = profileResponse.body.remoteOrbitaUrl;
1441
+ }
1442
+
1420
1443
  const { navigator = {}, fonts, os: profileOs } = profile;
1421
1444
  this.fontsMasking = fonts?.enableMasking;
1422
1445
  this.profileOs = profileOs;
@@ -1439,7 +1462,7 @@ export class GoLogin {
1439
1462
  height: parseInt(screenHeight, 10),
1440
1463
  };
1441
1464
 
1442
- const wsUrl = await this.waitDebuggingUrl(delay_ms);
1465
+ const wsUrl = await this.waitDebuggingUrl(delay_ms, 0, remoteOrbitaUrl);
1443
1466
  if (wsUrl !== '') {
1444
1467
  return { 'status': 'success', wsUrl };
1445
1468
  }
@@ -1449,10 +1472,8 @@ export class GoLogin {
1449
1472
 
1450
1473
  async stopRemote() {
1451
1474
  debug(`stopRemote ${this.profile_id}`);
1452
- const profileResponse = await requests.delete(`https://api.gologin.com/browser/${this.profile_id}/web`, {
1453
- headers: {
1454
- 'Authorization': `Bearer ${this.access_token}`,
1455
- },
1475
+ const profileResponse = await requests.delete(`${API_URL}/browser/${this.profile_id}/web?isNewCloudBrowser=${this.isNewCloudBrowser}`, {
1476
+ headers: { 'Authorization': `Bearer ${this.access_token}` },
1456
1477
  });
1457
1478
 
1458
1479
  console.log(`stopRemote ${profileResponse.body}`);