Skip to content

Commit

Permalink
Merge branch 'dev'
Browse files Browse the repository at this point in the history
  • Loading branch information
openbullet committed Aug 12, 2024
2 parents e846c52 + 0f40422 commit 73adf3b
Show file tree
Hide file tree
Showing 51 changed files with 1,414 additions and 252 deletions.
7 changes: 6 additions & 1 deletion CaptchaSharp.Tests/AntiCaptchaServiceTests.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using CaptchaSharp.Services;
using System.Reflection;
using CaptchaSharp.Services;
using System.Threading.Tasks;
using Xunit;
using Xunit.Abstractions;
Expand All @@ -10,6 +11,10 @@ public class AntiCaptchaFixture : ServiceFixture
public AntiCaptchaFixture()
{
Service = new AntiCaptchaService(Config.Credentials.AntiCaptchaApiKey);

Service.GetType().GetProperty("SoftId",
BindingFlags.NonPublic | BindingFlags.Instance)?
.SetValue(Service, null);
}
}

Expand Down
42 changes: 42 additions & 0 deletions CaptchaSharp.Tests/AycdServiceTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
using System.Threading.Tasks;
using CaptchaSharp.Services;
using Xunit;
using Xunit.Abstractions;

namespace CaptchaSharp.Tests;

public class AycdFixture : ServiceFixture
{
public AycdFixture()
{
Service = new AycdService(Config.Credentials.AycdApiKey);
}
}

public class AycdServiceTests(AycdFixture fixture, ITestOutputHelper output)
: ServiceTests(fixture, output), IClassFixture<AycdFixture>
{
[Fact] public Task GetBalanceAsync_ValidKey_GetsBalance() => BalanceTest();
[Fact] public Task SolveImageCaptchaAsync_ValidCaptcha_ValidSolution() => ImageCaptchaTest();
[Fact] public Task SolveRecaptchaV2Async_NoProxy_ValidSolution() => RecaptchaV2Test_NoProxy();
[Fact] public Task SolveRecaptchaV2Async_WithProxy_ValidSolution() => RecaptchaV2Test_WithProxy();
[Fact] public Task SolveRecaptchaV2InvisibleAsync_NoProxy_ValidSolution() => RecaptchaV2InvisibleTest_NoProxy();
[Fact] public Task SolveRecaptchaV2InvisibleAsync_WithProxy_ValidSolution() => RecaptchaV2InvisibleTest_WithProxy();
[Fact] public Task SolveRecaptchaV2EnterpriseAsync_NoProxy_ValidSolution() => RecaptchaV2EnterpriseTest_NoProxy();
[Fact] public Task SolveRecaptchaV2EnterpriseAsync_WithProxy_ValidSolution() => RecaptchaV2EnterpriseTest_WithProxy();
[Fact] public Task SolveRecaptchaV3Async_NoProxy_ValidSolution() => RecaptchaV3Test_NoProxy();
[Fact] public Task SolveRecaptchaV3Async_WithProxy_ValidSolution() => RecaptchaV3Test_WithProxy();
[Fact] public Task SolveRecaptchaV3EnterpriseAsync_NoProxy_ValidSolution() => RecaptchaV3EnterpriseTest_NoProxy();
[Fact] public Task SolveRecaptchaV3EnterpriseAsync_WithProxy_ValidSolution() => RecaptchaV3EnterpriseTest_WithProxy();
[Fact] public Task SolveFuncaptchaAsync_NoProxy_ValidSolution() => FunCaptchaTest_NoProxy();
[Fact] public Task SolveFuncaptchaAsync_WithProxy_ValidSolution() => FunCaptchaTest_WithProxy();
[Fact] public Task SolveHCaptchaAsync_NoProxy_ValidSolution() => HCaptchaTest_NoProxy();
[Fact] public Task SolveHCaptchaAsync_WithProxy_ValidSolution() => HCaptchaTest_WithProxy();
[Fact] public Task SolveGeeTestAsync_NoProxy_ValidSolution() => GeeTestTest_NoProxy();
[Fact] public Task SolveGeeTestAsync_WithProxy_ValidSolution() => GeeTestTest_WithProxy();
[Fact] public Task SolveDataDomeAsync_WithProxy_ValidSolution() => DataDomeTest_WithProxy();
[Fact] public Task SolveCloudflareTurnstileAsync_NoProxy_ValidSolution() => CloudflareTurnstileTest_NoProxy();
[Fact] public Task SolveCloudflareTurnstileAsync_WithProxy_ValidSolution() => CloudflareTurnstileTest_WithProxy();
[Fact] public Task SolveGeeTestV4Async_NoProxy_ValidSolution() => GeeTestV4Test_NoProxy();
[Fact] public Task SolveGeeTestV4Async_WithProxy_ValidSolution() => GeeTestV4Test_WithProxy();
}
6 changes: 6 additions & 0 deletions CaptchaSharp.Tests/BestCaptchaSolverServiceTests.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
using System;
using System.Reflection;
using System.Threading.Tasks;
using CaptchaSharp.Services;
using Xunit;
Expand All @@ -11,6 +13,10 @@ public BestCaptchaSolverFixture()
{
Service = new BestCaptchaSolverService(
Config.Credentials.BestCaptchaSolverApiKey);

Service.GetType().GetProperty("AffiliateId",
BindingFlags.NonPublic | BindingFlags.Instance)?
.SetValue(Service, "123");
}
}

Expand Down
6 changes: 6 additions & 0 deletions CaptchaSharp.Tests/CapMonsterCloudServiceTests.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using System.Reflection;
using System.Threading.Tasks;
using CaptchaSharp.Services;
using Xunit;
Expand All @@ -11,6 +12,10 @@ public CapMonsterCloudFixture()
{
Service = new CapMonsterCloudService(
Config.Credentials.CapMonsterCloudApiKey);

Service.GetType().GetProperty("SoftId",
BindingFlags.NonPublic | BindingFlags.Instance)?
.SetValue(Service, null);
}
}

Expand Down Expand Up @@ -42,4 +47,5 @@ public class CapMonsterCloudServiceTests(CapMonsterCloudFixture fixture, ITestOu
[Fact] public Task SolveCloudflareTurnstileAsync_WithProxy_ValidSolution() => CloudflareTurnstileTest_WithProxy();
[Fact] public Task SolveGeeTestV4Async_NoProxy_ValidSolution() => GeeTestV4Test_NoProxy();
[Fact] public Task SolveGeeTestV4Async_WithProxy_ValidSolution() => GeeTestV4Test_WithProxy();
[Fact] public Task SolveCloudflareChallengePageAsync_WithProxy_ValidSolution() => CloudflareChallengePageTest_WithProxy();
}
3 changes: 2 additions & 1 deletion CaptchaSharp.Tests/CapSolverServiceTests.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using CaptchaSharp.Services;
using System.Reflection;
using CaptchaSharp.Services;
using System.Threading.Tasks;
using Xunit;
using Xunit.Abstractions;
Expand Down
1 change: 1 addition & 0 deletions CaptchaSharp.Tests/ConfigFixture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -67,4 +67,5 @@ public class Credentials
public string EndCaptchaUsername { get; set; } = string.Empty;
public string EndCaptchaPassword { get; set; } = string.Empty;
public string CapGuruApiKey { get; set; } = string.Empty;
public string AycdApiKey { get; set; } = string.Empty;
}
43 changes: 43 additions & 0 deletions CaptchaSharp.Tests/CustomTwoCaptchaServiceTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
using System;
using System.Threading.Tasks;
using CaptchaSharp.Services;
using Xunit;
using Xunit.Abstractions;

namespace CaptchaSharp.Tests;

public class CustomTwoCaptchaFixture : ServiceFixture
{
public CustomTwoCaptchaFixture()
{
Service = new CustomTwoCaptchaService(
Config.Credentials.CustomTwoCaptchaApiKey,
GetUri(Config.Credentials.CustomTwoCaptchaHost, Config.Credentials.CustomTwoCaptchaPort),
httpClient: null,
overrideHostHeader: Config.Credentials.CustomTwoCaptchaOverrideHostHeader
);
}

private static Uri GetUri(string host, int port)
{
// If there is no http(s) then add http by default
if (!host.StartsWith("http"))
{
host = $"http://{host}";
}

return new Uri($"{host}:{port}");
}
}

public class CustomTwoCaptchaServiceTests(CustomTwoCaptchaFixture fixture, ITestOutputHelper output)
: ServiceTests(fixture, output), IClassFixture<CustomTwoCaptchaFixture>
{
[Fact] public Task GetBalanceAsync_ValidKey_GetsBalance() => BalanceTest();
[Fact] public Task SolveImageCaptchaAsync_ValidCaptcha_ValidSolution() => ImageCaptchaTest();
[Fact] public Task SolveRecaptchaV2Async_NoProxy_ValidSolution() => RecaptchaV2Test_NoProxy();
[Fact] public Task SolveRecaptchaV2InvisibleAsync_NoProxy_ValidSolution() => RecaptchaV2InvisibleTest_NoProxy();
[Fact] public Task SolveRecaptchaV2EnterpriseAsync_NoProxy_ValidSolution() => RecaptchaV2EnterpriseTest_NoProxy();
[Fact] public Task SolveFuncaptchaAsync_NoProxy_ValidSolution() => FunCaptchaTest_NoProxy();
[Fact] public Task SolveHCaptchaAsync_NoProxy_ValidSolution() => HCaptchaTest_NoProxy();
}
7 changes: 6 additions & 1 deletion CaptchaSharp.Tests/ImageTyperzServiceTests.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using CaptchaSharp.Services;
using System.Reflection;
using CaptchaSharp.Services;
using System.Threading.Tasks;
using Xunit;
using Xunit.Abstractions;
Expand All @@ -10,6 +11,10 @@ public class ImageTyperzFixture : ServiceFixture
public ImageTyperzFixture()
{
Service = new ImageTyperzService(Config.Credentials.ImageTyperzApiKey);

Service.GetType().GetProperty("AffiliateId",
BindingFlags.NonPublic | BindingFlags.Instance)?
.SetValue(Service, 0);
}
}

Expand Down
53 changes: 53 additions & 0 deletions CaptchaSharp.Tests/ServiceTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,9 @@ protected async Task TextCaptchaTest()
text: "What is 2+2?",
options);

_output.WriteLine($"Captcha ID: {solution.Id}");
_output.WriteLine($"Response: {solution.Response}");

Assert.Equal("4", solution.Response);
}

Expand All @@ -114,6 +117,9 @@ protected async Task ImageCaptchaTest()
base64: _captchaImageBase64,
options);

_output.WriteLine($"Captcha ID: {solution.Id}");
_output.WriteLine($"Response: {solution.Response}");

Assert.Equal("w68hp", solution.Response.Replace(" ", "").ToLower());
}

Expand Down Expand Up @@ -639,4 +645,51 @@ private async Task GeeTestV4Test(SessionParams? sessionParams)
protected Task GeeTestV4Test_NoProxy() => GeeTestV4Test(null);

protected Task GeeTestV4Test_WithProxy() => GeeTestV4Test(_fixture.Config.SessionParams);

private async Task CloudflareChallengePageTest(SessionParams sessionParams)
{
var pageUrl = "https://2captcha.com/demo/cloudflare-turnstile-challenge";
var proxy = sessionParams.Proxy;

if (string.IsNullOrEmpty(sessionParams.UserAgent))
{
throw new ArgumentNullException(
nameof(sessionParams), "Solving Cloudflare challenges requires a User-Agent");
}

if (string.IsNullOrEmpty(proxy?.Host))
{
throw new ArgumentNullException(
nameof(sessionParams), "Solving Cloudflare challenges requires a proxy");
}

var webProxy = new WebProxy($"{proxy.Type.ToString().ToLower()}://{proxy.Host}:{proxy.Port}");

if (proxy.RequiresAuthentication)
{
webProxy.Credentials = new NetworkCredential(proxy.Username, proxy.Password);
}

using var httpClientHandler = new HttpClientHandler();
httpClientHandler.Proxy = webProxy;
httpClientHandler.UseProxy = true;

using var httpClient = new HttpClient(httpClientHandler);
httpClient.DefaultRequestHeaders.Add("User-Agent", sessionParams.UserAgent);

var response = await httpClient.GetAsync(pageUrl);
var pageHtml = await response.Content.ReadAsStringAsync();

var solution = await Service.SolveCloudflareChallengePageAsync(
siteUrl: pageUrl,
pageHtml: pageHtml,
sessionParams: sessionParams);

Assert.NotEqual(string.Empty, solution.Response);

_output.WriteLine($"Captcha ID: {solution.Id}");
_output.WriteLine($"Response: {solution.Response}");
}

protected Task CloudflareChallengePageTest_WithProxy() => CloudflareChallengePageTest(_fixture.Config.SessionParams);
}
5 changes: 5 additions & 0 deletions CaptchaSharp.Tests/SolveCaptchaServiceTests.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using System.Reflection;
using System.Threading.Tasks;
using CaptchaSharp.Services;
using Xunit;
Expand All @@ -11,6 +12,10 @@ public SolveCaptchaFixture()
{
Service = new SolveCaptchaService(
Config.Credentials.SolveCaptchaApiKey);

Service.GetType().GetProperty("AffiliateId",
BindingFlags.NonPublic | BindingFlags.Instance)?
.SetValue(Service, "123");
}
}

Expand Down
5 changes: 5 additions & 0 deletions CaptchaSharp.Tests/TwoCaptchaServiceTests.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using System.Reflection;
using CaptchaSharp.Services;
using System.Threading.Tasks;
using Xunit;
Expand All @@ -10,6 +11,10 @@ public class TwoCaptchaFixture : ServiceFixture
public TwoCaptchaFixture()
{
Service = new TwoCaptchaService(Config.Credentials.TwoCaptchaApiKey);

Service.GetType().GetProperty("SoftId",
BindingFlags.NonPublic | BindingFlags.Instance)?
.SetValue(Service, 0);
}
}

Expand Down
Loading

0 comments on commit 73adf3b

Please sign in to comment.