-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNextCaptchaAPI.cs
260 lines (243 loc) · 11.4 KB
/
NextCaptchaAPI.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
using System;
using System.Collections.Generic;
using System.Net.Http;
using System.Threading.Tasks;
using Newtonsoft.Json;
namespace NextCaptchaSDK
{
public class NextCaptchaAPI
{
private const string HOST = "https://api.nextcaptcha.com";
private const string RECAPTCHAV2_TYPE = "RecaptchaV2TaskProxyless";
private const string RECAPTCHAV2_ENTERPRISE_TYPE = "RecaptchaV2EnterpriseTaskProxyless";
private const string RECAPTCHAV3_PROXYLESS_TYPE = "RecaptchaV3TaskProxyless";
private const string RECAPTCHAV3_TYPE = "RecaptchaV3Task";
private const string RECAPTCHA_MOBILE_TYPE = "RecaptchaMobileProxyless";
private const string HCAPTCHA_TYPE = "HCaptchaTask";
private const string HCAPTCHA_PROXYLESS_TYPE = "HCaptchaTaskProxyless";
private const string HCAPTCHA_ENTERPRISE_TYPE = "HCaptchaEnterpriseTask";
private const string FUNCAPTCHA_TYPE = "FunCaptchaTask";
private const string FUNCAPTCHA_PROXYLESS_TYPE = "FunCaptchaTaskProxyless";
private const int TIMEOUT = 45;
private const string PENDING_STATUS = "pending";
private const string PROCESSING_STATUS = "processing";
private const string READY_STATUS = "ready";
private const string FAILED_STATUS = "failed";
private readonly string _clientKey;
private readonly string _solftId;
private readonly string _callbackUrl;
private readonly bool _openLog;
private readonly HttpClient _httpClient;
public NextCaptchaAPI(string clientKey, string solftId = "", string callbackUrl = "", bool openLog = true)
{
_clientKey = clientKey;
_solftId = solftId;
_callbackUrl = callbackUrl;
_openLog = openLog;
_httpClient = new HttpClient();
}
private async Task<string> GetBalanceInternalAsync()
{
var response = await _httpClient.GetAsync($"{HOST}/getBalance?clientKey={_clientKey}");
if (!response.IsSuccessStatusCode)
{
if (_openLog)
{
Console.WriteLine($"Error: {response.StatusCode} {await response.Content.ReadAsStringAsync()}");
}
return null;
}
var result = JsonConvert.DeserializeObject<Dictionary<string, string>>(await response.Content.ReadAsStringAsync());
if (_openLog)
{
Console.WriteLine($"Balance: {result["balance"]}");
}
return result["balance"];
}
private async Task<Dictionary<string, string>> SendTaskAsync(Dictionary<string, object> task)
{
var data = new Dictionary<string, object>
{
{"clientKey", _clientKey},
{"solftId", _solftId},
{"callbackUrl", _callbackUrl},
{"task", task}
};
var response = await _httpClient.PostAsync($"{HOST}/createTask", new StringContent(JsonConvert.SerializeObject(data), System.Text.Encoding.UTF8, "application/json"));
if (!response.IsSuccessStatusCode)
{
if (_openLog)
{
Console.WriteLine($"Error: {response.StatusCode} {await response.Content.ReadAsStringAsync()}");
Console.WriteLine($"Data: {JsonConvert.SerializeObject(data)}");
}
return null;
}
var result = JsonConvert.DeserializeObject<Dictionary<string, string>>(await response.Content.ReadAsStringAsync());
var taskId = result["taskId"];
if (_openLog)
{
Console.WriteLine($"Task {taskId} created {JsonConvert.SerializeObject(result)}");
}
var startTime = DateTime.Now;
while (true)
{
if ((DateTime.Now - startTime).TotalSeconds > TIMEOUT)
{
return new Dictionary<string, string>
{
{"errorId", "12"},
{"errorDescription", "Timeout"},
{"status", "failed"}
};
}
response = await _httpClient.PostAsync($"{HOST}/getTaskResult", new StringContent(JsonConvert.SerializeObject(new Dictionary<string, string> { { "clientKey", _clientKey }, { "taskId", taskId } }), System.Text.Encoding.UTF8, "application/json"));
if (!response.IsSuccessStatusCode)
{
if (_openLog)
{
Console.WriteLine($"Error: {response.StatusCode} {await response.Content.ReadAsStringAsync()}");
}
return null;
}
result = JsonConvert.DeserializeObject<Dictionary<string, string>>(await response.Content.ReadAsStringAsync());
var status = result["status"];
if (_openLog)
{
Console.WriteLine($"Task status: {status}");
}
if (status == READY_STATUS)
{
Console.WriteLine($"Task {taskId} ready {JsonConvert.SerializeObject(result)}");
return result;
}
if (status == FAILED_STATUS)
{
Console.WriteLine($"Task {taskId} failed {JsonConvert.SerializeObject(result)}");
return result;
}
await Task.Delay(1000);
}
}
public async Task<Dictionary<string, string>> SolveRecaptchaV2Async(string websiteUrl, string websiteKey, string recaptchaDataSValue = "", bool isInvisible = false, string apiDomain = "")
{
var task = new Dictionary<string, object>
{
{"type", RECAPTCHAV2_TYPE},
{"websiteURL", websiteUrl},
{"websiteKey", websiteKey},
{"recaptchaDataSValue", recaptchaDataSValue},
{"isInvisible", isInvisible},
{"apiDomain", apiDomain}
};
return await SendTaskAsync(task);
}
public async Task<Dictionary<string, string>> SolveRecaptchaV2EnterpriseAsync(string websiteUrl, string websiteKey, Dictionary<string, object> enterprisePayload = null, bool isInvisible = false, string apiDomain = "")
{
var task = new Dictionary<string, object>
{
{"type", RECAPTCHAV2_ENTERPRISE_TYPE},
{"websiteURL", websiteUrl},
{"websiteKey", websiteKey},
{"enterprisePayload", enterprisePayload ?? new Dictionary<string, object>()},
{"isInvisible", isInvisible},
{"apiDomain", apiDomain}
};
return await SendTaskAsync(task);
}
public async Task<Dictionary<string, string>> SolveRecaptchaV3Async(string websiteUrl, string websiteKey, string pageAction = "", string apiDomain = "", string proxyType = "", string proxyAddress = "", int proxyPort = 0, string proxyLogin = "", string proxyPassword = "")
{
var task = new Dictionary<string, object>
{
{"type", RECAPTCHAV3_PROXYLESS_TYPE},
{"websiteURL", websiteUrl},
{"websiteKey", websiteKey},
{"pageAction", pageAction},
{"apiDomain", apiDomain}
};
if (!string.IsNullOrEmpty(proxyAddress))
{
task["type"] = RECAPTCHAV3_TYPE;
task["proxyType"] = proxyType;
task["proxyAddress"] = proxyAddress;
task["proxyPort"] = proxyPort;
task["proxyLogin"] = proxyLogin;
task["proxyPassword"] = proxyPassword;
}
return await SendTaskAsync(task);
}
public async Task<Dictionary<string, string>> SolveRecaptchaMobileAsync(string appKey, string appPackageName = "", string appAction = "")
{
var task = new Dictionary<string, object>
{
{"type", RECAPTCHA_MOBILE_TYPE},
{"appKey", appKey},
{"appPackageName", appPackageName},
{"appAction", appAction}
};
return await SendTaskAsync(task);
}
public async Task<Dictionary<string, string>> SolveHCaptchaAsync(string websiteUrl, string websiteKey, bool isInvisible = false, Dictionary<string, object> enterprisePayload = null, string proxyType = "", string proxyAddress = "", int proxyPort = 0, string proxyLogin = "", string proxyPassword = "")
{
var task = new Dictionary<string, object>
{
{"type", HCAPTCHA_PROXYLESS_TYPE},
{"websiteURL", websiteUrl},
{"websiteKey", websiteKey},
{"isInvisible", isInvisible},
{"enterprisePayload", enterprisePayload ?? new Dictionary<string, object>()}
};
if (!string.IsNullOrEmpty(proxyAddress))
{
task["type"] = HCAPTCHA_TYPE;
task["proxyType"] = proxyType;
task["proxyAddress"] = proxyAddress;
task["proxyPort"] = proxyPort;
task["proxyLogin"] = proxyLogin;
task["proxyPassword"] = proxyPassword;
}
return await SendTaskAsync(task);
}
public async Task<Dictionary<string, string>> SolveHCaptchaEnterpriseAsync(string websiteUrl, string websiteKey, Dictionary<string, object> enterprisePayload = null, bool isInvisible = false, string proxyType = "", string proxyAddress = "", int proxyPort = 0, string proxyLogin = "", string proxyPassword = "")
{
var task = new Dictionary<string, object>
{
{"type", HCAPTCHA_ENTERPRISE_TYPE},
{"websiteURL", websiteUrl},
{"websiteKey", websiteKey},
{"enterprisePayload", enterprisePayload ?? new Dictionary<string, object>()},
{"isInvisible", isInvisible},
{"proxyType", proxyType},
{"proxyAddress", proxyAddress},
{"proxyPort", proxyPort},
{"proxyLogin", proxyLogin},
{"proxyPassword", proxyPassword}
};
return await SendTaskAsync(task);
}
public async Task<Dictionary<string, string>> SolveFunCaptchaAsync(string websitePublicKey, string websiteUrl = "", string data = "", string proxyType = "", string proxyAddress = "", int proxyPort = 0, string proxyLogin = "", string proxyPassword = "")
{
var task = new Dictionary<string, object>
{
{"type", FUNCAPTCHA_PROXYLESS_TYPE},
{"websiteURL", websiteUrl},
{"websitePublicKey", websitePublicKey},
{"data", data}
};
if (!string.IsNullOrEmpty(proxyAddress))
{
task["type"] = FUNCAPTCHA_TYPE;
task["proxyType"] = proxyType;
task["proxyAddress"] = proxyAddress;
task["proxyPort"] = proxyPort;
task["proxyLogin"] = proxyLogin;
task["proxyPassword"] = proxyPassword;
}
return await SendTaskAsync(task);
}
public async Task<string> GetBalanceAsync()
{
return await GetBalanceInternalAsync();
}
}
}