fix: detect Cloudflare challenge on HTTP 200 responses

Some anti-bot challenge pages return HTTP 200 instead of 403/503,
bypassing the FS fallback. Read response body on 200, check for
challenge indicators, and fall through to FlareSolverr if detected.
This commit is contained in:
achmad
2026-05-14 23:19:31 +07:00
parent 4781fc9bb5
commit 452918ac82
+24 -2
View File
@@ -120,10 +120,32 @@ func (c *Client) Do(req *http.Request) (*http.Response, error) {
var directStatus int
if err == nil {
directStatus = resp.StatusCode
if resp.StatusCode != http.StatusForbidden && resp.StatusCode != http.StatusServiceUnavailable {
// Some Cloudflare challenge pages return HTTP 200 but contain challenge
// JavaScript. Check for challenge content on any response.
if resp.StatusCode == http.StatusOK {
body, readErr := io.ReadAll(io.LimitReader(resp.Body, 32*1024))
resp.Body.Close()
if readErr == nil && len(body) > 0 {
if isCloudflareChallenge(body) {
directStatus = http.StatusForbidden
} else {
// Not a challenge — wrap body and return
return &http.Response{
StatusCode: resp.StatusCode,
Header: resp.Header,
Body: io.NopCloser(bytes.NewReader(body)),
ContentLength: int64(len(body)),
Request: req,
}, nil
}
} else {
return resp, nil
}
} else if resp.StatusCode != http.StatusForbidden && resp.StatusCode != http.StatusServiceUnavailable {
return resp, nil
} else {
resp.Body.Close()
}
resp.Body.Close()
}
if c.fsClient == nil {