Files
dibs/internal/fetch/classify.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

105 lines
3.0 KiB
Go

package fetch
import (
"bytes"
"net/http"
"strings"
)
// Verdict says what a response means for the watcher.
type Verdict int
const (
// OK is a page the plugin can parse.
OK Verdict = iota
// NotFound (404, 410) means the item is not available, never an error.
NotFound
// Challenge is a bot check that a real browser may clear.
Challenge
// Blocked is a refusal without a recognisable challenge, such as a plain
// 403 or 429. The host is backed off and retried later.
Blocked
// HTTPError is any other non-2xx status.
HTTPError
// NeedsHuman is a bot check that headless Chrome could not clear either,
// such as a captcha. classify never returns it.
NeedsHuman
)
func (v Verdict) String() string {
switch v {
case OK:
return "ok"
case NotFound:
return "not-found"
case Challenge:
return "challenge"
case Blocked:
return "blocked"
case HTTPError:
return "http-error"
case NeedsHuman:
return "needs-human"
default:
return "unknown"
}
}
type marker struct {
name string
needle string
}
// strongMarkers only appear on bot check pages, so they count whatever the
// status. Amazon answers its captcha with a 200, for example.
var strongMarkers = []marker{
{"cloudflare", "window._cf_chl_opt"},
{"amazon-captcha", `action="/errors/validateCaptcha"`},
{"datadome", "geo.captcha-delivery.com"},
{"perimeterx", `id="px-captcha"`},
}
// weakMarkers also show up on normal pages (Cloudflare injects its
// challenge-platform script, shops embed Turnstile in forms), so they only
// count on a refusal status.
var weakMarkers = []marker{
{"cloudflare", "<title>Just a moment...</title>"},
{"cloudflare", "/cdn-cgi/challenge-platform/"},
{"turnstile", "challenges.cloudflare.com/turnstile"},
{"recaptcha", "www.google.com/recaptcha/"},
{"hcaptcha", "hcaptcha.com/1/api.js"},
{"captcha", "captcha"},
}
// classify decides the verdict for a response. For Challenge it also names
// the bot check found.
func classify(status int, header http.Header, finalURL string, body []byte) (Verdict, string) {
lower := bytes.ToLower(body)
if header.Get("cf-mitigated") == "challenge" { return Challenge, "cloudflare" }
if strings.Contains(finalURL, "/errors/validateCaptcha") { return Challenge, "amazon-captcha" }
if name := findMarker(lower, strongMarkers); name != "" { return Challenge, name }
switch {
case status >= 200 && status < 300:
return OK, ""
case status == http.StatusNotFound || status == http.StatusGone:
return NotFound, ""
case status == http.StatusForbidden || status == http.StatusTooManyRequests || status == http.StatusServiceUnavailable:
if name := findMarker(lower, weakMarkers); name != "" { return Challenge, name }
return Blocked, ""
default:
return HTTPError, ""
}
}
// findMarker returns the name of the first marker in the lowercased body.
func findMarker(lower []byte, markers []marker) string {
for _, m := range markers {
if bytes.Contains(lower, []byte(strings.ToLower(m.needle))) { return m.name }
}
return ""
}