Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
ulixee
GitHub Repository: ulixee/secret-agent
Path: blob/main/puppet-chrome/index.ts
1028 views
1
import IPuppetBrowser from '@secret-agent/interfaces/IPuppetBrowser';
2
import ILaunchedProcess from '@secret-agent/interfaces/ILaunchedProcess';
3
import IPuppetLauncher from '@secret-agent/interfaces/IPuppetLauncher';
4
import * as os from 'os';
5
import IBrowserEngine from '@secret-agent/interfaces/IBrowserEngine';
6
import { IPuppetLaunchError } from '@secret-agent/interfaces/IPuppetLaunchError';
7
import IPuppetLaunchArgs from '@secret-agent/interfaces/IPuppetLaunchArgs';
8
import { Browser } from './lib/Browser';
9
import { Connection } from './lib/Connection';
10
11
const PuppetLauncher: IPuppetLauncher = {
12
getLaunchArgs(options: IPuppetLaunchArgs, browserEngine: IBrowserEngine) {
13
const chromeArguments = browserEngine.launchArguments ?? [];
14
if (options.proxyPort !== undefined) {
15
chromeArguments.push(
16
// Use proxy for localhost URLs
17
'--proxy-bypass-list=<-loopback>',
18
`--proxy-server=localhost:${options.proxyPort}`,
19
);
20
}
21
22
chromeArguments.push('--remote-debugging-pipe', '--ignore-certificate-errors');
23
24
if (options.noChromeSandbox === true) {
25
chromeArguments.push('--no-sandbox');
26
} else if (os.platform() === 'linux') {
27
const runningAsRoot = process.geteuid && process.geteuid() === 0;
28
if (runningAsRoot) {
29
// eslint-disable-next-line no-console
30
console.warn(
31
'WARNING: Secret-Agent is being run under "root" user - disabling Chrome sandbox! ' +
32
'Run under regular user to get rid of this warning.',
33
);
34
chromeArguments.push('--no-sandbox');
35
}
36
}
37
38
browserEngine.isHeaded = options.showBrowser === true;
39
if (!browserEngine.isHeaded) {
40
chromeArguments.push('--headless');
41
}
42
43
if (browserEngine.beforeLaunch) browserEngine.beforeLaunch(options);
44
45
return browserEngine.launchArguments;
46
},
47
async createPuppet(
48
process: ILaunchedProcess,
49
browserEngine: IBrowserEngine,
50
): Promise<IPuppetBrowser> {
51
const { transport, close } = process;
52
try {
53
const connection = new Connection(transport);
54
return await Browser.create(connection, browserEngine, close);
55
} catch (error) {
56
await close();
57
throw error;
58
}
59
},
60
translateLaunchError(rawError: Error): IPuppetLaunchError {
61
// These error messages are taken from Chromium source code as of July, 2020:
62
// https://github.com/chromium/chromium/blob/70565f67e79f79e17663ad1337dc6e63ee207ce9/content/browser/zygote_host/zygote_host_impl_linux.cc
63
const error = {
64
message: rawError.message,
65
stack: rawError.stack,
66
name: 'PuppetLaunchError',
67
isSandboxError: false,
68
};
69
if (
70
error.message.includes('crbug.com/357670') ||
71
error.message.includes('No usable sandbox!') ||
72
error.message.includes('crbug.com/638180')
73
) {
74
error.stack += [
75
`\nChrome sandboxing failed!`,
76
`================================`,
77
`To workaround sandboxing issues, do either of the following:`,
78
` - (preferred): Configure environment to support sandboxing (as here: https://github.com/ulixee/secret-agent/tree/master/tools/docker)`,
79
` - (alternative): Launch Chrome without sandbox using 'SA_NO_CHROME_SANDBOX=false' environmental variable`,
80
`================================`,
81
``,
82
].join('\n');
83
error.isSandboxError = true;
84
}
85
return error;
86
},
87
};
88
export default PuppetLauncher;
89
90