-
Notifications
You must be signed in to change notification settings - Fork 9
/
anticaptcha.go
326 lines (266 loc) · 8.33 KB
/
anticaptcha.go
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
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
package gocaptcha
import (
"bytes"
"context"
"encoding/json"
"errors"
"fmt"
"io"
"net/http"
"strconv"
"github.com/justhyped/gocaptcha/internal"
)
type AntiCaptcha struct {
baseUrl string
apiKey string
}
func NewAntiCaptcha(apiKey string) *AntiCaptcha {
return &AntiCaptcha{
apiKey: apiKey,
baseUrl: "https://api.anti-captcha.com",
}
}
func NewCapMonsterCloud(apiKey string) *AntiCaptcha {
return &AntiCaptcha{
apiKey: apiKey,
baseUrl: "https://api.capmonster.cloud",
}
}
// NewCustomAntiCaptcha can be used to change the baseUrl, some providers such as CapMonster, XEVil and CapSolver
// have the exact same API as AntiCaptcha, thus allowing you to use these providers with ease.
func NewCustomAntiCaptcha(baseUrl, apiKey string) *AntiCaptcha {
return &AntiCaptcha{
baseUrl: baseUrl,
apiKey: apiKey,
}
}
func (a *AntiCaptcha) SolveImageCaptcha(ctx context.Context, settings *Settings, payload *ImageCaptchaPayload) (ICaptchaResponse, error) {
task := map[string]any{
"type": "ImageToTextTask",
"body": payload.Base64String,
"case": payload.CaseSensitive,
}
result, err := a.solveTask(ctx, settings, task)
if err != nil {
return nil, err
}
result.reportBad = a.report("/reportIncorrectImageCaptcha", result.taskId, settings)
return result, nil
}
func (a *AntiCaptcha) SolveRecaptchaV2(ctx context.Context, settings *Settings, payload *RecaptchaV2Payload) (ICaptchaResponse, error) {
task := map[string]any{
"type": "NoCaptchaTaskProxyless",
"websiteURL": payload.EndpointUrl,
"websiteKey": payload.EndpointKey,
"isInvisible": payload.IsInvisibleCaptcha}
result, err := a.solveTask(ctx, settings, task)
if err != nil {
return nil, err
}
result.reportBad = a.report("/reportIncorrectRecaptcha", result.taskId, settings)
result.reportGood = a.report("/reportCorrectRecaptcha", result.taskId, settings)
return result, nil
}
func (a *AntiCaptcha) SolveRecaptchaV3(ctx context.Context, settings *Settings, payload *RecaptchaV3Payload) (ICaptchaResponse, error) {
task := map[string]any{
"type": "RecaptchaV3TaskProxyless",
"websiteURL": payload.EndpointUrl,
"websiteKey": payload.EndpointKey,
"minScore": payload.MinScore,
"pageAction": payload.Action,
}
result, err := a.solveTask(ctx, settings, task)
if err != nil {
return nil, err
}
result.reportBad = a.report("/reportIncorrectRecaptcha", result.taskId, settings)
result.reportGood = a.report("/reportCorrectRecaptcha", result.taskId, settings)
return result, nil
}
func (a *AntiCaptcha) SolveHCaptcha(ctx context.Context, settings *Settings, payload *HCaptchaPayload) (ICaptchaResponse, error) {
task := map[string]any{
"type": "HCaptchaTaskProxyless",
"websiteURL": payload.EndpointUrl,
"websiteKey": payload.EndpointKey,
}
result, err := a.solveTask(ctx, settings, task)
if err != nil {
return nil, err
}
return result, nil
}
func (a *AntiCaptcha) SolveTurnstile(ctx context.Context, settings *Settings, payload *TurnstilePayload) (ICaptchaResponse, error) {
task := map[string]any{
"type": "TurnstileTaskProxyless",
"websiteURL": payload.EndpointUrl,
"websiteKey": payload.EndpointKey,
}
result, err := a.solveTask(ctx, settings, task)
if err != nil {
return nil, err
}
return result, nil
}
func (a *AntiCaptcha) solveTask(ctx context.Context, settings *Settings, task map[string]any) (*CaptchaResponse, error) {
taskId, syncAnswer, err := a.createTask(ctx, settings, task)
if err != nil {
return nil, err
}
if syncAnswer != nil {
return &CaptchaResponse{solution: *syncAnswer, taskId: taskId}, nil
}
if err := internal.SleepWithContext(ctx, settings.initialWaitTime); err != nil {
return nil, err
}
for i := 0; i < settings.maxRetries; i++ {
answer, err := a.getTaskResult(ctx, settings, taskId)
if err != nil {
return nil, err
}
if answer != "" {
return &CaptchaResponse{solution: answer, taskId: taskId}, nil
}
if err := internal.SleepWithContext(ctx, settings.pollInterval); err != nil {
return nil, err
}
}
return nil, errors.New("max tries exceeded")
}
func (a *AntiCaptcha) createTask(ctx context.Context, settings *Settings, task map[string]any) (string, *string, error) {
type antiCapSolution struct {
Text string `json:"text"`
}
type antiCaptchaCreateResponse struct {
ErrorID int `json:"errorId"`
ErrorDescription string `json:"errorDescription"`
TaskID any `json:"taskId"`
Status string `json:"status"`
Solution antiCapSolution `json:"solution"`
}
jsonValue, err := json.Marshal(map[string]any{"clientKey": a.apiKey, "task": task})
if err != nil {
return "", nil, err
}
req, err := http.NewRequestWithContext(ctx, http.MethodPost, a.baseUrl+"/createTask", bytes.NewBuffer(jsonValue))
if err != nil {
return "", nil, err
}
req.Header.Set("content-type", "application/json")
resp, err := settings.client.Do(req)
if err != nil {
return "", nil, err
}
defer resp.Body.Close()
respBody, err := io.ReadAll(resp.Body)
if err != nil {
return "", nil, err
}
var responseAsJSON antiCaptchaCreateResponse
if err := json.Unmarshal(respBody, &responseAsJSON); err != nil {
return "", nil, err
}
if responseAsJSON.ErrorID != 0 {
return "", nil, errors.New(responseAsJSON.ErrorDescription)
}
// if the task is solved synchronously, the solution is returned immediately
var result *string
if responseAsJSON.Status == "ready" {
result = &responseAsJSON.Solution.Text
}
switch responseAsJSON.TaskID.(type) {
case string:
// taskId is a string with CapSolver
return responseAsJSON.TaskID.(string), result, nil
case float64:
// taskId is a float64 with AntiCaptcha
return strconv.FormatFloat(responseAsJSON.TaskID.(float64), 'f', 0, 64), result, nil
}
// if you encounter this error with a custom provider, please open an issue
return "", nil, errors.New("unexpected taskId type, expecting string or float64")
}
func (a *AntiCaptcha) getTaskResult(ctx context.Context, settings *Settings, taskId string) (string, error) {
type antiCapSolution struct {
RecaptchaResponse string `json:"gRecaptchaResponse"`
Text string `json:"text"`
}
type resultResponse struct {
Status string `json:"status"`
ErrorID int `json:"errorId"`
ErrorDescription string `json:"errorDescription"`
Solution antiCapSolution `json:"solution"`
}
resultData := map[string]string{"clientKey": a.apiKey, "taskId": taskId}
jsonValue, err := json.Marshal(resultData)
if err != nil {
return "", err
}
req, err := http.NewRequestWithContext(ctx, http.MethodPost, a.baseUrl+"/getTaskResult", bytes.NewBuffer(jsonValue))
if err != nil {
return "", err
}
resp, err := settings.client.Do(req)
if err != nil {
return "", nil
}
defer resp.Body.Close()
respBody, err := io.ReadAll(resp.Body)
if err != nil {
return "", err
}
var respJson resultResponse
if err := json.Unmarshal(respBody, &respJson); err != nil {
return "", err
}
if respJson.ErrorID != 0 {
return "", errors.New(respJson.ErrorDescription)
}
if respJson.Status != "ready" {
return "", nil
}
if respJson.Solution.Text != "" {
return respJson.Solution.Text, nil
}
if respJson.Solution.RecaptchaResponse != "" {
return respJson.Solution.RecaptchaResponse, nil
}
return "", nil
}
func (a *AntiCaptcha) report(path, taskId string, settings *Settings) func(ctx context.Context) error {
type response struct {
ErrorID int64 `json:"errorId"`
ErrorCode string `json:"errorCode"`
ErrorDescription string `json:"errorDescription"`
}
return func(ctx context.Context) error {
payload := map[string]string{
"clientKey": a.apiKey,
"taskId": taskId,
}
rawPayload, err := json.Marshal(payload)
if err != nil {
return err
}
req, err := http.NewRequestWithContext(ctx, http.MethodPost, a.baseUrl+path, bytes.NewBuffer(rawPayload))
if err != nil {
return err
}
resp, err := settings.client.Do(req)
if err != nil {
return err
}
defer resp.Body.Close()
respBody, err := io.ReadAll(resp.Body)
if err != nil {
return err
}
var respJson response
if err := json.Unmarshal(respBody, &respJson); err != nil {
return err
}
if respJson.ErrorID != 0 {
return fmt.Errorf("%v: %v", respJson.ErrorCode, respJson.ErrorDescription)
}
return nil
}
}
var _ IProvider = (*AntiCaptcha)(nil)