Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Headless chrome proxy server settings

Could anyone help me with setting proxy-server for headless chrome while using the lighthouse chrome launcher in Node.js as mentioned here

const launcher = new ChromeLauncher({
    port: 9222,
    autoSelectChrome: true, // False to manually select which Chrome install.
    additionalFlags: [
      '--window-size=412,732',
      '--disable-gpu',
      '--proxy-server="IP:PORT"',
      headless ? '--headless' : ''
    ]
  });

However, the above script does not hit my proxy server at all. Chrome seems to fallback to DIRECT:// connections to the target website.

One other resource that talks about using HTTP/HTTPS proxy server in the context of headless chrome is this. But it does not give any example of how to use this from Node.js.

like image 832
alpha_cod Avatar asked May 09 '17 11:05

alpha_cod


1 Answers

I tried it using regular exec and it works just fine, here is my snippet:

const exec = require('child_process').exec;

function launchHeadlessChrome(url, callback) {
  // Assuming MacOSx.
  const CHROME = '/Users/h0x91b/Desktop/Google\\ Chrome\\ Beta.app/Contents/MacOS/Google\\ Chrome';
  exec(`${CHROME} --headless --disable-gpu --remote-debugging-port=9222 --proxy-server=127.0.0.1:8888 ${url}`, callback);
}

launchHeadlessChrome('https://www.chromestatus.com', (err, stdout, stderr) => {
    console.log('callback', err, stderr, stdout)
});

Then I navigated to http://localhost:9222 and in Developer tools I see : enter image description here

Proxy connection Error, which is ok, because I don't have proxy on this port, but this means that the Chrome tried to connect via proxy...

BTW Chrome version is 59.

Have checked the source code https://github.com/GoogleChrome/lighthouse/blob/master/chrome-launcher/chrome-launcher.ts#L38-L44

I see no additionalFlags here, there is only chromeFlags try to use it...

like image 76
h0x91B Avatar answered Sep 22 '22 13:09

h0x91B