With cart_mode = "session", dibs signs in to the shop's account and puts an item in the cart when it fires. The account's cookies are kept apart from the watcher's and stored encrypted, with the key in the OS keyring. Without a keyring, they're stored in a file only you can read. An alert now goes out right away, without a cart link, and a second one follows once the item is in the cart. If adding fails, the second alert carries the cart link instead. Amazon only accepts additions its page scripts have seen, so plugins can now press a button in headless Chrome with ctx:click. The TUI asks for one-time codes and shows the plugin's warning about account locks in a modal. 'dibs login' signs in from a terminal, or with --browser in a Chrome window, and 'dibs logout' deletes the cookies. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
205 lines
6.3 KiB
Go
205 lines
6.3 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, in
|
|
// Chrome's order. Without a referer, u was typed in or opened from a
|
|
// bookmark. A content type makes it the submission of a form on referer.
|
|
func (b Browser) navigationHeaders(u, referer *url.URL, contentType string) http.Header {
|
|
h := 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": {fetchSite(u, referer)},
|
|
"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: {
|
|
"content-length",
|
|
"cache-control",
|
|
"sec-ch-ua",
|
|
"sec-ch-ua-mobile",
|
|
"sec-ch-ua-platform",
|
|
"origin",
|
|
"content-type",
|
|
"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",
|
|
},
|
|
}
|
|
|
|
if referer != nil {
|
|
// Chrome's default referrer policy leaves out the path on requests
|
|
// to other origins.
|
|
ref := referer.Scheme + "://" + referer.Host + "/"
|
|
if sameOrigin(u, referer) { ref = (&url.URL{Scheme: referer.Scheme, Host: referer.Host, Path: referer.Path, RawPath: referer.RawPath, RawQuery: referer.RawQuery}).String() }
|
|
|
|
h.Set("referer", ref)
|
|
}
|
|
|
|
if contentType != "" {
|
|
h.Set("cache-control", "max-age=0")
|
|
h.Set("content-type", contentType)
|
|
if referer != nil { h.Set("origin", referer.Scheme+"://"+referer.Host) } else { h.Set("origin", "null") }
|
|
}
|
|
|
|
return h
|
|
}
|
|
|
|
// fetchSite is the Sec-Fetch-Site of a navigation to u from referer.
|
|
func fetchSite(u, referer *url.URL) string {
|
|
switch {
|
|
case referer == nil:
|
|
return "none"
|
|
case sameOrigin(u, referer):
|
|
return "same-origin"
|
|
case u.Scheme == referer.Scheme && site(u.Hostname()) == site(referer.Hostname()):
|
|
return "same-site"
|
|
default:
|
|
return "cross-site"
|
|
}
|
|
}
|
|
|
|
func sameOrigin(a, b *url.URL) bool {
|
|
return a.Scheme == b.Scheme && strings.EqualFold(a.Host, b.Host)
|
|
}
|
|
|
|
// site is the registrable domain of host, such as amazon.co.uk.
|
|
func site(host string) string {
|
|
s, err := publicsuffix.EffectiveTLDPlusOne(strings.ToLower(host))
|
|
if err != nil { return strings.ToLower(host) }
|
|
|
|
return s
|
|
}
|
|
|
|
// 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"]
|
|
}
|