-
Notifications
You must be signed in to change notification settings - Fork 2
/
acmethods.go
507 lines (402 loc) · 10.6 KB
/
acmethods.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
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
package captchago
import (
"errors"
"fmt"
"strings"
"time"
)
func antiCaptchaMethods(solver *Solver, preferredDomain string) *solveMethods {
domain := func() string {
r := preferredDomain
if solver.ForcedDomain != "" {
r = solver.ForcedDomain
}
if !strings.Contains(r, "://") {
// detects if it's an ip or a domain
if strings.Contains(r, ":") || strings.Count(r, ".") == 4 {
r = "http://" + r
} else {
r = "https://" + r
}
}
return r
}
createTask := func(task map[string]interface{}) (any, error) {
d := domain()
payload := map[string]interface{}{
"clientKey": solver.ApiKey,
"task": task,
}
if strings.Contains(d, "anti-captcha.com") {
payload["softId"] = 1080
} else if strings.Contains(d, "capmonster.cloud") {
payload["softId"] = 59
} else if strings.Contains(d, "capsolver.com") {
payload["appId"] = "B7E57F27-0AD3-434D-A5B7-CF9EE7D093EF"
}
body, err := postJSON(d+"/createTask", payload)
if err != nil {
return 0, err
}
createErr, hasError := body["errorDescription"]
if hasError && createErr != nil {
return 0, errors.New(createErr.(string))
}
taskId, hasTaskId := body["taskId"]
if !hasTaskId {
return 0, errors.New("no taskId")
}
taskStr, ok := taskId.(string)
if ok {
return taskStr, nil
}
return int(taskId.(float64)), nil
}
// parseResponse returns should continue, solution, error
parseResponse := func(body map[string]interface{}) (bool, *Solution, error) {
errMsg, hasError := body["errorDescription"]
if hasError && errMsg != nil {
return false, nil, errors.New(errMsg.(string))
}
status := body["status"]
switch status {
case "processing":
return true, nil, nil
case "ready":
solution, hasSolution := body["solution"].(map[string]interface{})
if !hasSolution {
return false, nil, errors.New("no solution")
}
// response can either be gRecaptchaResponse or token
response := solution["gRecaptchaResponse"]
if response == nil {
response = solution["token"]
}
if response == nil {
response = solution["x-kpsdk-cd"]
}
if response == nil {
response = solution["x-kpsdk-ct"]
}
if response == nil {
if solver.Verbose {
fmt.Println(body)
}
return false, nil, errors.New("no solution text")
}
ip := ""
cost := ""
rIP, hasIP := body["ip"]
if hasIP && rIP != nil {
ip = rIP.(string)
}
rCost, hasCost := body["cost"]
if hasCost && rCost != nil {
cost = fmt.Sprintf("%v", rCost)
}
text, _ := response.(string)
return false, &Solution{
Text: text,
RawSolution: solution,
IP: ip,
Cost: cost,
}, nil
default:
return false, nil, errors.New("unknown status")
}
}
// keeps retrying until it has returned error or solved
getResponse := func(taskId any) (*Solution, error) {
for {
time.Sleep(solver.UpdateDelay)
if solver.Verbose {
fmt.Println("getting response for task", taskId)
}
d := domain()
payload := map[string]interface{}{
"clientKey": solver.ApiKey,
"taskId": taskId,
}
body, err := postJSON(d+"/getTaskResult", payload)
if err != nil {
if solver.Verbose {
_ = fmt.Errorf("error while getting task result: %s\n", err)
}
continue
}
shouldContinue, sol, err := parseResponse(body)
if err != nil {
return nil, err
}
if shouldContinue {
continue
}
sol.TaskId = taskId
return sol, nil
}
}
createResponse := func(taskData map[string]interface{}) (*Solution, error) {
start := time.Now().UnixMilli()
taskId, err := createTask(taskData)
if err != nil {
return nil, err
}
if solver.Verbose {
fmt.Printf("created task with id %v\n", taskId)
}
sol, err := getResponse(taskId)
if sol != nil {
sol.Speed = time.Now().UnixMilli() - start
}
if solver.Verbose && err == nil {
fmt.Printf("solved task with id %v\n", taskId)
}
return sol, err
}
applyProxy := func(data map[string]interface{}, p *Proxy, taskName string) {
if p == nil {
data["type"] = taskName + "Proxyless"
return
}
data["type"] = taskName
data["proxyAddress"] = p.address
data["proxyPort"] = p.port
if p.login != nil {
data["proxyLogin"] = p.login.Username
data["proxyPassword"] = p.login.Password
}
t := p.pType
if t == ProxyTypeHTTPS {
t = ProxyTypeHTTP
}
data["proxyType"] = t
}
methods := &solveMethods{
GetBalance: func() (float64, error) {
d := domain()
payload := map[string]interface{}{
"clientKey": solver.ApiKey,
}
body, err := postJSON(d+"/getBalance", payload)
if err != nil {
return 0, err
}
errMsg, hasError := body["errorDescription"]
if hasError && errMsg != nil {
return 0, errors.New(errMsg.(string))
}
balance, hasBalance := body["balance"]
if !hasBalance {
return 0, errors.New("no balance")
}
return balance.(float64), nil
},
RecaptchaV2: func(o RecaptchaV2Options) (*Solution, error) {
taskData := map[string]interface{}{
"websiteURL": o.PageURL,
"websiteKey": o.SiteKey,
"isInvisible": o.Invisible,
}
if o.Enterprise != nil {
applyProxy(taskData, o.Proxy, "RecaptchaV2EnterpriseTask")
taskData["enterprisePayload"] = o.Enterprise
} else {
applyProxy(taskData, o.Proxy, "RecaptchaV2Task")
}
if o.UserAgent != "" {
taskData["userAgent"] = o.UserAgent
}
if o.APIDomain != "" {
taskData["apiDomain"] = o.APIDomain
}
if o.Cookies != nil && len(o.Cookies) > 0 {
taskData["cookies"] = cookiesToString(o.Cookies)
}
if o.DataS != "" {
taskData["recaptchaDataSValue"] = o.DataS
}
return createResponse(taskData)
},
HCaptcha: func(o HCaptchaOptions) (*Solution, error) {
taskData := map[string]interface{}{
"websiteURL": o.PageURL,
"websiteKey": o.SiteKey,
"isInvisible": o.Invisible,
}
baseTask := "HCaptchaTask"
// for capsolver.com HCaptchaTurboTask than the regular task is better, but requires proxy
if solver.service == CapSolver && o.Proxy != nil {
baseTask = "HCaptchaTurboTask"
}
applyProxy(taskData, o.Proxy, baseTask)
if o.UserAgent != "" {
taskData["userAgent"] = o.UserAgent
}
if o.EnterprisePayload != nil {
ep := o.EnterprisePayload
// some apis are slightly different
if ep.RQData != "" {
taskData["data"] = o.EnterprisePayload.RQData
}
payload := map[string]interface{}{}
if ep.Sentry {
payload["sentry"] = ep.Sentry
}
if ep.RQData != "" {
payload["rqdata"] = ep.RQData
}
if ep.APIEndpoint != "" {
payload["apiEndpoint"] = ep.APIEndpoint
}
if ep.Endpoint != "" {
payload["endpoint"] = ep.APIEndpoint
}
if ep.ReportAPI != "" {
payload["reportapi"] = ep.ReportAPI
}
if ep.AssetHost != "" {
payload["assethost"] = ep.AssetHost
}
if ep.ImgHost != "" {
payload["imghost"] = ep.ImgHost
}
taskData["enterprisePayload"] = payload
}
return createResponse(taskData)
},
FunCaptcha: func(o FunCaptchaOptions) (*Solution, error) {
taskData := map[string]interface{}{
"websiteURL": o.PageURL,
"websitePublicKey": o.PublicKey,
"funcaptchaApiJSSubdomain": o.Subdomain,
}
applyProxy(taskData, o.Proxy, "FunCaptchaTask")
taskData["userAgent"] = o.UserAgent
return createResponse(taskData)
},
RecaptchaV3: func(o RecaptchaV3Options) (*Solution, error) {
taskData := map[string]interface{}{
"type": "RecaptchaV3TaskProxyless",
"websiteURL": o.PageURL,
"websiteKey": o.SiteKey,
"minScore": o.MinScore,
"pageAction": o.Action,
"isEnterprise": o.Enterprise,
}
return createResponse(taskData)
},
}
// kasada method
if solver.service == CapSolver {
methods.Kasada = func(o KasadaOptions) (*KasadaSolution, error) {
if o.Proxy == nil {
return nil, errors.New("proxy is required")
}
taskData := map[string]interface{}{
"pageURL": o.PageURL,
"cd": o.DetailedCD,
"onlyCD": o.OnlyCD,
}
applyProxy(taskData, o.Proxy, "AntiKasadaTask")
if o.Version != "" {
taskData["version"] = o.Version
}
if o.UserAgent != "" {
taskData["userAgent"] = o.UserAgent
}
// send request to /kasada/invoke
payload := map[string]interface{}{
"clientKey": solver.ApiKey,
"task": taskData,
"appId": "B7E57F27-0AD3-434D-A5B7-CF9EE7D093EF",
}
body, err := postJSON(domain()+"/kasada/invoke", payload)
if err != nil {
return nil, err
}
_, sol, err := parseResponse(body)
if err != nil {
return nil, err
}
if sol == nil {
return nil, errors.New("no solution")
}
kpsdkCD := ""
kpsdkCT := ""
userAgent := ""
raw := sol.RawSolution["x-kpsdk-cd"]
if raw != nil {
kpsdkCD = raw.(string)
}
raw = sol.RawSolution["x-kpsdk-ct"]
if raw != nil {
kpsdkCT = raw.(string)
}
raw = sol.RawSolution["user-agent"]
if raw != nil {
userAgent = raw.(string)
}
return &KasadaSolution{
Solution: sol,
KpsdkCD: kpsdkCD,
KpsdkCT: kpsdkCT,
UserAgent: userAgent,
}, nil
}
methods.Cloudflare = func(o CloudflareOptions) (*Solution, error) {
if o.Proxy == nil {
return nil, errors.New("proxy is required")
}
if o.Metadata == nil {
o.Metadata = map[string]string{}
}
// only set metadata type if one wasn't provided
if _, ok := o.Metadata["type"]; !ok {
switch o.Type {
case CloudflareTypeChallenge:
o.Metadata["type"] = "challenge"
case CloudflareTypeTurnstile:
o.Metadata["type"] = "turnstile"
default:
o.Metadata["type"] = ""
}
}
if _, ok := o.Metadata["action"]; !ok && o.Action != "" {
o.Metadata["action"] = o.Action
}
if _, ok := o.Metadata["cdata"]; !ok && o.CData != "" {
o.Metadata["cdata"] = o.CData
}
taskData := map[string]interface{}{
"websiteURL": o.PageURL,
"metadata": o.Metadata,
}
if o.SiteKey != "" {
taskData["websiteKey"] = o.SiteKey
}
applyProxy(taskData, o.Proxy, "AntiCloudflareTask")
return createResponse(taskData)
}
} else {
methods.Cloudflare = func(o CloudflareOptions) (*Solution, error) {
if o.Type == CloudflareTypeChallenge {
return nil, errors.New("cloudflare challenge type is not supported by this solver")
}
taskData := map[string]interface{}{
"websiteURL": o.PageURL,
"websiteKey": o.SiteKey,
}
if o.Action != "" {
taskData["action"] = o.Action
}
if o.Metadata != nil {
for k, v := range o.Metadata {
taskData[k] = v
}
}
applyProxy(taskData, o.Proxy, "TurnstileTask")
return createResponse(taskData)
}
}
return methods
}