Documentation
¶
Overview ¶
Package cloudfetch is an HTTP client that gets past Cloudflare bot detection by sending a real browser's TLS fingerprint and headers. It solves classic IUAM challenges itself and hands harder ones to an optional Solver.
Index ¶
- Variables
- type Browser
- type Clearance
- type Client
- func (c *Client) CloseIdleConnections()
- func (c *Client) Cookies(rawURL string) []*http.Cookie
- func (c *Client) Do(req *http.Request) (*Response, error)
- func (c *Client) Get(url string) (*Response, error)
- func (c *Client) Post(url, contentType string, body io.Reader) (*Response, error)
- func (c *Client) Underlying() *http.Client
- type Option
- type Response
- type Solver
Constants ¶
This section is empty.
Variables ¶
var ( // Chrome is Chrome 133 on Windows, the default. Chrome = Browser{utls.HelloChrome_133, [][2]string{ {"sec-ch-ua", `"Not(A:Brand";v="99", "Google Chrome";v="133", "Chromium";v="133"`}, {"sec-ch-ua-mobile", "?0"}, {"sec-ch-ua-platform", `"Windows"`}, {"Upgrade-Insecure-Requests", "1"}, {"User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/133.0.0.0 Safari/537.36"}, {"Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.7"}, {"Sec-Fetch-Site", "none"}, {"Sec-Fetch-Mode", "navigate"}, {"Sec-Fetch-User", "?1"}, {"Sec-Fetch-Dest", "document"}, {"Accept-Encoding", "gzip, deflate, br, zstd"}, {"Accept-Language", "en-US,en;q=0.9"}, {"Priority", "u=0, i"}, }} // Firefox is Firefox 120 on Windows. Firefox = Browser{utls.HelloFirefox_120, [][2]string{ {"User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:120.0) Gecko/20100101 Firefox/120.0"}, {"Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,*/*;q=0.8"}, {"Accept-Language", "en-US,en;q=0.5"}, {"Accept-Encoding", "gzip, deflate, br"}, {"Upgrade-Insecure-Requests", "1"}, {"Sec-Fetch-Dest", "document"}, {"Sec-Fetch-Mode", "navigate"}, {"Sec-Fetch-Site", "none"}, {"Sec-Fetch-User", "?1"}, }} // Safari is Safari 16.0 on macOS. Safari = Browser{utls.HelloSafari_16_0, [][2]string{ {"Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8"}, {"User-Agent", "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/16.0 Safari/605.1.15"}, {"Accept-Language", "en-US,en;q=0.9"}, {"Accept-Encoding", "gzip, deflate, br"}, }} // Edge is Edge 85 on Windows. Edge = Browser{utls.HelloEdge_85, [][2]string{ {"Upgrade-Insecure-Requests", "1"}, {"User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/85.0.4183.102 Safari/537.36 Edg/85.0.564.51"}, {"Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9"}, {"Sec-Fetch-Site", "none"}, {"Sec-Fetch-Mode", "navigate"}, {"Sec-Fetch-User", "?1"}, {"Sec-Fetch-Dest", "document"}, {"Accept-Encoding", "gzip, deflate, br"}, {"Accept-Language", "en-US,en;q=0.9"}, }} )
Browsers to impersonate with WithBrowser.
var ( // ErrManagedChallenge means a Managed or Interactive Challenge that needs // a real browser, and no Solver got past it. ErrManagedChallenge = errors.New("cloudfetch: managed challenge (Turnstile) needs a real browser") // ErrCaptcha means the site served a CAPTCHA. ErrCaptcha = errors.New("cloudfetch: CAPTCHA challenge needs an external solving service") // ErrChallengeFailed means a JS challenge wasn't solved within // WithMaxRetries attempts or by the Solver. ErrChallengeFailed = errors.New("cloudfetch: challenge not solved") )
Errors returned by Client when a challenge can't be solved.
Functions ¶
This section is empty.
Types ¶
type Browser ¶ added in v0.2.0
type Browser struct {
// contains filtered or unexported fields
}
Browser is a browser to impersonate: its TLS fingerprint and the default headers it sends, both for the same version.
type Clearance ¶ added in v0.3.0
Clearance holds the cookies a Solver earned (typically cf_clearance) and the identity headers it sent (User-Agent, Sec-CH-UA*). Cloudflare ties the cookie to them, so the Client sends them to that host from then on.
type Client ¶
type Client struct {
// contains filtered or unexported fields
}
Client is an HTTP client with browser TLS fingerprinting. It is safe for concurrent use.
func (*Client) CloseIdleConnections ¶ added in v0.2.0
func (c *Client) CloseIdleConnections()
CloseIdleConnections closes connections kept open by earlier requests that are not in use. It does not interrupt requests in progress.
func (*Client) Do ¶
Do sends req, solving Cloudflare challenges on the way, and fills in headers req doesn't set without modifying it. If a challenge can't be solved it returns ErrManagedChallenge, ErrCaptcha or ErrChallengeFailed; otherwise the caller must close the body, directly or via Text or Bytes.
func (*Client) Underlying ¶
Underlying returns the *http.Client behind c: fingerprint and cookie jar, but no default headers or challenge solving.
type Option ¶
type Option func(*Client)
Option configures a Client.
func WithBrowser ¶
WithBrowser sets the browser to impersonate: its TLS fingerprint and its default headers. Defaults to Chrome.
func WithHeader ¶
WithHeader adds a header to every request, over the browser's default but under headers set on the request.
func WithMaxRetries ¶ added in v0.2.0
WithMaxRetries sets how many times to solve a challenge for one request before giving up with ErrChallengeFailed. Defaults to 3; 0 disables solving.
func WithProxy ¶
WithProxy tunnels connections through an HTTP CONNECT proxy, "http://[user:pass@]host:port". The fingerprint is unchanged.
func WithSolver ¶ added in v0.3.0
WithSolver sets a Solver for challenges cloudfetch can't solve: managed and challenge-platform ones at once, IUAM ones after WithMaxRetries attempts. It runs at most once per request.
func WithTimeout ¶
WithTimeout sets the HTTP client timeout. Defaults to 30s.
type Response ¶
Response is an HTTP response from a Client. Text and Bytes return the decompressed body and can be called repeatedly; Body is raw. Use them for Cloudflare 403/429/503 responses, whose body the Client has already read.
type Solver ¶ added in v0.3.0
type Solver interface {
// Solve gets past u's challenge and returns the clearance.
Solve(ctx context.Context, u *url.URL) (*Clearance, error)
}
A Solver gets past challenges cloudfetch can't, usually with a real browser. See github.com/luinor223/cloudfetch/chromesolver.
Source Files
¶
Directories
¶
| Path | Synopsis |
|---|---|
|
examples
|
|
|
basic
command
Command basic fetches a page through cloudfetch and prints its status and size.
|
Command basic fetches a page through cloudfetch and prints its status and size. |
|
request
command
Command request sends a custom request with a deadline and its own headers, impersonating Firefox, and reports challenges it can't solve.
|
Command request sends a custom request with a deadline and its own headers, impersonating Firefox, and reports challenges it can't solve. |