openbullet / CaptchaSharp

.NET universal interface for the Web APIs of all major captcha solving services
MIT License
95 stars 22 forks source link

Added support for Cloudflare Turnstile to 2Captcha #32

Closed GekySan closed 4 months ago

GekySan commented 4 months ago

Added support for Cloudflare Turnstile to 2Captcha. Here is a usage example that I used to test :

using System;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;
using CaptchaSharp.Services;

public class Program
{
    private static readonly HttpClient httpClient = new HttpClient();

    public static async Task Main(string[] args)
    {
        string apiKey = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";
        string siteKey = "0x4AAAAAAAVrOwQWPlm3Bnr5";
        string siteUrl = "https://2captcha.com/demo/cloudflare-turnstile";

        var twoCaptchaService = new TwoCaptchaService(apiKey);

        var response = await twoCaptchaService.SolveTurnstileCaptchaAsync(siteKey, siteUrl);
        // Console.WriteLine(response.Response);

        var verificationResponse = await VerifyCaptchaAsync(response.Response);

        Console.WriteLine($"Verification Response: {verificationResponse}");

    }

    private static async Task<string> VerifyCaptchaAsync(string captchaSolution)
    {
        var requestUrl = "https://2captcha.com/api/v1/captcha-demo/cloudflare-turnstile/verify";
        var requestData = $"{{\"response\":\"{captchaSolution}\"}}";

        var request = new HttpRequestMessage(HttpMethod.Post, requestUrl)
        {
            Content = new StringContent(requestData, Encoding.UTF8, "application/json")
        };

        request.Headers.Add("accept", "*/*");
        request.Headers.Add("accept-language", "fr-FR,fr;q=0.9,en-US;q=0.8,en;q=0.7");
        request.Headers.Add("priority", "u=1, i");
        request.Headers.Add("referer", "https://2captcha.com/demo/cloudflare-turnstile");
        request.Headers.Add("user-agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/126.0.0.0 Safari/537.36");

        var response = await httpClient.SendAsync(request);
        var responseBody = await response.Content.ReadAsStringAsync();

        return responseBody;
    }

}
openbullet commented 4 months ago

Hello, thank you for the PR.

I'm currently in the process of making a huge amount of changes to CaptchaSharp to add support for all missing features, so I think it would be best if you waited until the new release to add new functionality to avoid duplication of work, since I already did this exact change locally and was going to push it to the dev branch.

Next time, please open an issue first if it doesn't already exist, and please announce that you're going to work on it, otherwise we end up doing the same things ^^ I'm going to close this since my change will also integrates unit tests, sorry about it.

openbullet commented 4 months ago

Also, on another note, please use better commit names instead of Update CaptchaType.cs etc. otherwise it looks like you're editing from GitHub web and the commit has no useful information about the change that is being done. Thanks!