Skip to content

Commit

Permalink
Added ReCaptchaMobile support for nextcaptcha
Browse files Browse the repository at this point in the history
  • Loading branch information
openbullet committed Aug 2, 2024
1 parent 2f6c2aa commit 637ae93
Show file tree
Hide file tree
Showing 10 changed files with 162 additions and 2 deletions.
2 changes: 2 additions & 0 deletions CaptchaSharp.Tests/NextCaptchaServiceTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,6 @@ public class NextCaptchaServiceTests(NextCaptchaFixture fixture, ITestOutputHelp
[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 SolveRecaptchaMobileAsync_NoProxy_ValidSolution() => RecaptchaMobileTest_NoProxy();
[Fact] public Task SolveRecaptchaMobileAsync_WithProxy_ValidSolution() => RecaptchaMobileTest_WithProxy();
}
18 changes: 18 additions & 0 deletions CaptchaSharp.Tests/ServiceTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -587,4 +587,22 @@ protected async Task AudioCaptchaTest()
_output.WriteLine($"Captcha ID: {solution.Id}");
_output.WriteLine($"Response: {solution.Response}");
}

private async Task RecaptchaMobileTest(Proxy? proxy)
{
var solution = await Service.SolveRecaptchaMobileAsync(
appPackageName: "",
appKey: "",
appAction: "login",
proxy: proxy);

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

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

protected Task RecaptchaMobileTest_NoProxy() => RecaptchaMobileTest(null);

protected Task RecaptchaMobileTest_WithProxy() => RecaptchaMobileTest(_fixture.Config.Proxy);
}
31 changes: 31 additions & 0 deletions CaptchaSharp/CaptchaSharp.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions CaptchaSharp/Enums/CaptchaType.cs
Original file line number Diff line number Diff line change
Expand Up @@ -65,4 +65,7 @@ public enum CaptchaType

/// <summary>A captcha that is an audio file with some text to recognize.</summary>
AudioCaptcha = 1 << 19,

/// <summary>Google ReCaptcha Mobile.</summary>
ReCaptchaMobile = 1 << 20,
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
using CaptchaSharp.Models.AntiCaptcha.Requests.Tasks.Proxied;

namespace CaptchaSharp.Models.NextCaptcha.Requests.Tasks.Proxied;

internal class RecaptchaMobileTask : AntiCaptchaTask
{
public required string AppPackageName { get; set; }
public required string AppKey { get; set; }
public required string AppAction { get; set; }

public RecaptchaMobileTask()
{
Type = "RecaptchaMobileTask";
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
using CaptchaSharp.Models.AntiCaptcha.Requests.Tasks;

namespace CaptchaSharp.Models.NextCaptcha.Requests.Tasks;

internal class RecaptchaMobileTaskProxyless : AntiCaptchaTaskProxyless
{
public required string AppPackageName { get; set; }
public required string AppKey { get; set; }
public required string AppAction { get; set; }

public RecaptchaMobileTaskProxyless()
{
Type = "RecaptchaMobileTaskProxyless";
}
}
2 changes: 1 addition & 1 deletion CaptchaSharp/Services/AntiCaptchaService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -393,7 +393,7 @@ protected async Task<T> GetResult<T>(

result.AntiCaptchaTaskSolution = task.Type switch
{
CaptchaType.ReCaptchaV2 or CaptchaType.ReCaptchaV3 or CaptchaType.HCaptcha =>
CaptchaType.ReCaptchaV2 or CaptchaType.ReCaptchaV3 or CaptchaType.HCaptcha or CaptchaType.ReCaptchaMobile =>
solution.ToObject<RecaptchaAntiCaptchaTaskSolution>()! as AntiCaptchaTaskSolution,
CaptchaType.FunCaptcha => solution.ToObject<FuncaptchaAntiCaptchaTaskSolution>()!,
CaptchaType.ImageCaptcha => solution.ToObject<ImageCaptchaAntiCaptchaTaskSolution>(),
Expand Down
30 changes: 30 additions & 0 deletions CaptchaSharp/Services/CaptchaService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -654,6 +654,36 @@ public virtual Task<StringResponse> SolveAudioCaptchaAsync(
throw new NotSupportedException();
}

/// <summary>Solves a Google ReCaptcha for mobile apps.</summary>
///
/// <param name="appPackageName">The package name of the app.</param>
/// <param name="appKey">The app key, can be found in the app source or by sniffing requests.</param>
/// <param name="appAction">The action to execute. Can be found in the app source or in a js file.</param>
///
/// <param name="proxy">
/// A proxy that can be used by the captcha service to fetch the captcha challenge from the same IP you are
/// going to send it from when you submit the form. It can help bypass some blocks. If null, the service will
/// fetch the captcha without using a proxy.
/// </param>
///
/// <param name="cancellationToken">A token that can be used to cancel the async task.</param>
///
/// <returns>
/// A <see cref="StringResponse"/> containing the captcha id to be used with
/// <see cref="ReportSolution(string, CaptchaType, bool, CancellationToken)"/> and the
/// captcha solution as plaintext.
/// </returns>
///
/// <exception cref="TaskCreationException"></exception>
/// <exception cref="TaskSolutionException"></exception>
/// <exception cref="TimeoutException"></exception>
public virtual Task<StringResponse> SolveRecaptchaMobileAsync(
string appPackageName, string appKey, string appAction, Proxy? proxy = null,
CancellationToken cancellationToken = default)
{
throw new NotSupportedException();
}

/// <summary>
/// Reports a captcha solution as good or bad to the service.
/// Mostly used for reporting bad solutions for image captchas and get the funds back.
Expand Down
45 changes: 45 additions & 0 deletions CaptchaSharp/Services/NextCaptchaService.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
using System;
using System.Net.Http;
using System.Threading;
using System.Threading.Tasks;
using CaptchaSharp.Enums;
using CaptchaSharp.Extensions;
using CaptchaSharp.Models;
using CaptchaSharp.Models.AntiCaptcha.Responses;
using CaptchaSharp.Models.NextCaptcha.Requests.Tasks;
using CaptchaSharp.Models.NextCaptcha.Requests.Tasks.Proxied;

namespace CaptchaSharp.Services;

Expand All @@ -25,4 +32,42 @@ public NextCaptchaService(string apiKey, HttpClient? httpClient = null)

SoftId = null;
}

#region Solve Methods
/// <inheritdoc/>
public override async Task<StringResponse> SolveRecaptchaMobileAsync(
string appPackageName, string appKey, string appAction, Proxy? proxy = null,
CancellationToken cancellationToken = default)
{
var content = CreateTaskRequest();

if (proxy is not null)
{
content.Task = new RecaptchaMobileTask
{
AppPackageName = appPackageName,
AppKey = appKey,
AppAction = appAction,
}.SetProxy(proxy);
}
else
{
content.Task = new RecaptchaMobileTaskProxyless
{
AppPackageName = appPackageName,
AppKey = appKey,
AppAction = appAction,
};
}

var response = await HttpClient.PostJsonAsync<TaskCreationAntiCaptchaResponse>(
"createTask",
content,
cancellationToken: cancellationToken)
.ConfigureAwait(false);

return await GetResult<StringResponse>(response, CaptchaType.ReCaptchaMobile,
cancellationToken).ConfigureAwait(false);
}
#endregion
}
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,9 @@ This library supports the following captcha types
- atb Captcha
- Tencent Captcha
- Audio Captcha (with language options)
- ReCaptcha Mobile

Proxies are supported for services that support them.
Proxies are supported for services that accept them.

## Availability Table

Expand Down

0 comments on commit 637ae93

Please sign in to comment.