diogenes
diogenes

Reputation: 45

Is there a way to use a proxy in Puppeteer for Firefox?

Is there a way to configure Puppeteer to use a proxy with Firefox, without manually having to adjust my operating system's proxy settings?

I am able to accomplish this in Chrome by using the command line argument args: [ '--proxy-server=http://0.0.0.0:0000' ], but Firefox doesn't seem to have this capability.

Upvotes: 4

Views: 4774

Answers (4)

aaaaaa
aaaaaa

Reputation: 1396

Henrik's answer got me on the right track. Here's what worked for me

const browser = await puppeteer.launch({
  product: 'firefox',
  extraPrefsFirefox: {
    'network.proxy.type': 1,
    'network.proxy.http': 'localhost',
    'network.proxy.http_port': 8080,
    'network.proxy.ssl': 'localhost',
    'network.proxy.ssl_port': 8080,

    // 'network.proxy.share_proxy_settings': true,
    // note: this setting doesn't work with puppeteer.  When I inspected
    // the preferences saved in the profile directory, it seems this
    // setting is used by firefox to internally set the ssl proxy
    // preferences. This internal process doesn't occur with puppeteer
    // so instead we need to declare the ssl prefs separately.
  },
})

To find which key/value pairs I needed for the desired preferences, I visited about:config and filtered by 'network.proxy'. I'd then manually change the preferences I cared about via the gui and checked back in about:config to see what values were modified. For instance 'network.proxy.type': 1 corresponds to the gui's Manual proxy configuration radio button.

Upvotes: 0

Henrik
Henrik

Reputation: 259

Proxies in Firefox can be configured via preferences. Here a list of these with their default values:

pref("network.proxy.ftp",                   "");
pref("network.proxy.ftp_port",              0);
pref("network.proxy.http",                  "");
pref("network.proxy.http_port",             0);
pref("network.proxy.ssl",                   "");
pref("network.proxy.ssl_port",              0);
pref("network.proxy.socks",                 "");
pref("network.proxy.socks_port",            0);
pref("network.proxy.socks_version",         5);
pref("network.proxy.proxy_over_tls",        true);
pref("network.proxy.no_proxies_on",         "");

To actually make use of them install the official puppeteer node.js package with Firefox as selected product (note that puppeteer-firefox is deprecated). Then preferences can be specified via extraPrefsFirefox for the call to puppeteer.launch(). Here an example for the necessary steps from the puppeteer repository.

Upvotes: 5

polybius
polybius

Reputation: 61

With Yevhen's example, you may run into issue's using the import statement. Instead I recommend using the following:

const puppeteer = require('puppeteer');
const { proxyRequest } = require('puppeteer-proxy');

Upvotes: 3

Yevhen Laichenkov
Yevhen Laichenkov

Reputation: 8692

Unfortunately, there is no 'proxy-server' argument in Firefox.

However, you can intercept the request and set a proxy with the puppeteer-proxy library.

Here is an example.

import puppeteer from 'puppeteer';
import { proxyRequest } from 'puppeteer-proxy';

(async () => {
  const browser = await puppeteer.launch();
  const page = await browser.newPage();

  await page.setRequestInterception(true);

  page.on('request', async (request) => {
    await proxyRequest({
      page,
      proxyUrl: 'http://127.0.0.1:3000',
      request,
    });
  });

  await page.goto('http://gajus.com');
})();

It will work in Chrome and Firefox as well.

Upvotes: 3

Related Questions