vvanglro / cf-clearance

Purpose To make a cloudflare v2 challenge pass successfully, Can be use cf_clearance bypassed by cloudflare, However, with the cf_clearance, make sure you use the same IP and UA as when you got it.
https://github.com/vvanglro/cf_clearance
353 stars 58 forks source link

custom user-agent #20

Closed glira closed 1 year ago

glira commented 1 year ago

We need of support to custom user-agent, thank for brilliant project , time and effort

vvanglro commented 1 year ago

You can add this line of code to customize ua.

context = await browser.new_context(
  user_agent='My user agent'
)

This is playwright api docs. https://playwright.dev/python/docs/emulation#user-agent

Example:

import asyncio
from playwright.async_api import async_playwright
from cf_clearance import async_cf_retry, async_stealth

async def main():
    async with async_playwright() as p:
        browser = await p.chromium.launch(headless=False, proxy={"server": "socks5://localhost:7890"})
        context = await browser.new_context(
            user_agent='My user agent'
        )
        page = await context.new_page()
        await async_stealth(page, pure=True)
        await page.goto('https://nowsecure.nl')
        res = await async_cf_retry(page)
        if res:
            cookies = await page.context.cookies()
            for cookie in cookies:
                if cookie.get('name') == 'cf_clearance':
                    cf_clearance_value = cookie.get('value')
                    print(cf_clearance_value)
            ua = await page.evaluate('() => {return navigator.userAgent}')
            print(ua)
        else:
            print("cf challenge fail")
        await browser.close()

asyncio.get_event_loop().run_until_complete(main())

Docker image will be updated later.