stealth

package module
v1.32.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 15, 2026 License: Apache-2.0 Imports: 44 Imported by: 7 modules, 5 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

README

go-stealth

A Go toolkit for HTTP clients that must present as a real browser: TLS fingerprinting, proxy rotation, rate limiting, middleware, session management, and a generic pool with health tracking.

It is a reusable HTTP layer, not an application: it carries no crawling or parsing logic.

Features

  • TLS Fingerprinting — browser profiles via tls-client: Chrome (up to chrome_156), Firefox, Safari, Brave and Edge (edge_156 measured on Windows and Linux), across macOS, Windows, Linux, Android and iOS; BuiltinProfiles lists the User-Agent paired with each
  • Default-deny SSRF guard — a client refuses loopback, private, link-local and cloud-metadata targets at connect time and on redirects unless you replace the guard (WithDialControl, WithRedirectGuard, WithRequestURLGuard) or opt out with WithoutSSRFGuard
  • Proxy Rotation — static list or Webshare API, per-proxy health tracking with auto-skip
  • Rate Limiting — per-key sliding window + per-domain limiter with wildcard matching
  • Middleware — composable Handler/Middleware/Chain pattern (logging, retry, rate limit, client hints)
  • Retry & Backoff — exponential backoff with jitter, retryable error detection, generic RetryDo[T]()
  • Generic Pool — Pool[T Identity] with round-robin, health tracking, soft/permanent deactivation, cooldown
  • Sessions — fixed profile + cookie jar, request counting, file-based persistence
  • http.RoundTripper — drop-in replacement for http.DefaultTransport

Install

go get github.com/anatolykoptev/go-stealth

Quick Start

p := stealth.RandomProfile(stealth.WithBrowser("chrome"))
client, _ := stealth.NewClient(
    stealth.WithIdentity(stealth.BrowserIdentity{BrowserProfile: p}), // TLS profile + User-Agent together
)
body, headers, status, err := client.Do("GET", "https://example.com", nil, nil)
With Proxy Pool
pool, _ := proxypool.NewWebshare(os.Getenv("WEBSHARE_API_KEY"))
client, _ := stealth.NewClient(
    stealth.WithProxyPool(pool),
    stealth.WithRetryOnBlock(2),
)
Country Targeting

By default NewWebshare targets the US. Use WebshareConfig or the rotating constructor for finer control.

// 1. Backbone with default US (existing call — unchanged)
pool, _ := proxypool.NewWebshare(apiKey)

// 2. Multi-country via API filter + username injection
pool, _ := proxypool.NewWebshareWithConfig(apiKey, proxypool.WebshareConfig{
    Countries: []string{"US", "GB", "DE"},
})

// 3. Rotating endpoint — no API key needed, Webshare rotates IPs internally
pool, _ := proxypool.NewWebshareRotating(os.Getenv("WEBSHARE_USER"), os.Getenv("WEBSHARE_PASS"), "US")

The pool round-robins across all entries. With multiple countries, each base proxy is duplicated per country so rotations naturally spread across geographies.

Bounding the Response Body

Do buffers the whole body. WithMaxBodyBytes caps it, measured after gzip, deflate and zstd decoding: reading stops one byte past the cap, the connection is closed, and Do returns an error that matches errors.Is(err, stealth.ErrBodyTooLarge). A small compressed payload that expands past the cap is stopped while it decodes.

client, _ := stealth.NewClient(stealth.WithMaxBodyBytes(10 << 20))
_, _, _, err := client.Do("GET", "https://example.com/big", nil, nil)
if errors.Is(err, stealth.ErrBodyTooLarge) { /* skip it */ }

A custom backend (WithBackend) must enforce BackendConfig.MaxBodyBytes itself; the client's check after the backend returns only rejects an oversized body, it cannot prevent the allocation. stealth.ReadBodyLimit(resp.Body, cfg.MaxBodyBytes) is a drop-in for io.ReadAll that does this.

As http.RoundTripper
client, _ := stealth.NewClient()
resp, err := client.StdClient().Get("https://example.com")
Middleware
client, _ := stealth.NewClient()
client.Use(stealth.LoggingMiddleware)
client.Use(stealth.RetryMiddleware(stealth.DefaultRetryConfig))
client.Use(stealth.RateLimitMiddleware(ratelimit.NewDomainLimiter(
    ratelimit.DomainConfig{Domain: "*.example.com", RequestsPerWindow: 30, WindowDuration: time.Minute},
)))
Rate Limiting
// Rules are checked in order; the first matching rule wins.
limiter := ratelimit.NewDomainLimiter(
    ratelimit.DomainConfig{Domain: "api.example.com", RequestsPerWindow: 10, WindowDuration: time.Minute},
    ratelimit.DomainConfig{Domain: "*.example.com", RequestsPerWindow: 30, WindowDuration: time.Minute},
)
limiter.Wait(ctx, "https://api.example.com/v1/users")
Generic Pool
pool := pool.New(accounts, pool.Config{
    AlertHook: func(topic string, payload any) { log.Println(topic, payload) },
})
acc, err := pool.Next(func(a *Account) bool { return a.IsReady() })

Packages

Package Purpose
stealth BrowserClient, middleware, profiles, retry, backoff
pool Generic Pool[T Identity] with health tracking
proxypool ProxyPool interface + Static, Webshare, HealthyProxyPool
ratelimit Per-key sliding window + per-domain limiter
session Stateful browsing with persistence
internal/fingerprint Reference types + oracle comparison for the fingerprint measurement
cmd/fingerprint-capture Captures a real Chrome's fingerprint as an oracle reference

Fingerprint oracle

make fingerprint runs the TLS/HTTP2 fingerprint oracle, which checks that each Chrome profile in BuiltinProfiles actually emits the fingerprint a real Chrome of the same major version emits. It is not part of make preflight (it hits the network and needs reference files); run it explicitly.

A failure means a go-stealth Chrome profile's emitted fingerprint differs from a real Chrome's — a true result (the profile is stale or wrong), not a test defect. Fix the profile in a separate reviewed change; do not weaken the comparison to make it green.

The oracle compares each metric against a service that is spec-faithful for that metric: JA4 (and ja4_o / ja3n_hash) against browserleaks (FoxIO-faithful — peet.ws strips the padding extension 0x0015 from JA4), and JA3, peetprint, HTTP/2 Akamai, header order, and sec-ch-ua against peet (spec-faithful JA3; the only service with peetprint and sent-frames). Each reference records per-metric provenance in sources, and the oracle FAILs if a reference and a measurement for the same metric come from different services — a cross-service comparison reports a tooling artefact as a fingerprint defect.

References live in testdata/reference_chrome_<major>.json, captured by:

go run ./cmd/fingerprint-capture -major 146   # amd64 host; no arm64 Chrome-for-Testing build

See testdata/README.md for the headless caveat and the per-metric provenance contract.

Fetch mode

By default a request is shaped like a top-level navigation (HTTP/2 priority weight 256, priority: u=0, i). stealth.WithFetchMode() makes a client send what Chrome sends for fetch() and XMLHttpRequest instead: weight 220 and priority: u=1, i (a priority header you set yourself is kept). The mode is per client because tls-client holds the HTTP/2 priority on the profile; build one client of each kind if you mix navigations and fetches. It needs a Chrome family profile and is opt-in for every profile, chrome_131 included.

Request headers for an identity

One place builds the browser request headers, so callers do not hand-copy a User-Agent, sec-ch-ua string or accept value that goes stale:

  • stealth.DefaultProfile() and stealth.DefaultIdentity() are what NewClient() presents, without building a client.
  • identity.RequestHeaders(stealth.HeaderOptions{...}) (or client.RequestHeaders) returns the header values and their Chrome order for a navigation (FetchNavigate) or a page fetch()/XMLHttpRequest (FetchCORS), with an optional AcceptLanguage, Site and OmitAcceptEncoding. Pass Order to WithHeaderOrder. The values and order are tested against the Chrome 156 captures in testdata/wire.
  • identity.UserAgentData(fullVersion) is the same brand list as structured data (brands, fullVersionList) for a DevTools Protocol session.
  • stealth.HonestUserAgent(name, version, contact) names the calling program for plain net/http callers whose TLS is Go's own.
  • RandomUserAgent and the package-level ChromeHeaders are deprecated: they can name a browser other than the one the client's TLS fingerprint shows. Use RandomChromeUserAgent or the client's Identity().

Wire check against a real browser

make fingerprint-wire (build tag wirelive, network required, unreachable endpoint fails) measures chrome_156, the default client, chrome_152, edge_156, fetch mode and chrome_131 through tls.peet.ws and diffs JA4, ja4_r, peetprint, the HTTP/2 fingerprint and priority against the captures in testdata/wire (installed Google Chrome 156.0.8078.80 and Microsoft Edge 156.0.4314.58; Chrome for Testing 131 for information). JA3 must vary across connections for the modern profiles, as in Chrome, and stay constant for chrome_131. Offline, wire_*_test.go capture the ClientHello and first HTTP/2 frames on a local listener and pin the chrome_131 wire byte for byte.

Used By

Plus private services in the same fleet: a web-search MCP server and a crypto-intelligence pipeline.

Configuration

The library reads no configuration file. Everything is passed as options (NewClient(...), pool.Config, proxypool.WebshareConfig). The only environment variable it reads itself is INTERNAL_SERVICE_SECRET, which WithOxBrowser sends as X-Internal-Secret to the ox-browser service when set (it is not sent through an external proxy). Names such as WEBSHARE_API_KEY in the examples are the caller's own.

Development

make preflight           # gofmt -s, go vet, build, race tests, then three non-race live tests
make lint                # golangci-lint
make fingerprint         # TLS/HTTP2 oracle, network, not part of preflight
make fingerprint-wire    # wire check against real-browser captures, network

See CONTRIBUTING.md and, for every document in the repository, docs/INDEX.md. Releases and the changelog are produced by release-please from Conventional Commits.

License

Apache-2.0, see LICENSE.

Documentation

Index

Constants

View Source
const DefaultMinWeight = 0.01

DefaultMinWeight is the MinWeight used when Constraints leaves it zero: 1%.

Variables

View Source
var BuiltinProfiles = []BrowserProfile{

	{
		UserAgent:  "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/156.0.0.0 Safari/537.36",
		TLSProfile: ProfileChrome156, Browser: "chrome", OS: "windows",
	},
	{
		UserAgent:  "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/156.0.0.0 Safari/537.36",
		TLSProfile: ProfileChrome156, Browser: "chrome", OS: "macos",
	},
	{
		UserAgent:  "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/156.0.0.0 Safari/537.36",
		TLSProfile: ProfileChrome156, Browser: "chrome", OS: "linux",
	},
	{
		UserAgent:  "Mozilla/5.0 (Linux; Android 10; K) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/156.0.0.0 Mobile Safari/537.36",
		TLSProfile: ProfileChrome156, Browser: "chrome", OS: "android", Mobile: true,
	},

	{
		UserAgent:  "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/152.0.0.0 Safari/537.36",
		TLSProfile: ProfileChrome152, Browser: "chrome", OS: "windows",
	},
	{
		UserAgent:  "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/152.0.0.0 Safari/537.36",
		TLSProfile: ProfileChrome152, Browser: "chrome", OS: "macos",
	},
	{
		UserAgent:  "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/152.0.0.0 Safari/537.36",
		TLSProfile: ProfileChrome152, Browser: "chrome", OS: "linux",
	},
	{
		UserAgent:  "Mozilla/5.0 (Linux; Android 10; K) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/152.0.0.0 Mobile Safari/537.36",
		TLSProfile: ProfileChrome152, Browser: "chrome", OS: "android", Mobile: true,
	},

	{
		UserAgent:  "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/150.0.0.0 Safari/537.36",
		TLSProfile: ProfileChrome150, Browser: "chrome", OS: "windows",
	},
	{
		UserAgent:  "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/150.0.0.0 Safari/537.36",
		TLSProfile: ProfileChrome150, Browser: "chrome", OS: "macos",
	},
	{
		UserAgent:  "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/150.0.0.0 Safari/537.36",
		TLSProfile: ProfileChrome150, Browser: "chrome", OS: "linux",
	},

	{
		UserAgent:  "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/146.0.0.0 Safari/537.36",
		TLSProfile: ProfileChrome146, Browser: "chrome", OS: "windows",
	},
	{
		UserAgent:  "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/146.0.0.0 Safari/537.36",
		TLSProfile: ProfileChrome146, Browser: "chrome", OS: "macos",
	},
	{
		UserAgent:  "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/146.0.0.0 Safari/537.36",
		TLSProfile: ProfileChrome146, Browser: "chrome", OS: "linux",
	},
	{
		UserAgent:  "Mozilla/5.0 (Linux; Android 10; K) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/146.0.0.0 Mobile Safari/537.36",
		TLSProfile: ProfileChrome146, Browser: "chrome", OS: "android", Mobile: true,
	},

	{
		UserAgent:  "Mozilla/5.0 (iPhone; CPU iPhone OS 18_6 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/26.0 Mobile/15E148 Safari/604.1",
		TLSProfile: ProfileSafariIOS26, Browser: "safari", OS: "ios", Mobile: true,
	},

	{
		UserAgent:  "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:148.0) Gecko/20100101 Firefox/148.0",
		TLSProfile: ProfileFirefox148, Browser: "firefox", OS: "windows",
	},
	{
		UserAgent:  "Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:148.0) Gecko/20100101 Firefox/148.0",
		TLSProfile: ProfileFirefox148, Browser: "firefox", OS: "macos",
	},
	{
		UserAgent:  "Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:148.0) Gecko/20100101 Firefox/148.0",
		TLSProfile: ProfileFirefox148, Browser: "firefox", OS: "linux",
	},

	{
		UserAgent:  "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/146.0.0.0 Safari/537.36",
		TLSProfile: ProfileBrave146, Browser: "brave", OS: "windows",
	},
	{
		UserAgent:  "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/146.0.0.0 Safari/537.36",
		TLSProfile: ProfileBrave146, Browser: "brave", OS: "macos",
	},
	{
		UserAgent:  "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/146.0.0.0 Safari/537.36",
		TLSProfile: ProfileBrave146, Browser: "brave", OS: "linux",
	},

	{
		UserAgent:  "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/156.0.0.0 Safari/537.36 Edg/156.0.0.0",
		TLSProfile: ProfileEdge156, Browser: "edge", OS: "windows",
	},
	{
		UserAgent:  "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/156.0.0.0 Safari/537.36 Edg/156.0.0.0",
		TLSProfile: ProfileEdge156, Browser: "edge", OS: "linux",
	},

	{
		UserAgent:  "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/152.0.0.0 Safari/537.36 Edg/152.0.0.0",
		TLSProfile: ProfileChrome152, Browser: "edge", OS: "windows",
	},
	{
		UserAgent:  "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/152.0.0.0 Safari/537.36 Edg/152.0.0.0",
		TLSProfile: ProfileChrome152, Browser: "edge", OS: "macos",
	},
}

BuiltinProfiles is the set of current browser request profiles, with per-OS variants. Every entry describes ONE browser: the TLS profile major, the User-Agent major and the Client Hints derived from it agree (pinned by TestBuiltinProfilesCoherent). RandomProfile and PlatformMatchedProfile draw only from this set; the legacy set below stays reachable through UserAgentForProfile / Identity() for callers that pin an older profile.

The newest Chrome here is bounded by what has been measured, not by the current Chrome release: a UA newer than its TLS profile would describe two different browsers. chrome_156 reuses tls-client's Chrome_152 because that profile's fingerprint was measured identical to Chrome 156's.

View Source
var ChromeNavigationHeaderOrder = []string{
	"sec-ch-ua", "sec-ch-ua-mobile", "sec-ch-ua-platform",
	"upgrade-insecure-requests", "user-agent", "accept",
	"sec-fetch-site", "sec-fetch-mode", "sec-fetch-user", "sec-fetch-dest",
	"referer", "accept-encoding", "accept-language", "cookie", "priority",
}

ChromeNavigationHeaderOrder is the request-header order of a top-level navigation in stable Google Chrome 156, taken from a capture of Google Chrome 156.0.8078.80 (testdata/wire/google_chrome_156_nav.json). It is the order the default client (chrome_156) uses.

It differs from DefaultHeaderOrder in one place: accept-language comes after accept-encoding. DefaultHeaderOrder was captured from Chrome for Testing, which puts accept-language right after sec-ch-ua-platform; branded Google Chrome does not. The difference follows the build, not the version (Chrome for Testing 131, 152 and 156 all have the early placement), so every modern Chrome profile whose TLS extension order is shuffled (chrome_146, 150, 152, 156) uses this order, and chrome_131, 133 and 144 keep DefaultHeaderOrder. Branded 146 to 152 were not captured themselves; the placement is carried over from the branded 156 capture on the build argument.

referer and cookie are absent from a first request, so the capture does not place them; they take the positions they have in the fetch captures (referer before accept-encoding, cookie between accept-language and priority), which share the same network stack.

View Source
var DefaultBackoff = BackoffConfig{
	InitialWait: 500 * time.Millisecond,
	MaxWait:     10 * time.Second,
	Multiplier:  2.0,
	JitterPct:   0.3,
}

DefaultBackoff is the standard backoff for API retry loops.

View Source
var DefaultHeaderOrder = []string{
	"sec-ch-ua",
	"sec-ch-ua-mobile",
	"sec-ch-ua-platform",
	"accept-language",
	"upgrade-insecure-requests",
	"user-agent",
	"accept",
	"sec-fetch-site",
	"sec-fetch-mode",
	"sec-fetch-user",
	"sec-fetch-dest",
	"accept-encoding",
	"priority",
	"referer",
	"cookie",
}

DefaultHeaderOrder is the Chrome request-header order, taken from the real-Chrome references in internal/fingerprint/testdata/reference_chrome_*.json (header_order field). Real Chrome sends these 13 headers in this exact order on a top-level GET navigation. referer and cookie are not in the reference (the capture was a clean first request) but are appended so they stay ordered when a caller supplies them; their position is approximate — the references do not record it.

View Source
var DefaultJitter = pacing.DefaultJitter

DefaultJitter is the canonical anti-fingerprint jitter (500ms–2.5s), backed by pacing.DefaultJitter. Callers that need a different range should construct their own pacing.Jitter.

View Source
var DefaultRetryConfig = RetryConfig{
	MaxRetries:  3,
	InitialWait: 500 * time.Millisecond,
	MaxWait:     10 * time.Second,
	Multiplier:  2.0,
}

DefaultRetryConfig is suitable for most HTTP calls.

View Source
var ErrBodyTooLarge = errors.New("stealth: response body exceeds the configured limit")

ErrBodyTooLarge is returned (wrapped; match it with errors.Is) when a response body is larger than the limit set with WithMaxBodyBytes. The limit applies to the body after content-decoding, and reading stops one byte past it: the rest of the body is neither buffered nor drained, and the connection is closed rather than reused.

View Source
var ErrFetchModeUnsupported = errors.New("stealth: fetch mode needs a Chrome-family profile")

ErrFetchModeUnsupported is returned by NewClient when WithFetchMode is used with a profile outside the Chrome family.

View Source
var ErrNoIdentity = errors.New("stealth: no browser identity satisfies the constraints")

ErrNoIdentity is returned when the constraints leave no browser identity that both the model knows and a built-in TLS profile backs.

View Source
var ErrNotChromium = errors.New("stealth: identity is not Chromium-based")

ErrNotChromium is returned by the request-header and UserAgentData builders for an identity that is not Chromium-based: Firefox and Safari send no Client Hints and their request shape is not modelled here.

View Source
var ErrSSRFBlocked = errors.New("stealth: SSRF-blocked address")

ErrSSRFBlocked wraps every error the built-in default-deny guards return when a fetch target resolves to loopback, private (RFC1918 / RFC4193 ULA), link-local (including the cloud-metadata address 169.254.169.254), or the unspecified address — the address classes an SSRF payload targets to reach internal infrastructure from a caller-supplied URL (an advertiser-provided website, a redirect Location header, a scraped image src).

This is a deliberately dependency-free, stdlib-only floor: a BrowserClient is fail-closed BY CONSTRUCTION (a forgotten guard config denies internal targets), and the public option signatures stay free of any framework or bogdanfinn/fhttp type. A consumer that wants the fleet's fuller SSRF policy (CGNAT, NAT64/6to4, alt-encoded-IP rejection) wires go-kit/httputil's SSRFGuards()/CheckURL through WithDialControl / WithRedirectGuard / WithRequestURLGuard, which OVERRIDE these defaults.

A caller-supplied guard's error need NOT wrap this sentinel itself: the With* setters tag it (see tagGuardErr2), so errors.Is(err, ErrSSRFBlocked) holds for any configured guard's rejection, built-in or framework-wired. That is what makes doWithRetry treat the rejection as non-retryable — a blocked target is about the URL/address, not the proxy, so every retry re-blocks identically.

View Source
var ErrUnknownTLSProfile = errors.New("stealth: unknown TLS profile")

ErrUnknownTLSProfile is returned when a TLSProfile has no backend mapping. An unmapped profile must never quietly become a different browser, so client construction fails instead of substituting one.

View Source
var FirefoxHeaderOrder = []string{
	"user-agent",
	"accept",
	"accept-language",
	"accept-encoding",
	"referer",
	"cookie",
	"upgrade-insecure-requests",
	"sec-fetch-dest",
	"sec-fetch-mode",
	"sec-fetch-site",
	"sec-fetch-user",
	"priority",
	"te",
}

FirefoxHeaderOrder is the Firefox request-header order on a top-level GET navigation over HTTP/2, as observed from a real Firefox. cookie was captured between accept-encoding and upgrade-insecure-requests; referer is placed just before it (its position was not captured).

View Source
var RandomUserAgents = []string{
	"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/156.0.0.0 Safari/537.36",
	"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/156.0.0.0 Safari/537.36",
	"Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/156.0.0.0 Safari/537.36",
	"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/17.2 Safari/605.1.15",
	"Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:148.0) Gecko/20100101 Firefox/148.0",
	"Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:148.0) Gecko/20100101 Firefox/148.0",
	"Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:148.0) Gecko/20100101 Firefox/148.0",
}

RandomUserAgents is a pool of browser User-Agents for rotation, for callers that do not care which browser they look like (connectivity probes). Its Chrome entries describe ONLY the default TLS profile (ProfileChrome156); pairing a User-Agent from here with a client built on another profile advertises a different browser than the one fingerprinted. Use (*BrowserClient).Identity() or ChromeHeadersFor to get a matching one. Firefox entries are kept in line with ProfileFirefox148. The Safari 17.2 entry is stale relative to BuiltinProfiles (17.6/18.2) but out of scope here — left as-is.

Deprecated: the pool mixes Chrome, Firefox and Safari, so a User-Agent drawn from it over a Chrome TLS fingerprint (any BrowserClient built on a chrome_* profile) names a different browser than the handshake shows, and drawn per request it changes browser family mid-session. For a client use (*BrowserClient).Identity().UserAgent or (*BrowserClient).RequestHeaders; for a plain net/http request use DefaultIdentity().UserAgent, RandomChromeUserAgent, or HonestUserAgent when no browser identity is needed.

Functions

func ChromeFetchHeaderOrder added in v1.27.0

func ChromeFetchHeaderOrder(custom []string) []string

ChromeFetchHeaderOrder returns the request-header order of a fetch() or XMLHttpRequest in stable Google Chrome 156, for use with WithHeaderOrder or DoWithHeaderOrder. custom lists the page's own headers (authorization, x-csrf-token, ...) in the order you send them; they are placed as one block after sec-ch-ua-platform, before the rest.

Headers missing from an order are sent after the listed ones in alphabetical order (fhttp), so a custom header you leave out ends up after priority. A client built with WithFetchMode and no WithHeaderOrder uses ChromeFetchHeaderOrder(nil).

What is established and what is not. Established, from the captures testdata/wire/google_chrome_156_fetch.json and the Chrome for Testing fetches: sec-ch-ua-platform first, user-agent after the page's headers, then accept, sec-fetch-site, sec-fetch-mode, sec-fetch-dest, referer, accept-encoding, accept-language (branded Chrome only), cookie, priority. Not established: where Chrome interleaves the page's own headers with sec-ch-ua, sec-ch-ua-mobile and user-agent. In the capture they are spread between them in a pattern that one sample cannot generalise (and sec-ch-ua-mobile and user-agent swap places when the page sets no headers); this function puts the page's headers in one block. A caller that has captured its own target's exact order should pass that order itself.

func ChromeHeaders deprecated

func ChromeHeaders() map[string]string

ChromeHeaders returns Chrome navigation headers for the DEFAULT client profile: the User-Agent is a random desktop-OS variant of the browser that NewClient() presents when no WithProfile/WithIdentity is given, so its major matches the default TLS fingerprint. It does not describe a client built with another profile; for that use ChromeHeadersFor or (*BrowserClient).ChromeHeaders.

The set and the accept value match the real-Chrome references captured per major in internal/fingerprint/testdata/reference_chrome_*.json (header_order + accept fields). Real Chrome sends 13 request headers on a top-level GET navigation; the sec-ch-ua-* trio is added separately by ClientHintsHeaders (it is profile-version-dependent), so ChromeHeaders emits the remaining 10. The sec-fetch-*, upgrade-insecure-requests, and priority values are the canonical Chrome top-level-navigation values; the references record only these headers' names and order (in header_order), not their values, and the oracle compares only names+order for them.

Deprecated: the result describes the default profile, not the client that sends it. A client built with WithProfile or WithIdentity, or a module whose go-stealth pin moved the default, gets a User-Agent of one Chrome major over the TLS fingerprint of another, and the OS variant changes on every call. It also carries no sec-ch-ua-* hints. Use (*BrowserClient).ChromeHeaders or (*BrowserClient).RequestHeaders, which follow the client's identity.

func ChromeHeadersFor added in v1.26.1

func ChromeHeadersFor(p BrowserProfile) map[string]string

ChromeHeadersFor returns Chrome navigation headers that describe profile p: its own User-Agent plus the sec-ch-ua-* Client Hints derived from it, so the advertised browser matches the TLS fingerprint p.TLSProfile. Use it with the profile the client was built with (BuiltinProfiles, RandomProfile, PlatformMatchedProfile). For a non-Chromium profile (Firefox, Safari) it returns the same navigation headers with that profile's own User-Agent and no Client Hints, since those browsers send none. The result is never nil.

func ClientHintsHeaders

func ClientHintsHeaders(ua string) map[string]string

ClientHintsHeaders returns sec-ch-ua-* headers for Chromium-based UAs. Returns nil for Safari/Firefox (they don't send Client Hints).

The sec-ch-ua brand list is built the way Chromium builds it (components/embedder_support/user_agent_utils.cc, GenerateBrandVersionList): a GREASE brand, "Chromium" and the browser brand ("Google Chrome", or "Microsoft Edge" for Edge), each carrying the major version, shuffled by a permutation seeded with the Chromium major. Both the GREASE brand/version and the order therefore change with the major; a fixed brand string agrees with at most one release.

func DefaultHeaderOrderFor added in v1.26.0

func DefaultHeaderOrderFor(browser string) []string

DefaultHeaderOrderFor returns the request-header order that matches the given BrowserProfile.Browser value: Firefox gets its own order, every other browser the Chrome order (the order for Safari has not been captured).

func ExtractChromeVersion

func ExtractChromeVersion(ua string) string

ExtractChromeVersion extracts the major Chrome version from a User-Agent string.

func HonestUserAgent added in v1.30.0

func HonestUserAgent(name, version, contact string) string

HonestUserAgent returns a User-Agent that names the calling program rather than a browser, in the RFC 9110 product form "name/version (+contact)". Use it for plain net/http callers whose TLS handshake is Go's own: a browser User-Agent over that handshake describes a client that is not there, and an API that expects no browser needs none.

It sanitizes instead of returning an error, so the call stays a one-liner and its result is always safe to put in a header, even for callers that serialize headers themselves: name and version keep only RFC 9110 token characters (anything else becomes '-'; an empty name becomes "go-client", an empty version is omitted), and contact keeps only printable ASCII with '(', ')' and '\' also replaced by '-', so control characters (CR, LF, NUL) cannot end a header or close the comment. A contact that is empty after trimming is omitted.

func IdentityPoolVersion added in v1.32.0

func IdentityPoolVersion(c Constraints) (string, error)

IdentityPoolVersion returns the PoolVersion a draw under c would carry, without drawing. Compare it with the PoolVersion stored beside an identity to learn whether the candidate set has changed since.

func IdentitySeed added in v1.32.0

func IdentitySeed(key string) uint64

IdentitySeed turns a stable key (an account id, a session id) into a seed. It is the first eight bytes of the key's SHA-256, big-endian, so any language reproduces it.

A seed is not an identity. What GenerateIdentity returns for a seed depends on the model data, the built-in profile set and the weight floor, all of which change in library upgrades. Persist the identity you draw and render from the stored value; never re-derive it from the seed after an upgrade.

func IsRetryable

func IsRetryable(err error) bool

IsRetryable returns true for transient errors worth retrying.

func IsRetryableStatus

func IsRetryableStatus(code int) bool

IsRetryableStatus returns true for HTTP status codes worth retrying.

func MaskProxy

func MaskProxy(proxyURL string) string

MaskProxy masks credentials in a proxy URL for safe logging. "socks5://user:pass@host:1080" -> "socks5://***@host:1080"

func ParseRetryAfter

func ParseRetryAfter(resp *http.Response) time.Duration

ParseRetryAfter extracts the wait duration from a Retry-After header. Handles both delta-seconds ("120") and HTTP-date ("Thu, 01 Dec 2025 16:00:00 GMT") formats. Returns 0 if the header is missing or unparseable.

func RandomChromeUserAgent added in v1.30.0

func RandomChromeUserAgent() string

RandomChromeUserAgent returns the User-Agent of a random desktop OS variant of the default Chrome profile (see DefaultProfile). It is always Chrome and always the major of the default TLS fingerprint, so it is the safe replacement for RandomUserAgent on a client built with no WithProfile. For a client built with another profile, take the User-Agent from its Identity().

func RandomUserAgent deprecated

func RandomUserAgent() string

RandomUserAgent returns a random entry of RandomUserAgents: Chrome, Firefox or Safari, whatever the pool holds, not only Chrome.

Deprecated: it is not safe to pair with a Chrome TLS fingerprint, because roughly half of the pool is not Chrome (see RandomUserAgents). Use RandomChromeUserAgent for a Chrome User-Agent that matches the default profile, (*BrowserClient).Identity().UserAgent for the one a client presents, or HonestUserAgent for an API that needs no browser identity.

func ReadBodyLimit added in v1.29.0

func ReadBodyLimit(r io.Reader, limit int64) ([]byte, error)

ReadBodyLimit is io.ReadAll bounded by limit, for a custom backend (see WithBackend) to read its response body with: it stops at limit+1 bytes and returns an error wrapping ErrBodyTooLarge when the source has more than limit bytes, without buffering the rest. Pass BackendConfig.MaxBodyBytes as the limit; a limit <= 0 reads without a bound. Close the body unread afterwards, so the connection is dropped rather than drained.

func RetryDo

func RetryDo[T any](ctx context.Context, rc RetryConfig, fn func() (T, error)) (T, error)

RetryDo retries fn up to MaxRetries times with exponential backoff. Retries only on retryable errors; returns immediately on non-retryable or context cancellation.

func RetryDoWithReset

func RetryDoWithReset[T any](
	ctx context.Context,
	rc RetryConfig,
	resetFn func(),
	fn func() (T, error),
) (T, error)

RetryDoWithReset is like RetryDo but calls resetFn before each retry attempt (not before the first attempt). If resetFn is nil, it behaves identically to RetryDo.

func RetryHTTP

func RetryHTTP(ctx context.Context, rc RetryConfig, fn func() (*http.Response, error)) (*http.Response, error)

RetryHTTP executes an HTTP request function with retry logic.

func RetryHTTPWithReset

func RetryHTTPWithReset(
	ctx context.Context,
	rc RetryConfig,
	resetFn func(),
	fn func() (*http.Response, error),
) (*http.Response, error)

RetryHTTPWithReset is like RetryHTTP but calls resetFn before each retry attempt. If resetFn is nil, it behaves identically to RetryHTTP.

func UserAgentForProfile added in v1.21.0

func UserAgentForProfile(p TLSProfile) string

UserAgentForProfile returns a User-Agent string from BuiltinProfiles whose TLSProfile matches p. It returns the FIRST matching entry (BuiltinProfiles holds per-OS variants sharing one TLS profile; the first is Windows where available). Returns "" when no BuiltinProfiles entry matches p — this is the documented no-entry behaviour, NOT a fallback: callers pairing a UA with an unknown profile must handle the empty string rather than silently presenting a mismatched UA. This is the single lookup the consumer repos use to delete their hardcoded UA literals.

func ValidateProxy

func ValidateProxy(client *BrowserClient, testURL string) error

ValidateProxy tests proxy connectivity by fetching the given URL. Returns nil if the proxy is reachable, error otherwise.

func WithRetryHook

func WithRetryHook(ctx context.Context, fn RetryHookFunc) context.Context

WithRetryHook returns a context carrying the given retry hook.

Types

type AnalyzeResponse

type AnalyzeResponse struct {
	URL          string        `json:"url"`
	Status       int           `json:"status"`
	Technologies []AnalyzeTech `json:"technologies"`
	Error        string        `json:"error,omitempty"`
}

AnalyzeResponse is the response from /analyze.

type AnalyzeTech

type AnalyzeTech struct {
	Name       string   `json:"name"`
	Categories []string `json:"categories"`
	Confidence int      `json:"confidence"`
	Version    *string  `json:"version,omitempty"`
}

AnalyzeTech is a single detected technology.

type BackendConfig

type BackendConfig struct {
	Profile         TLSProfile
	ProxyURL        string
	TimeoutSeconds  int
	FollowRedirects bool
	HTTP3           bool

	// FetchMode requests the fetch/XHR request shape: HTTP/2 priority weight
	// 220 on HEADERS instead of the navigation weight 256 (see WithFetchMode).
	FetchMode bool

	// CookieJar, when non-nil, replaces the backend's own cookie jar (see
	// WithCookieJar). nil = each backend's default jar.
	CookieJar http.CookieJar

	// InsecureSkipVerify, when true, disables TLS certificate verification.
	// Honored by the tls-client backend; the std backend verifies by default
	// (net/http) and will honor this field when wired.
	InsecureSkipVerify bool

	// DialControl, when non-nil, is installed as the connect-time SSRF guard
	// on the backend's dialer (net.Dialer.Control on the std backend;
	// tls-client WithDialer on the tls backend). It fires on the
	// already-resolved address, per redirect hop, on the DIRECT path.
	DialControl func(network, address string) error

	// RedirectGuard, when non-nil AND FollowRedirects is true, is installed as
	// the per-hop redirect check (http.Client.CheckRedirect on the std
	// backend; an fhttp→stdlib-adapted WithCustomRedirectFunc on the tls
	// backend). It must re-own the redirect hop cap.
	RedirectGuard func(req *http.Request, via []*http.Request) error

	// MaxBodyBytes, when > 0, bounds the decoded response body (see
	// WithMaxBodyBytes): a backend must stop reading at MaxBodyBytes+1 bytes,
	// close the connection and return an error wrapping ErrBodyTooLarge.
	// 0 = unbounded. A custom backend must enforce it itself (ReadBodyLimit
	// bounds a read in one call): the client's check after the backend
	// returns only rejects an oversized body, it cannot stop the backend
	// from allocating it.
	MaxBodyBytes int64
	// contains filtered or unexported fields
}

BackendConfig holds backend-agnostic configuration for creating an HTTPDoer.

type BackendFactory

type BackendFactory func(cfg BackendConfig) (HTTPDoer, error)

BackendFactory creates an HTTPDoer from configuration.

type BackoffConfig

type BackoffConfig struct {
	InitialWait time.Duration // base delay for attempt 0
	MaxWait     time.Duration // ceiling
	Multiplier  float64       // growth factor per attempt
	JitterPct   float64       // random variation (0.3 = +/-30%)
}

BackoffConfig defines exponential backoff parameters with jitter.

func (BackoffConfig) Duration

func (b BackoffConfig) Duration(attempt int) time.Duration

Duration returns the backoff delay for the given attempt (0-indexed), delegating to pacing.ExponentialBackoff for the canonical jitter calculation.

type BrowserClient

type BrowserClient struct {
	// contains filtered or unexported fields
}

BrowserClient wraps an HTTPDoer backend with middleware, proxy rotation, and TLS fingerprint impersonation.

func NewClient

func NewClient(opts ...ClientOption) (*BrowserClient, error)

NewClient creates a BrowserClient with the given options.

func (*BrowserClient) ChromeHeaders added in v1.26.1

func (bc *BrowserClient) ChromeHeaders() map[string]string

ChromeHeaders returns Chrome navigation headers matching the identity this client presents (see Identity): the identity's User-Agent and Client Hints over the same header set as the package-level ChromeHeaders. Never nil; a non-Chromium identity carries no Client Hints.

func (*BrowserClient) Close added in v1.22.4

func (bc *BrowserClient) Close() error

Close releases resources the client owns. Only pools the client created itself — via WithWebshareCountry or WithWebshareRotating — are closed (this stops the Webshare credential refresher); a pool supplied through WithProxyPool belongs to the caller and is left alone. Likewise the backend the client built itself (the default tls-client, or WithStdHTTP) is closed, while one supplied through WithBackend belongs to the caller and is left alone. Closing the backend releases its idle connections (and HTTP/3 sockets); a connection serving a request is left to finish, and an HTTP/2 connection used by an in-flight request can stay open until its idle timeout (about 90 s). A Do after Close opens new connections that nothing closes, and Close must not run concurrently with Do. Idempotent; returns the owned pool's first close error, if any (the backend is closed regardless).

func (*BrowserClient) CloseIdleConnections added in v1.25.0

func (bc *BrowserClient) CloseIdleConnections()

CloseIdleConnections closes the idle connections of the backend the client built itself (the default tls-client, or WithStdHTTP); a backend supplied through WithBackend belongs to the caller and is left alone. A connection in use is left alone, and one opened later idles out by itself. Unlike Close it is safe to call concurrently with Do, repeatedly, and after Close.

func (*BrowserClient) Do

func (bc *BrowserClient) Do(method, urlStr string, headers map[string]string, body io.Reader) ([]byte, map[string]string, int, error)

Do executes an HTTP request with TLS fingerprint impersonation. Returns (body bytes, response headers, HTTP status code, error). If a ProxyPool was configured, each call rotates to the next proxy. Middleware added via Use() is applied to each request.

func (*BrowserClient) DoCtx

func (bc *BrowserClient) DoCtx(ctx context.Context, method, urlStr string, headers map[string]string, body io.Reader) ([]byte, map[string]string, int, error)

DoCtx executes an HTTP request with context cancellation/deadline support. If ctx is already cancelled, returns ctx.Err() immediately. Otherwise runs Do in a goroutine and returns when the request completes or ctx is done.

func (*BrowserClient) DoWithHeaderOrder

func (bc *BrowserClient) DoWithHeaderOrder(method, urlStr string, headers map[string]string, body io.Reader, order []string) ([]byte, map[string]string, int, error)

DoWithHeaderOrder executes a request with a custom header order. Middleware and proxy rotation are applied.

func (*BrowserClient) DoWithHeaderOrderCtx

func (bc *BrowserClient) DoWithHeaderOrderCtx(ctx context.Context, method, urlStr string, headers map[string]string, body io.Reader, order []string) ([]byte, map[string]string, int, error)

DoWithHeaderOrderCtx executes a request with a custom header order and context support. If ctx is already cancelled, returns ctx.Err() immediately. Otherwise runs DoWithHeaderOrder in a goroutine and returns when the request completes or ctx is done.

func (*BrowserClient) GetCookieValue

func (bc *BrowserClient) GetCookieValue(rawURL, name string) string

GetCookieValue returns the value of a named cookie for the given URL.

func (*BrowserClient) Identity added in v1.21.0

func (bc *BrowserClient) Identity() BrowserIdentity

Identity returns the BrowserIdentity the client is actually presenting: the TLS profile installed on the backend, the User-Agent paired with it, and the Client Hints derived from that User-Agent. For a client built with only WithProfile (or the bare default), the UA is resolved from BuiltinProfiles so it agrees with the JA3 by contract. For a client built with WithIdentity, the supplied identity is returned verbatim.

This is the accessor consumer repos use to obtain the User-Agent that matches the fingerprint they are presenting, instead of hardcoding their own UA literal that can drift from the library's default profile.

func (*BrowserClient) RequestHeaders added in v1.30.0

func (bc *BrowserClient) RequestHeaders(opts HeaderOptions) (RequestHeaders, error)

RequestHeaders builds the request headers for the identity this client presents (see Identity), so the User-Agent, Client Hints and order always agree with the TLS profile the client was built with. See BrowserIdentity.RequestHeaders.

Order is the order the client will actually send: its configured header order (WithHeaderOrder, or the default for its identity and mode) filtered to the generated names, not the identity's computed default.

func (*BrowserClient) RoundTrip

func (bc *BrowserClient) RoundTrip(req *http.Request) (*http.Response, error)

RoundTrip implements the http.RoundTripper interface. This allows BrowserClient to be used as a Transport for any standard net/http consumer (resty, go-retryablehttp, etc.) while maintaining TLS fingerprint impersonation.

Middleware added via Use() is applied. Proxy rotation is applied.

func (*BrowserClient) SetProxy

func (bc *BrowserClient) SetProxy(proxyURL string) error

SetProxy changes the proxy URL for subsequent requests.

func (*BrowserClient) StdClient

func (bc *BrowserClient) StdClient() *http.Client

StdClient returns a standard *http.Client that uses this BrowserClient as transport. The returned client preserves TLS fingerprinting, proxy rotation, and middleware.

func (*BrowserClient) Use

func (bc *BrowserClient) Use(mw ...Middleware)

Use appends middlewares to the client's middleware chain. Middlewares execute in the order they are added (first added = outermost).

type BrowserIdentity added in v1.21.0

type BrowserIdentity struct {
	BrowserProfile
	// ClientHints holds the sec-ch-ua-* headers derived from UserAgent via
	// ClientHintsHeaders. nil for Safari/Firefox (they send no Client Hints).
	ClientHints map[string]string
}

BrowserIdentity is the single owner of everything a target observes about this client's browser half: the TLS fingerprint (JA3/JA4), the User-Agent, and the Client Hints (sec-ch-ua-*) derived from that User-Agent. It embeds BrowserProfile as its core so every existing caller of BuiltinProfiles, RandomProfile, PlatformMatchedProfile and session.WithProfile keeps compiling unchanged — promoted fields give id.UserAgent, id.TLSProfile, id.Browser, id.OS, id.Mobile directly, and id.BrowserProfile is reachable when the whole struct is needed.

A BrowserClient built with only a TLSProfile (the common case) resolves its Identity() from BuiltinProfiles so the UA and Client Hints agree with the JA3 by contract, not coincidence. WithIdentity sets profile and UA together so they can never drift apart.

func DefaultIdentity added in v1.30.0

func DefaultIdentity() BrowserIdentity

DefaultIdentity returns the BrowserIdentity of a client built with no WithProfile or WithIdentity: the default TLS profile, the first BuiltinProfiles User-Agent for it (the Windows desktop variant) and the Client Hints derived from that User-Agent. It needs no client and opens no connection, so it suits callers that only need the matching headers, for example a plain net/http request or a CDP session; a caller that holds a BrowserClient should use its Identity() instead, which also covers WithProfile and WithIdentity.

The identity changes when the default profile changes (a minor release), so code that persists a User-Agent must store the string, not the call.

func (BrowserIdentity) RequestHeaders added in v1.30.0

func (id BrowserIdentity) RequestHeaders(opts HeaderOptions) (RequestHeaders, error)

RequestHeaders builds the request headers a Chromium browser with this identity sends: user-agent, the sec-ch-ua Client Hints derived from it and the navigation or fetch set of opts, with their values and Chrome's order.

Values come from the Google Chrome 156 captures in testdata/wire (google_chrome_156_nav.json, google_chrome_156_fetch.json); the accept value of earlier majors from testdata/reference_chrome_*.json, and the order is the one the client itself uses for this identity (see ChromeNavigationHeaderOrder and ChromeFetchHeaderOrder). referer, cookie and a page's own headers are not part of the set; add them to Values and keep Order's positions with ChromeFetchHeaderOrder(custom) when you need more than the built names.

A Firefox or Safari identity returns ErrNotChromium. An unknown Site is an error. The result is a fresh copy the caller may modify.

func (BrowserIdentity) UserAgentData added in v1.30.0

func (id BrowserIdentity) UserAgentData(fullVersion string) (UserAgentData, error)

UserAgentData builds the structured Client Hints of the identity. The brand list is produced by the same code as the sec-ch-ua header, so the two agree by construction, GREASE brand and order included.

fullVersion is the browser's full version (for example "156.0.8078.80"). A Chrome User-Agent carries only the reduced "156.0.0.0", so the build and patch numbers cannot be derived from the identity; pass the real ones when the page can observe them. Empty means the reduced form of the identity's own major, "N.0.0.0".

type BrowserProfile

type BrowserProfile struct {
	UserAgent  string
	TLSProfile TLSProfile
	Browser    string // "chrome", "firefox", "safari", "edge"
	OS         string // "windows", "macos", "linux", "android", "ios"
	Mobile     bool
}

BrowserProfile pairs a User-Agent string with a matching TLS fingerprint and metadata for filtering.

func PlatformMatchedProfile

func PlatformMatchedProfile() BrowserProfile

PlatformMatchedProfile returns a BrowserProfile whose OS matches the actual runtime platform (runtime.GOOS).

func RandomProfile

func RandomProfile(opts ...ProfileOption) BrowserProfile

RandomProfile returns a random BrowserProfile matching the given filters. With no options, returns any profile.

When no profile satisfies every filter it never substitutes an unrelated browser: if a browser filter was given, the OS and mobile filters are relaxed (the browser is kept, e.g. WithBrowser("safari")+WithOS("macos") yields an iOS Safari profile); if that still matches nothing, or no browser filter was given, it returns the zero BrowserProfile, whose UserAgent is "". Callers that filter must check for that.

type ByparrConfig

type ByparrConfig struct {
	// BaseURL is the solver endpoint (e.g. "http://127.0.0.1:8191").
	BaseURL string

	// Timeout for solve requests. Default: 60s.
	Timeout time.Duration

	// CacheTTL for solved cookies. Default: 25 minutes (cf_clearance lifetime).
	CacheTTL time.Duration
}

ByparrConfig configures the Byparr/FlareSolverr solver.

type ByparrSolver

type ByparrSolver struct {
	// contains filtered or unexported fields
}

ByparrSolver implements CookieProvider by calling a FlareSolverr-compatible API. Caches solved cookies per domain with TTL.

func NewByparrSolver

func NewByparrSolver(cfg ByparrConfig) *ByparrSolver

NewByparrSolver creates a CookieProvider that calls Byparr/FlareSolverr.

func (*ByparrSolver) GetCookie

func (s *ByparrSolver) GetCookie(domain string) string

GetCookie returns a cached cf_clearance cookie for the domain.

func (*ByparrSolver) Solve

func (s *ByparrSolver) Solve(domain string, challenge *CloudflareError) (string, error)

Solve calls the Byparr/FlareSolverr API to solve a Cloudflare challenge.

type ChallengeType

type ChallengeType string

ChallengeType identifies the kind of Cloudflare challenge.

const (
	ChallengeJS           ChallengeType = "js_challenge"
	ChallengeTurnstile    ChallengeType = "managed_challenge"
	ChallengeBlock        ChallengeType = "block"
	ChallengeManagedAt200 ChallengeType = "managed_challenge_200"
)

type ClientOption

type ClientOption func(*clientConfig)

ClientOption configures a BrowserClient.

func WithBackend

func WithBackend(factory BackendFactory) ClientOption

WithBackend sets a custom backend factory for creating the HTTPDoer. If not set, the default bogdanfinn/tls-client backend is used. The backend owns reading the response body, so it must honour BackendConfig.MaxBodyBytes (WithMaxBodyBytes) itself; see ReadBodyLimit.

func WithCookieJar added in v1.28.0

func WithCookieJar(jar http.CookieJar) ClientOption

WithCookieJar makes the client store and send cookies through jar instead of its own private jar. Pass the same jar to several clients to share one cookie store, as the tabs and workers of one browser do: a cookie set by a response to one client is sent by the next request of another.

The jar decides everything about a cookie: which domain and path it is scoped to, expiry and deletion (a cookie set with MaxAge < 0 is removed), and the order cookies are sent in. net/http/cookiejar orders them as RFC 6265 does and as Chrome does: longer path first, then earliest creation, so cookies from one response keep their Set-Cookie order. The caller can seed, read or delete cookies by calling the jar directly, which lets it drop an explicit Cookie header (a Cookie header set on a request is sent in addition to the jar's cookies, so a cookie in both is sent twice).

The jar must be safe for concurrent use (net/http/cookiejar is). A nil jar keeps the default per-client jar. The tls-client default jar is not changed: it keys cookies by the last two host labels, ignores Domain and Path, and returns one response's cookies in reverse Set-Cookie order. Works with both backends and does not touch the TLS or HTTP/2 wire.

func WithCookieSolver

func WithCookieSolver(provider CookieProvider) ClientOption

WithCookieSolver enables Cloudflare challenge solving via a CookieProvider. Automatically adds CloudflareDetectMiddleware and CloudflareCookieMiddleware.

func WithDebug

func WithDebug() ClientOption

WithDebug enables request/response logging via slog.Debug. Automatically adds LoggingMiddleware to the middleware chain.

func WithDialControl

func WithDialControl(fn func(network, address string) error) ClientOption

WithDialControl overrides the connect-time (tier-1) SSRF guard: the rebind-proof check run on the already-resolved address immediately before connect(2), on both backends' dialers. Pass go-kit/httputil's SSRFGuards() dial closure (or DenyBlockedAddress) here for the framework's full policy. A nil fn disables the connect-time guard.

On a PROXIED client this hook sees only the proxy's address, never the real target — pair it with WithRequestURLGuard (tier 3), which does. The client passes the configured proxy's own endpoint (its resolved addresses and port, following SetProxy) without consulting fn, so a proxy on a private address stays reachable; every other address still goes through fn. The consequence: a stricter caller guard is bypassed for those proxy endpoints (every proxy set on this client recently, at most the last 64 distinct URLs), because they are operator configuration.

func WithFetchMode added in v1.27.0

func WithFetchMode() ClientOption

WithFetchMode makes the client behave like a Chrome page issuing fetch() and XMLHttpRequest calls rather than top-level navigations (the default). Two things change, and both match what Chrome sends for such requests:

  • HTTP/2: the HEADERS frame carries exclusive priority weight 220 instead of the navigation weight 256.
  • Headers: a request without its own "priority" header gets "priority: u=1, i". A priority header the caller sets is kept as is.

The mode belongs to the whole client because HTTP/2 priority is a property of the connection's transport, not of a single request; a caller that mixes navigations and fetches builds one client of each (they then use separate connections, which Chrome does not). The cookie jar is per client.

It needs a Chrome-family TLS profile (chrome_*, brave_*) and a Chromium identity (chrome, edge, brave); on Firefox or Safari, or an identity that names one of them, NewClient returns ErrFetchModeUnsupported rather than present a Chrome request shape over another browser's fingerprint. It is opt-in for every profile, chrome_131 included: without it the wire is unchanged. With WithStdHTTP only the header part applies (net/http cannot set a priority).

func WithFollowRedirects

func WithFollowRedirects() ClientOption

WithFollowRedirects enables redirect following (disabled by default).

func WithHTTP3

func WithHTTP3() ClientOption

WithHTTP3 enables HTTP/3 QUIC support (tls-client backend only).

func WithHeaderOrder

func WithHeaderOrder(order []string) ClientOption

WithHeaderOrder sets the default HTTP header ordering for requests.

func WithIdentity added in v1.21.0

func WithIdentity(id BrowserIdentity) ClientOption

WithIdentity sets the browser identity — TLS profile and User-Agent (plus Client Hints) — together, so they can never be configured apart and drift. The identity's TLSProfile is installed as the backend's fingerprint, and Identity() returns the supplied identity verbatim. If ClientHints is nil for a Chromium UA, it is derived from the User-Agent via ClientHintsHeaders so the identity the client reports is always complete.

WithIdentity and WithProfile may both be passed; WithIdentity wins for the profile (it sets c.profile too) and supplies the UA, which WithProfile alone cannot do. Use WithIdentity when the caller already holds a BrowserProfile from RandomProfile/PlatformMatchedProfile and wants the client's reported identity to match exactly.

func WithInsecureSkipVerify

func WithInsecureSkipVerify() ClientOption

WithInsecureSkipVerify disables TLS certificate verification. WARNING: this makes connections vulnerable to man-in-the-middle attacks. Use only for local testing (httptest.NewTLSServer) or explicit MITM proxy inspection. Never use in production against public endpoints.

func WithMaxBodyBytes added in v1.29.0

func WithMaxBodyBytes(n int64) ClientOption

WithMaxBodyBytes bounds the response body Do returns to n bytes, measured after content-decoding (gzip, deflate, zstd). Reading stops at n+1 bytes: a larger body is neither buffered nor drained, the connection is closed, and Do returns an error that satisfies errors.Is(err, ErrBodyTooLarge). A highly compressed body is stopped while it is being decoded, so a small payload cannot expand past n. A body of exactly n bytes is returned whole.

The limit covers every request the client makes: Do, DoCtx, DoWithHeaderOrder, RoundTrip, each redirect hop's final response, and a body replaced by the ox-browser fallback. The error is never retried on another proxy. A backend from WithBackend is handed the limit in BackendConfig.MaxBodyBytes and must enforce it itself, for example by reading its body with ReadBodyLimit: the client's check on the finished body only rejects it, it cannot prevent a backend that ignores the limit from allocating the whole body first.

n must be positive; NewClient returns an error otherwise. Without this option the body is read in full, as before.

func WithOxBrowser

func WithOxBrowser(url string) ClientOption

WithOxBrowser enables ox-browser integration for CF solving and smart fetch. If no CookieProvider is already set, adds OxBrowserSolver + CloudflareDetectMiddleware. Always adds SmartFetchMiddleware as a fallback for CF-challenged responses. url is the ox-browser base URL (e.g. "http://127.0.0.1:8901"). Calls to ox-browser carry X-Internal-Secret from INTERNAL_SERVICE_SECRET when set — unless a proxy pool routes them through an external proxy, where the header would transit the proxy and is not sent.

func WithProfile

func WithProfile(p TLSProfile) ClientOption

WithProfile sets the TLS client profile for fingerprint impersonation. The zero value "" means "no preference" and keeps the default profile; a non-empty name with no backend mapping makes NewClient fail with ErrUnknownTLSProfile.

func WithProxy

func WithProxy(url string) ClientOption

WithProxy sets the proxy URL (e.g. "socks5://user:pass@host:port").

func WithProxyPool

func WithProxyPool(pool ProxyPoolProvider) ClientOption

WithProxyPool enables per-request proxy rotation. Each call to Do() will cycle to the next proxy in the pool.

func WithRedirectGuard

func WithRedirectGuard(fn func(req *http.Request, via []*http.Request) error) ClientOption

WithRedirectGuard overrides the per-hop (tier-2) SSRF guard: a CheckRedirect-shaped closure run on every redirect hop the backend follows (only relevant with WithFollowRedirects). It MUST re-own a hop cap — a custom CheckRedirect replaces net/http's built-in 10-hop limit. Pass go-kit/httputil's SSRFGuards() redirect closure here. A nil fn disables the per-hop guard (the backend then falls back to its built-in redirect cap).

On the std backend, req and via are the exact *http.Request values net/http's own redirect loop uses (Body/Cancel/ctx and all). On the tls backend, req and via are adapted from bogdanfinn/fhttp's redirect chain: Method, URL, and Header are populated faithfully hop-by-hop (fhttp builds its own via chain from real prior requests, same shape as net/http's), but Body/GetBody/Cancel/Context and any other *http.Request field are left at their zero value — a guard that needs only len(via)/via[i].URL/via[i].Host (the go-kit SSRFGuards() shape) works unchanged on both backends; a guard reading Body or Context should not assume either backend populates them.

func WithRequestURLGuard

func WithRequestURLGuard(fn func(ctx context.Context, u *url.URL) error) ClientOption

WithRequestURLGuard overrides the pre-request (tier-3) SSRF guard: a check on the INITIAL target URL, evaluated before the (possibly proxied) fetch. This is the ONLY tier that guards a proxied fetch's initial target, since a proxied dial control (tier 1) sees only the proxy. Signature matches go-kit/httputil.CheckURL, so a consumer can wire it directly. A nil fn disables the pre-request guard.

func WithRetryOnBlock

func WithRetryOnBlock(n int) ClientOption

WithRetryOnBlock enables automatic retry with proxy rotation when the server returns a block status (403, 429). Each retry uses the next proxy from the pool. Requires WithProxyPool. n is the number of extra retries (total attempts = 1 + n).

func WithStdHTTP

func WithStdHTTP() ClientOption

WithStdHTTP uses the standard net/http backend instead of tls-client. No TLS fingerprinting — useful for testing and CGO-free environments.

func WithTimeout

func WithTimeout(seconds int) ClientOption

WithTimeout sets the request timeout in seconds.

func WithWebshareCountry

func WithWebshareCountry(apiKey string, countries ...string) ClientOption

WithWebshareCountry creates a Webshare backbone proxy pool targeting the given ISO-2 country codes (e.g. "US", "GB"). Defaults to ["US"] if no countries are given. The API call is made eagerly; if it fails, NewClient returns the error.

The client owns the pool this option builds: call BrowserClient.Close to stop the pool's periodic credential refresher when the client is done.

func WithWebshareRotating

func WithWebshareRotating(username, password string, countries ...string) ClientOption

WithWebshareRotating creates a Webshare rotating-endpoint proxy pool without calling the Webshare API. Uses username-CC-rotate syntax. Defaults to ["US"] when no countries are given.

The client owns the pool this option builds (BrowserClient.Close releases it); a rotating pool runs no refresher, so closing it is a no-op today.

func WithoutSSRFGuard

func WithoutSSRFGuard() ClientOption

WithoutSSRFGuard clears all three SSRF guard tiers (dial, redirect, pre-request), restoring the pre-guard byte-for-byte behavior.

FOR TESTS ONLY. A BrowserClient is fail-closed by construction — its built-in stdlib default-deny refuses loopback/private/link-local targets, which is exactly what an httptest (127.0.0.1) suite needs to opt out of. Never call this from production code; the fleet fitness function forbids it outside _test.go.

type CloudflareError

type CloudflareError struct {
	Type       ChallengeType
	StatusCode int
	RayID      string
}

CloudflareError indicates a Cloudflare challenge or block was detected.

func DetectCloudflare

func DetectCloudflare(resp *Response) *CloudflareError

DetectCloudflare inspects a Response for Cloudflare challenge markers. Returns nil if the response is not a Cloudflare challenge.

Signal precedence (under the server~cloudflare gate):

  1. Block markers (any status) — a block page that also mentions challenge-platform or carries cf-mitigated must stay a block, not be promoted to a solvable challenge.
  2. cf-mitigated: challenge header (any status) — the documented, status-independent challenge-page signal. Top precedence among challenge signals.
  3. Body markers (any status): turnstile, then challenge-platform/_cf_chl_opt.

Status is a kind hint, not a gate: 200 → ChallengeManagedAt200, otherwise ChallengeJS. The fix is additive (recognise more challenges), never promotive (a 403 WAF block carrying neither cf-mitigated nor block/body markers returns nil — promoting unknown 403s would burn solver time on hard blocks).

Cloudflare moved the JS/managed-challenge status from 503 to 403 on 2023-03-01; cf-mitigated: challenge is set on all challenge-page types regardless of status and is the only valid value for that header.

func (*CloudflareError) Error

func (e *CloudflareError) Error() string

type Constraints added in v1.32.0

type Constraints struct {
	// Profiles lists the TLS profiles an identity may ride. Empty means the
	// profiles of BuiltinProfiles. A profile outside BuiltinProfiles (for
	// example a legacy one) is used only when listed here, so a frozen
	// profile is never drawn by accident. A listed profile the model has no
	// entry for contributes nothing: Brave (it sends a Chrome User-Agent, so
	// the model cannot tell it apart), and Firefox or Safari whenever the
	// model lacks their exact version. When no listed profile has an entry,
	// the call returns ErrNoIdentity.
	Profiles []TLSProfile `json:"profiles,omitempty"`
	// OS limits the operating systems ("windows", "macos", "linux",
	// "android", "ios"). Empty means all.
	OS []string `json:"os,omitempty"`
	// IncludeMobile also draws mobile identities, in the model's
	// desktop/mobile proportion. Off by default: the built-in TLS profiles
	// were measured as desktop Chrome.
	IncludeMobile bool `json:"include_mobile,omitempty"`
	// Locales lists the locales to pick the accept-language from, as BCP 47
	// tags such as "de-DE"; case is normalised ("en-us" becomes "en-US"). The
	// model does not cover language; the pick is
	// uniform and seeded. Empty means "en-US".
	Locales []string `json:"locales,omitempty"`
	// MinWeight is the smallest probability an identity may have. An
	// identity the model gives less than this is dropped and the rest
	// renormalised, repeating until none is below the floor: a fleet of a
	// few accounts that drew a 0.1% browser would stand out more than the
	// model's long tail is worth. Zero means DefaultMinWeight; a negative
	// value turns the floor off.
	MinWeight float64 `json:"min_weight,omitempty"`
}

Constraints narrows the identities GenerateIdentity and IdentityDistribution draw from. The zero value means: every built-in profile, desktop only, every OS, locale en-US.

type CookieProvider

type CookieProvider interface {
	// GetCookie returns a cached cf_clearance cookie for the domain.
	// Returns empty string if no cached cookie is available.
	GetCookie(domain string) string

	// Solve attempts to solve a Cloudflare challenge and returns the cookie string.
	// The cookie string should be in "cf_clearance=value" format.
	Solve(domain string, challenge *CloudflareError) (string, error)
}

CookieProvider obtains Cloudflare clearance cookies from an external source.

type FetchMode added in v1.30.0

type FetchMode int

FetchMode is the kind of request a header set describes. Chrome sends a different set, values and order for a top-level navigation than for a fetch() or XMLHttpRequest issued by a page.

const (
	// FetchNavigate is a top-level document navigation (the default).
	FetchNavigate FetchMode = iota
	// FetchCORS is a fetch() or XMLHttpRequest issued by a page: accept
	// "*/*", sec-fetch-mode cors, sec-fetch-dest empty, priority "u=1, i".
	FetchCORS
)

type FetchSite added in v1.30.0

type FetchSite string

FetchSite is the value of sec-fetch-site: how the request's initiator relates to its target.

const (
	// FetchSiteNone is a navigation the user typed or picked from a bookmark.
	FetchSiteNone FetchSite = "none"
	// FetchSiteSameOrigin is a request to the initiator's own origin.
	FetchSiteSameOrigin FetchSite = "same-origin"
	// FetchSiteSameSite is a request to another origin of the same site.
	FetchSiteSameSite FetchSite = "same-site"
	// FetchSiteCrossSite is a request to a different site.
	FetchSiteCrossSite FetchSite = "cross-site"
)

type FetchSmartResponse

type FetchSmartResponse struct {
	Status     int    `json:"status"`
	Body       string `json:"body"`
	Method     string `json:"method"`
	CfDetected bool   `json:"cf_detected"`
	ElapsedMs  int64  `json:"elapsed_ms"`
	Error      string `json:"error,omitempty"`
}

FetchSmartResponse is the response from /fetch-smart.

type GeneratedIdentity added in v1.32.0

type GeneratedIdentity struct {
	BrowserIdentity
	// Locale is the drawn locale tag.
	Locale string
	// AcceptLanguage is the accept-language value for Locale.
	AcceptLanguage string
	// ModelVersion labels the model data the identity was drawn from.
	ModelVersion string
	// PoolVersion labels the set of identities the draw chose among: a hash
	// of every candidate entry (OS, device, model value, TLS profile,
	// browser, User-Agent) left after the constraints and the weight floor.
	// A change to the built-in profile set, or to a constraint or the floor
	// that changes which entries remain, changes it. Constraints.Locales is
	// not part of it: it changes the locale draw, so keep the locale list
	// with the stored identity. The label belongs to this implementation;
	// another language does not reproduce it.
	PoolVersion string
}

GeneratedIdentity is a BrowserIdentity together with the request language drawn for it.

Only GeneratedIdentity.RequestHeaders applies AcceptLanguage. Passing g.BrowserIdentity to WithIdentity, or building headers with BrowserClient.RequestHeaders, drops it: those see the plain BrowserIdentity and fall back to en-US unless HeaderOptions.AcceptLanguage is set from g.AcceptLanguage.

func GenerateIdentity added in v1.32.0

func GenerateIdentity(seed uint64, c Constraints) (GeneratedIdentity, error)

GenerateIdentity draws one browser identity from a model of real browser traffic, restricted to identities a built-in TLS profile backs.

PERSIST THE RESULT. Do not store the seed and call this again later: the same seed and constraints give the same identity only while the model data (GeneratedIdentity.ModelVersion), the pool of candidate entries (GeneratedIdentity.PoolVersion; it moves whenever BuiltinProfiles or the weight floor change) and the algorithm are unchanged, and a library upgrade can change any of them. Store the drawn identity, for example its TLS profile, OS and locale, and render from that. IdentityPoolVersion lets a caller see in advance whether a seed would still land where it did.

The model chooses which built-in entry (browser, major version, OS) is presented. The draw follows the model's level-wise distribution, as the upstream sampler does: device, then OS given the device, then browser given both, each renormalised over what the pool allows at that step. It is not the model's joint distribution conditioned on the pool, so a constraint on one level does not reweight the levels above it. The model never supplies strings: the User-Agent, Client Hints and header values come from the built-in entry, so the captured Chrome 156 values stay authoritative.

Draw once per session or account and keep the result; do not draw per request.

func (GeneratedIdentity) RequestHeaders added in v1.32.0

func (g GeneratedIdentity) RequestHeaders(opts HeaderOptions) (RequestHeaders, error)

RequestHeaders is BrowserIdentity.RequestHeaders with AcceptLanguage filled in from the drawn locale unless opts sets one.

type HTTPDoer

type HTTPDoer interface {
	Do(req *Request) (*Response, error)
	SetProxy(url string) error
	GetCookieValue(rawURL, name string) string
}

HTTPDoer executes HTTP requests. TLS backends implement this interface.

type Handler

type Handler func(req *Request) (*Response, error)

Handler processes a Request and returns a Response.

func ClientHintsMiddleware

func ClientHintsMiddleware(next Handler) Handler

ClientHintsMiddleware auto-injects sec-ch-ua-* headers for Chromium-based profiles. This ensures Client Hints match the User-Agent, preventing fingerprint mismatch detection.

func CloudflareDetectMiddleware

func CloudflareDetectMiddleware(next Handler) Handler

CloudflareDetectMiddleware inspects responses for Cloudflare challenges. If a challenge is detected, it returns a *CloudflareError (use errors.As to extract). Non-challenge responses pass through unchanged.

func LoggingMiddleware

func LoggingMiddleware(next Handler) Handler

LoggingMiddleware logs each request's method, URL, status, and latency via slog.

type HeaderOptions added in v1.30.0

type HeaderOptions struct {
	// Mode is FetchNavigate (zero value) or FetchCORS.
	Mode FetchMode
	// Site is the sec-fetch-site value. Empty means FetchSiteNone for a
	// navigation and FetchSiteSameOrigin for FetchCORS.
	Site FetchSite
	// AcceptLanguage overrides the accept-language value, for a caller whose
	// locale is not en-US. Empty keeps "en-US,en;q=0.9".
	AcceptLanguage string
	// OmitAcceptEncoding leaves accept-encoding out, for a caller that
	// decodes nothing itself and wants an identity-encoded body.
	OmitAcceptEncoding bool
}

HeaderOptions selects the variant of a request header set. The zero value is a top-level navigation with Chrome's default locale and accept-encoding.

type HttpStatusError

type HttpStatusError struct {
	StatusCode int
}

HttpStatusError wraps a retryable HTTP status code. Exported so consumers can use stealth.RetryDo/RetryHTTP directly without reimplementing their own error type for errors.As matching.

func (*HttpStatusError) Error

func (e *HttpStatusError) Error() string

type Jitter

type Jitter = pacing.Jitter

Jitter is an alias for pacing.Jitter, preserving the stealth-package API for existing callers (go-twitter vendor). New code should import pacing directly.

type Middleware

type Middleware func(next Handler) Handler

Middleware wraps a Handler to add behavior before/after request execution.

func Chain

func Chain(middlewares ...Middleware) Middleware

Chain composes multiple middlewares into a single Middleware. Middlewares are applied in order: first middleware wraps the outermost layer.

Chain(logging, retry, rateLimit)(handler)
// execution order: logging → retry → rateLimit → handler

func CloudflareCookieMiddleware

func CloudflareCookieMiddleware(provider CookieProvider) Middleware

CloudflareCookieMiddleware returns a middleware that:

  1. Injects cached cf_clearance cookies from the provider before each request.
  2. On Cloudflare challenge response, calls provider.Solve() to get a cookie and retries once.

func RateLimitMiddleware

func RateLimitMiddleware(limiter *ratelimit.DomainLimiter) Middleware

RateLimitMiddleware returns a middleware that enforces per-domain rate limits. Blocks until the request is allowed or context deadline is exceeded.

func RateLimitMiddlewareWithContext

func RateLimitMiddlewareWithContext(ctx context.Context, limiter *ratelimit.DomainLimiter) Middleware

RateLimitMiddlewareWithContext returns a rate limit middleware that respects context cancellation.

func RetryMiddleware

func RetryMiddleware(cfg RetryConfig) Middleware

RetryMiddleware returns a middleware that retries failed requests using the given config.

func RetryMiddlewareWithContext

func RetryMiddlewareWithContext(ctx context.Context, cfg RetryConfig) Middleware

RetryMiddlewareWithContext returns a retry middleware that respects context cancellation.

func SmartFetchMiddleware

func SmartFetchMiddleware(oxClient *OxBrowserClient) Middleware

SmartFetchMiddleware returns a middleware that detects Cloudflare challenges and delegates to ox-browser /fetch-smart for automatic solving. On CF detection, the original response is replaced with ox-browser's solved response. Non-CF responses pass through unchanged.

type OxBrowserClient

type OxBrowserClient struct {
	// contains filtered or unexported fields
}

OxBrowserClient calls the ox-browser REST API.

func NewOxBrowserClient

func NewOxBrowserClient(baseURL string) *OxBrowserClient

NewOxBrowserClient creates a client for ox-browser at the given base URL. Does NOT route through proxy. Use NewOxBrowserClientWithProxy for stealth scenarios. Requests to ox-browser carry the X-Internal-Secret header from INTERNAL_SERVICE_SECRET when that env var is set (go-kit svcauth); the header is scoped to baseURL's origin and stripped on requests and redirect hops to anywhere else.

func NewOxBrowserClientWithProxy

func NewOxBrowserClientWithProxy(baseURL string, proxyFn func(*http.Request) (*url.URL, error)) *OxBrowserClient

NewOxBrowserClientWithProxy creates a client for ox-browser that routes all requests (to the ox-browser API itself) through the given proxy function. proxyFn is compatible with http.Transport.Proxy — pass proxyPool.TransportProxy() to ensure each call to ox-browser uses a fresh rotated residential IP.

No X-Internal-Secret is attached on this path, by design: a plain-http request to ox-browser transits the external proxy in cleartext, so the secret would be visible to the proxy operator.

func (*OxBrowserClient) Analyze

func (c *OxBrowserClient) Analyze(ctx context.Context, url string) (*AnalyzeResponse, error)

Analyze calls ox-browser /analyze for tech detection.

func (*OxBrowserClient) FetchSmart

func (c *OxBrowserClient) FetchSmart(ctx context.Context, url string) (*FetchSmartResponse, error)

FetchSmart calls ox-browser /fetch-smart (auto CF bypass).

func (*OxBrowserClient) Solve

func (c *OxBrowserClient) Solve(ctx context.Context, url, challengeType string) (map[string]string, error)

Solve calls ox-browser /solve to get CF clearance cookies.

type OxBrowserSolver

type OxBrowserSolver struct {
	// contains filtered or unexported fields
}

OxBrowserSolver implements CookieProvider using ox-browser /solve. Drop-in replacement for ByparrSolver.

func NewOxBrowserSolver

func NewOxBrowserSolver(cfg OxBrowserSolverConfig) *OxBrowserSolver

NewOxBrowserSolver creates a CookieProvider backed by ox-browser. Its calls to ox-browser carry X-Internal-Secret from INTERNAL_SERVICE_SECRET when set — unless cfg.ProxyFn routes them through an external proxy (see NewOxBrowserClientWithProxy).

func (*OxBrowserSolver) GetCookie

func (s *OxBrowserSolver) GetCookie(domain string) string

GetCookie returns a cached cf_clearance cookie for the domain.

func (*OxBrowserSolver) Solve

func (s *OxBrowserSolver) Solve(domain string, challenge *CloudflareError) (string, error)

Solve calls ox-browser /solve to obtain CF clearance cookies.

type OxBrowserSolverConfig

type OxBrowserSolverConfig struct {
	// BaseURL of ox-browser (e.g. "http://127.0.0.1:8901").
	BaseURL string

	// CacheTTL for solved cookies. Default: 25 minutes.
	CacheTTL time.Duration

	// ProxyFn, if non-nil, routes requests to ox-browser through a proxy.
	// Compatible with http.Transport.Proxy — pass proxyPool.TransportProxy().
	// With ProxyFn set, no X-Internal-Secret is sent to ox-browser: the
	// request itself transits the external proxy, which would see the header.
	ProxyFn func(*http.Request) (*url.URL, error)
}

OxBrowserSolverConfig configures the ox-browser CF solver.

type ProfileOption

type ProfileOption func(*profileFilter)

ProfileOption configures profile filtering for RandomProfile.

func WithBrowser

func WithBrowser(b string) ProfileOption

WithBrowser filters profiles by browser. Valid values: "chrome", "firefox", "safari", "edge".

func WithMobile

func WithMobile(mobile bool) ProfileOption

WithMobile filters for mobile or desktop profiles.

func WithOS

func WithOS(os string) ProfileOption

WithOS filters profiles by operating system. Valid values: "windows", "macos", "linux", "android", "ios".

type ProxyPoolProvider

type ProxyPoolProvider interface {
	Next() string
}

ProxyPoolProvider returns the next proxy URL for rotation.

type Request

type Request struct {
	Method      string
	URL         string
	Headers     map[string]string
	Body        io.Reader
	HeaderOrder []string // backend uses this for header ordering
}

Request represents an outgoing HTTP request for the middleware chain.

type RequestHeaders added in v1.30.0

type RequestHeaders struct {
	Values map[string]string
	Order  []string
}

RequestHeaders is a browser request header set: lower-case names with their values, and the order Chrome writes them in. Pass Values to a request and Order to WithHeaderOrder or DoWithHeaderOrder. Order lists exactly the names in Values.

type Response

type Response struct {
	Body       []byte
	Headers    map[string]string
	StatusCode int
}

Response represents an HTTP response from the middleware chain.

type RetryConfig

type RetryConfig struct {
	MaxRetries  int
	InitialWait time.Duration
	MaxWait     time.Duration
	Multiplier  float64
	JitterPct   float64 // random variation (0 = no jitter)
}

RetryConfig controls retry behavior.

type RetryHookFunc

type RetryHookFunc func(ctx context.Context, attempt int, maxAttempts int, err error)

RetryHookFunc is called before each retry attempt. attempt is 1-based; err is the error from the previous attempt.

type RetryTracker

type RetryTracker struct {
	// contains filtered or unexported fields
}

RetryTracker tracks per-URL retry state across calls. Thread-safe. Entries expire after TTL via lazy eviction.

func NewRetryTracker

func NewRetryTracker(maxAttempts int, ttl time.Duration) *RetryTracker

NewRetryTracker creates a tracker that allows up to maxAttempts per URL before blocking retries. Entries expire after ttl (lazy eviction).

func (*RetryTracker) RecordAttempt

func (t *RetryTracker) RecordAttempt(url string, err error)

RecordAttempt records a failed attempt for the URL. If the error is a permanent HTTP status (4xx client errors), the URL is marked as permanently failed and no further retries are allowed regardless of attempt count.

func (*RetryTracker) RecordSuccess

func (t *RetryTracker) RecordSuccess(url string)

RecordSuccess clears all retry state for the URL, making it fully retryable again.

func (*RetryTracker) ShouldRetry

func (t *RetryTracker) ShouldRetry(url string) bool

ShouldRetry returns true if the URL is eligible for another attempt. Unknown URLs are always retryable. Expired entries are evicted lazily.

type SiteInfo

type SiteInfo struct {
	WAF          string        // Detected WAF/CDN name, or ""
	Technologies []AnalyzeTech // Full tech list
	FetchedAt    time.Time
}

SiteInfo contains cached intelligence about a domain.

func (*SiteInfo) HasTech

func (si *SiteInfo) HasTech(name string) bool

HasTech returns true if the site has the named technology.

type SiteIntel

type SiteIntel struct {
	// contains filtered or unexported fields
}

SiteIntel provides per-domain tech/WAF intelligence with caching.

func NewSiteIntel

func NewSiteIntel(client *OxBrowserClient) *SiteIntel

NewSiteIntel creates a SiteIntel cache backed by ox-browser /analyze.

func (*SiteIntel) Get

func (si *SiteIntel) Get(rawURL string) (*SiteInfo, error)

Get returns cached SiteInfo for the domain, fetching via /analyze if needed. Concurrent calls for the same domain are deduplicated via singleflight.

func (*SiteIntel) SuggestProfile

func (si *SiteIntel) SuggestProfile(rawURL string) BrowserProfile

SuggestProfile returns a BrowserProfile optimized for the target site's WAF.

type SolveResponse

type SolveResponse struct {
	Status  string            `json:"status"`
	Cookies map[string]string `json:"cookies"`
	Error   string            `json:"error,omitempty"`
}

SolveResponse is the response from /solve.

type TLSProfile

type TLSProfile string

TLSProfile identifies a TLS fingerprint profile.

const (
	ProfileChrome131   TLSProfile = "chrome_131"
	ProfileChrome133   TLSProfile = "chrome_133"
	ProfileChrome144   TLSProfile = "chrome_144"
	ProfileChrome146   TLSProfile = "chrome_146"
	ProfileChrome150   TLSProfile = "chrome_150"
	ProfileChrome152   TLSProfile = "chrome_152"
	ProfileChrome156   TLSProfile = "chrome_156"
	ProfileEdge156     TLSProfile = "edge_156"
	ProfileFirefox133  TLSProfile = "firefox_133"
	ProfileFirefox148  TLSProfile = "firefox_148"
	ProfileBrave146    TLSProfile = "brave_146"
	ProfileSafari16    TLSProfile = "safari_16_0"
	ProfileSafariIOS18 TLSProfile = "safari_ios_18_0"
	ProfileSafariIOS17 TLSProfile = "safari_ios_17_0"
	ProfileSafariIOS26 TLSProfile = "safari_ios_26_0"
)

Built-in TLS profiles.

func DefaultProfile added in v1.30.0

func DefaultProfile() TLSProfile

DefaultProfile returns the TLS profile NewClient uses when no WithProfile or WithIdentity is given. It is the value to compare against or log, instead of hard-coding a ProfileChrome* constant that stops matching the client when the default moves.

type UserAgentBrand added in v1.30.0

type UserAgentBrand struct {
	Brand   string
	Version string
}

UserAgentBrand is one entry of a brand list: the name and a version.

type UserAgentData added in v1.30.0

type UserAgentData struct {
	Brands          []UserAgentBrand
	FullVersionList []UserAgentBrand
	Mobile          bool
	Platform        string

	PlatformVersion string
	Architecture    string
	Bitness         string
	Model           string
	Wow64           bool
}

UserAgentData is the structured form of an identity's Client Hints, in the shape the Chrome DevTools Protocol takes in Emulation.setUserAgentOverride's userAgentMetadata and that navigator.userAgentData reports. Brands is the low-entropy list (major versions, the same list and order as the sec-ch-ua header) and FullVersionList the high-entropy one (sec-ch-ua-full-version-list).

PlatformVersion, Architecture, Bitness, Model and Wow64 are not derivable from the User-Agent, so they are left zero for the caller, who knows the platform it presents.

type WeightedIdentity added in v1.32.0

type WeightedIdentity struct {
	Identity BrowserIdentity
	Weight   float64
}

WeightedIdentity is an identity and its probability under the constraints.

func IdentityDistribution added in v1.32.0

func IdentityDistribution(c Constraints) ([]WeightedIdentity, error)

IdentityDistribution returns every identity GenerateIdentity can draw under c and its exact probability, most likely first. It is the weight table for a caller that picks by its own stable scheme (for example rendezvous hashing, which keeps an assignment when the candidate list grows).

Directories

Path Synopsis
cmd
fingerprint-capture command
Command fingerprint-capture downloads a Chrome-for-Testing build matching a given major version, runs it against the peet.ws and browserleaks echo endpoints, and writes the observed fingerprint to testdata/reference_chrome_<major>.json with full per-metric provenance.
Command fingerprint-capture downloads a Chrome-for-Testing build matching a given major version, runs it against the peet.ws and browserleaks echo endpoints, and writes the observed fingerprint to testdata/reference_chrome_<major>.json with full per-metric provenance.
identity-model-import command
Command identity-model-import converts the browser-identity network of the fingerprint-suite project (Apache-2.0) into the model file read by internal/identitymodel.
Command identity-model-import converts the browser-identity network of the fingerprint-suite project (Apache-2.0) into the model file read by internal/identitymodel.
internal
fingerprint
Package fingerprint holds the reference-capture and oracle infrastructure that measures whether go-stealth's emitted TLS/HTTP2 fingerprint matches a real browser's.
Package fingerprint holds the reference-capture and oracle infrastructure that measures whether go-stealth's emitted TLS/HTTP2 fingerprint matches a real browser's.
identitymodel
Package identitymodel samples browser identities from a Bayesian network whose probabilities were fitted to real browser traffic.
Package identitymodel samples browser identities from a Bayesian network whose probabilities were fitted to real browser traffic.
uri
Package uri provides shared URL utility functions.
Package uri provides shared URL utility functions.
Package ratelimit provides token bucket rate limiters and per-key pacing.
Package ratelimit provides token bucket rate limiters and per-key pacing.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL
⌂ 换网址