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

error while use headless=True #23

Closed hereTac closed 1 year ago

hereTac commented 1 year ago

It is all good while using headless=False, got error when use headless=true, started it with xvfb-run python3 test_s.py.

image

Here is the whole source code .

import time

from playwright.sync_api import sync_playwright
import sys
sys.path.append("..")
import cf_clearance
#from cf_clearance import sync_stealth, sync_cf_retry
import requests

# not use cf_clearance, cf challenge is fail
proxies = {
   "all": "http://192.168.181.104:7890"
}
res = requests.get('https://steamdb.info/charts/', proxies=proxies)
if '<title>Just a moment...</title>' in res.text:
   print("cf challenge fail")
# get cf_clearance
with sync_playwright() as p:
   browser = p.chromium.launch(headless=True, proxy={"server": "http://192.168.181.104:7890"})
   page = browser.new_page()
   cf_clearance.sync_stealth(page, pure=True)
   page.goto('https://steamdb.info/charts/')
   res = cf_clearance.sync_cf_retry(page)
   time.sleep(6)
   page.screenshot(path="example.png")
   if res:
       cookies = page.context.cookies()
       for cookie in cookies:
           if cookie.get('name') == 'cf_clearance':
               cf_clearance_value = cookie.get('value')
               print(cf_clearance_value)
       ua = page.evaluate('() => {return navigator.userAgent}')
       print(ua)
   else:
       print("cf challenge fail")
   time.sleep(10)
   browser.close()
# use cf_clearance, must be same IP and UA
headers = {"user-agent": ua}
cookies = {"cf_clearance": cf_clearance_value}
res = requests.get('https://steamdb.info/charts/', proxies=proxies, headers=headers, cookies=cookies)
if '<title>Just a moment...</title>' not in res.text:
   print("cf challenge success")
vvanglro commented 1 year ago

Please use interface mode, You must add headless=False. If you use it on linux or docker, use XVFB.

hereTac commented 1 year ago

I apologize for any confusion. Let me explain again. I used headless=True and XVFB on Linux, then start it by running xvfb-run python3 test_s.py, cloudflare response the access denied page. However, when I use headless=False and the interface mode on Ubuntu Desktop, that works fine and it give me the correct target page. @vvanglro

vvanglro commented 1 year ago

You can only pass the challenge with headless=false, if you are using a server with an interface, you don't need to use XVFB.

hereTac commented 1 year ago

In my case, my purpose is to run the script on Linux without an interface, so must be XVFB, is it right? But, unfortunated, is can not pass the challenge. (Just tested on Ubuntu Desktop and it is good with an interface.

vvanglro commented 1 year ago

try new code.

pip install git+https://github.com/vvanglro/cf-clearance.git@main
hereTac commented 1 year ago

Try it already yesterday. I build the test code in git clone https://github.com/vvanglro/cf-clearance. image

vvanglro commented 1 year ago

Works fine, doesn't reproduce your problem.

image