forked from Theyka/Turnstile-Solver
-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathsync_solver.py
More file actions
413 lines (350 loc) · 16.8 KB
/
Copy pathsync_solver.py
File metadata and controls
413 lines (350 loc) · 16.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
import time
import os
import random
import string
from typing import Dict, Optional
from dataclasses import dataclass
from patchright.sync_api import sync_playwright, Page, BrowserContext
from camoufox.sync_api import Camoufox
from logmagix import Loader, Logger
from functools import wraps
log = Logger(github_repository="https://github.com/sexfrance/Turnstile-Solver")
DEBUG = False
def set_debug(value: bool):
global DEBUG
DEBUG = value
def debug(func_or_message, *args, **kwargs):
global DEBUG
if callable(func_or_message):
@wraps(func_or_message)
def wrapper(*args, **kwargs):
result = func_or_message(*args, **kwargs)
if DEBUG:
log.debug(f"{func_or_message.__name__} returned: {result}")
return result
return wrapper
else:
if DEBUG:
log.debug(f"Debug: {func_or_message}")
@dataclass
class TurnstileResult:
turnstile_value: Optional[str]
elapsed_time_seconds: float
status: str
reason: Optional[str] = None
class TurnstileSolver:
HTML_TEMPLATE = """
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Turnstile Solver</title>
<script
src="https://challenges.cloudflare.com/turnstile/v0/api.js?onload=onloadTurnstileCallback"
async=""
defer=""
></script>
</head>
<body>
<!-- cf turnstile -->
</body>
</html>
"""
def __init__(self, debug: bool = False, headless: Optional[bool] = None, useragent: Optional[str] = None, browser_type: str = "chromium"):
self.debug = debug
self.browser_type = browser_type
self.headless = headless if headless is not None else False
self.useragent = useragent
self.log = log
self.loader = Loader(desc="Solving captcha...", timeout=0.05)
self.browser_args = [
"--disable-blink-features=AutomationControlled",
"--no-sandbox",
"--disable-dev-shm-usage",
"--disable-background-networking",
"--disable-background-timer-throttling",
"--disable-backgrounding-occluded-windows",
"--disable-renderer-backgrounding",
"--window-position=2000,2000",
]
if self.useragent:
self.browser_args.append(f"--user-agent={self.useragent}")
if self.headless and not self.useragent:
self.log.warning("To solve captchas with headless mode you need to set the useragent!")
@debug
def _setup_page(self, browser, url: str, sitekey: str = None, action: str = None, cdata: str = None) -> Page:
"""Set up the page with or without Turnstile widget."""
if self.browser_type == "chrome":
page = browser.pages[0]
else:
page = browser.new_page()
url_with_slash = url + "/" if not url.endswith("/") else url
debug(f"Navigating to URL: {url_with_slash}")
if sitekey:
turnstile_div = f'<div class="cf-turnstile" data-sitekey="{sitekey}"' + (f' data-action="{action}"' if action else '') + (f' data-cdata="{cdata}"' if cdata else '') + ' data-theme="light"></div>'
page_data = self.HTML_TEMPLATE.replace("<!-- cf turnstile -->", turnstile_div)
page.route(url_with_slash, lambda route: route.fulfill(body=page_data, status=200))
page.goto(url_with_slash)
return page
@debug
def _get_turnstile_response(self, page: Page, max_attempts: int = 10, invisible: bool = False) -> Optional[str]:
"""Attempt to retrieve Turnstile response."""
attempts = 0
while attempts < max_attempts:
turnstile_check = page.eval_on_selector(
"[name=cf-turnstile-response]",
"el => el.value"
)
if turnstile_check == "":
debug(f"Attempt {attempts + 1}: No Turnstile response yet.")
if not invisible:
page.evaluate("document.querySelector('.cf-turnstile').style.width = '70px'")
page.click(".cf-turnstile")
time.sleep(0.5)
attempts += 1
else:
turnstile_element = page.query_selector("[name=cf-turnstile-response]")
if turnstile_element:
return turnstile_element.get_attribute("value")
break
return None
@debug
def solve(self, url: str, sitekey: str = None, invisible: bool = False, cookies: dict = None, action: str = None, cdata: str = None) -> TurnstileResult:
"""
Solve the Turnstile challenge and return the result.
Args:
url: The URL where the Turnstile challenge is hosted
sitekey: The Turnstile sitekey
invisible: Whether the Turnstile challenge is invisible
cookies: Optional dictionary of cookies to set
action: Optional action parameter for Turnstile
cdata: Optional custom data parameter for Turnstile
Returns:
TurnstileResult object containing the solution details
"""
self.loader.start()
start_time = time.time()
try:
if self.browser_type == "chromium":
with sync_playwright() as playwright:
browser = playwright.chromium.launch(headless=self.headless, args=self.browser_args)
context = browser.new_context()
if cookies:
domain = url.split("//")[-1].split("/")[0]
cookie_list = []
for name, value in cookies.items():
cookie_list.append({
"name": name,
"value": str(value),
"domain": domain,
"path": "/"
})
if cookie_list:
context.add_cookies(cookie_list)
try:
page = self._setup_page(context, url, sitekey, action, cdata)
turnstile_value = self._get_turnstile_response(page, invisible=invisible)
elapsed_time = round(time.time() - start_time, 3)
if not turnstile_value:
result = TurnstileResult(
turnstile_value=None,
elapsed_time_seconds=elapsed_time,
status="failure",
reason="Max attempts reached without token retrieval"
)
self.log.failure("Failed to retrieve Turnstile value.")
else:
result = TurnstileResult(
turnstile_value=turnstile_value,
elapsed_time_seconds=elapsed_time,
status="success"
)
self.loader.stop()
self.log.message(
"Cloudflare",
f"Successfully solved captcha: {turnstile_value[:45]}...",
start=start_time,
end=time.time()
)
except Exception as e:
self.log.failure(f"Error during captcha solving: {str(e)}")
raise
finally:
try:
context.close()
browser.close()
except Exception as e:
self.log.failure(f"Error closing browser: {str(e)}")
debug(f"Elapsed time: {result.elapsed_time_seconds} seconds")
debug("Browser closed. Returning result.")
elif self.browser_type == "chrome":
with sync_playwright() as playwright:
browser = playwright.chromium.launch_persistent_context(
user_data_dir=f"{os.getcwd()}/tmp/turnstile-chrome-{''.join(random.choices(string.ascii_letters + string.digits, k=10))}",
channel="chrome",
headless=self.headless,
no_viewport=True,
)
try:
page = self._setup_page(browser, url, sitekey, action, cdata)
turnstile_value = self._get_turnstile_response(page, invisible=invisible)
elapsed_time = round(time.time() - start_time, 3)
if not turnstile_value:
result = TurnstileResult(
turnstile_value=None,
elapsed_time_seconds=elapsed_time,
status="failure",
reason="Max attempts reached without token retrieval"
)
self.log.failure("Failed to retrieve Turnstile value.")
else:
result = TurnstileResult(
turnstile_value=turnstile_value,
elapsed_time_seconds=elapsed_time,
status="success"
)
self.loader.stop()
self.log.message(
"Cloudflare",
f"Successfully solved captcha: {turnstile_value[:45]}...",
start=start_time,
end=time.time()
)
except Exception as e:
self.log.failure(f"Error during captcha solving: {str(e)}")
raise
finally:
try:
browser.close()
except Exception as e:
self.log.failure(f"Error closing browser: {str(e)}")
debug(f"Elapsed time: {result.elapsed_time_seconds} seconds")
debug("Browser closed. Returning result.")
elif self.browser_type == "msedge":
with sync_playwright() as playwright:
browser = playwright.chromium.launch_persistent_context(
user_data_dir=f"{os.getcwd()}/tmp/turnstile-edge-{''.join(random.choices(string.ascii_letters + string.digits, k=10))}",
channel="msedge",
headless=self.headless,
no_viewport=True,
)
try:
page = self._setup_page(browser, url, sitekey, action, cdata)
turnstile_value = self._get_turnstile_response(page, invisible=invisible)
elapsed_time = round(time.time() - start_time, 3)
if not turnstile_value:
result = TurnstileResult(
turnstile_value=None,
elapsed_time_seconds=elapsed_time,
status="failure",
reason="Max attempts reached without token retrieval"
)
self.log.failure("Failed to retrieve Turnstile value.")
else:
result = TurnstileResult(
turnstile_value=turnstile_value,
elapsed_time_seconds=elapsed_time,
status="success"
)
self.loader.stop()
self.log.message(
"Cloudflare",
f"Successfully solved captcha: {turnstile_value[:45]}...",
start=start_time,
end=time.time()
)
except Exception as e:
self.log.failure(f"Error during captcha solving: {str(e)}")
raise
finally:
try:
browser.close()
except Exception as e:
self.log.failure(f"Error closing browser: {str(e)}")
debug(f"Elapsed time: {result.elapsed_time_seconds} seconds")
debug("Browser closed. Returning result.")
elif self.browser_type == "camoufox":
browser = Camoufox(headless=self.headless).start()
try:
page = self._setup_page(browser, url, sitekey, action, cdata)
turnstile_value = self._get_turnstile_response(page, invisible=invisible)
elapsed_time = round(time.time() - start_time, 3)
if not turnstile_value:
result = TurnstileResult(
turnstile_value=None,
elapsed_time_seconds=elapsed_time,
status="failure",
reason="Max attempts reached without token retrieval"
)
self.log.failure("Failed to retrieve Turnstile value.")
else:
result = TurnstileResult(
turnstile_value=turnstile_value,
elapsed_time_seconds=elapsed_time,
status="success"
)
self.loader.stop()
self.log.message(
"Cloudflare",
f"Successfully solved captcha: {turnstile_value[:45]}...",
start=start_time,
end=time.time()
)
except Exception as e:
self.log.failure(f"Error during captcha solving: {str(e)}")
raise
finally:
try:
browser.close()
except Exception as e:
self.log.failure(f"Error closing browser: {str(e)}")
debug(f"Elapsed time: {result.elapsed_time_seconds} seconds")
debug("Browser closed. Returning result.")
except Exception as e:
elapsed_time = round(time.time() - start_time, 3)
self.loader.stop()
return TurnstileResult(
turnstile_value=None,
elapsed_time_seconds=elapsed_time,
status="error",
reason=str(e)
)
return result
class ChallengeSolver: #TODO
pass
@debug
def get_turnstile_token(headless: bool = False, url: str = None, sitekey: str = None, invisible: bool = False,
cookies: dict = None, user_agent: str = None, debug: bool = False, browser_type: str = "chromium",
action: str = None, cdata: str = None) -> Dict:
"""Legacy wrapper function for backward compatibility."""
browser_types = [
'chromium',
'chrome',
'camoufox',
'msedge',
]
if browser_type not in browser_types:
log.error(f"Unknown browser type: {browser_type} Available browser types: {browser_types}")
return {"status": "error", "error": f"Unknown browser type: {browser_type}"}
elif headless is True and user_agent is None and browser_type != "camoufox":
log.error(f"You must specify a User-Agent for Turnstile Solver or use camoufox without useragent")
return {"status": "error", "error": "Headless mode requires a useragent"}
solver = TurnstileSolver(headless=headless, useragent=user_agent, debug=debug, browser_type=browser_type)
result = solver.solve(
url=url,
sitekey=sitekey,
invisible=invisible,
cookies=cookies,
action=action,
cdata=cdata
)
return result.__dict__
if __name__ == "__main__":
result = get_turnstile_token(
url="https://onlyfans.com",
sitekey="0x4AAAAAAAxTpmbMvo7Qj6zy",
browser_type="chromium",
debug=True
)
print(result)