Add session mode: signing in and adding to the cart.
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>
This commit is contained in:
1 parent
e2cbc1a55c
commit
15a34e1f84
39 files changed
+3529
-163
No files matched your search
@@ -3,12 +3,14 @@
|
||||
package fetch
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
stdhttp "net/http"
|
||||
"net/url"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
http "github.com/bogdanfinn/fhttp"
|
||||
@@ -27,7 +29,10 @@ var ErrBodyTooLarge = errors.New("response body too large")
|
||||
|
||||
// Limiter decides when a page may be fetched. Every request waits for it.
|
||||
type Limiter interface {
|
||||
// WaitCheck is for checks of a page, which Get makes.
|
||||
WaitCheck(ctx context.Context, url string) error
|
||||
// Wait is for other requests to host, which Do makes.
|
||||
Wait(ctx context.Context, host string) error
|
||||
}
|
||||
|
||||
type Options struct {
|
||||
@@ -93,16 +98,85 @@ func (c *Client) Jar() *Jar { return c.jar }
|
||||
// a check of it. A response with a bad status is not an error; its Verdict
|
||||
// says what it is.
|
||||
func (c *Client) Get(ctx context.Context, rawURL string) (*Response, error) {
|
||||
u, err := url.Parse(rawURL)
|
||||
u, err := parseURL(rawURL)
|
||||
if err != nil { return nil, err }
|
||||
if u.Scheme != "http" && u.Scheme != "https" { return nil, fmt.Errorf("unsupported URL scheme %q", u.Scheme) }
|
||||
|
||||
if err := c.limiter.WaitCheck(ctx, rawURL); err != nil { return nil, err }
|
||||
|
||||
req, err := http.NewRequestWithContext(ctx, http.MethodGet, u.String(), nil)
|
||||
if err != nil { return nil, err }
|
||||
req.Header = c.browser.navigationHeaders(u)
|
||||
req.Header = c.browser.navigationHeaders(u, nil, "")
|
||||
|
||||
return c.send(req, rawURL)
|
||||
}
|
||||
|
||||
// Request is a request made for the user in session mode, such as
|
||||
// submitting a sign-in form.
|
||||
type Request struct {
|
||||
// Method is GET or POST.
|
||||
Method string
|
||||
URL string
|
||||
// Form is the body of a POST, sent as a submitted HTML form. Body and
|
||||
// ContentType replace it for other bodies.
|
||||
Form url.Values
|
||||
Body []byte
|
||||
ContentType string
|
||||
// Referer is the page the request comes from. Without one, the request
|
||||
// looks typed in.
|
||||
Referer string
|
||||
// Header adds or replaces headers.
|
||||
Header map[string]string
|
||||
}
|
||||
|
||||
// Do sends r the way Chrome follows a link or submits a form, once the
|
||||
// limiter allows a request to the host. Unlike Get, it is not a check of the
|
||||
// page, so the URL floor does not apply.
|
||||
func (c *Client) Do(ctx context.Context, r Request) (*Response, error) {
|
||||
u, err := parseURL(r.URL)
|
||||
if err != nil { return nil, err }
|
||||
|
||||
var ref *url.URL
|
||||
if r.Referer != "" {
|
||||
if ref, err = parseURL(r.Referer); err != nil { return nil, fmt.Errorf("referer: %w", err) }
|
||||
}
|
||||
|
||||
var body io.Reader
|
||||
contentType := r.ContentType
|
||||
|
||||
switch {
|
||||
case r.Method == http.MethodGet && (r.Form != nil || r.Body != nil):
|
||||
return nil, errors.New("a GET request has no body")
|
||||
case r.Method == http.MethodGet:
|
||||
case r.Method == http.MethodPost && r.Body == nil:
|
||||
body, contentType = strings.NewReader(r.Form.Encode()), "application/x-www-form-urlencoded"
|
||||
case r.Method == http.MethodPost && contentType == "":
|
||||
return nil, errors.New("a POST body needs a content type")
|
||||
case r.Method == http.MethodPost:
|
||||
body = bytes.NewReader(r.Body)
|
||||
default:
|
||||
return nil, fmt.Errorf("unsupported method %q", r.Method)
|
||||
}
|
||||
|
||||
if err := c.limiter.Wait(ctx, u.Hostname()); err != nil { return nil, err }
|
||||
|
||||
req, err := http.NewRequestWithContext(ctx, r.Method, u.String(), body)
|
||||
if err != nil { return nil, err }
|
||||
|
||||
req.Header = c.browser.navigationHeaders(u, ref, contentType)
|
||||
for k, v := range r.Header { req.Header.Set(strings.ToLower(k), v) }
|
||||
|
||||
return c.send(req, r.URL)
|
||||
}
|
||||
|
||||
func parseURL(rawURL string) (*url.URL, error) {
|
||||
u, err := url.Parse(rawURL)
|
||||
if err != nil { return nil, err }
|
||||
if u.Scheme != "http" && u.Scheme != "https" { return nil, fmt.Errorf("unsupported URL scheme %q", u.Scheme) }
|
||||
|
||||
return u, nil
|
||||
}
|
||||
|
||||
func (c *Client) send(req *http.Request, rawURL string) (*Response, error) {
|
||||
start := time.Now()
|
||||
resp, err := c.http.Do(req)
|
||||
if err != nil { return nil, err }
|
||||
@@ -138,3 +212,20 @@ func NewResponse(rawURL, finalURL string, status int, header stdhttp.Header, bod
|
||||
|
||||
return r
|
||||
}
|
||||
|
||||
// RecordOutcome tells the limiter how a request to host went: bot checks,
|
||||
// refusals and failed requests back the host off, anything else clears its
|
||||
// backoff. It returns the backoff, or 0.
|
||||
func RecordOutcome(l *ratelimit.Limiter, host string, resp *Response, err error) time.Duration {
|
||||
switch {
|
||||
case errors.Is(err, context.Canceled):
|
||||
return 0
|
||||
case err != nil:
|
||||
return l.Failure(host)
|
||||
case resp.Verdict == Challenge || resp.Verdict == NeedsHuman || resp.Verdict == Blocked || resp.Verdict == HTTPError:
|
||||
return l.Failure(host)
|
||||
default:
|
||||
l.Success(host)
|
||||
return 0
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user