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.
52 lines
1.5 KiB
Go
52 lines
1.5 KiB
Go
package chrome
|
|
|
|
import (
|
|
"context"
|
|
"log/slog"
|
|
|
|
"git.security-command.org/Overlord/dibs/internal/fetch"
|
|
)
|
|
|
|
// Limiter is ratelimit.Limiter.
|
|
type Limiter interface {
|
|
WaitCheck(ctx context.Context, url string) error
|
|
}
|
|
|
|
// Pipeline fetches pages the way the shops expect a browser to, escalating
|
|
// only on failure: tls-client first, then headless Chrome when a bot check
|
|
// answers. A bot check Chrome cannot clear either comes back as
|
|
// fetch.NeedsHuman.
|
|
type Pipeline struct {
|
|
Client *fetch.Client
|
|
// Chrome may be nil, which leaves bot checks as they are.
|
|
Chrome *Chrome
|
|
Limiter Limiter
|
|
Logger *slog.Logger
|
|
}
|
|
|
|
func (p *Pipeline) Get(ctx context.Context, rawURL string) (*fetch.Response, error) {
|
|
resp, err := p.Client.Get(ctx, rawURL)
|
|
if err != nil || resp.Verdict != fetch.Challenge || p.Chrome == nil { return resp, err }
|
|
|
|
p.Logger.Info("bot check, retrying in headless Chrome once the rate limits allow", "url", rawURL, "challenge", resp.Challenge)
|
|
|
|
// Chrome's visit is a check of the URL like any other.
|
|
if err := p.Limiter.WaitCheck(ctx, rawURL); err != nil { return nil, err }
|
|
|
|
cr, err := p.Chrome.Fetch(ctx, rawURL)
|
|
|
|
switch {
|
|
case ctx.Err() != nil:
|
|
return nil, ctx.Err()
|
|
case err != nil:
|
|
p.Logger.Warn("headless Chrome failed", "url", rawURL, "err", err)
|
|
return resp, nil
|
|
case cr.Verdict == fetch.Challenge:
|
|
cr.Verdict = fetch.NeedsHuman
|
|
default:
|
|
p.Logger.Info("headless Chrome cleared the bot check", "url", rawURL, "challenge", resp.Challenge)
|
|
}
|
|
|
|
return cr, nil
|
|
}
|