Skip to content

Instantly share code, notes, and snippets.

@Raidus
Last active May 16, 2018 15:32
Show Gist options
  • Save Raidus/b1ced3a1a9606405907aa002ff584b4f to your computer and use it in GitHub Desktop.
Save Raidus/b1ced3a1a9606405907aa002ff584b4f to your computer and use it in GitHub Desktop.
const puppeteer = require('puppeteer');
const ProxyChain = require('proxy-chain');
const ROUTER_PROXY = 'http://127.0.0.1:8000';
const uas = [
'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/66.0.3359.139 Safari/537.36',
'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36',
];
// Needs to be set prior to puppeteer.launch()
const defaultUa =
'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/65.0.3312.0 Safari/537.36';
const proxies = ['http://127.0.0.1:24000', 'http://127.0.0.1:24001', 'http://127.0.0.1:24003'];
(async () => {
const browser = await puppeteer.launch({
headless: false,
args: [
'--no-sandbox',
`--proxy-server=${ROUTER_PROXY}`,
`--user-agent=${defaultUa} `,
],
});
const page1 = await browser.newPage();
const page2 = await browser.newPage();
try {
await page1.setUserAgent(uas[0]);
await page1.goto('http://www.whatsmyip.org/');
} catch (e) {
console.log(e);
}
try {
await page2.setUserAgent(uas[1]);
await page2.goto('http://www.whatsmyip.org/');
} catch (e) {
console.log(e);
}
await browser.close();
})();
const server = new ProxyChain.Server({
// Port where the server the server will listen. By default 8000.
port: 8000,
// Enables verbose logging
verbose: false,
prepareRequestFunction: ({
request,
username,
password,
hostname,
port,
isHttp,
}) => {
let upstreamProxyUrl;
if (request.headers['user-agent'] === uas[0]) upstreamProxyUrl = proxies[0];
if (request.headers['user-agent'] === uas[1]) upstreamProxyUrl = proxies[1];
// some requests are sent from browser
// (headless=false & incognito=false)
if (request.headers['user-agent'] === defaultUa) upstreamProxyUrl = proxies[2];
return { upstreamProxyUrl };
},
});
server.listen(() => {
console.log(`Router Proxy server is listening on port ${8000}`);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment