Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
// Parse and validate proxy URL
const parsedProxyUrl = parseUrl(opts.proxyUrl);
if (!parsedProxyUrl.host || !parsedProxyUrl.port) {
throw new Error('Invalid "proxyUrl" option: both hostname and port must be provided.');
}
if (!/^(http|https|socks4|socks5)$/.test(parsedProxyUrl.scheme)) {
throw new Error(`Invalid "proxyUrl" option: Unsupported scheme (${parsedProxyUrl.scheme}).`);
}
// Anonymize proxy URL if it has username or password
let anonymizedProxyUrl = null;
if (parsedProxyUrl.username || parsedProxyUrl.password) {
if (parsedProxyUrl.scheme !== 'http') {
throw new Error('Invalid "proxyUrl" option: authentication is only supported for HTTP proxy type.');
}
anonymizedProxyUrl = await anonymizeProxy(opts.proxyUrl);
}
opts.args.push(`--proxy-server=${anonymizedProxyUrl || opts.proxyUrl}`);
const optsForLog = _.omit(opts, LAUNCH_PUPPETEER_LOG_OMIT_OPTS);
optsForLog.proxyUrl = redactUrl(opts.proxyUrl);
optsForLog.args = opts.args.slice(0, opts.args.length - 1);
log.info('Launching Puppeteer', optsForLog);
const browser = await puppeteer.launch(opts);
// Close anonymization proxy server when Puppeteer finishes
if (anonymizedProxyUrl) {
const cleanUp = () => {
// Don't wait for finish, only log errors
closeAnonymizedProxy(anonymizedProxyUrl, true)
.catch(err => log.exception(err, 'closeAnonymizedProxy() failed.'));
_initialize() {
let promise = null;
// Applies options.proxyUrl setting to the WebDriver's Capabilities and Chrome Options.
// For proxy servers with authentication, this class starts a local proxy server
// NOTE: to view effective proxy settings in Chrome, open chrome://net-internals/#proxy
if (this.options.proxyUrl) {
// NOTE: call anonymizeProxy() outside of promise, so that errors in proxyUrl are thrown!
promise = anonymizeProxy(this.options.proxyUrl)
.then((result) => {
this.anonymizedProxyUrl = result;
if (/^chrome$/i.test(this.options.browserName)) {
// In Chrome, Capabilities.setProxy() has no effect,
// so we setup the proxy manually
this.chromeOptions.addArguments(`--proxy-server=${this.anonymizedProxyUrl}`);
} else {
const proxyConfig = {
proxyType: 'MANUAL',
httpProxy: this.anonymizedProxyUrl,
sslProxy: this.anonymizedProxyUrl,
ftpProxy: this.anonymizedProxyUrl,
};
this.capabilities.setProxy(proxyConfig);
}