Last active
July 9, 2020 01:04
-
-
Save hmmhmmhm/9fe134ce45d3bb633214e8e594c9e237 to your computer and use it in GitHub Desktop.
Anti-Cloudflare Script
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
import puppeteer from 'puppeteer' | |
import puppeteerExtra from 'puppeteer-extra' | |
import pluginStealth from 'puppeteer-extra-plugin-stealth' | |
import cloudscraper from 'cloudscraper' | |
import {User} from "../domain/user"; | |
import {accessing} from "../handlers/nasHandler"; | |
let scraper: any = cloudscraper | |
let browser; | |
export const getBrowser = async () => { | |
if (!browser) { | |
browser = await init({isHeadless: false}); | |
} | |
return browser; | |
} | |
export const init = async ({ isHeadless = true }) => { | |
console.log(`π§ μ΄κΈ° μ€ν μ§ν μ€...`) | |
console.log(`π§ ν€λλ¦¬μ€ ν¬λ‘¬μ μμνκ³ μμ΅λλ€..`) | |
console.log(`π§ μ€κ°μ μΈμ λ Ctrl+Cλ‘ μ’ λ£μν¬ μ μμ΅λλ€.\n`) | |
try { | |
const args = [ | |
'--no-sandbox', | |
'--disable-setuid-sandbox', | |
'--disable-infobars', | |
'--window-position=0,0', | |
'--ignore-certifcate-errors', | |
'--ignore-certifcate-errors-spki-list', | |
//'--user-agent="Mozilla/6.0 (Macintosh; Intel Mac OS X 10_12_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/65.0.3312.0 Safari/537.36"', | |
] | |
const options = { | |
args, | |
headless: isHeadless, | |
ignoreHTTPSErrors: true, | |
userDataDir: './tmp', | |
} | |
// @ts-ignore | |
puppeteerExtra.use(pluginStealth()) | |
let browser = await puppeteerExtra.launch(options) | |
console.log(`π§ ν€λλ¦¬μ€ ν¬λ‘¬μ΄ μμλμμ΅λλ€.`) | |
return browser | |
} catch (e) { | |
console.log(`π§ ν€λλ¦¬μ€ ν¬λ‘¬ μλ μ€ μ€λ₯κ° λ°μνμμ΅λλ€.`) | |
console.log(e) | |
} | |
return undefined | |
} | |
// νμ΄μ§ μ΄λ | |
export const navigatePage = async ( | |
page: puppeteer.Page, | |
targetUrl: string, | |
waitCode: string = 'networkidle0' | |
) => { | |
try { | |
let hookHeaders: any = await scrapeCloudflareHttpHeaderCookie(targetUrl) | |
// Anti Cloud Flare | |
await page.setRequestInterception(true) | |
page.on('request', request => { | |
const headers = request.headers() | |
request.continue({ ...hookHeaders }) | |
}) | |
// @ts-ignore | |
await page.goto(targetUrl, { | |
waitUntil: ['load', waitCode], | |
}) | |
return true | |
} catch (e) { | |
console.log('νμ΄μ§ μ μ μ€ μ€λ₯κ° λ°μνμ΅λλ€.') | |
console.log('5μ΄ λ€ ν΄λΉ νμ΄μ§ μ μμ λ€μ μλν©λλ€.') | |
console.log(`λ¬Έμ κ° λ νμ΄μ§: ${targetUrl}\n`) | |
await page.waitFor(5000) | |
return false | |
} | |
} | |
// νμ΄μ§ μμ μ΄λ | |
export const saftyGoto = async ( | |
page: puppeteer.Page, | |
targetUrl: string, | |
waitCode: string = 'networkidle0' | |
) => { | |
// μ±κ³΅ν λκΉμ§ κ³μ μ μμ μλν©λλ€. | |
let isSuccess = false | |
while (!isSuccess) isSuccess = await navigatePage(page, targetUrl, waitCode) | |
} | |
export const delay = time => new Promise(res => setTimeout(res, time)) | |
// μ€ν¬λ μ΄νΌ μ΄κΈ°ν | |
export const scrapeCloudflareHttpHeaderCookie = url => | |
new Promise((resolve, reject) => | |
scraper.get(url, function(error, response, body) { | |
if (error) { | |
reject(error) | |
} else { | |
resolve(response.request.headers) | |
} | |
}) | |
) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
μ΅μ λ²μ μ νΌν«ν°μ΄ 5λ‘ μλνλ
νλ‘μ νΈ ν νλ¦Ώμ λ§λ€μ΄ 곡μ ν©λλ€.
https://github.com/hmmhmmhm/puppeteer-template