Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Puppeteer Use Multiple Proxies and Change Automatic Proxy if Proxy Refused Connection

Can you tell me if this is possible?

I want to use multiple proxies and automatically change the proxy if the proxy refused connection.

args: [
    '--proxy-server=127.0.0.1:9876', // Or whatever the address is
]

So with this, you can use one single proxy, but how is it possible to use multiple and let it automatically change if it refuses connection?

like image 245
JohnnyDip Avatar asked Feb 13 '18 14:02

JohnnyDip


1 Answers

Use Tor.

You can install the tor package which will allow you to browse with Puppeteer through the Tor network, and change your identity (IP address) very easily.

Launch Puppeteer with Tor using the --proxy-server flag:

const browser = await puppeteer.launch({
  args: [
    '--proxy-server=socks5://127.0.0.1:9050',
  ],
});

Then, on page.on('response'), change the proxy using child_process.exec() if the response was not successful ( response.ok() === false ).

The following command will create a new Tor identity:

(echo authenticate \'""\'; echo signal newnym; echo quit) | nc localhost 9051

Example Usage:

'use strict';

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

(async () => {
  const browser = await puppeteer.launch({
    args: [
      '--proxy-server=socks5://127.0.0.1:9050'
    ],
  });
  const page = await browser.newPage();
  let current_ip_address = '';
  
  page.on('response', response => {
    if (response.ok() === false) {
      exec('(echo authenticate \'""\'; echo signal newnym; echo quit) | nc localhost 9051', (error, stdout, stderr) => {
        if (stdout.match(/250/g).length === 3) {
          console.log('Success: The IP Address has been changed.');
        } else {
          console.log('Error: A problem occured while attempting to change the IP Address.');
        }
      });
    } else {
      console.log('Success: The Page Response was successful (no need to change the IP Address).');
    }
  });
  
  await page.goto('http://checkip.amazonaws.com/');
  
  current_ip_address = await page.evaluate(() => document.body.textContent.trim());
  
  console.log(current_ip_address);
  
  await browser.close();
})();

Note: Tor may take a moment to change identities, so it might be a good call to verify that the IP Address is different before continuing with your program.

like image 179
Grant Miller Avatar answered Nov 04 '22 19:11

Grant Miller