File size: 9,179 Bytes
f26e640
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
# Violetics Solver

Local HTTP service that solves Cloudflare Turnstile widgets and clears
Cloudflare "Just a moment..." JS/interactive challenges. Built on
`nodriver` for the Turnstile widget path and FlareSolverr for the
JS-challenge path, behind a single unified HTTP API.

## Features

- `POST /solve` — solves a Turnstile widget and returns the token.
  Uses `nodriver` with a persistent, warm Chromium profile.
- `POST /solve-challenge` — clears a Cloudflare JS or interactive
  challenge and returns the final URL, title, cookies (filtered to the
  target domain), user-agent, and full HTML. Delegates to a bundled
  FlareSolverr instance by default; falls back to `nodriver` if
  FlareSolverr is unreachable.
- Structured, single-block log per request with per-step progress output.
- Async HTTP server (aiohttp) with internal queue. Clients may send
  requests in parallel; solves are executed serially by the browser.
- `docker compose up -d` brings up both services together — solver and
  FlareSolverr — with health-gated startup.

### Why delegate to FlareSolverr?

On headless Linux hosts (typical VPS / docker-compose targets),
Cloudflare's interactive challenge page now fingerprints every stock
Chromium build tested (Playwright, Google Chrome Stable, Debian
Chromium, Patchright, even Camoufox) and simply never mounts the
Turnstile iframe. With no iframe, there is nothing to click and the
in-process `nodriver` path times out at 60 s.

FlareSolverr ships its own stealth Chromium build that still clears
those pages (typical clearance: 10–20 s, cold profile). The Turnstile
token path (`/solve`) is unaffected — it injects the widget onto a
benign page the solver controls, so fingerprinting of the container
doesn't break iframe mount there.

## Requirements

- Docker and Docker Compose, or
- Python 3.11+ (tested on 3.13) and Xvfb for a host install

No manual Chromium install is needed. Patchright downloads and pins a
compatible Chromium build.

## Quick start

```bash
git clone git@github.com:cv3inx/turnstile-solver.git
cd turnstile-solver
docker compose up -d
```

This starts two containers:

- `violetics-solver` (this service) on `:9988`
- `violetics-flaresolverr` (FlareSolverr backend) on the internal
  compose network only — not exposed on the host.

The solver waits for FlareSolverr to report healthy before starting.

Check it:

```bash
curl http://localhost:9988/health
```

### Disabling the FlareSolverr delegation

Remove the `flaresolverr` service and the `FLARESOLVERR_URL` env from
`docker-compose.yml`, or set `FLARESOLVERR_URL=""`. `/solve-challenge`
will then run the pure-`nodriver` path. Expect timeouts on hosts where
Cloudflare fingerprints the container — see the section above.

### Plain Docker

```bash
docker build -t violetics-solver .
docker run -d --name flaresolverr --restart unless-stopped \
  ghcr.io/flaresolverr/flaresolverr:latest
docker run -d --name solver --shm-size=1gb \
  --link flaresolverr \
  -e FLARESOLVERR_URL=http://flaresolverr:8191 \
  -p 9988:9988 \
  -v solver-profile:/tmp/ts_profile \
  violetics-solver
```

`--shm-size=1gb` is required — Chromium crashes with the default 64 MB
`/dev/shm`. The volume mount preserves the Cloudflare cookie profile across
container restarts.

### Host install (optional)

```bash
pip install -r requirements.txt patchright
python -m patchright install chromium
# FlareSolverr is optional for a host install; without it /solve-challenge
# runs the nodriver path only.
export FLARESOLVERR_URL=http://localhost:8191   # if running FS locally
python service.py
```

## Configuration

Environment variables:

| Variable            | Default           | Description                                                                 |
|---------------------|-------------------|-----------------------------------------------------------------------------|
| `PORT`              | `9988`            | HTTP port                                                                   |
| `MAX_WORKERS`       | `8`               | Max concurrent HTTP requests (solves are serialised internally)             |
| `FLARESOLVERR_URL`  | _(unset)_         | Base URL of a FlareSolverr instance. When set, `/solve-challenge` delegates to it. The compose file sets this to `http://flaresolverr:8191`. |
| `CHROME_PATH`       | auto-detected     | Path to Chromium. Default looks up Patchright's install path.               |
| `TS_PROFILE_DIR`    | `/tmp/ts_profile` | Persistent profile directory                                                |
| `DISPLAY`           | `:99`             | Xvfb display (container sets this for you)                                  |

Chromium auto-detection searches:
1. `$CHROME_PATH`
2. `~/.cache/ms-playwright/chromium-*/chrome-linux*/chrome`
3. System `/usr/bin/google-chrome*` or `/usr/bin/chromium*`

When `FLARESOLVERR_URL` is set, the solver does **not** warm the
in-process browser at startup. `/solve-challenge` goes straight to
FlareSolverr; `/solve` (Turnstile token) still starts the browser on
first use.

## API

All endpoints accept and return JSON.

### `POST /solve`

Solves a Turnstile widget.

Request:

```json
{
  "sitekey": "0x4AAAAAAC3x1HiBz5IFyj7s",
  "siteurl": "https://www.example.com/",
  "timeout": 45
}
```

Response (200):

```json
{
  "token": "1.abc...xyz",
  "elapsed": 8.39
}
```

Response (500):

```json
{
  "error": "turnstile timeout after 45s",
  "elapsed": 45.2
}
```

### `POST /solve-challenge`

Clears a Cloudflare JS or interactive challenge and returns the page state.
When `FLARESOLVERR_URL` is configured, the request is proxied to
FlareSolverr transparently — callers see the same response shape either
way.

Request:

```json
{
  "siteurl": "https://api.example.com/docs",
  "timeout": 45
}
```

Response (200):

```json
{
  "url": "https://api.example.com/docs/",
  "title": "Example API",
  "user_agent": "Mozilla/5.0 ...",
  "cookies": [
    {
      "name": "cf_clearance",
      "value": "...",
      "domain": ".example.com",
      "path": "/",
      "expires": 1811226000
    }
  ],
  "html": "<!doctype html>...",
  "elapsed": 15.52
}
```

Use the returned `cf_clearance` cookie together with `user_agent` when
proxying the protected API. Both must match — Cloudflare rejects the
cookie if the user-agent differs from the one that earned it.

Note: FlareSolverr is sensitive to the exact URL form for some sites
(e.g. `/docs` times out, but `/docs/` clears). The solver automatically
retries extensionless paths with a trailing slash.

### `GET /health`

Returns service status counters.

When in FlareSolverr-delegate mode:

```json
{
  "status": "ok",
  "mode": "flaresolverr",
  "flaresolverr_url": "http://flaresolverr:8191",
  "in_flight": 0,
  "solved": 30,
  "errors": 1,
  "challenges": 11
}
```

When running the pure `nodriver` path:

```json
{
  "status": "ok",
  "max_concurrent": 8,
  "solved_total": 42,
  "in_flight": 0,
  "solved": 30,
  "errors": 1,
  "challenges": 11
}
```

## Log format

Each request produces one block with real-time progress steps in between:

```
「 NEW REQUEST 」
» ID     : 29241879
» FROM   : 172.20.0.1
» POST   : /solve
» URL    : https://www.example.com/
» KEY    : 0x4AAAAAAC3x1H...
  [29241879] opening tab -> https://www.example.com/
  [29241879] waiting for page load...
  [29241879] page loaded (1.2s)
  [29241879] injecting turnstile widget
  [29241879] turnstile ready (1.2s)
  [29241879] click #1 at (46,51)
  [29241879] click #2 at (49,52)
  [29241879] token obtained (9.0s)
» SPEED  : 9.08s
» STATUS : 200 - token 1.1Tqrqdroaa...26cb55 (538 chars)
```

For JS-challenge requests routed through FlareSolverr:

```
「 NEW REQUEST 」
» ID     : cdd14513
» FROM   : 172.20.0.1
» POST   : /solve-challenge
» URL    : https://api.example.com/docs
  [cdd14513] delegating to FlareSolverr -> http://flaresolverr:8191
  [cdd14513] flaresolverr cleared (15.5s, cookies=1)
» SPEED  : 15.52s
» STATUS : 200 - title='Example API' cookies=1 html=74236b
```

All output is written to stdout. Internal library warnings are suppressed.

## Concurrency

The service accepts many HTTP requests in parallel, but Cloudflare escalates
difficulty when multiple tabs on the same profile request a token for the
same sitekey at once. Solves are therefore serialised inside the service.

Typical throughput:

- Turnstile (`/solve`): one token every ~8 seconds
- JS challenge via FlareSolverr: ~15 s per solve (single FS worker)
- JS challenge via in-process `nodriver`, warm profile: under 2 s
- JS challenge via in-process `nodriver`, cold profile: 8–12 s

Scaling beyond single-browser throughput requires multiple independent
solver instances, each with its own warm profile and IP.

## File layout

```
solver.py            Core browser automation + FlareSolverr delegation
service.py           aiohttp HTTP wrapper and request logging
requirements.txt     Python dependencies (nodriver, aiohttp)
Dockerfile           Container image (Python + Patchright + Xvfb)
docker-compose.yml   Compose stack: solver + flaresolverr
entrypoint.sh        Container entrypoint (starts Xvfb, then service)
```

## License

MIT