Last active
May 8, 2018 12:58
-
-
Save Raidus/fcd0d495e641cb2ff1eddee767c8e06c to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const request = require('request-promise'); | |
const puppeteer = require('puppeteer'); | |
const fs = require('fs'); | |
const Sock5HttpAgent = require('socks5-http-client/lib/Agent'); | |
const Sock5HttpsAgent = require('socks5-https-client/lib/Agent'); | |
const logDir = 'tmp'; | |
if (!fs.existsSync(logDir)) { | |
fs.mkdirSync(logDir); | |
} | |
const uri = 'https://www.whatismyip.com/de/'; | |
async function newPage(browser) { | |
let page = await browser.newPage(); | |
await page.setRequestInterception(true); | |
page.on('request', async interceptedRequest => { | |
const resType = interceptedRequest.resourceType(); | |
if (['document', 'xhr'].indexOf(resType) !== -1) { | |
const url = interceptedRequest.url(); | |
const options = { | |
uri: url, | |
method: interceptedRequest.method(), | |
headers: interceptedRequest.headers(), | |
body: interceptedRequest.postData(), | |
usingProxy: true, | |
}; | |
const response = await fetch(options); | |
interceptedRequest.respond({ | |
status: response.statusCode, | |
contentType: response.headers['content-type'], | |
headers: response.headers, | |
body: response.body, | |
}); | |
} else { | |
interceptedRequest.continue(); | |
} | |
}); | |
return page; | |
} | |
function fetch(options) { | |
// let baseUrl = options.baseUrl || request.globals.baseUrl; | |
let isHttps; | |
if (options.uri.startsWith('https')) { | |
isHttps = true; | |
} else if (options.uri.startsWith('http')) { | |
isHttps = false; | |
} | |
if (options.usingProxy || process.env.NODE_ENV === 'production') { | |
options.agentClass = isHttps ? Sock5HttpsAgent : Sock5HttpAgent; | |
isHttps && (options.strictSSL = true); | |
options.agentOptions = { | |
socksHost: 'localhost', | |
socksPort: 24008, // Socks5 Round-Robin-Port | |
}; | |
//options.proxy = 'http://127.0.0.1:24003'; // Round-Robin-Proxy | |
} | |
options.resolveWithFullResponse = true; | |
return request(options); | |
} | |
async function run() { | |
try { | |
const browser = await puppeteer.launch(); | |
const pages = []; | |
for (let i = 0; i < 10; i++) { | |
const page = await newPage(browser); | |
pages.push(page); | |
} | |
const promises = []; | |
pages.forEach((page, i) => promises.push(takeScreenshot(page, i))); | |
await Promise.all(promises); | |
await browser.close(); | |
console.log('done'); | |
} catch (e) { | |
console.log(e); | |
} | |
} | |
async function takeScreenshot(page, i) { | |
await page.goto(uri, { waitUntil: 'networkidle2' }); | |
await page.screenshot({ path: `./tmp/proxy_per_page_${i}.png` }); | |
} | |
run(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment