Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
Decimation committed Oct 23, 2024
1 parent 2033709 commit e806042
Show file tree
Hide file tree
Showing 13 changed files with 313 additions and 106 deletions.
3 changes: 2 additions & 1 deletion src/FlareSolverrSharp/ChallengeDetector.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,13 @@ private static bool IsCloudflareProtectedAsync(HttpResponseMessage response)
i.Product != null
&& CloudflareValues.CloudflareServerNames.Contains(
i.Product.Name.ToLower()))) {
// return false;
return true;
}

// detect CloudFlare and DDoS-GUARD
if (response.StatusCode is HttpStatusCode.ServiceUnavailable or HttpStatusCode.Forbidden
or (HttpStatusCode) 523) {
or (HttpStatusCode) CloudflareValues.CloudflareStatusCodes.OriginUnreachable) {
var responseHtml = response.Content.ReadAsStringAsync().Result;


Expand Down
71 changes: 65 additions & 6 deletions src/FlareSolverrSharp/ClearanceHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
using FlareSolverrSharp.Solvers;
using FlareSolverrSharp.Types;
using Cookie = System.Net.Cookie;

// ReSharper disable InconsistentNaming

// ReSharper disable InvalidXmlDocComment
Expand Down Expand Up @@ -43,16 +44,18 @@ public class ClearanceHandler : DelegatingHandler

public bool CookieCapacity { get; set; }

private readonly IFlaresolverrResponseStorage _responseStorage;

/// <summary>
/// Creates a new instance of the <see cref="ClearanceHandler"/>.
/// </summary>
/// <param name="flareSolverrApiUrl">FlareSolverr API URL. If null or empty it will detect the challenges, but
/// they will not be solved. Example: "http://localhost:8191/"</param>
public ClearanceHandler(string api)
: this(new FlareSolverr(api)) { }
public ClearanceHandler(string api)
: this(new FlareSolverr(api), new DefaultFlaresolverrResponseStorage()) { }

public ClearanceHandler(FlareSolverr solverr)

public ClearanceHandler(FlareSolverr solverr, IFlaresolverrResponseStorage storage)
: base(new HttpClientHandler())
{
m_client = new HttpClient(new HttpClientHandler
Expand All @@ -63,7 +66,8 @@ public ClearanceHandler(FlareSolverr solverr)
});


Solverr = solverr;
Solverr = solverr;
_responseStorage = storage;
}

/// <summary>
Expand All @@ -87,6 +91,7 @@ protected override async Task<HttpResponseMessage> SendAsync(HttpRequestMessage
var response = await base.SendAsync(request, cancellationToken).ConfigureAwait(false);

// Detect if there is a challenge in the response
/*
if (ChallengeDetector.IsClearanceRequiredAsync(response)) {
// Resolve the challenge using FlareSolverr API
Expand All @@ -111,13 +116,67 @@ protected override async Task<HttpResponseMessage> SendAsync(HttpRequestMessage
if (EnsureResponseIntegrity) {
if (ChallengeDetector.IsClearanceRequiredAsync(response)) {
// throw new FlareSolverrException("The cookies provided by FlareSolverr are not valid");
throw new FlareSolverrException("The cookies provided by FlareSolverr are not valid");
}
}
// Add the "Set-Cookie" header in the response with the cookies provided by FlareSolverr
InjectSetCookieHeader(response, flareSolverrResponse);
}
*/
if (!ChallengeDetector.IsClearanceRequiredAsync(response)) {

return response;
}

var flareSolverrResponse = await _responseStorage.LoadAsync();

if (flareSolverrResponse != null) {
// Set user agent
if (flareSolverrResponse.Solution.UserAgent != null
&& flareSolverrResponse.Solution.UserAgent !=(request.Headers.UserAgent.ToString())) {
// Set the User-Agent if required
m_userAgent = flareSolverrResponse.Solution.UserAgent;
SetUserAgentHeader(request);
}

// Retry request with saved response
InjectCookies(request, flareSolverrResponse);
response = await base.SendAsync(request, cancellationToken).ConfigureAwait(false);

if (!ChallengeDetector.IsClearanceRequiredAsync(response)) {
// Success with saved response
InjectSetCookieHeader(response, flareSolverrResponse);
return response;
}
}

// Resolve the challenge using FlareSolverr API
flareSolverrResponse = await Solverr.SolveAsync(request);

// Save the FlareSolverr User-Agent for the following requests
var flareSolverUserAgent = flareSolverrResponse.Solution.UserAgent;

if (flareSolverUserAgent != null && !flareSolverUserAgent.Equals(request.Headers.UserAgent.ToString()))
{
m_userAgent = flareSolverUserAgent;

// Set the User-Agent if required
SetUserAgentHeader(request);
}

// Change the cookies in the original request with the cookies provided by FlareSolverr
InjectCookies(request, flareSolverrResponse);
response = await base.SendAsync(request, cancellationToken).ConfigureAwait(false);

// Detect if there is a challenge in the response
if (EnsureResponseIntegrity && ChallengeDetector.IsClearanceRequiredAsync(response)) {
throw new FlareSolverrException("The cookies provided by FlareSolverr are not valid");
}

// Add the "Set-Cookie" header in the response with the cookies provided by FlareSolverr
InjectSetCookieHeader(response, flareSolverrResponse);
await _responseStorage.SaveAsync(flareSolverrResponse);

return response;
}
Expand Down
6 changes: 3 additions & 3 deletions src/FlareSolverrSharp/Exceptions/FlareSolverrException.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ namespace FlareSolverrSharp.Exceptions;
/// </summary>
public class FlareSolverrException : HttpRequestException
{
public FlareSolverrException(string message) : base(message)
{
}

public FlareSolverrException(string message) : base(message) { }

}
2 changes: 2 additions & 0 deletions src/FlareSolverrSharp/FlareSolverrSharp.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
<TargetFramework>net8.0</TargetFramework>
<PackageId>FlareSolverrSharp</PackageId>
<RootNamespace>FlareSolverrSharp</RootNamespace>
<LangVersion>latest</LangVersion>
<Version>3.0.7</Version>
<Authors>Diego Heras (ngosang)</Authors>
<Description>FlareSolverr .Net / Proxy server to bypass Cloudflare protection.</Description>
Expand All @@ -25,6 +26,7 @@

<ItemGroup>
<PackageReference Include="Flurl.Http" Version="4.0.2" />
<PackageReference Include="System.Text.Json" Version="8.0.5" />
</ItemGroup>

</Project>
Loading

0 comments on commit e806042

Please sign in to comment.