mirror of
https://github.com/BaaRai/ARTEX.git
synced 2026-12-06 04:14:15 +00:00
99 lines
3.2 KiB
Go
99 lines
3.2 KiB
Go
package egress
|
|
|
|
import (
|
|
"bytes"
|
|
"fmt"
|
|
"io"
|
|
"net/http"
|
|
"strings"
|
|
)
|
|
|
|
// BotCheckpoint identifies a non-retryable JS bot-mitigation checkpoint. It is
|
|
// returned as an error so HTTP tools that omit response headers still surface
|
|
// the browser fallback guidance to the agent.
|
|
type BotCheckpoint struct {
|
|
Provider string
|
|
Reason string
|
|
}
|
|
|
|
func (c BotCheckpoint) Error() string {
|
|
return fmt.Sprintf("egress: non-retryable %s JS bot-mitigation checkpoint (%s); stop raw HTTP retries and use browser tooling (playwright-cli / Playwright MCP)", c.Provider, c.Reason)
|
|
}
|
|
|
|
// DetectBotCheckpoint recognizes explicit mitigation headers or Vercel HTML
|
|
// markers on 403/429/503 responses. Generic CDN headers and Retry-After alone
|
|
// are not evidence of a JS checkpoint.
|
|
func DetectBotCheckpoint(statusCode int, h http.Header, bodyPrefix string) (BotCheckpoint, bool) {
|
|
if !botCheckpointStatus(statusCode) {
|
|
return BotCheckpoint{}, false
|
|
}
|
|
switch {
|
|
case checkpointHeader(h, "X-Vercel-Mitigated", ""):
|
|
return BotCheckpoint{Provider: "vercel", Reason: "x-vercel-mitigated"}, true
|
|
case checkpointHeader(h, "Cf-Mitigated", "challenge"):
|
|
return BotCheckpoint{Provider: "cloudflare", Reason: "cf-mitigated"}, true
|
|
case checkpointHeader(h, "X-Amzn-Waf-Action", "challenge"):
|
|
return BotCheckpoint{Provider: "aws-waf", Reason: "x-amzn-waf-action"}, true
|
|
}
|
|
bodyPrefix = strings.ToLower(bodyPrefix)
|
|
if strings.Contains(bodyPrefix, "vercel security checkpoint") || strings.Contains(bodyPrefix, "/.well-known/vercel/security/request-challenge") {
|
|
return BotCheckpoint{Provider: "vercel", Reason: "html-challenge-marker"}, true
|
|
}
|
|
return BotCheckpoint{}, false
|
|
}
|
|
|
|
func botCheckpointStatus(code int) bool {
|
|
return code == http.StatusForbidden || code == http.StatusTooManyRequests || code == http.StatusServiceUnavailable
|
|
}
|
|
|
|
// An empty value matches header presence, including an empty field value.
|
|
// Iterate keys as well as values to support non-canonical RoundTripper headers.
|
|
func checkpointHeader(h http.Header, name, value string) bool {
|
|
for k, values := range h {
|
|
if !strings.EqualFold(k, name) {
|
|
continue
|
|
}
|
|
if value == "" {
|
|
return true
|
|
}
|
|
for _, v := range values {
|
|
if strings.EqualFold(strings.TrimSpace(v), value) {
|
|
return true
|
|
}
|
|
}
|
|
}
|
|
return false
|
|
}
|
|
|
|
const maxCheckpointBodyPrefix = 8 << 10
|
|
|
|
func detectResponseBotCheckpoint(resp *http.Response) (BotCheckpoint, bool) {
|
|
c, ok := DetectBotCheckpoint(resp.StatusCode, resp.Header, "")
|
|
if ok || !botCheckpointStatus(resp.StatusCode) || resp.Body == nil {
|
|
return c, ok
|
|
}
|
|
// Only peek when headers are inconclusive. Replay both bytes and any read
|
|
// error so an ordinary error response retains its body and Close contract.
|
|
prefix, err := io.ReadAll(io.LimitReader(resp.Body, maxCheckpointBodyPrefix))
|
|
resp.Body = &checkpointBody{ReadCloser: resp.Body, prefix: bytes.NewReader(prefix), err: err}
|
|
return DetectBotCheckpoint(resp.StatusCode, resp.Header, string(prefix))
|
|
}
|
|
|
|
type checkpointBody struct {
|
|
io.ReadCloser
|
|
prefix *bytes.Reader
|
|
err error
|
|
}
|
|
|
|
func (b *checkpointBody) Read(p []byte) (int, error) {
|
|
if b.prefix.Len() > 0 {
|
|
return b.prefix.Read(p)
|
|
}
|
|
if b.err != nil {
|
|
err := b.err
|
|
b.err = nil
|
|
return 0, err
|
|
}
|
|
return b.ReadCloser.Read(p)
|
|
}
|