From 769413f39bc39cc62001febaefab542ddb9853bb Mon Sep 17 00:00:00 2001 From: SlavaObrezkov Date: Mon, 11 Aug 2025 16:42:28 +0300 Subject: [PATCH] CCL-6504 Add support for solving MTCaptcha tasks --- .../IntegrationTests.cs | 44 ++++++++++++++ .../ObjectGen.cs | 31 ++++++++++ .../Sut.cs | 3 + ...CapMonsterCloudClient_GetResultTimeouts.cs | 10 ++++ .../Requests/MTCaptchaTaskRequest.cs | 57 +++++++++++++++++++ .../Responses/MTCaptchaTaskResponse.cs | 16 ++++++ 6 files changed, 161 insertions(+) create mode 100644 CapMonsterCloud.Client/Requests/MTCaptchaTaskRequest.cs create mode 100644 CapMonsterCloud.Client/Responses/MTCaptchaTaskResponse.cs diff --git a/CapMonsterCloud.Client.IntegrationTests/IntegrationTests.cs b/CapMonsterCloud.Client.IntegrationTests/IntegrationTests.cs index 7efb922..ef6f70c 100644 --- a/CapMonsterCloud.Client.IntegrationTests/IntegrationTests.cs +++ b/CapMonsterCloud.Client.IntegrationTests/IntegrationTests.cs @@ -1071,5 +1071,49 @@ public async Task Binance_ShouldSolve() sut.GetActualRequests().Should().BeEquivalentTo(expectedRequests); actual.Should().BeEquivalentTo(expectedResult); } + + [Test] + public async Task MTCaptcha_ShouldSolve() + { + var clientKey = Gen.RandomString(); + var taskId = Gen.RandomInt(); + + var captchaRequest = ObjectGen.MTCaptchaTask.CreateTask(); + var expectedResult = ObjectGen.MTCaptchaTask.CreateSolution(); + + var expectedRequests = new List<(RequestType Type, string ExpectedRequest)> + { + ( + Type: RequestType.CreateTask, + ExpectedRequest: JsonConvert.SerializeObject(new + { clientKey = clientKey, task = captchaRequest, softId = 53 }) + ), + ( + Type: RequestType.GetTaskResult, + ExpectedRequest: JsonConvert.SerializeObject(new { clientKey = clientKey, taskId = taskId }) + ), + }; + + var captchaResults = new List + { + new { taskId = taskId, errorId = 0, errorCode = (string)null! }, + new + { + status = "ready", + solution = new { token = expectedResult.Solution.Value }, + errorId = 0, + errorCode = (string)null! + } + }; + + var sut = new Sut(clientKey); + sut.SetupHttpServer(captchaResults); + + var actual = await sut.SolveAsync(captchaRequest); + + sut.GetActualRequests().Should().BeEquivalentTo(expectedRequests); + actual.Should().BeEquivalentTo(expectedResult); + } + } } \ No newline at end of file diff --git a/CapMonsterCloud.Client.IntegrationTests/ObjectGen.cs b/CapMonsterCloud.Client.IntegrationTests/ObjectGen.cs index 652ca8c..fd6befe 100644 --- a/CapMonsterCloud.Client.IntegrationTests/ObjectGen.cs +++ b/CapMonsterCloud.Client.IntegrationTests/ObjectGen.cs @@ -546,5 +546,36 @@ public static CaptchaResult CreateSolution() }; } } + + public static class MTCaptchaTask + { + public static MTCaptchaTaskRequest CreateTask() + { + return new MTCaptchaTaskRequest + { + WebsiteUrl = Gen.RandomUri().ToString(), + WebsiteKey = Gen.RandomString(), + Invisible = Gen.RandomBool(), + PageAction = Gen.RandomString(), + UserAgent = Gen.UserAgent(), + Proxy = new ProxyContainer( + Gen.RandomString(), Gen.RandomInt(0, 65535), + Gen.RandomEnum(), Gen.RandomString(), Gen.RandomString()) + }; + } + + public static CaptchaResult CreateSolution() + { + return new CaptchaResult + { + Error = null, + Solution = new MTCaptchaTaskResponse + { + Value = Gen.RandomString() + } + }; + } + } + } } \ No newline at end of file diff --git a/CapMonsterCloud.Client.IntegrationTests/Sut.cs b/CapMonsterCloud.Client.IntegrationTests/Sut.cs index 57cc413..3839da1 100644 --- a/CapMonsterCloud.Client.IntegrationTests/Sut.cs +++ b/CapMonsterCloud.Client.IntegrationTests/Sut.cs @@ -98,6 +98,9 @@ public async Task> SolveAsync( public async Task> SolveAsync( RecognitionComplexImageTaskRequest request) => await _cloudClient.SolveAsync(request); + public async Task> SolveAsync( + MTCaptchaTaskRequest request) => await _cloudClient.SolveAsync(request); + public async Task GetBalanceAsync() { return await _cloudClient.GetBalanceAsync(); diff --git a/CapMonsterCloud.Client/CapMonsterCloudClient_GetResultTimeouts.cs b/CapMonsterCloud.Client/CapMonsterCloudClient_GetResultTimeouts.cs index 2cd856c..fce40f5 100644 --- a/CapMonsterCloud.Client/CapMonsterCloudClient_GetResultTimeouts.cs +++ b/CapMonsterCloud.Client/CapMonsterCloudClient_GetResultTimeouts.cs @@ -183,6 +183,16 @@ private static GetResultTimeouts GetTimeouts(Type type) Timeout = TimeSpan.FromSeconds(15) } }, + { + typeof(MTCaptchaTaskRequest), + new GetResultTimeouts + { + FirstRequestDelay = TimeSpan.FromSeconds(1), + FirstRequestNoCacheDelay = TimeSpan.FromSeconds(10), + RequestsInterval = TimeSpan.FromSeconds(3), + Timeout = TimeSpan.FromSeconds(180) + } + }, }; } } diff --git a/CapMonsterCloud.Client/Requests/MTCaptchaTaskRequest.cs b/CapMonsterCloud.Client/Requests/MTCaptchaTaskRequest.cs new file mode 100644 index 0000000..94f59a2 --- /dev/null +++ b/CapMonsterCloud.Client/Requests/MTCaptchaTaskRequest.cs @@ -0,0 +1,57 @@ +using Newtonsoft.Json; +using System.ComponentModel.DataAnnotations; +using Zennolab.CapMonsterCloud.Responses; + +namespace Zennolab.CapMonsterCloud.Requests +{ + /// + /// MTCaptcha recognition request. + /// + /// + /// https://docs.capmonster.cloud/docs/captchas/mtcaptcha-task/ + /// + public sealed class MTCaptchaTaskRequest : CaptchaRequestBaseWithProxy + { + /// + /// Recognition task type + /// + public const string TaskType = "MTCaptchaTask"; + + /// + [JsonProperty("type", Required = Required.Always)] + public sealed override string Type => TaskType; + + /// + /// Address of a web page with MTCaptcha. + /// + [JsonProperty("websiteURL", Required = Required.Always)] + [Url] + public string WebsiteUrl { get; set; } + + /// + /// The MTCaptcha key (sk/sitekey). + /// + [JsonProperty("websiteKey", Required = Required.Always)] + [StringLength(int.MaxValue, MinimumLength = 1)] + public string WebsiteKey { get; set; } + + /// + /// true for invisible widget (has hidden confirmation field). + /// + [JsonProperty("isInvisible")] + public bool Invisible { get; set; } + + /// + /// Action value (passed as "act" and shown during token validation). + /// Provide only if it differs from default "%24". + /// + [JsonProperty("pageAction")] + public string PageAction { get; set; } + + /// + /// Browser's User-Agent (actual Windows UA recommended). + /// + [JsonProperty("userAgent")] + public string UserAgent { get; set; } + } +} \ No newline at end of file diff --git a/CapMonsterCloud.Client/Responses/MTCaptchaTaskResponse.cs b/CapMonsterCloud.Client/Responses/MTCaptchaTaskResponse.cs new file mode 100644 index 0000000..298e743 --- /dev/null +++ b/CapMonsterCloud.Client/Responses/MTCaptchaTaskResponse.cs @@ -0,0 +1,16 @@ +using Newtonsoft.Json; + +namespace Zennolab.CapMonsterCloud.Responses +{ + /// + /// MTCaptcha recognition response + /// + public sealed class MTCaptchaTaskResponse : CaptchaResponseBase + { + /// + /// MTCaptcha token to submit to the target site. + /// + [JsonProperty("token")] + public string Value { get; set; } + } +} \ No newline at end of file