-
-
Notifications
You must be signed in to change notification settings - Fork 72
/
browser.ts
606 lines (508 loc) · 16.3 KB
/
browser.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
import * as child_process from "child_process";
import fs from "fs";
import { pipeline } from "node:stream/promises";
import { Readable } from "node:stream";
import os from "os";
import path from "path";
import { LogContext, logger } from "./logger.js";
import { initStorage } from "./storage.js";
import type { ServiceWorkerOpt } from "./constants.js";
import puppeteer, {
Frame,
HTTPRequest,
Page,
PuppeteerLaunchOptions,
Viewport,
} from "puppeteer-core";
import { CDPSession, Target, Browser as PptrBrowser } from "puppeteer-core";
import { Recorder } from "./recorder.js";
type BtrixChromeOpts = {
proxy?: boolean;
userAgent?: string | null;
extraArgs?: string[];
};
type LaunchOpts = {
profileUrl: string;
chromeOptions: BtrixChromeOpts;
signals: boolean;
headless: boolean;
// TODO: Fix this the next time the file is edited.
// eslint-disable-next-line @typescript-eslint/no-explicit-any
emulateDevice?: Record<string, any>;
ondisconnect?: ((err: unknown) => NonNullable<unknown>) | null;
swOpt?: ServiceWorkerOpt;
recording: boolean;
};
// ==================================================================
export class Browser {
profileDir: string;
customProfile = false;
// TODO: Fix this the next time the file is edited.
// eslint-disable-next-line @typescript-eslint/no-explicit-any
emulateDevice: Record<string, any> | null = null;
browser?: PptrBrowser | null = null;
firstCDP: CDPSession | null = null;
recorders: Recorder[] = [];
swOpt?: ServiceWorkerOpt = "disabled";
constructor() {
this.profileDir = fs.mkdtempSync(path.join(os.tmpdir(), "profile-"));
}
async launch({
profileUrl,
chromeOptions,
signals = false,
headless = false,
emulateDevice = {},
swOpt = "disabled",
ondisconnect = null,
recording = true,
}: LaunchOpts) {
if (this.isLaunched()) {
return;
}
if (profileUrl) {
this.customProfile = await this.loadProfile(profileUrl);
}
this.swOpt = swOpt;
this.emulateDevice = emulateDevice;
const args = this.chromeArgs(chromeOptions);
if (recording) {
args.push("--disable-site-isolation-trials");
}
let defaultViewport = null;
if (process.env.GEOMETRY) {
const geom = process.env.GEOMETRY.split("x");
defaultViewport = { width: Number(geom[0]), height: Number(geom[1]) };
}
const launchOpts: PuppeteerLaunchOptions = {
args,
headless,
executablePath: this.getBrowserExe(),
ignoreDefaultArgs: ["--enable-automation", "--hide-scrollbars"],
ignoreHTTPSErrors: true,
handleSIGHUP: signals,
handleSIGINT: signals,
handleSIGTERM: signals,
protocolTimeout: 0,
defaultViewport,
waitForInitialPage: false,
userDataDir: this.profileDir,
targetFilter: recording
? undefined
: (target) => this.targetFilter(target),
};
await this._init(launchOpts, ondisconnect, recording);
}
targetFilter(target: Target) {
const attach = !(!target.url() && target.type() === "other");
logger.debug(
"Target Filter",
{ url: target.url(), type: target.type(), attach },
"browser",
);
return attach;
}
async setupPage({ page }: { page: Page; cdp: CDPSession }) {
await this.addInitScript(
page,
'Object.defineProperty(navigator, "webdriver", {value: false});',
);
switch (this.swOpt) {
case "disabled":
logger.debug("Service Workers: always disabled", {}, "browser");
await page.setBypassServiceWorker(true);
break;
case "disabled-if-profile":
if (this.customProfile) {
logger.debug(
"Service Workers: disabled since using profile",
{},
"browser",
);
await page.setBypassServiceWorker(true);
}
break;
case "enabled":
logger.debug("Service Workers: always enabled", {}, "browser");
break;
}
}
async loadProfile(profileFilename: string): Promise<boolean> {
const targetFilename = "/tmp/profile.tar.gz";
if (
profileFilename &&
(profileFilename.startsWith("http:") ||
profileFilename.startsWith("https:"))
) {
logger.info(
`Downloading ${profileFilename} to ${targetFilename}`,
{},
"browser",
);
const resp = await fetch(profileFilename);
await pipeline(
// TODO: Fix this the next time the file is edited.
// eslint-disable-next-line @typescript-eslint/no-explicit-any
Readable.fromWeb(resp.body as any),
fs.createWriteStream(targetFilename),
);
profileFilename = targetFilename;
} else if (profileFilename && profileFilename.startsWith("@")) {
const storage = initStorage();
if (!storage) {
logger.fatal(
"Profile specified relative to s3 storage, but no S3 storage defined",
);
return false;
}
await storage.downloadFile(profileFilename.slice(1), targetFilename);
profileFilename = targetFilename;
}
if (profileFilename) {
try {
child_process.execSync("tar xvfz " + profileFilename, {
cwd: this.profileDir,
});
return true;
} catch (e) {
logger.error(`Profile filename ${profileFilename} not a valid tar.gz`);
}
}
return false;
}
saveProfile(profileFilename: string) {
child_process.execFileSync("tar", ["cvfz", profileFilename, "./"], {
cwd: this.profileDir,
});
}
chromeArgs({
proxy = true,
userAgent = null,
extraArgs = [],
}: BtrixChromeOpts) {
// Chrome Flags, including proxy server
const args = [
// eslint-disable-next-line no-use-before-define
...defaultArgs,
...(process.env.CHROME_FLAGS ?? "").split(" ").filter(Boolean),
//"--no-xshm", // needed for Chrome >80 (check if puppeteer adds automatically)
"--no-sandbox",
"--disable-background-media-suspend",
"--remote-debugging-port=9221",
"--remote-allow-origins=*",
"--autoplay-policy=no-user-gesture-required",
`--user-agent=${userAgent || this.getDefaultUA()}`,
...extraArgs,
];
if (proxy) {
args.push("--ignore-certificate-errors");
args.push(
`--proxy-server=http://${process.env.PROXY_HOST}:${process.env.PROXY_PORT}`,
);
}
return args;
}
getDefaultUA() {
let version: string | undefined = process.env.BROWSER_VERSION;
try {
const browser = this.getBrowserExe();
if (browser) {
version = child_process.execFileSync(browser, ["--version"], {
encoding: "utf8",
});
const match = version && version.match(/[\d.]+/);
if (match) {
version = match[0];
}
}
} catch (e) {
console.error(e);
}
return `Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/${version} Safari/537.36`;
}
getBrowserExe() {
const files = [
process.env.BROWSER_BIN,
"/usr/bin/google-chrome",
"/usr/bin/chromium-browser",
];
for (const file of files) {
if (file && fs.existsSync(file)) {
return file;
}
}
}
async evaluateWithCLI(
cdp: CDPSession,
frame: Frame,
frameIdToExecId: Map<string, number>,
funcString: string,
logData: Record<string, string>,
contextName: LogContext,
) {
const frameUrl = frame.url();
// TODO: Fix this the next time the file is edited.
// eslint-disable-next-line @typescript-eslint/no-explicit-any
let details: Record<string, any> = { frameUrl, ...logData };
if (!frameUrl || frame.detached) {
logger.info(
"Run Script Skipped, frame no longer attached or has no URL",
details,
contextName,
);
return false;
}
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const frameId = (frame as any)._id;
const contextId = frameIdToExecId.get(frameId);
if (!contextId) {
logger.warn(
"Not running behavior, missing CDP context id for frame id",
{ frameId },
"browser",
);
return;
}
logger.info("Run Script Started", details, contextName);
// from puppeteer _evaluateInternal() but with includeCommandLineAPI: true
//const contextId = context._contextId;
const expression = funcString + "\n//# sourceURL=__evaluation_script__";
const { exceptionDetails, result } = await cdp.send("Runtime.evaluate", {
expression,
contextId,
returnByValue: true,
awaitPromise: true,
userGesture: true,
includeCommandLineAPI: true,
});
if (exceptionDetails) {
if (exceptionDetails.stackTrace) {
details = {
...exceptionDetails.stackTrace,
text: exceptionDetails.text,
...details,
};
}
logger.error("Run Script Failed", details, contextName);
} else {
logger.info("Run Script Finished", details, contextName);
}
return result.value;
}
isLaunched() {
if (this.browser) {
logger.warn("Context already inited", {}, "browser");
return true;
}
return false;
}
async close() {
if (this.browser) {
this.browser.removeAllListeners("disconnected");
await this.browser.close();
this.browser = null;
}
}
addInitScript(page: Page, script: string) {
return page.evaluateOnNewDocument(script);
}
async checkScript(cdp: CDPSession, filename: string, script: string) {
const { exceptionDetails } = await cdp.send("Runtime.evaluate", {
expression: script,
});
if (exceptionDetails) {
logger.fatal(
"Custom behavior load error, aborting",
{ filename, ...exceptionDetails },
"behavior",
);
}
}
async _init(
launchOpts: PuppeteerLaunchOptions,
// eslint-disable-next-line @typescript-eslint/ban-types
ondisconnect: Function | null = null,
recording: boolean,
) {
this.browser = await puppeteer.launch(launchOpts);
const target = this.browser.target();
this.firstCDP = await target.createCDPSession();
if (recording) {
await this.serviceWorkerFetch();
}
if (ondisconnect) {
this.browser.on("disconnected", (err) => ondisconnect(err));
}
this.browser.on("disconnected", () => {
this.browser = null;
});
}
async newWindowPageWithCDP(): Promise<{ cdp: CDPSession; page: Page }> {
// unique url to detect new pages
const startPage =
"about:blank?_browsertrix" + Math.random().toString(36).slice(2);
const p = new Promise<Target>((resolve) => {
const listener = (target: Target) => {
if (target.url() === startPage) {
resolve(target);
if (this.browser) {
this.browser.off("targetcreated", listener);
}
}
};
if (this.browser) {
this.browser.on("targetcreated", listener);
}
});
if (!this.firstCDP) {
throw new Error("CDP missing");
}
try {
await this.firstCDP.send("Target.createTarget", {
url: startPage,
newWindow: true,
});
} catch (e) {
if (!this.browser) {
throw e;
}
const target = this.browser.target();
this.firstCDP = await target.createCDPSession();
await this.firstCDP.send("Target.createTarget", {
url: startPage,
newWindow: true,
});
}
const target = await p;
const page = await target.page();
if (!page) {
throw new Error("page missing");
}
const device = this.emulateDevice;
if (device && page) {
if (device.viewport && device.userAgent) {
// TODO: Fix this the next time the file is edited.
// eslint-disable-next-line @typescript-eslint/no-explicit-any
await page.emulate(device as any);
} else if (device.userAgent) {
await page.setUserAgent(device.userAgent);
}
}
const cdp = await target.createCDPSession();
return { page, cdp };
}
async serviceWorkerFetch() {
if (!this.firstCDP) {
return;
}
this.firstCDP.on("Fetch.requestPaused", async (params) => {
const { frameId, requestId, networkId, request } = params;
if (!this.firstCDP) {
throw new Error("CDP missing");
}
if (networkId) {
try {
await this.firstCDP.send("Fetch.continueResponse", { requestId });
} catch (e) {
logger.warn(
"continueResponse failed",
{ url: request.url },
"recorder",
);
}
return;
}
let foundRecorder = null;
for (const recorder of this.recorders) {
if (recorder.swUrls.has(request.url)) {
recorder.swFrameIds.add(frameId);
}
if (recorder.swFrameIds && recorder.swFrameIds.has(frameId)) {
foundRecorder = recorder;
break;
}
}
if (!foundRecorder) {
logger.debug(
"Skipping URL from unknown frame",
{ url: request.url, frameId },
"recorder",
);
try {
await this.firstCDP.send("Fetch.continueResponse", { requestId });
} catch (e) {
logger.warn(
"continueResponse failed",
{ url: request.url },
"recorder",
);
}
return;
}
await foundRecorder.handleRequestPaused(params, this.firstCDP, true);
});
await this.firstCDP.send("Fetch.enable", {
patterns: [{ urlPattern: "*", requestStage: "Response" }],
});
}
interceptRequest(page: Page, callback: (event: HTTPRequest) => void) {
page.on("request", callback);
}
async waitForNetworkIdle(page: Page, params: { timeout?: number }) {
return await page.waitForNetworkIdle(params);
}
async setViewport(page: Page, params: Viewport) {
await page.setViewport(params);
}
async getCookies(page: Page) {
return await page.cookies();
}
// TODO: Fix this the next time the file is edited.
// eslint-disable-next-line @typescript-eslint/no-explicit-any
async setCookies(page: Page, cookies: any) {
return await page.setCookie(...cookies);
}
}
// ==================================================================
// Default Chromium args from playwright
export const defaultArgs = [
"--disable-field-trial-config", // https://source.chromium.org/chromium/chromium/src/+/main:testing/variations/README.md
"--disable-background-networking",
"--enable-features=NetworkService,NetworkServiceInProcess",
"--disable-background-timer-throttling",
"--disable-backgrounding-occluded-windows",
"--disable-back-forward-cache", // Avoids surprises like main request not being intercepted during page.goBack().
"--disable-breakpad",
"--disable-client-side-phishing-detection",
"--disable-component-extensions-with-background-pages",
"--disable-component-update", // Avoids unneeded network activity after startup.
"--no-default-browser-check",
"--disable-default-apps",
"--disable-dev-shm-usage",
"--disable-extensions",
// AvoidUnnecessaryBeforeUnloadCheckSync - https://github.com/microsoft/playwright/issues/14047
// Translate - https://github.com/microsoft/playwright/issues/16126
// Optimization* - https://bugs.chromium.org/p/chromium/issues/detail?id=1311753
"--disable-features=ImprovedCookieControls,LazyFrameLoading,GlobalMediaControls,DestroyProfileOnBrowserClose,MediaRouter,DialMediaRouteProvider,AcceptCHFrame,AutoExpandDetailsElement,CertificateTransparencyComponentUpdater,AvoidUnnecessaryBeforeUnloadCheckSync,Translate,OptimizationGuideModelDownloading,OptimizationHintsFetching,OptimizationTargetPrediction,OptimizationHints",
"--allow-pre-commit-input",
"--disable-hang-monitor",
"--disable-ipc-flooding-protection",
"--disable-popup-blocking",
"--disable-prompt-on-repost",
"--disable-renderer-backgrounding",
"--disable-sync",
"--force-color-profile=srgb",
"--metrics-recording-only",
"--no-first-run",
"--enable-automation",
"--password-store=basic",
"--use-mock-keychain",
// See https://chromium-review.googlesource.com/c/chromium/src/+/2436773
"--no-service-autorun",
"--export-tagged-pdf",
"--apps-keep-chrome-alive-in-tests",
"--apps-gallery-url=https://gallery.invalid/",
"--apps-gallery-update-url=https://gallery-update.invalid/",
//"--component-updater=url-source=http://updater.invalid/",
"--brave-stats-updater-server=url-source=http://stats-updater.invalid/",
];