Files
dibs/internal/fetch/browser.go
T
Overlord e2cbc1a55c Add the Chrome fallback and the needs-human flow.
Bot checks escalate to headless Chrome with stealth, which claims the
same identity as tls-client and hands its cookies back to the jar. The
visit counts against the rate limits. If Chrome can't clear a check,
the URL is marked needs-human. Pressing Enter on it in the TUI, or
running 'dibs solve', opens a visible window; dibs carries on once the
page loads. Processes now merge the cookie file on save, with the
newest cookie winning.
2026-10-14 14:08:56 +02:00

156 lines
4.8 KiB
Go

package fetch
import (
"fmt"
"net/url"
"runtime"
"strconv"
"strings"
http "github.com/bogdanfinn/fhttp"
"github.com/bogdanfinn/tls-client/profiles"
"golang.org/x/net/publicsuffix"
)
// chromeMajor must match chromeProfile: the TLS and HTTP/2 fingerprint come
// from the profile, the headers from the version, so the two change together.
const chromeMajor = 152
var chromeProfile = profiles.Chrome_152
// Browser is the browser that requests claim to come from.
type Browser struct {
Profile profiles.ClientProfile
Major int
UserAgent string
Platform string // sec-ch-ua-platform, without quotes
}
// DefaultBrowser returns desktop Chrome on the current OS, so that cookies a
// real browser on this machine obtains later stay valid for these requests.
func DefaultBrowser() Browser {
ua, platform := "Windows NT 10.0; Win64; x64", "Windows"
switch runtime.GOOS {
case "darwin":
ua, platform = "Macintosh; Intel Mac OS X 10_15_7", "macOS"
case "linux":
ua, platform = "X11; Linux x86_64", "Linux"
default:
// Windows. Other systems pass as Windows too.
}
return Browser{
Profile: chromeProfile,
Major: chromeMajor,
UserAgent: fmt.Sprintf("Mozilla/5.0 (%s) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/%d.0.0.0 Safari/537.36", ua, chromeMajor),
Platform: platform,
}
}
// navigationHeaders are the headers Chrome sends when the user opens u by
// typing it or from a bookmark, in Chrome's order.
func (b Browser) navigationHeaders(u *url.URL) http.Header {
return http.Header{
"sec-ch-ua": {secCHUA(b.Major)},
"sec-ch-ua-mobile": {"?0"},
"sec-ch-ua-platform": {`"` + b.Platform + `"`},
"upgrade-insecure-requests": {"1"},
"user-agent": {b.UserAgent},
"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": {acceptLanguage(u.Hostname())},
"priority": {"u=0, i"},
http.HeaderOrderKey: {
"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",
"accept-encoding",
"accept-language",
"cookie",
"priority",
},
}
}
// Brand is one entry of Chrome's brand list, which sec-ch-ua and
// navigator.userAgentData report.
type Brand struct {
Name string
Version string
}
// Brands is Chrome's brand list. Chrome derives the GREASE brand and the
// brand order from its major version (GenerateBrandVersionList in
// Chromium's user_agent_utils.cc), so a fixed list would stand out after an
// update.
func (b Browser) Brands() []Brand {
return brands(b.Major)
}
func brands(major int) []Brand {
chars := []string{" ", "(", ":", "-", ".", "/", ")", ";", "=", "?", "_"}
versions := []string{"8", "99", "24"}
orders := [][3]int{{0, 1, 2}, {0, 2, 1}, {1, 0, 2}, {1, 2, 0}, {2, 0, 1}, {2, 1, 0}}
grease := Brand{"Not" + chars[major%len(chars)] + "A" + chars[(major+1)%len(chars)] + "Brand", versions[major%len(versions)]}
order := orders[major%len(orders)]
var out [3]Brand
out[order[0]] = grease
out[order[1]] = Brand{"Chromium", strconv.Itoa(major)}
out[order[2]] = Brand{"Google Chrome", strconv.Itoa(major)}
return out[:]
}
func secCHUA(major int) string {
var parts []string
for _, b := range brands(major) { parts = append(parts, fmt.Sprintf(`"%s";v="%s"`, b.Name, b.Version)) }
return strings.Join(parts, ", ")
}
// AcceptLanguage is the Accept-Language a local shopper's browser would
// send to host.
func AcceptLanguage(host string) string {
return acceptLanguage(host)
}
// languages maps a public suffix to the Accept-Language a local shopper's
// browser would send.
var languages = map[string]string{
"de": "de-DE,de;q=0.9,en-US;q=0.8,en;q=0.7",
"at": "de-AT,de;q=0.9,en-US;q=0.8,en;q=0.7",
"ch": "de-CH,de;q=0.9,en-US;q=0.8,en;q=0.7",
"co.uk": "en-GB,en;q=0.9",
"uk": "en-GB,en;q=0.9",
"ie": "en-IE,en;q=0.9",
"fr": "fr-FR,fr;q=0.9,en-US;q=0.8,en;q=0.7",
"be": "nl-BE,nl;q=0.9,fr-BE;q=0.8,fr;q=0.7,en;q=0.6",
"it": "it-IT,it;q=0.9,en-US;q=0.8,en;q=0.7",
"es": "es-ES,es;q=0.9,en-US;q=0.8,en;q=0.7",
"nl": "nl-NL,nl;q=0.9,en-US;q=0.8,en;q=0.7",
"pl": "pl-PL,pl;q=0.9,en-US;q=0.8,en;q=0.7",
"se": "sv-SE,sv;q=0.9,en-US;q=0.8,en;q=0.7",
"com": "en-US,en;q=0.9",
}
func acceptLanguage(host string) string {
suffix, _ := publicsuffix.PublicSuffix(strings.ToLower(host))
if lang, ok := languages[suffix]; ok { return lang }
return languages["com"]
}