Created
April 9, 2019 05:50
-
-
Save dobakay/61e48ef67ec1b5a8d59fd399e7ffdde7 to your computer and use it in GitHub Desktop.
This file contains 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
'use strict'; | |
const puppeteer = require('puppeteer'); | |
const ProxyChain = require('proxy-chain'); | |
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', | |
]; | |
const proxies = [ | |
'203.205.34.52:25', | |
'109.120.224.33:33221', | |
'202.128.22.65:53549', | |
'109.95.84.114:52125', | |
'190.53.46.50:51529', | |
'1.2.169.95:49687', | |
'86.61.182.109:46446', | |
'185.255.46.14:53281', | |
'109.86.199.155:51447', | |
'109.101.139.126:49081', | |
'96.9.73.79:36360', | |
'125.27.251.124:45861', | |
'217.73.192.11:50890', | |
'118.174.233.44:43724', | |
'103.31.45.169:57655', | |
'185.251.33.194:39613', | |
'103.44.139.81:58123', | |
'103.200.37.170:40090', | |
'110.169.50.130:44898', | |
'176.197.103.210:53281' | |
]; | |
const blockedResourceTypes = [ | |
'image', | |
'media', | |
'font', | |
'texttrack', | |
'object', | |
'beacon', | |
'csp_report', | |
'imageset', | |
]; | |
const skippedResources = [ | |
'quantserve', | |
'adzerk', | |
'doubleclick', | |
'adition', | |
'exelator', | |
'sharethrough', | |
'cdn.api.twitter', | |
'google-analytics', | |
'googletagmanager', | |
'google', | |
'fontawesome', | |
'facebook', | |
'analytics', | |
'optimizely', | |
'clicktale', | |
'mixpanel', | |
'zedo', | |
'clicksor', | |
'tiqcdn', | |
]; | |
function changeProxy() { | |
return proxies[Math.floor(Math.random() * proxies.length)] | |
} | |
function timeout(ms) { | |
return new Promise(resolve => setTimeout(resolve, ms)); | |
} | |
function above50PercentWatched(page) { | |
return page.evaluate(async () => { | |
let element = document.querySelector('.ytp-progress-bar'); | |
console.log(element); | |
let loaderValue = element[0].getAttribute('aria-valuenow'); | |
console.log('Loaded about %', loaderValue); | |
return loaderValue >= 127; | |
}); | |
} | |
(async function OpenInstance() { | |
const proxyInfo = await changeProxy(); | |
console.log(proxyInfo); | |
try { | |
const browser = await puppeteer.launch({ | |
args: ['--proxy-server=https=' + proxyInfo, | |
'--no-sandbox', | |
'--disable-setuid-sandbox', | |
'--disable-dev-shm-usage', | |
'--disable-accelerated-2d-canvas', | |
'--disable-gpu',], | |
headless: true, | |
}); | |
for(let i=0; i < 50; i++) { | |
const page = await browser.newPage(); | |
await page.setRequestInterception(true); | |
await page.setUserAgent('Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/61.0.3163.100 Safari/537.36'); | |
page.on('request', request => { | |
const requestUrl = request._url.split('?')[0].split('#')[0]; | |
if ( | |
blockedResourceTypes.indexOf(request.resourceType()) !== -1 || | |
skippedResources.some(resource => requestUrl.indexOf(resource) !== -1) | |
) { | |
request.abort(); | |
} else { | |
request.continue(); | |
} | |
}); | |
console.log('Connection Loading'); | |
const response = await page.goto('https://www.youtube.com/watch?v=Y4daOw4f4_U', { | |
timeout: 25000, | |
waitUntil: 'networkidle2', | |
}); | |
console.log('Connected'); | |
// if (response._status < 400) { | |
// console.log('SERVER IS SLOW'); | |
// await page.waitFor(3000); | |
// let html = await page.content(); | |
// await page.close(); | |
// } else { | |
console.log('GOT RESPONSE'); | |
await timeout(5000); | |
while(await above50PercentWatched(page)) { | |
await above50PercentWatched(page); | |
}; | |
await page.close(); | |
// } | |
console.log('Content Loaded'); | |
} | |
} catch(e) { | |
console.log(e); | |
} finally { | |
OpenInstance(); | |
} | |
})(); | |
const server = new ProxyChain.Server({ | |
// Port where the server the server will listen. By default 8000. | |
port: 8000, | |
// Enables verbose logging | |
verbose: true, | |
prepareRequestFunction: ({ | |
request, | |
username, | |
password, | |
hostname, | |
port, | |
isHttp, | |
}) => { | |
var upstreamProxyUrl; | |
if (request.headers['user-agent'] === uas[0]) { | |
upstreamProxyUrl = changeProxy(); | |
} | |
if (request.headers['user-agent'] === uas[1]) { | |
upstreamProxyUrl = changeProxy(); | |
} | |
console.log('Using proxy: ' + upstreamProxyUrl); | |
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