package fetch import ( "fmt" "net/url" "runtime" "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. Bump both together when // updating tls-client. 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 us. 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, and anything else, which Chrome supports no better. } 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", }, } } // secCHUA builds Chrome's sec-ch-ua 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 string would give us away // after an update. func secCHUA(major int) string { 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 := fmt.Sprintf(`"Not%sA%sBrand";v="%s"`, chars[major%len(chars)], chars[(major+1)%len(chars)], versions[major%len(versions)]) order := orders[major%len(orders)] var brands [3]string brands[order[0]] = grease brands[order[1]] = fmt.Sprintf(`"Chromium";v="%d"`, major) brands[order[2]] = fmt.Sprintf(`"Google Chrome";v="%d"`, major) return strings.Join(brands[:], ", ") } // 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"] }