Metadata-Version: 2.5
Name: cloudflare-skip
Version: 2.1.0
Summary: Skip Cloudflare challenges: solve once in a real browser, then reuse the clearance with fast HTTP requests.
Project-URL: Homepage, https://github.com/tn3w/cloudflare-skip
Project-URL: Source, https://github.com/tn3w/cloudflare-skip
Project-URL: Issues, https://github.com/tn3w/cloudflare-skip/issues
Author-email: tn3w <tn3w@protonmail.com>
License-Expression: Apache-2.0
License-File: LICENSE
Keywords: anti-bot,captcha,cf-clearance,chrome-devtools-protocol,cloudflare,cloudflare-bypass,curl-cffi,scraping,turnstile
Classifier: Development Status :: 5 - Production/Stable
Classifier: Intended Audience :: Developers
Classifier: Operating System :: OS Independent
Classifier: Programming Language :: Python :: 3 :: Only
Classifier: Programming Language :: Python :: 3.10
Classifier: Programming Language :: Python :: 3.11
Classifier: Programming Language :: Python :: 3.12
Classifier: Programming Language :: Python :: 3.13
Classifier: Programming Language :: Python :: 3.14
Classifier: Topic :: Internet :: WWW/HTTP
Classifier: Typing :: Typed
Requires-Python: >=3.10
Requires-Dist: curl-cffi>=0.16.3
Requires-Dist: websockets>=14
Description-Content-Type: text/markdown

<div align="center">

<p>
  <picture>
    <source media="(prefers-color-scheme: dark)" srcset="https://raw.githubusercontent.com/tn3w/cloudflare-skip/master/assets/non-interactive-dark.gif">
    <img src="https://raw.githubusercontent.com/tn3w/cloudflare-skip/master/assets/non-interactive-light.gif" width="49%" alt="Non-interactive challenge">
  </picture>
  <picture>
    <source media="(prefers-color-scheme: dark)" srcset="https://raw.githubusercontent.com/tn3w/cloudflare-skip/master/assets/interactive-dark.gif">
    <img src="https://raw.githubusercontent.com/tn3w/cloudflare-skip/master/assets/interactive-light.gif" width="49%" alt="Interactive challenge">
  </picture>
</p>

# cloudflare-skip

**Skip Cloudflare challenges: solve once in headless Chrome, then reuse the clearance
with fast HTTP requests. No display needed.**

[![PyPI](https://img.shields.io/pypi/v/cloudflare-skip?color=1868f2)](https://pypi.org/project/cloudflare-skip)
[![Python](https://img.shields.io/pypi/pyversions/cloudflare-skip?color=1868f2)](https://pypi.org/project/cloudflare-skip)
[![License](https://img.shields.io/badge/license-Apache--2.0-1868f2)](https://github.com/tn3w/cloudflare-skip/blob/master/LICENSE)

</div>

## Quick start

```bash
pip install cloudflare-skip
cloudflare-skip https://cl-skip.tn3w.dev               # non-interactive challenge
cloudflare-skip https://cl-skip.tn3w.dev/interactive   # interactive (checkbox) challenge
cloudflare-skip --turnstile https://cl-skip.tn3w.dev/turnstile   # print a widget token
```

```python
from cloudflare_skip import get, solve, solve_turnstile

response = get("https://cl-skip.tn3w.dev", params={"q": "x"}, timeout=10)
print(response.status_code, response.text)

clearance = solve("https://cl-skip.tn3w.dev")  # {"cookies": {...}, "user_agent": "..."}
token = solve_turnstile("https://cl-skip.tn3w.dev/turnstile")  # cf-turnstile-response
```

`get(url, **kwargs)` returns a `curl_cffi` response; keyword arguments (`params`,
`headers`, `cookies`, `timeout`, ...) go to `curl_cffi`. It raises `ChallengeError` if
Cloudflare still challenges after a solve and `TimeoutError` if the browser cannot solve
within 30s. The CLI prints the body, writes the status to stderr and exits 1 on 4xx/5xx.

### Turnstile widgets in your own flows

Sites that embed Turnstile themselves answer `200` without `cf-mitigated`, so `get`
can't see it. Get a token with `solve_turnstile` and send it where the site's own
JavaScript would (form field, JSON body or header; check the browser's network tab):

```python
from cloudflare_skip import session, solve_turnstile

token = solve_turnstile("https://cl-skip.tn3w.dev/turnstile")
response = session.post(
    "https://cl-skip.tn3w.dev/turnstile/verify", data={"cf-turnstile-response": token}
)
```

If the widget only appears after a click (`turnstile.render(...)` in the site's
script), pass its sitekey and the widget is rendered on that page directly, no click
needed. Extra keyword arguments (`action`, `cData`, ...) go to `turnstile.render`; copy
them from the site's call, since the server may check `action`:

```python
token = solve_turnstile("https://example.com/watch", sitekey="0x4AAAA...", action="play")
```

```bash
cloudflare-skip --sitekey 0x4AAAA... --action play https://example.com/watch
```

Tokens are single-use and expire after 300s, so get one right before each request.

Needs Chrome or Chromium (found on `PATH`, or set `CHROME_PATH`). Runs headless, so it
works on servers without a display; `solve(url, headless=False)` shows the window.
As root (e.g. in Docker) the sandbox is disabled automatically.

## How it works

1. Request with `curl_cffi` (Chrome TLS fingerprint) and the cached clearance.
2. `cf-mitigated: challenge` → headless Chrome opens the page, driven over the DevTools
   protocol directly (no automation framework).
3. Interactive Turnstile: once its checkbox renders (found through the frame's DOM,
   closed shadow roots included), it is focused and ticked with `Space`.
4. As soon as `cf_clearance` is set, cookies + user agent are cached per host in
   `~/.cache/cloudflare_skip.json` and replayed over HTTP.
5. Turnstile widgets on normal pages: `solve_turnstile` ticks the same way and returns
   the `cf-turnstile-response` token as soon as it is filled. With a sitekey it renders
   its own widget on the page (loading Turnstile's script if missing) instead.

| Run | Time |
|-----|------|
| First, browser solve (non-interactive, `/`) | ~2.7-3.1s |
| First, browser solve (interactive, `/interactive`) | ~3.2-3.9s |
| Turnstile widget token (`/turnstile`) | ~2.3-2.8s |
| Cached, in-process | ~0.02-0.1s |

Almost all of the solve time is Cloudflare itself: Chrome is ready after 0.2s, the
challenge loads at ~0.8s, the rest is Cloudflare's check (and showing the checkbox).

## Notes

- The API is synchronous; from async code use `await asyncio.to_thread(get, url)`.
- A clearance is bound to the user agent and IP of the solving browser: `get` always
  sends the browser's user agent, and a proxy passed in `kwargs` makes the replay fail
  with `ChallengeError`. The cache holds session cookies, so it is created with mode 600.

Things Cloudflare detects, found the hard way:

- `--remote-debugging-port=0` and `--remote-debugging-pipe`: the clearance is issued but
  rejected on the next request, so the page reloads forever. A fixed free port works.
- The `HeadlessChrome` user agent. It is replaced with `--user-agent` (the reduced UA
  of headful Chrome, major version from `chrome --version`); a CDP user agent override
  breaks client hints and gets flagged too.
- CDP mouse clicks on Turnstile are always rejected; keyboard input passes.
- Attaching to the Turnstile frame on every poll; it is attached once and reused.

## Development

```bash
uv run pytest
uv run scripts/record.py <non-interactive|interactive> [light|dark]   # header GIFs
```

`site/` is the test page behind https://cl-skip.tn3w.dev, put behind Cloudflare
challenge rules: `/` gets a non-interactive challenge, `/interactive` an interactive
one. `/turnstile` is a standalone managed Turnstile widget (no challenge rule), its
token verified server side at `/turnstile/verify`. `pages.yml` builds it on every
push to `site/`.

`scripts/record.py` screencasts headless Chrome solving the challenge and encodes the
frames with `gifski` (needs `gifski`, no display).
