From f449ad6682ae06ab5022e47f0ba64f978a15198f Mon Sep 17 00:00:00 2001 From: Etheirystech Date: Thu, 9 Apr 2026 16:36:05 +0200 Subject: [PATCH] Try cookie-based retry first, fall back to response body Attempt the cookie+UA retry before using FlareSolverr's response body so that when cookies work, subsequent requests can reuse them without going through FlareSolverr. Only fall back to the response body when the retry is still CF-protected. --- .../FlareSolverr/FlareSolverr.cs | 21 +++++++++++-------- 1 file changed, 12 insertions(+), 9 deletions(-) diff --git a/src/NzbDrone.Core/IndexerProxies/FlareSolverr/FlareSolverr.cs b/src/NzbDrone.Core/IndexerProxies/FlareSolverr/FlareSolverr.cs index 2fcb1b7bb..cc92c4cc9 100644 --- a/src/NzbDrone.Core/IndexerProxies/FlareSolverr/FlareSolverr.cs +++ b/src/NzbDrone.Core/IndexerProxies/FlareSolverr/FlareSolverr.cs @@ -71,16 +71,22 @@ public override HttpResponse PostResponse(HttpResponse response) InjectCookies(newRequest, result); - // Use FlareSolverr's response body directly when available. - // When CF selectively challenges by IP/UA, FlareSolverr may not - // receive a challenge and thus returns no cf_clearance cookie. - // A second request without that cookie gets 403'd. + // Try cookie-based retry first so subsequent requests can + // reuse the cookies without going through FlareSolverr. + var finalResponse = _httpClient.Execute(newRequest); + + if (!CloudFlareDetectionService.IsCloudflareProtected(finalResponse)) + { + return finalResponse; + } + + // Cookie retry was blocked — fall back to FlareSolverr's response body. + // This happens when CF selectively challenges by IP/UA and FlareSolverr + // doesn't receive a challenge, so no cf_clearance cookie is returned. if (result.Solution.Response.IsNotNullOrWhiteSpace()) { var headers = new HttpHeader(); - // Preserve the Content-Type from FlareSolverr's solution so downstream - // parsers (e.g. JSON indexers) interpret the body correctly. if (result.Solution.Headers?.ContentType.IsNotNullOrWhiteSpace() == true) { headers.ContentType = result.Solution.Headers.ContentType; @@ -95,9 +101,6 @@ public override HttpResponse PostResponse(HttpResponse response) HttpStatusCode.OK); } - // Fallback: if FlareSolverr returned no body, retry with cookies (original behavior) - var finalResponse = _httpClient.Execute(newRequest); - return finalResponse; }