Spaces:
Paused
Paused
File size: 16,569 Bytes
17b5c38 |
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 |
import os
import sys
import time
import uuid
import json
import random
import logging
import asyncio
import argparse
from quart import Quart, request, jsonify
from camoufox.async_api import AsyncCamoufox
from patchright.async_api import async_playwright
COLORS = {
'MAGENTA': '\033[35m',
'BLUE': '\033[34m',
'GREEN': '\033[32m',
'YELLOW': '\033[33m',
'RED': '\033[31m',
'RESET': '\033[0m',
}
class CustomLogger(logging.Logger):
@staticmethod
def format_message(level, color, message):
timestamp = time.strftime('%H:%M:%S')
return f"[{timestamp}] [{COLORS.get(color)}{level}{COLORS.get('RESET')}] -> {message}"
def debug(self, message, *args, **kwargs):
super().debug(self.format_message('DEBUG', 'MAGENTA', message), *args, **kwargs)
def info(self, message, *args, **kwargs):
super().info(self.format_message('INFO', 'BLUE', message), *args, **kwargs)
def success(self, message, *args, **kwargs):
super().info(self.format_message('SUCCESS', 'GREEN', message), *args, **kwargs)
def warning(self, message, *args, **kwargs):
super().warning(self.format_message('WARNING', 'YELLOW', message), *args, **kwargs)
def error(self, message, *args, **kwargs):
super().error(self.format_message('ERROR', 'RED', message), *args, **kwargs)
logging.setLoggerClass(CustomLogger)
logger = logging.getLogger("TurnstileAPIServer")
logger.setLevel(logging.DEBUG)
handler = logging.StreamHandler(sys.stdout)
logger.addHandler(handler)
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>
<script>
async function fetchIP() {
try {
const response = await fetch('https://api64.ipify.org?format=json');
const data = await response.json();
document.getElementById('ip-display').innerText = `Your IP: ${data.ip}`;
} catch (error) {
console.error('Error fetching IP:', error);
document.getElementById('ip-display').innerText = 'Failed to fetch IP';
}
}
window.onload = fetchIP;
</script>
</head>
<body>
<!-- cf turnstile -->
<p id="ip-display">Fetching your IP...</p>
</body>
</html>
"""
def __init__(self, headless: bool, useragent: str, debug: bool, browser_type: str, thread: int, proxy_support: bool):
self.app = Quart(__name__)
self.debug = debug
self.results = self._load_results()
self.browser_type = browser_type
self.headless = headless
self.useragent = useragent
self.thread_count = thread
self.proxy_support = proxy_support
self.browser_pool = asyncio.Queue()
self.browser_args = []
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:
logger.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:
logger.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."""
logger.info("Starting browser initialization")
try:
await self._initialize_browser()
except Exception as e:
logger.error(f"Failed to initialize browser: {str(e)}")
raise
async def _initialize_browser(self) -> None:
"""Initialize the browser and create the page pool."""
if self.browser_type in ['chromium', 'chrome', 'msedge']:
playwright = await async_playwright().start()
elif self.browser_type == "camoufox":
camoufox = AsyncCamoufox(headless=self.headless)
for _ in range(self.thread_count):
if self.browser_type in ['chromium', 'chrome', 'msedge']:
browser = await playwright.chromium.launch(
channel=self.browser_type,
headless=self.headless,
args=self.browser_args
)
elif self.browser_type == "camoufox":
browser = await camoufox.start()
await self.browser_pool.put((_+1, browser))
if self.debug:
logger.success(f"Browser {_ + 1} initialized successfully")
logger.success(f"Browser pool initialized with {self.browser_pool.qsize()} browsers")
async def _solve_turnstile(self, task_id: str, url: str, sitekey: str, action: str = None, cdata: str = None):
"""Solve the Turnstile challenge."""
proxy = None
index, browser = await self.browser_pool.get()
if self.proxy_support:
proxy_file_path = os.path.join(os.getcwd(), "proxies.txt")
with open(proxy_file_path) as proxy_file:
proxies = [line.strip() for line in proxy_file if line.strip()]
proxy = random.choice(proxies) if proxies else None
if proxy:
parts = proxy.split(':')
if len(parts) == 3:
context = await browser.new_context(proxy={"server": f"{proxy}"})
elif len(parts) == 5:
proxy_scheme, proxy_ip, proxy_port, proxy_user, proxy_pass = parts
context = await browser.new_context(proxy={"server": f"{proxy_scheme}://{proxy_ip}:{proxy_port}", "username": proxy_user, "password": proxy_pass})
else:
raise ValueError("Invalid proxy format")
else:
context = await browser.new_context()
else:
context = await browser.new_context()
page = await context.new_page()
start_time = time.time()
try:
if self.debug:
logger.debug(f"Browser {index}: Starting Turnstile solve for URL: {url} with Sitekey: {sitekey} | Proxy: {proxy}")
logger.debug(f"Browser {index}: Setting up page data and route")
url_with_slash = url + "/" if not url.endswith("/") else url
turnstile_div = f'<div class="cf-turnstile" style="background: white;" data-sitekey="{sitekey}"' + (f' data-action="{action}"' if action else '') + (f' data-cdata="{cdata}"' if cdata else '') + '></div>'
page_data = self.HTML_TEMPLATE.replace("<!-- cf turnstile -->", turnstile_div)
await page.route(url_with_slash, lambda route: route.fulfill(body=page_data, status=200))
await page.goto(url_with_slash)
if self.debug:
logger.debug(f"Browser {index}: Setting up Turnstile widget dimensions")
await page.eval_on_selector("//div[@class='cf-turnstile']", "el => el.style.width = '70px'")
if self.debug:
logger.debug(f"Browser {index}: Starting Turnstile response retrieval loop")
for _ in range(10):
try:
turnstile_check = await page.input_value("[name=cf-turnstile-response]", timeout=2000)
if turnstile_check == "":
if self.debug:
logger.debug(f"Browser {index}: Attempt {_} - No Turnstile response yet")
await page.locator("//div[@class='cf-turnstile']").click(timeout=1000)
await asyncio.sleep(0.5)
else:
elapsed_time = round(time.time() - start_time, 3)
logger.success(f"Browser {index}: Successfully solved captcha - {COLORS.get('MAGENTA')}{turnstile_check[:10]}{COLORS.get('RESET')} in {COLORS.get('GREEN')}{elapsed_time}{COLORS.get('RESET')} Seconds")
self.results[task_id] = {"value": turnstile_check, "elapsed_time": elapsed_time}
self._save_results()
break
except:
pass
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}
if self.debug:
logger.error(f"Browser {index}: Error solving Turnstile in {COLORS.get('RED')}{elapsed_time}{COLORS.get('RESET')} Seconds")
except Exception as e:
elapsed_time = round(time.time() - start_time, 3)
self.results[task_id] = {"value": "CAPTCHA_FAIL", "elapsed_time": elapsed_time}
if self.debug:
logger.error(f"Browser {index}: Error solving Turnstile: {str(e)}")
finally:
if self.debug:
logger.debug(f"Browser {index}: Clearing page state")
await context.close()
await self.browser_pool.put((index, browser))
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')
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))
if self.debug:
logger.debug(f"Request completed with taskid {task_id}.")
return jsonify({"task_id": task_id}), 202
except Exception as e:
logger.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]
status_code = 200
if "CAPTCHA_FAIL" in result:
status_code = 422
return 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>
</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</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/Theyka" class="text-red-300 hover:underline">Theyka</a>
and <a href="https://github.com/sexfrance" class="text-red-300 hover:underline">Sexfrance</a>.</p>
</div>
</div>
</body>
</html>
"""
def parse_args():
"""Parse command-line arguments."""
parser = argparse.ArgumentParser(description="Turnstile API Server")
parser.add_argument('--headless', type=bool, default=False, help='Run the browser in headless mode, without opening a graphical interface. This option requires the --useragent argument to be set (default: False)')
parser.add_argument('--useragent', type=str, default=None, help='Specify a custom User-Agent string for the browser. If not provided, the default User-Agent is used')
parser.add_argument('--debug', type=bool, default=False, help='Enable or disable debug mode for additional logging and troubleshooting information (default: False)')
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='Set the number of browser threads to use for multi-threaded mode. Increasing this will speed up execution but requires more resources (default: 1)')
parser.add_argument('--proxy', type=bool, default=False, help='Enable proxy support for the solver (Default: False)')
parser.add_argument('--host', type=str, default='127.0.0.1', help='Specify the IP address where the API solver runs. (Default: 127.0.0.1)')
parser.add_argument('--port', type=str, default='5000', help='Set the port for the API solver to listen on. (Default: 5000)')
return parser.parse_args()
def create_app(headless: bool, useragent: str, debug: bool, browser_type: str, thread: int, proxy_support: bool) -> Quart:
server = TurnstileAPIServer(headless=headless, useragent=useragent, debug=debug, browser_type=browser_type, thread=thread, proxy_support=proxy_support)
return server.app
if __name__ == '__main__':
args = parse_args()
browser_types = [
'chromium',
'chrome',
'msedge',
'camoufox',
]
if args.browser_type not in browser_types:
logger.error(f"Unknown browser type: {COLORS.get('RED')}{args.browser_type}{COLORS.get('RESET')} Available browser types: {browser_types}")
elif args.headless is True and args.useragent is None and "camoufox" not in args.browser_type:
logger.error(f"You must specify a {COLORS.get('YELLOW')}User-Agent{COLORS.get('RESET')} for Turnstile Solver or use {COLORS.get('GREEN')}camoufox{COLORS.get('RESET')} without useragent")
else:
app = create_app(headless=args.headless, debug=args.debug, useragent=args.useragent, browser_type=args.browser_type, thread=args.thread, proxy_support=args.proxy)
app.run(host=args.host, port=int(args.port))
|