Spaces:
Runtime error
Runtime error
cloudflare
Browse files- Dockerfile +12 -0
- README.md +1 -2
- main.py +84 -0
- requirements.txt +3 -0
- turnstile.py +63 -0
Dockerfile
ADDED
|
@@ -0,0 +1,12 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
FROM mcr.microsoft.com/playwright/python:v1.47.0-jammy
|
| 2 |
+
|
| 3 |
+
WORKDIR /app
|
| 4 |
+
COPY requirements.txt .
|
| 5 |
+
RUN pip install --no-cache-dir -r requirements.txt
|
| 6 |
+
|
| 7 |
+
COPY . .
|
| 8 |
+
|
| 9 |
+
ENV PORT=7860
|
| 10 |
+
EXPOSE 7860
|
| 11 |
+
|
| 12 |
+
CMD ["uvicorn", "main:app", "--host", "0.0.0.0", "--port", "7860"]
|
README.md
CHANGED
|
@@ -3,10 +3,9 @@ title: Cloudflare
|
|
| 3 |
emoji: 🐢
|
| 4 |
colorFrom: pink
|
| 5 |
colorTo: blue
|
| 6 |
-
sdk:
|
| 7 |
sdk_version: 6.32.0
|
| 8 |
python_version: '3.12'
|
| 9 |
-
app_file: app.py
|
| 10 |
pinned: false
|
| 11 |
license: apache-2.0
|
| 12 |
short_description: cloudflare
|
|
|
|
| 3 |
emoji: 🐢
|
| 4 |
colorFrom: pink
|
| 5 |
colorTo: blue
|
| 6 |
+
sdk: docker
|
| 7 |
sdk_version: 6.32.0
|
| 8 |
python_version: '3.12'
|
|
|
|
| 9 |
pinned: false
|
| 10 |
license: apache-2.0
|
| 11 |
short_description: cloudflare
|
main.py
ADDED
|
@@ -0,0 +1,84 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
from fastapi import FastAPI
|
| 3 |
+
from fastapi.responses import JSONResponse
|
| 4 |
+
from playwright.async_api import async_playwright
|
| 5 |
+
|
| 6 |
+
from turnstile import solve_turnstile
|
| 7 |
+
|
| 8 |
+
app = FastAPI()
|
| 9 |
+
|
| 10 |
+
DOMAIN = os.getenv("DOMAIN", "https://fourstore-cloudflare.hf.space")
|
| 11 |
+
TIMEOUT = int(os.getenv("timeOut", 60000))
|
| 12 |
+
BROWSER_LIMIT = int(os.getenv("browserLimit", 20))
|
| 13 |
+
|
| 14 |
+
_state = {"browser_limit": BROWSER_LIMIT}
|
| 15 |
+
|
| 16 |
+
|
| 17 |
+
@app.get("/")
|
| 18 |
+
async def root():
|
| 19 |
+
return {
|
| 20 |
+
"message": "Server is running!",
|
| 21 |
+
"domain": DOMAIN,
|
| 22 |
+
"endpoint": f"{DOMAIN}/turnstile",
|
| 23 |
+
"status": {
|
| 24 |
+
"browserLimit": _state["browser_limit"],
|
| 25 |
+
"timeOut": TIMEOUT,
|
| 26 |
+
},
|
| 27 |
+
}
|
| 28 |
+
|
| 29 |
+
|
| 30 |
+
@app.post("/turnstile")
|
| 31 |
+
async def turnstile_route(body: dict):
|
| 32 |
+
domain = body.get("domain")
|
| 33 |
+
site_key = body.get("siteKey")
|
| 34 |
+
proxy = body.get("proxy")
|
| 35 |
+
|
| 36 |
+
if not domain or not site_key:
|
| 37 |
+
return JSONResponse(
|
| 38 |
+
{"message": "Missing domain or siteKey"}, status_code=400
|
| 39 |
+
)
|
| 40 |
+
|
| 41 |
+
if _state["browser_limit"] <= 0:
|
| 42 |
+
return JSONResponse({"message": "Too Many Requests"}, status_code=429)
|
| 43 |
+
|
| 44 |
+
_state["browser_limit"] -= 1
|
| 45 |
+
pw = browser = None
|
| 46 |
+
|
| 47 |
+
try:
|
| 48 |
+
pw = await async_playwright().start()
|
| 49 |
+
|
| 50 |
+
launch_args = {
|
| 51 |
+
"headless": True,
|
| 52 |
+
"args": ["--no-sandbox", "--disable-dev-shm-usage"],
|
| 53 |
+
}
|
| 54 |
+
if proxy and proxy.get("hostname"):
|
| 55 |
+
launch_args["proxy"] = {
|
| 56 |
+
"server": f"http://{proxy['hostname']}:{proxy['port']}"
|
| 57 |
+
}
|
| 58 |
+
|
| 59 |
+
browser = await pw.chromium.launch(**launch_args)
|
| 60 |
+
context = await browser.new_context()
|
| 61 |
+
page = await context.new_page()
|
| 62 |
+
|
| 63 |
+
# block resource berat
|
| 64 |
+
async def block(route):
|
| 65 |
+
if route.request.resource_type in ("image", "stylesheet", "font", "media"):
|
| 66 |
+
await route.abort()
|
| 67 |
+
else:
|
| 68 |
+
await route.continue_()
|
| 69 |
+
await page.route("**/*", block)
|
| 70 |
+
await page.goto("about:blank")
|
| 71 |
+
|
| 72 |
+
token = await solve_turnstile(domain, site_key, page, proxy)
|
| 73 |
+
return {"code": 200, "token": token}
|
| 74 |
+
|
| 75 |
+
except Exception as e:
|
| 76 |
+
return JSONResponse({"code": 500, "message": str(e)}, status_code=500)
|
| 77 |
+
finally:
|
| 78 |
+
if browser:
|
| 79 |
+
try: await browser.close()
|
| 80 |
+
except Exception: pass
|
| 81 |
+
if pw:
|
| 82 |
+
try: await pw.stop()
|
| 83 |
+
except Exception: pass
|
| 84 |
+
_state["browser_limit"] += 1
|
requirements.txt
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
fastapi==0.115.0
|
| 2 |
+
uvicorn[standard]==0.30.6
|
| 3 |
+
playwright==1.47.0
|
turnstile.py
ADDED
|
@@ -0,0 +1,63 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
from playwright.async_api import Page
|
| 3 |
+
|
| 4 |
+
TIMEOUT = int(os.getenv("timeOut", 60000))
|
| 5 |
+
|
| 6 |
+
|
| 7 |
+
async def solve_turnstile(domain: str, site_key: str, page: Page,
|
| 8 |
+
proxy: dict | None = None) -> str:
|
| 9 |
+
if not domain:
|
| 10 |
+
raise ValueError("Missing domain parameter")
|
| 11 |
+
if not site_key:
|
| 12 |
+
raise ValueError("Missing siteKey parameter")
|
| 13 |
+
|
| 14 |
+
# auth proxy (opsional)
|
| 15 |
+
if proxy and proxy.get("username") and proxy.get("password"):
|
| 16 |
+
await page.context.set_http_credentials({
|
| 17 |
+
"username": proxy["username"],
|
| 18 |
+
"password": proxy["password"],
|
| 19 |
+
})
|
| 20 |
+
|
| 21 |
+
html_content = f"""
|
| 22 |
+
<!DOCTYPE html>
|
| 23 |
+
<html lang="en">
|
| 24 |
+
<body>
|
| 25 |
+
<div class="turnstile"></div>
|
| 26 |
+
<script src="https://challenges.cloudflare.com/turnstile/v0/api.js?onload=onloadTurnstileCallback" defer></script>
|
| 27 |
+
<script>
|
| 28 |
+
window.onloadTurnstileCallback = function () {{
|
| 29 |
+
turnstile.render('.turnstile', {{
|
| 30 |
+
sitekey: '{site_key}',
|
| 31 |
+
callback: function (token) {{
|
| 32 |
+
var c = document.createElement('input');
|
| 33 |
+
c.type = 'hidden';
|
| 34 |
+
c.name = 'cf-response';
|
| 35 |
+
c.value = token;
|
| 36 |
+
document.body.appendChild(c);
|
| 37 |
+
}},
|
| 38 |
+
}});
|
| 39 |
+
}};
|
| 40 |
+
</script>
|
| 41 |
+
</body>
|
| 42 |
+
</html>
|
| 43 |
+
"""
|
| 44 |
+
|
| 45 |
+
async def handler(route):
|
| 46 |
+
url = route.request.url
|
| 47 |
+
if url in (domain, domain + "/") and route.request.resource_type == "document":
|
| 48 |
+
await route.fulfill(status=200, content_type="text/html", body=html_content)
|
| 49 |
+
else:
|
| 50 |
+
await route.continue_()
|
| 51 |
+
|
| 52 |
+
await page.route("**/*", handler)
|
| 53 |
+
await page.goto(domain, wait_until="domcontentloaded")
|
| 54 |
+
await page.wait_for_selector('[name="cf-response"]', timeout=TIMEOUT)
|
| 55 |
+
|
| 56 |
+
token = await page.evaluate(
|
| 57 |
+
"() => document.querySelector('[name=\"cf-response\"]')?.value || null"
|
| 58 |
+
)
|
| 59 |
+
|
| 60 |
+
if not token or len(token) < 10:
|
| 61 |
+
raise RuntimeError("Failed to get token")
|
| 62 |
+
|
| 63 |
+
return token
|