forked from Theyka/Turnstile-Solver
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapi_solver.py
435 lines (373 loc) · 17.7 KB
/
api_solver.py
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
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
import os
import time
import json
import uuid
import argparse
import random
import string
import asyncio
from quart import Quart, request, jsonify
from typing import Dict, Optional
from dataclasses import dataclass
from patchright.async_api import async_playwright, Page, BrowserContext
from camoufox.async_api import AsyncCamoufox
from logmagix import Loader
from collections import deque
from functools import wraps
from .logger import log
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)
async def wrapper(*args, **kwargs):
result = await 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 TurnstileAPIServer:
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" async></script>
</head>
<body>
<!-- cf turnstile -->
</body>
</html>
"""
def __init__(self, headless: bool = False, useragent: str = None, debug: bool = False, browser_type: str = "chromium", thread: int = 1):
global DEBUG
DEBUG = debug
self.debug = debug
self.app = Quart(__name__)
self.log = log
self.loader = Loader(desc="Solving captcha...", timeout=0.05)
self.results = self._load_results()
self.browser_type = browser_type
self.headless = headless
self.useragent = useragent
self.thread_count = thread
self.browser_pool = asyncio.Queue()
self.pages_per_browser = 10 # Number of pages per browser instance
self.page_pool = asyncio.Queue()
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 useragent:
self.browser_args.append(f"--user-agent={useragent}")
self._setup_routes()
@staticmethod
def _load_results():
"""Load previous results from results.json."""
try:
if os.path.exists("results.json"):
with open("results.json", "r") as f:
return json.load(f)
except (json.JSONDecodeError, IOError) as e:
log.warning(f"Error loading results: {str(e)}. Starting with an empty results dictionary.")
return {}
def _save_results(self):
"""Save results to results.json."""
try:
with open("results.json", "w") as result_file:
json.dump(self.results, result_file, indent=4)
except IOError as e:
log.error(f"Error saving results to file: {str(e)}")
def _setup_routes(self) -> None:
"""Set up the application routes."""
self.app.before_serving(self._startup)
self.app.route('/turnstile', methods=['GET'])(self.process_turnstile)
self.app.route('/result', methods=['GET'])(self.get_result)
self.app.route('/')(self.index)
async def _startup(self) -> None:
"""Initialize the browser and page pool on startup."""
log.info("Starting browser initialization")
try:
await self._initialize_browser()
except Exception as e:
log.error(f"Failed to initialize browser: {str(e)}")
raise
async def _initialize_browser(self) -> None:
"""Initialize browsers with multiple pages per instance."""
if self.browser_type == "chromium" or self.browser_type == "chrome" or self.browser_type == "msedge":
playwright = await async_playwright().start()
elif self.browser_type == "camoufox":
camoufox = AsyncCamoufox(headless=self.headless)
page_counter = 0
for browser_idx in range(self.thread_count):
if self.browser_type == "chromium":
browser = await playwright.chromium.launch(
headless=self.headless,
args=self.browser_args
)
elif self.browser_type == "chrome":
browser = await 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,
)
elif self.browser_type == "msedge":
browser = await 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,
)
elif self.browser_type == "camoufox":
browser = await camoufox.start()
# Create multiple pages per browser
for page_idx in range(self.pages_per_browser):
page_counter += 1
if self.browser_type in ["chrome", "msedge"] and page_idx == 0:
page = browser.pages[0]
else:
page = await browser.new_page()
await self.page_pool.put({
'id': page_counter,
'browser': browser,
'page': page,
'in_use': False
})
if self.debug:
log.success(f"Browser {browser_idx + 1} initialized with {self.pages_per_browser} pages")
log.success(f"Page pool initialized with {self.page_pool.qsize()} total pages")
async def _cleanup_page(self, page_data: dict) -> None:
"""Clean up a page after use."""
try:
page = page_data['page']
await page.goto("about:blank")
await page.evaluate("""() => {
try {
window.localStorage.clear();
window.sessionStorage.clear();
} catch (e) {}
}""")
except Exception as e:
if self.debug:
log.warning(f"Error during page cleanup: {str(e)}")
async def _solve_turnstile(self, task_id: str, url: str, sitekey: str, action: str = None, cdata: str = None, invisible: bool = False):
"""Solve the Turnstile challenge using the page pool."""
page_data = await self.page_pool.get()
start_time = time.time()
try:
if self.debug:
log.debug(f"Page {page_data['id']}: Starting Turnstile solve for URL: {url}")
url_with_slash = url + "/" if not url.endswith("/") else url
turnstile_div = f'<div class="cf-turnstile" data-sitekey="{sitekey}"' + (f' data-action="{action}"' if action else '') + (f' data-cdata="{cdata}"' if cdata else '') + '></div>'
page_data['page_data'] = self.HTML_TEMPLATE.replace("<!-- cf turnstile -->", turnstile_div)
await page_data['page'].route(url_with_slash, lambda route: route.fulfill(body=page_data['page_data'], status=200))
await page_data['page'].goto(url_with_slash)
if self.debug:
log.debug(f"Page {page_data['id']}: Starting Turnstile response retrieval loop")
for attempt in range(10):
try:
turnstile_check = await page_data['page'].eval_on_selector(
"[name=cf-turnstile-response]",
"el => el.value"
)
if turnstile_check == "":
if self.debug:
log.debug(f"Page {page_data['id']}: Attempt {attempt+1} - No Turnstile response yet")
if not invisible:
await page_data['page'].evaluate("document.querySelector('.cf-turnstile').style.width = '70px'")
await page_data['page'].click(".cf-turnstile")
await asyncio.sleep(0.5)
else:
element = await page_data['page'].query_selector("[name=cf-turnstile-response]")
if element:
value = await element.get_attribute("value")
elapsed_time = round(time.time() - start_time, 3)
log.success(f"Page {page_data['id']}: Successfully solved captcha in {elapsed_time} seconds")
self.results[task_id] = {"value": value, "elapsed_time": elapsed_time}
self._save_results()
break
except Exception as e:
log.warning(f"Page {page_data['id']}: Error during attempt {attempt+1}: {str(e)}")
await asyncio.sleep(0.5)
continue
if self.results.get(task_id) == "CAPTCHA_NOT_READY":
elapsed_time = round(time.time() - start_time, 3)
self.results[task_id] = {"value": "CAPTCHA_FAIL", "elapsed_time": elapsed_time}
log.error(f"Page {page_data['id']}: Failed to solve Turnstile in {elapsed_time} seconds")
except Exception as e:
elapsed_time = round(time.time() - start_time, 3)
self.results[task_id] = {"value": "CAPTCHA_FAIL", "elapsed_time": elapsed_time}
log.error(f"Page {page_data['id']}: Error solving Turnstile: {str(e)}")
finally:
await self._cleanup_page(page_data)
await self.page_pool.put(page_data)
async def process_turnstile(self):
"""Handle the /turnstile endpoint requests."""
url = request.args.get('url')
sitekey = request.args.get('sitekey')
action = request.args.get('action')
cdata = request.args.get('cdata')
invisible = request.args.get('invisible', 'false').lower() == 'true'
if not url or not sitekey:
return jsonify({
"status": "error",
"error": "Both 'url' and 'sitekey' are required"
}), 400
task_id = str(uuid.uuid4())
self.results[task_id] = "CAPTCHA_NOT_READY"
try:
asyncio.create_task(self._solve_turnstile(
task_id=task_id,
url=url,
sitekey=sitekey,
action=action,
cdata=cdata,
invisible=invisible
))
if self.debug:
log.debug(f"Request completed with taskid {task_id}.")
return jsonify({"task_id": task_id}), 202
except Exception as e:
log.error(f"Unexpected error processing request: {str(e)}")
return jsonify({
"status": "error",
"error": str(e)
}), 500
async def get_result(self):
"""Return solved data"""
task_id = request.args.get('id')
if not task_id or task_id not in self.results:
return jsonify({"status": "error", "error": "Invalid task ID/Request parameter"}), 400
result = self.results[task_id]
# If it's still processing
if result == "CAPTCHA_NOT_READY":
return jsonify({"status": "processing"}), 202
status_code = 200
if result.get('value') == "CAPTCHA_FAIL":
status_code = 422
return jsonify(result), status_code
@staticmethod
async def index():
"""Serve the API documentation page."""
return """
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Turnstile Solver API</title>
<script src="https://cdn.tailwindcss.com"></script>
</head>
<body class="bg-gray-900 text-gray-200 min-h-screen flex items-center justify-center">
<div class="bg-gray-800 p-8 rounded-lg shadow-md max-w-2xl w-full border border-red-500">
<h1 class="text-3xl font-bold mb-6 text-center text-red-500">Welcome to Turnstile Solver API</h1>
<p class="mb-4 text-gray-300">To use the turnstile service, send a GET request to
<code class="bg-red-700 text-white px-2 py-1 rounded">/turnstile</code> with the following query parameters:</p>
<ul class="list-disc pl-6 mb-6 text-gray-300">
<li><strong>url</strong>: The URL where Turnstile is to be validated</li>
<li><strong>sitekey</strong>: The site key for Turnstile</li>
<li><strong>action</strong>: (Optional) Custom action parameter</li>
<li><strong>cdata</strong>: (Optional) Custom data parameter</li>
<li><strong>invisible</strong>: (Optional) Set to 'true' for invisible captchas</li>
</ul>
<div class="bg-gray-700 p-4 rounded-lg mb-6 border border-red-500">
<p class="font-semibold mb-2 text-red-400">Example usage:</p>
<code class="text-sm break-all text-red-300">/turnstile?url=https://example.com&sitekey=sitekey&invisible=true</code>
</div>
<div class="bg-gray-700 p-4 rounded-lg mb-6 border border-red-500">
<p class="font-semibold mb-2 text-red-400">Then check the result with:</p>
<code class="text-sm break-all text-red-300">/result?id=your_task_id</code>
</div>
<div class="bg-red-900 border-l-4 border-red-600 p-4 mb-6">
<p class="text-red-200 font-semibold">This project is inspired by
<a href="https://github.com/Body-Alhoha/turnaround" class="text-red-300 hover:underline">Turnaround</a>
and is currently maintained by
<a href="https://github.com/sexfrance" class="text-red-300 hover:underline">Sexfrance</a>.</p>
</div>
</div>
</body>
</html>
"""
async def shutdown(self):
"""Cleanup all browsers and pages."""
while not self.page_pool.empty():
try:
page_data = await self.page_pool.get()
await self._cleanup_page(page_data)
if hasattr(page_data['browser'], 'close'):
await page_data['browser'].close()
except Exception as e:
if self.debug:
log.warning(f"Error during shutdown cleanup: {str(e)}")
def create_app(self):
"""Create and configure the application instance."""
return self.app
def parse_args():
"""Parse command-line arguments."""
parser = argparse.ArgumentParser(description="Turnstile API Server")
parser.add_argument('--headless', action='store_true', help='Run the browser in headless mode')
parser.add_argument('--useragent', type=str, default=None, help='Custom User-Agent string')
parser.add_argument('--debug', action='store_true', help='Enable debug mode')
parser.add_argument('--browser_type', type=str, default='chromium', help='Specify the browser type for the solver. Supported options: chromium, chrome, msedge, camoufox (default: chromium)')
parser.add_argument('--thread', type=int, default=1, help='Number of browser threads')
parser.add_argument('--host', type=str, default='127.0.0.1', help='Host IP address')
parser.add_argument('--port', type=int, default=5000, help='Port to listen on')
return parser.parse_args()
def create_app(headless=False, useragent=None, debug=False, browser_type="chromium", thread=1):
"""Create the Quart application."""
server = TurnstileAPIServer(
headless=headless,
useragent=useragent,
debug=debug,
browser_type=browser_type,
thread=thread
)
return server.app
if __name__ == "__main__":
args = parse_args()
browser_types = [
'chromium',
'chrome',
'msedge',
'camoufox',
]
if args.headless and args.useragent is None and args.browser_type != "camoufox":
log.critical("You must specify a User-Agent for headless mode or use camoufox")
if args.browser_type not in browser_types:
log.critical("Invalid browser type specified. Supported options: chromium, chrome, msedge, camoufox")
app = create_app(
headless=args.headless,
useragent=args.useragent,
debug=args.debug,
browser_type=args.browser_type,
thread=args.thread
)
try:
app.run(host=args.host, port=args.port)
finally:
# Ensure proper cleanup on shutdown
if hasattr(app, 'shutdown'):
asyncio.run(app.shutdown())
# Credits for the changes: github.com/sexfrance
# Credit for the original script: github.com/Theyka