Skip to content

Commit

Permalink
More fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
Decimation committed Oct 23, 2024
1 parent 4f79593 commit 2033709
Show file tree
Hide file tree
Showing 10 changed files with 192 additions and 56 deletions.
41 changes: 36 additions & 5 deletions sample/FlareSolverrSharp.Sample/Program.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,42 @@

using System;
using System.Collections.Generic;
using FlareSolverrSharp.Exceptions;
using System.Net.Http;
using System.Threading.Tasks;

namespace FlareSolverrSharp.Sample;

static class Program
public static class Program
{
static void Main()

public static async Task Main()
{
ClearanceHandlerSample.SampleGet().Wait();
ClearanceHandlerSample.SamplePostUrlEncoded().Wait();
/*ClearanceHandlerSample.SampleGet().Wait();
ClearanceHandlerSample.SamplePostUrlEncoded().Wait();*/

var handler = new ClearanceHandler(Settings.FlareSolverrApiUrl)
{
EnsureResponseIntegrity = false,
Solverr =
{
MaxTimeout = 60000
}
};

var client = new HttpClient(handler);

var x = await client.GetAsync(Settings.ProtectedUri);

Console.WriteLine(x);
Console.WriteLine( ChallengeDetector.IsClearanceRequiredAsync(x));
/*foreach (KeyValuePair<string, IEnumerable<string>> pair in x.Headers) {
Console.WriteLine(pair);
}*/
foreach (var y in x.Headers.Server) {

Console.WriteLine(y.Product.Name);
}
// Assert.IsTrue(c.Message.Contains("Error connecting to FlareSolverr server"));
}

}
Original file line number Diff line number Diff line change
@@ -1,13 +1,20 @@
using System;
using System.Runtime.CompilerServices;

namespace FlareSolverrSharp.Tests;
[assembly: InternalsVisibleTo("FlareSolverrSharp.Tests")]

namespace FlareSolverrSharp.Sample;

internal static class Settings
{
internal const string FlareSolverrApiUrl = "http://localhost:8191/";
internal const string ProxyUrl = "http://127.0.0.1:8888/";
internal static readonly Uri ProtectedUri = new Uri("https://nowsecure.nl");
internal static readonly Uri ProtectedPostUri = new Uri("https://badasstorrents.com/torrents/search/720p/date/desc");

internal const string FlareSolverrApiUrl = "http://localhost:8191/";
internal const string ProxyUrl = "http://127.0.0.1:8888/";
internal static readonly Uri ProtectedUri = new Uri("https://nowsecure.nl");

internal static readonly Uri ProtectedPostUri =
new Uri("https://badasstorrents.com/torrents/search/720p/date/desc");

internal static readonly Uri ProtectedDdgUri = new Uri("https://anidex.info/?q=text");
internal static readonly Uri ProtectedCcfUri = new Uri("https://www.muziekfabriek.org");

Expand All @@ -23,4 +30,5 @@ internal static class Settings
* sudo tinyproxy -d
* sudo tail -f /tmp/tinyproxy.log
*/

}
7 changes: 4 additions & 3 deletions src/FlareSolverrSharp/ChallengeDetector.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,12 @@ public static bool IsClearanceRequiredAsync(HttpResponseMessage response)
private static bool IsCloudflareProtectedAsync(HttpResponseMessage response)
{
// check response headers
if (!response.Headers.Server.Any(i =>
if (response.Headers.Server.Any(i =>
i.Product != null
&& CloudflareValues.CloudflareServerNames.Contains(
i.Product.Name.ToLower())))
return false;
i.Product.Name.ToLower()))) {
return true;
}

// detect CloudFlare and DDoS-GUARD
if (response.StatusCode is HttpStatusCode.ServiceUnavailable or HttpStatusCode.Forbidden
Expand Down
26 changes: 15 additions & 11 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 All @@ -26,9 +27,9 @@ namespace FlareSolverrSharp;
public class ClearanceHandler : DelegatingHandler
{

private readonly HttpClient _client;
private readonly HttpClient m_client;

private string _userAgent;
private string m_userAgent;

public FlareSolverr Solverr { get; }

Expand All @@ -40,6 +41,8 @@ public class ClearanceHandler : DelegatingHandler

public bool EnsureResponseIntegrity { get; set; }

public bool CookieCapacity { get; set; }

/// <summary>
/// Creates a new instance of the <see cref="ClearanceHandler"/>.
/// </summary>
Expand All @@ -52,7 +55,7 @@ public ClearanceHandler(string api)
public ClearanceHandler(FlareSolverr solverr)
: base(new HttpClientHandler())
{
_client = new HttpClient(new HttpClientHandler
m_client = new HttpClient(new HttpClientHandler
{
AllowAutoRedirect = false,
AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate,
Expand Down Expand Up @@ -94,7 +97,7 @@ protected override async Task<HttpResponseMessage> SendAsync(HttpRequestMessage

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

// Set the User-Agent if required
SetUserAgentHeader(request);
Expand All @@ -105,8 +108,11 @@ protected override async Task<HttpResponseMessage> SendAsync(HttpRequestMessage
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");
if (EnsureResponseIntegrity) {

if (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
Expand All @@ -118,15 +124,13 @@ protected override async Task<HttpResponseMessage> SendAsync(HttpRequestMessage

private void SetUserAgentHeader(HttpRequestMessage request)
{
if (_userAgent != null) {
if (m_userAgent != null) {
// Overwrite the header
request.Headers.Remove(FlareSolverrValues.UserAgent);
request.Headers.Add(FlareSolverrValues.UserAgent, _userAgent);
request.Headers.Add(FlareSolverrValues.UserAgent, m_userAgent);
}
}

public bool CookieCapacity { get; set; }

private void InjectCookies(HttpRequestMessage request, FlareSolverrResponse flareSolverrResponse)
{
// use only Cloudflare and DDoS-GUARD cookies
Expand Down Expand Up @@ -206,7 +210,7 @@ private static bool IsCloudflareCookie(string cookieName)
protected override void Dispose(bool disposing)
{
if (disposing)
_client.Dispose();
m_client.Dispose();

base.Dispose(disposing);
}
Expand Down
40 changes: 40 additions & 0 deletions src/FlareSolverrSharp/Constants/CloudflareValues.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,44 @@ public static class CloudflareValues
"<title>Attention Required! | Cloudflare</title>" // Cloudflare Blocked
];

/*
*Cloudflare
Cloudflare's reverse proxy service expands the 5xx series of errors space to signal issues with the origin server.[51]
520 Web Server Returned an Unknown Error
The origin server returned an empty, unknown, or unexpected response to Cloudflare.[52]
521 Web Server Is Down
The origin server refused connections from Cloudflare. Security solutions at the origin may be blocking legitimate connections from certain Cloudflare IP addresses.
522 Connection Timed Out
Cloudflare timed out contacting the origin server.
523 Origin Is Unreachable
Cloudflare could not reach the origin server; for example, if the DNS records for the origin server are incorrect or missing.
524 A Timeout Occurred
Cloudflare was able to complete a TCP connection to the origin server, but did not receive a timely HTTP response.
525 SSL Handshake Failed
Cloudflare could not negotiate a SSL/TLS handshake with the origin server.
526 Invalid SSL Certificate
Cloudflare could not validate the SSL certificate on the origin web server. Also used by Cloud Foundry's gorouter.
527 Railgun Error (obsolete)
Error 527 indicated an interrupted connection between Cloudflare and the origin server's Railgun server.[53] This error is obsolete as Cloudflare has deprecated Railgun.
530
Error 530 is returned along with a 1xxx error.[54]
*/


public enum CloudflareStatusCodes : int
{

WebServerUnknown = 520,
WebServerDown = 521,
ConnectionTimedOut = 522,
OriginUnreachable = 523,
TimeoutOccurred = 524,
SslHandshakeFailed = 525,
InvalidSslCertificate = 526,
RailgunError = 527,

}

}
44 changes: 36 additions & 8 deletions src/FlareSolverrSharp/Solvers/FlareSolverr.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public class FlareSolverr : INotifyPropertyChanged

private static readonly SemaphoreLocker s_locker = new SemaphoreLocker();

private readonly HttpClient m_httpClient;
private /*readonly*/ HttpClient m_httpClient;

public Uri FlareSolverrApi { get; }

Expand All @@ -53,7 +53,7 @@ public int MaxTimeout
set
{
SetField(ref m_maxTimeout, value);
m_httpClient.Timeout = AdjustHttpClientTimeout();
// m_httpClient.Timeout = AdjustHttpClientTimeout();
}
}

Expand All @@ -76,10 +76,10 @@ public FlareSolverr(string flareSolverrApiUrl)

FlareSolverrApi = new Uri($"{apiUrl}v1");

m_httpClient = new HttpClient()
/*m_httpClient = new HttpClient()
{
// Timeout = AdjustHttpClientTimeout()
};
};*/

MaxTimeout = FlareSolverrValues.MAX_TIMEOUT_DEFAULT;
Proxy = new FlareSolverrRequestProxy();
Expand Down Expand Up @@ -143,13 +143,13 @@ private async Task<FlareSolverrResponse> SendFlareSolverrRequestAsync(HttpConten
//todo: what is this "semaphore locker" for

// await s_locker.LockAsync(() => SendRequestAsync(flareSolverrRequest));
HttpResponseMessage response;
HttpResponseMessage response;

try {
// m_httpClient = new HttpClient();
m_httpClient = new HttpClient();

// wait 5 more seconds to make sure we return the FlareSolverr timeout message
// m_httpClient.Timeout = TimeSpan.FromMilliseconds(MaxTimeout + 5000);
m_httpClient.Timeout = TimeSpan.FromMilliseconds(MaxTimeout + 5000);
response = await m_httpClient.PostAsync(FlareSolverrApi, flareSolverrRequest);
}
catch (HttpRequestException e) {
Expand All @@ -159,7 +159,7 @@ private async Task<FlareSolverrResponse> SendFlareSolverrRequestAsync(HttpConten
throw new FlareSolverrException($"Exception: {e}");
}
finally {
// m_httpClient.Dispose();
m_httpClient.Dispose();
}

// Don't try parsing if FlareSolverr hasn't returned 200 or 500
Expand Down Expand Up @@ -270,6 +270,34 @@ private HttpContent GenerateFlareSolverrRequest(HttpRequestMessage request, stri
Session = sessionId
};
}
/*else if (request.Method == HttpMethod.Post) {
// request.Content.GetType() doesn't work well when encoding != utf-8
var contentMediaType = request.Content.Headers.ContentType?.MediaType.ToLower() ?? "<null>";
if (contentMediaType.Contains("application/x-www-form-urlencoded")) {
req = new FlareSolverrRequestPost
{
Command = FlareSolverrValues.CMD_REQUEST_POST,
Url = url,
PostData = request.Content.ReadAsStringAsync().Result,
MaxTimeout = MaxTimeout,
Proxy = Proxy,
Session = sessionId
};
}
else if (contentMediaType.Contains("multipart/form-data")
|| contentMediaType.Contains("text/html")) {
//TODO Implement - check if we just need to pass the content-type with the relevant headers
throw new FlareSolverrException("Unimplemented POST Content-Type: " + contentMediaType);
}
else {
throw new FlareSolverrException("Unsupported POST Content-Type: " + contentMediaType);
}
}
else {
throw new FlareSolverrException("Unsupported HttpMethod: " + request.Method);
}*/

else if (request.Method == HttpMethod.Post) {
// request.Content.GetType() doesn't work well when encoding != utf-8
var contentType = request.Content.Headers.ContentType;
Expand Down
2 changes: 1 addition & 1 deletion src/FlareSolverrSharp/Types/FlareSolverrResponse.cs
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ public class FlareSolverrSolution
public string Url;

[JsonPropertyName("status")]
public string Status;
public int Status;

[JsonPropertyName("headers")]
public FlareSolverrHeaders Headers;
Expand Down
1 change: 1 addition & 0 deletions test/FlareSolverrSharp.Tests/ClearanceHandlerTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
using System.Threading.Tasks;
using System.Web;
using FlareSolverrSharp.Exceptions;
using FlareSolverrSharp.Sample;
using FlareSolverrSharp.Solvers;
using Microsoft.VisualStudio.TestTools.UnitTesting;

Expand Down
3 changes: 2 additions & 1 deletion test/FlareSolverrSharp.Tests/FlareSolverrSharp.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<AssemblyName>FlareSolverrSharp.Tests</AssemblyName>
<RootNamespace>FlareSolverrSharp.Tests</RootNamespace>
<Version>3.0.7</Version>
<!-- <CopyLocalLockFileAssemblies>true</CopyLocalLockFileAssemblies> -->
<CopyLocalLockFileAssemblies>true</CopyLocalLockFileAssemblies>
</PropertyGroup>

<ItemGroup>
Expand All @@ -15,6 +15,7 @@
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\..\sample\FlareSolverrSharp.Sample\FlareSolverrSharp.Sample.csproj" />
<ProjectReference Include="..\..\src\FlareSolverrSharp\FlareSolverrSharp.csproj" />
</ItemGroup>

Expand Down
Loading

0 comments on commit 2033709

Please sign in to comment.