Skip to content

Commit

Permalink
Fixes; refactoring; updates; integrates PR FlareSolverr#22, FlareSolv…
Browse files Browse the repository at this point in the history
  • Loading branch information
Decimation committed Oct 23, 2024
1 parent e806042 commit 9e7c3a1
Show file tree
Hide file tree
Showing 9 changed files with 39 additions and 42 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<TargetFramework>net8.0</TargetFramework>
<RootNamespace>FlareSolverrSharp.Sample</RootNamespace>
<AssemblyName>FlareSolverrSharp.Sample</AssemblyName>
<Version>3.0.7</Version>
<Version>3.0.8</Version>
</PropertyGroup>

<ItemGroup>
Expand Down
6 changes: 3 additions & 3 deletions src/FlareSolverrSharp/ClearanceHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,18 +44,18 @@ public class ClearanceHandler : DelegatingHandler

public bool CookieCapacity { get; set; }

private readonly IFlaresolverrResponseStorage _responseStorage;
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), new DefaultFlaresolverrResponseStorage()) { }
: this(new FlareSolverr(api), new DefaultFlareSolverrResponseStorage()) { }


public ClearanceHandler(FlareSolverr solverr, IFlaresolverrResponseStorage storage)
public ClearanceHandler(FlareSolverr solverr, IFlareSolverrResponseStorage storage)
: base(new HttpClientHandler())
{
m_client = new HttpClient(new HttpClientHandler
Expand Down
2 changes: 1 addition & 1 deletion src/FlareSolverrSharp/FlareSolverrSharp.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<PackageId>FlareSolverrSharp</PackageId>
<RootNamespace>FlareSolverrSharp</RootNamespace>
<LangVersion>latest</LangVersion>
<Version>3.0.7</Version>
<Version>3.0.8</Version>
<Authors>Diego Heras (ngosang)</Authors>
<Description>FlareSolverr .Net / Proxy server to bypass Cloudflare protection.</Description>
<PackageTags>flaresolverr, flaresolver, cloudflare, solver, bypass, protection, solving, library, cloudflaresolver, delegatinghandler, recaptcha, captcha, javascript, challenge, utilities</PackageTags>
Expand Down
13 changes: 7 additions & 6 deletions src/FlareSolverrSharp/Solvers/FlareSolverr.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,14 +40,15 @@ public class FlareSolverr : INotifyPropertyChanged
JsonIgnoreCondition.WhenWritingDefault,
};

private static readonly SemaphoreLocker s_locker = new SemaphoreLocker();
private static readonly SemaphoreLocker s_locker = new();

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

public Uri FlareSolverrApi { get; }

private readonly Uri _flareSolverrIndexUri;
private int m_maxTimeout;
public Uri FlareSolverrIndexUri { get; }

private int m_maxTimeout;

public int MaxTimeout
{
Expand Down Expand Up @@ -77,7 +78,7 @@ public FlareSolverr(string flareSolverrApiUrl)
}

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

FlareSolverrIndexUri = new Uri(apiUrl);
m_httpClient = new HttpClient()
{
// Timeout = AdjustHttpClientTimeout()
Expand Down Expand Up @@ -200,7 +201,7 @@ await s_locker.LockAsync(async () =>
// m_httpClient.Timeout = TimeSpan.FromMilliseconds(MaxTimeout + 5000);

if (flareSolverrRequest == null) {
response = await m_httpClient.GetAsync(_flareSolverrIndexUri);
response = await m_httpClient.GetAsync(FlareSolverrIndexUri);
}
else {
response = await m_httpClient.PostAsync(FlareSolverrApi, flareSolverrRequest);
Expand Down
6 changes: 3 additions & 3 deletions src/FlareSolverrSharp/Types/FlareSolverrRequestProxy.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@ public class FlareSolverrRequestProxy
{

[JsonPropertyName("url")]
public string Url {get;set;}
public string Url { get; set; }

[JsonPropertyName("username")]
public string Username{get;set;}
public string Username { get; set; }

[JsonPropertyName("password")]
public string Password{get;set;}
public string Password { get; set; }

public FlareSolverrRequestProxy() { }

Expand Down
23 changes: 0 additions & 23 deletions src/FlareSolverrSharp/Types/FlareSolverrResponse.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,36 +3,13 @@
using System.Collections.Generic;
using System.ComponentModel;
using System.Net;
using System.Text.Json;
using System.Text.Json.Serialization;

// ReSharper disable UnusedMember.Global
// ReSharper disable UnassignedField.Global
// ReSharper disable ClassNeverInstantiated.Global
namespace FlareSolverrSharp.Types;

public class StringOrNumberConverter : JsonConverter<string>
{

public override string Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
{
if (reader.TokenType == JsonTokenType.Number) {
return reader.GetInt32().ToString();
}
else if (reader.TokenType == JsonTokenType.String) {
return reader.GetString();
}

throw new JsonException("Unexpected token type");
}

public override void Write(Utf8JsonWriter writer, string value, JsonSerializerOptions options)
{
writer.WriteStringValue(value);
}

}

public class FlareSolverrResponse
{

Expand Down
6 changes: 3 additions & 3 deletions src/FlareSolverrSharp/Types/IFlaresolverrResponseStorage.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
// Author: Deci | Project: FlareSolverrSharp | Name: IFlaresolverrResponseStorage.cs
// Author: Deci | Project: FlareSolverrSharp | Name: IFlareSolverrResponseStorage.cs
// Date: 2024/10/23 @ 17:10:02

using System.Threading.Tasks;

namespace FlareSolverrSharp.Types;

public interface IFlaresolverrResponseStorage
public interface IFlareSolverrResponseStorage
{

Task SaveAsync(FlareSolverrResponse result);
Expand All @@ -14,7 +14,7 @@ public interface IFlaresolverrResponseStorage

}

public class DefaultFlaresolverrResponseStorage : IFlaresolverrResponseStorage
public class DefaultFlareSolverrResponseStorage : IFlareSolverrResponseStorage
{

public Task<FlareSolverrResponse?> LoadAsync()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<IsPackable>false</IsPackable>
<AssemblyName>FlareSolverrSharp.Tests</AssemblyName>
<RootNamespace>FlareSolverrSharp.Tests</RootNamespace>
<Version>3.0.7</Version>
<Version>3.0.8</Version>
<CopyLocalLockFileAssemblies>true</CopyLocalLockFileAssemblies>
</PropertyGroup>

Expand Down
21 changes: 20 additions & 1 deletion test/FlareSolverrSharp.Tests/FlareSolverrTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public async Task SolveOk()
[TestMethod]
public async Task SolveOk2()
{
var uri = new Uri("https://ascii2d.net/search/url/https://pomf2.lain.la/f/m2otcpa1.jpeg");
var uri = new Uri("https://ascii2d.net/search/url/https://www.reddit.com/media?url=https%3A%2F%2Fi.redd.it%2Fxixxli0axz7b1.jpg");
var flareSolverr = new FlareSolverr(Settings.FlareSolverrApiUrl);
var request = new HttpRequestMessage(HttpMethod.Get, uri);

Expand All @@ -60,7 +60,26 @@ public async Task SolveOk2()
Assert.IsTrue(!string.IsNullOrWhiteSpace(firstCookie.Name));
Assert.IsTrue(!string.IsNullOrWhiteSpace(firstCookie.Value));
}
[TestMethod]
public async Task SolveOk3()
{
var uri = new Uri("https://ascii2d.net/search/url/https://www.reddit.com/media?url=https%3A%2F%2Fi.redd.it%2Fxixxli0axz7b1.jpg");
var flareSolverr = new FlareSolverr(Settings.FlareSolverrApiUrl);
var request = new HttpRequestMessage(HttpMethod.Get, uri);

var flareSolverrResponse = await flareSolverr.SolveAsync(request);
Assert.AreEqual("ok", flareSolverrResponse.Status);

// Assert.AreEqual("", flareSolverrResponse.Message);
Assert.IsTrue(flareSolverrResponse.StartTimestamp > 0);
Assert.IsTrue(flareSolverrResponse.EndTimestamp > flareSolverrResponse.StartTimestamp);
Assert.IsTrue(flareSolverrResponse.Version.Major >= 2);


var firstCookie = flareSolverrResponse.Solution.Cookies.First();
Assert.IsTrue(!string.IsNullOrWhiteSpace(firstCookie.Name));
Assert.IsTrue(!string.IsNullOrWhiteSpace(firstCookie.Value));
}
[TestMethod]
public async Task SolveOkUserAgent()
{
Expand Down

0 comments on commit 9e7c3a1

Please sign in to comment.