File size: 2,682 Bytes
c3d945e
a261b8e
30ee8a0
c612743
a261b8e
 
 
 
 
 
 
 
 
 
 
 
 
 
c612743
 
 
 
 
30ee8a0
 
a261b8e
 
 
30ee8a0
a261b8e
 
 
 
 
 
 
 
 
 
 
 
 
 
30ee8a0
a261b8e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
30ee8a0
a261b8e
 
 
 
 
 
 
 
 
 
30ee8a0
 
 
 
a261b8e
30ee8a0
 
 
 
 
 
 
 
 
 
50b7548
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
# main.py
import os
import gradio as gr
import spaces
from fastapi import FastAPI
from fastapi.responses import JSONResponse
from playwright.async_api import async_playwright
from turnstile import solve_turnstile

app = FastAPI()

DOMAIN = os.getenv("DOMAIN", "https://fourstore-cloudflare.hf.space")
TIMEOUT = int(os.getenv("timeOut", 60000))
BROWSER_LIMIT = int(os.getenv("browserLimit", 20))

_state = {"browser_limit": BROWSER_LIMIT}


@spaces.GPU
def _noop():
    pass


@app.get("/api")
async def api_root():
    return {
        "message": "Server is running!",
        "domain": DOMAIN,
        "endpoint": f"{DOMAIN}/gradio/turnstile",
        "status": {
            "browserLimit": _state["browser_limit"],
            "timeOut": TIMEOUT,
        },
    }


@app.post("/turnstile")
async def turnstile_route(body: dict):
    domain = body.get("domain")
    site_key = body.get("siteKey")
    proxy = body.get("proxy")

    if not domain or not site_key:
        return JSONResponse({"message": "Missing domain or siteKey"}, status_code=400)

    if _state["browser_limit"] <= 0:
        return JSONResponse({"message": "Too Many Requests"}, status_code=429)

    _state["browser_limit"] -= 1
    pw = browser = None

    try:
        pw = await async_playwright().start()
        launch_args = {
            "headless": True,
            "args": ["--no-sandbox", "--disable-dev-shm-usage"],
        }
        if proxy and proxy.get("hostname"):
            launch_args["proxy"] = {
                "server": f"http://{proxy['hostname']}:{proxy['port']}"
            }
        browser = await pw.chromium.launch(**launch_args)
        context = await browser.new_context()
        page = await context.new_page()

        async def block(route):
            if route.request.resource_type in ("image", "stylesheet", "font", "media"):
                await route.abort()
            else:
                await route.continue_()

        await page.route("**/*", block)
        await page.goto("about:blank")

        token = await solve_turnstile(domain, site_key, page, proxy)
        return {"code": 200, "token": token}

    except Exception as e:
        return JSONResponse({"code": 500, "message": str(e)}, status_code=500)
    finally:
        if browser:
            try:
                await browser.close()
            except Exception:
                pass
        if pw:
            try:
                await pw.stop()
            except Exception:
                pass
        _state["browser_limit"] += 1


with gr.Blocks() as demo:
    gr.Markdown("Turnstile Service. API endpoint: /gradio/turnstile")

app = gr.mount_gradio_app(app, demo, path="/gradio")