mirror of
https://github.com/Megghy/MegghysAPI.git
synced 2025-12-06 14:16:56 +08:00
162 lines
5.3 KiB
C#
162 lines
5.3 KiB
C#
using System.Net.Http;
|
|
using System.Net.Http.Json;
|
|
using System.Text.Json;
|
|
using System.Text.Json.Serialization;
|
|
using System.Threading.Tasks;
|
|
using System.Threading;
|
|
using System;
|
|
|
|
namespace MegghysAPI.Modules
|
|
{
|
|
public static class CapResolver
|
|
{
|
|
private const string ApiKey = "CAP-5537BB057BE85FC11F04BB5BB6E80E73AD44279056AC22D0E70664B24964C3D9";
|
|
private const string CreateTaskUrl = "https://api.capsolver.com/createTask";
|
|
private const string GetTaskResultUrl = "https://api.capsolver.com/getTaskResult";
|
|
private static readonly HttpClient HttpClient = new HttpClient();
|
|
|
|
private class CreateTaskRequest
|
|
{
|
|
[JsonPropertyName("clientKey")]
|
|
public string ClientKey { get; set; }
|
|
|
|
[JsonPropertyName("task")]
|
|
public object Task { get; set; }
|
|
}
|
|
|
|
private class GeetestTask
|
|
{
|
|
[JsonPropertyName("type")]
|
|
public string Type { get; set; } = "GeeTestTaskProxyLess";
|
|
|
|
[JsonPropertyName("websiteURL")]
|
|
public string WebsiteURL { get; set; }
|
|
|
|
[JsonPropertyName("gt")]
|
|
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)]
|
|
public string Gt { get; set; }
|
|
|
|
[JsonPropertyName("challenge")]
|
|
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)]
|
|
public string Challenge { get; set; }
|
|
|
|
[JsonPropertyName("captchaId")]
|
|
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)]
|
|
public string CaptchaId { get; set; }
|
|
|
|
[JsonPropertyName("geetestApiServerSubdomain")]
|
|
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)]
|
|
public string GeetestApiServerSubdomain { get; set; }
|
|
}
|
|
|
|
private class CreateTaskResponse
|
|
{
|
|
[JsonPropertyName("errorId")]
|
|
public int ErrorId { get; set; }
|
|
|
|
[JsonPropertyName("status")]
|
|
public string Status { get; set; }
|
|
|
|
[JsonPropertyName("taskId")]
|
|
public string TaskId { get; set; }
|
|
}
|
|
|
|
private class GetTaskResultRequest
|
|
{
|
|
[JsonPropertyName("clientKey")]
|
|
public string ClientKey { get; set; }
|
|
|
|
[JsonPropertyName("taskId")]
|
|
public string TaskId { get; set; }
|
|
}
|
|
|
|
private class GetTaskResultResponse
|
|
{
|
|
[JsonPropertyName("errorId")]
|
|
public int ErrorId { get; set; }
|
|
|
|
[JsonPropertyName("status")]
|
|
public string Status { get; set; }
|
|
|
|
[JsonPropertyName("solution")]
|
|
public GeetestSolution Solution { get; set; }
|
|
}
|
|
public class GeetestSolution
|
|
{
|
|
[JsonPropertyName("challenge")]
|
|
public string Challenge { get; set; }
|
|
[JsonPropertyName("validate")]
|
|
public string Validate { get; set; }
|
|
}
|
|
|
|
public static async Task<GeetestSolution?> SolveGeetestAsync(string websiteUrl, string gt = null, string challenge = null, string captchaId = null, string apiServerSubdomain = null)
|
|
{
|
|
var geetestTask = new GeetestTask
|
|
{
|
|
WebsiteURL = websiteUrl,
|
|
Gt = gt,
|
|
Challenge = challenge,
|
|
CaptchaId = captchaId,
|
|
GeetestApiServerSubdomain = apiServerSubdomain
|
|
};
|
|
|
|
var createTaskRequest = new CreateTaskRequest
|
|
{
|
|
ClientKey = ApiKey,
|
|
Task = geetestTask
|
|
};
|
|
|
|
var response = await HttpClient.PostAsJsonAsync(CreateTaskUrl, createTaskRequest);
|
|
if (!response.IsSuccessStatusCode)
|
|
{
|
|
// 可以添加日志记录
|
|
return null;
|
|
}
|
|
|
|
var createTaskResponse = await response.Content.ReadFromJsonAsync<CreateTaskResponse>();
|
|
if (createTaskResponse == null || createTaskResponse.ErrorId != 0 || string.IsNullOrEmpty(createTaskResponse.TaskId))
|
|
{
|
|
// 可以添加日志记录
|
|
return null;
|
|
}
|
|
|
|
var getTaskResultRequest = new GetTaskResultRequest
|
|
{
|
|
ClientKey = ApiKey,
|
|
TaskId = createTaskResponse.TaskId
|
|
};
|
|
|
|
await Task.Delay(3000);
|
|
|
|
while (true)
|
|
{
|
|
await Task.Delay(1000);
|
|
|
|
var resultResponse = await HttpClient.PostAsJsonAsync(GetTaskResultUrl, getTaskResultRequest);
|
|
if (!resultResponse.IsSuccessStatusCode)
|
|
{
|
|
// 可以添加日志记录
|
|
continue;
|
|
}
|
|
|
|
var getTaskResultResponse = await resultResponse.Content.ReadFromJsonAsync<GetTaskResultResponse>();
|
|
if (getTaskResultResponse == null)
|
|
{
|
|
continue;
|
|
}
|
|
|
|
if (getTaskResultResponse.Status == "ready")
|
|
{
|
|
return getTaskResultResponse.Solution;
|
|
}
|
|
|
|
if (getTaskResultResponse.Status == "failed" || getTaskResultResponse.ErrorId != 0)
|
|
{
|
|
// 可以添加日志记录
|
|
return null;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|