Skip to content

Commit 174c272

Browse files
authored
feat: add firefox usage and example (#51)
* feat: add example Signed-off-by: vvanglro <[email protected]> * feat: add example Signed-off-by: vvanglro <[email protected]> * feat: update every_day_cron_challenge.yml Signed-off-by: vvanglro <[email protected]> * feat: update README.md Signed-off-by: vvanglro <[email protected]> * feat: update pre-commit Signed-off-by: vvanglro <[email protected]> --------- Signed-off-by: vvanglro <[email protected]>
1 parent cd11f8d commit 174c272

File tree

6 files changed

+97
-104
lines changed

6 files changed

+97
-104
lines changed

.github/workflows/every_day_cron_challenge.yml

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,9 @@ jobs:
1919
run: |
2020
python -m pip install --upgrade pdm
2121
pdm install
22-
pdm run playwright install chromium
22+
pdm run playwright install chromium firefox
2323
sudo apt install xvfb
2424
- name: Run script
25-
run: xvfb-run pdm run python tests/test_cron_challenge.py
25+
run: |
26+
xvfb-run pdm run python example/test_chromium_cron_challenge.py
27+
pdm run python example/test_firefox_cron_challenge.py

.pre-commit-config.yaml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ repos:
55
- id: trailing-whitespace
66
- id: end-of-file-fixer
77
- id: check-yaml
8+
- id: check-toml
89

910
- repo: https://github.com/astral-sh/ruff-pre-commit
1011
rev: v0.0.277
@@ -32,4 +33,4 @@ repos:
3233
entry: '\bprint\('
3334
language: pygrep
3435
types: [ python ]
35-
exclude: "tests/test_cron_challenge.py"
36+
exclude: "example/test_chromium_cron_challenge.py|example/test_firefox_cron_challenge.py"

README.md

Lines changed: 31 additions & 99 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,11 @@ the cf_clearance, make sure you use the same IP and UA as when you got it.
1414

1515
## Warning
1616

17-
Please use interface mode, You must add headless=False.
17+
If you use chromium, Please use interface mode, You must add headless=False.
1818
If you use it on linux or docker, use XVFB.
1919

20+
If you use firefox you don't need interface mode and XVFB.
21+
2022
Challenge are not always successful. Please try more and handle exceptions.
2123

2224

@@ -39,22 +41,39 @@ curl http://localhost:8000/challenge -H "Content-Type:application/json" -X POST
3941
import requests
4042

4143
proxy = "socks5://localhost:7890"
42-
resp = requests.post("http://localhost:8000/challenge",
43-
json={"proxy": {"server": proxy}, "timeout": 20,
44-
"url": "https://nowsecure.nl"})
44+
resp = requests.post(
45+
"http://localhost:8000/challenge",
46+
json={
47+
"proxy": {"server": proxy},
48+
"timeout": 20,
49+
"url": "https://nowsecure.nl",
50+
"pure": True,
51+
"browser": 2,
52+
"cookies": [
53+
{
54+
"url": "https://www.example.com",
55+
"name": "example-cookie",
56+
"value": "example-value",
57+
}
58+
],
59+
"headers": {"example-ua": "example-ua-value"},
60+
"exec_js": "() => {return navigator.userAgent}",
61+
},
62+
)
4563
data = resp.json()
4664
# In some cases, the cloudflare challenge will not be triggered,
4765
# so when cf in the return parameter is true, it means that the challenge has been encountered.
4866
if data.get("success") and data.get("cf"):
4967
ua = data.get("user_agent")
68+
exec_js_resp = data.get("exec_js_resp")
5069
cf_clearance_value = data.get("cookies").get("cf_clearance")
5170
# use cf_clearance, must be same IP and UA
5271
headers = {"user-agent": ua}
5372
cookies = {"cf_clearance": cf_clearance_value}
54-
res = requests.get('https://nowsecure.nl', proxies={
55-
"all": proxy
56-
}, headers=headers, cookies=cookies)
57-
if '<title>Just a moment...</title>' not in res.text:
73+
res = requests.get(
74+
"https://nowsecure.nl", proxies={"all": proxy}, headers=headers, cookies=cookies
75+
)
76+
if "<title>Just a moment...</title>" not in res.text:
5877
print("cf challenge success")
5978
```
6079

@@ -66,7 +85,7 @@ pip install cf-clearance
6685

6786
## Usage
6887

69-
Please make sure it is the latest package.
88+
Please make sure it is the latest package. See [example](https://github.com/vvanglro/cf-clearance/tree/main/example).
7089

7190
```
7291
pip install --upgrade cf-clearance
@@ -76,94 +95,7 @@ or
7695
pip install git+https://github.com/vvanglro/cf-clearance.git@main
7796
```
7897

79-
### sync
80-
81-
```python
82-
from playwright.sync_api import sync_playwright
83-
from cf_clearance import sync_cf_retry, sync_stealth
84-
import requests
85-
86-
# not use cf_clearance, cf challenge is fail
87-
proxies = {
88-
"all": "socks5://localhost:7890"
89-
}
90-
res = requests.get('https://nowsecure.nl', proxies=proxies)
91-
if '<title>Just a moment...</title>' in res.text:
92-
print("cf challenge fail")
93-
# get cf_clearance
94-
with sync_playwright() as p:
95-
browser = p.chromium.launch(headless=False, proxy={"server": "socks5://localhost:7890"})
96-
page = browser.new_page()
97-
sync_stealth(page, pure=True)
98-
page.goto('https://nowsecure.nl')
99-
res, cf = sync_cf_retry(page)
100-
if cf:
101-
if res:
102-
cookies = page.context.cookies()
103-
for cookie in cookies:
104-
if cookie.get('name') == 'cf_clearance':
105-
cf_clearance_value = cookie.get('value')
106-
print(cf_clearance_value)
107-
ua = page.evaluate('() => {return navigator.userAgent}')
108-
print(ua)
109-
else:
110-
print("cf challenge fail")
111-
else:
112-
print("No cloudflare challenges encountered")
113-
browser.close()
114-
# use cf_clearance, must be same IP and UA
115-
headers = {"user-agent": ua}
116-
cookies = {"cf_clearance": cf_clearance_value}
117-
res = requests.get('https://nowsecure.nl', proxies=proxies, headers=headers, cookies=cookies)
118-
if '<title>Just a moment...</title>' not in res.text:
119-
print("cf challenge success")
120-
```
121-
122-
### async
123-
124-
```python
125-
import asyncio
126-
from playwright.async_api import async_playwright
127-
from cf_clearance import async_cf_retry, async_stealth
128-
import requests
129-
130-
131-
async def main():
132-
# not use cf_clearance, cf challenge is fail
133-
proxies = {
134-
"all": "socks5://localhost:7890"
135-
}
136-
res = requests.get('https://nowsecure.nl', proxies=proxies)
137-
if '<title>Just a moment...</title>' in res.text:
138-
print("cf challenge fail")
139-
# get cf_clearance
140-
async with async_playwright() as p:
141-
browser = await p.chromium.launch(headless=False, proxy={"server": "socks5://localhost:7890"})
142-
page = await browser.new_page()
143-
await async_stealth(page, pure=True)
144-
await page.goto('https://nowsecure.nl')
145-
res, cf = await async_cf_retry(page)
146-
if cf:
147-
if res:
148-
cookies = await page.context.cookies()
149-
for cookie in cookies:
150-
if cookie.get('name') == 'cf_clearance':
151-
cf_clearance_value = cookie.get('value')
152-
print(cf_clearance_value)
153-
ua = await page.evaluate('() => {return navigator.userAgent}')
154-
print(ua)
155-
else:
156-
print("cf challenge fail")
157-
else:
158-
print("No cloudflare challenges encountered")
159-
await browser.close()
160-
# use cf_clearance, must be same IP and UA
161-
headers = {"user-agent": ua}
162-
cookies = {"cf_clearance": cf_clearance_value}
163-
res = requests.get('https://nowsecure.nl', proxies=proxies, headers=headers, cookies=cookies)
164-
if '<title>Just a moment...</title>' not in res.text:
165-
print("cf challenge success")
166-
167-
168-
asyncio.get_event_loop().run_until_complete(main())
98+
## Install Playwright Depends
99+
```shell
100+
playwright install chromium firefox
169101
```

example/__init__.py

Whitespace-only changes.

tests/test_cron_challenge.py renamed to example/test_chromium_cron_challenge.py

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,14 @@
1+
import requests
12
from playwright.async_api import async_playwright
23

34
from cf_clearance import async_cf_retry, async_stealth
45

56

67
async def test_cf_challenge(url: str):
8+
# not use cf_clearance, cf challenge is fail
9+
res = requests.get("https://nowsecure.nl")
10+
assert "<title>Just a moment...</title>" in res.text
11+
# get cf_clearance
712
async with async_playwright() as p:
813
browser = await p.chromium.launch(
914
headless=False,
@@ -12,19 +17,27 @@ async def test_cf_challenge(url: str):
1217
page = await context.new_page()
1318
await async_stealth(page, pure=True)
1419
await page.goto(url)
15-
res, cf = await async_cf_retry(page)
20+
success, cf = await async_cf_retry(page)
1621
if cf:
17-
if res:
22+
if success:
1823
cookies = await page.context.cookies()
1924
for cookie in cookies:
2025
if cookie.get("name") == "cf_clearance":
2126
cf_clearance_value = cookie.get("value")
27+
print(cf_clearance_value)
28+
ua = await page.evaluate("() => {return navigator.userAgent}")
2229
assert cf_clearance_value
2330
else:
2431
raise
2532
else:
2633
print("No cloudflare challenges encountered")
34+
2735
await browser.close()
36+
# use cf_clearance, must be same IP and UA
37+
headers = {"user-agent": ua}
38+
cookies = {"cf_clearance": cf_clearance_value}
39+
res = requests.get("https://nowsecure.nl", headers=headers, cookies=cookies)
40+
assert "<title>Just a moment...</title>" not in res.text
2841

2942

3043
if __name__ == "__main__":
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
import requests
2+
from playwright.async_api import async_playwright
3+
4+
from cf_clearance import async_cf_retry
5+
6+
7+
async def test_cf_challenge(url: str):
8+
# not use cf_clearance, cf challenge is fail
9+
res = requests.get("https://nowsecure.nl")
10+
assert "<title>Just a moment...</title>" in res.text
11+
# get cf_clearance
12+
async with async_playwright() as p:
13+
browser = await p.firefox.launch(
14+
headless=True,
15+
)
16+
context = await browser.new_context()
17+
page = await context.new_page()
18+
await page.goto(url)
19+
success, cf = await async_cf_retry(page)
20+
if cf:
21+
if success:
22+
cookies = await page.context.cookies()
23+
for cookie in cookies:
24+
if cookie.get("name") == "cf_clearance":
25+
cf_clearance_value = cookie.get("value")
26+
print(cf_clearance_value)
27+
ua = await page.evaluate("() => {return navigator.userAgent}")
28+
assert cf_clearance_value
29+
else:
30+
raise
31+
else:
32+
print("No cloudflare challenges encountered")
33+
34+
await browser.close()
35+
# use cf_clearance, must be same IP and UA
36+
headers = {"user-agent": ua}
37+
cookies = {"cf_clearance": cf_clearance_value}
38+
res = requests.get("https://nowsecure.nl", headers=headers, cookies=cookies)
39+
assert "<title>Just a moment...</title>" not in res.text
40+
41+
42+
if __name__ == "__main__":
43+
import asyncio
44+
45+
asyncio.run(test_cf_challenge("https://nowsecure.nl"))

0 commit comments

Comments
 (0)