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.