-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathsolver.go
113 lines (92 loc) · 2.64 KB
/
solver.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
package captchago
import (
"errors"
"strings"
"time"
)
const (
AntiCaptcha SolveService = "anticaptcha"
AnyCaptcha SolveService = "anycaptcha"
CapSolver SolveService = "capsolver"
TwoCaptcha SolveService = "2captcha"
CapMonster SolveService = "capmonster"
)
func New(service SolveService, apiKey string) (*Solver, error) {
solver := &Solver{
Verbose: true,
ApiKey: apiKey,
UpdateDelay: time.Second * 2,
}
err := solver.SetService(service)
if err != nil {
return nil, err
}
return solver, nil
}
func (s *Solver) SetService(service SolveService) error {
oldService := s.service
s.service = formatService(service)
switch s.service {
case AntiCaptcha:
s.methods = antiCaptchaMethods(s, "api.anti-captcha.com")
break
case AnyCaptcha:
s.methods = antiCaptchaMethods(s, "api.anycaptcha.com")
break
case CapMonster:
s.methods = antiCaptchaMethods(s, "api.capmonster.cloud")
break
case CapSolver:
s.methods = antiCaptchaMethods(s, "api.capsolver.com")
break
case TwoCaptcha:
s.methods = twoCaptchaMethods(s, "2captcha.com")
break
default:
s.service = oldService
return errors.New("that service isn't supported")
}
return nil
}
func (s *Solver) IsValidService(service SolveService) bool {
service = formatService(service)
switch service {
case AntiCaptcha, AnyCaptcha, CapMonster, CapSolver, TwoCaptcha:
return true
}
return false
}
func (s *Solver) GetService() SolveService {
return s.service
}
// formatService formats the service name to reduce the chance of human errors
func formatService(s SolveService) SolveService {
// remove spaces, dashes, and tlds
s = strings.ReplaceAll(strings.ReplaceAll(strings.ToLower(s), " ", ""), "-", "")
s = strings.Split(s, ".")[0]
if strings.Contains(s, "://") {
s = strings.Split(s, "://")[1]
}
// rucaptcha uses same api as 2captcha, its just different name
if s == "rucaptcha" {
return "2captcha"
}
return s
}
type Solver struct {
// UpdateDelay is the delay between each update getTaskResult request
UpdateDelay time.Duration
// Disabling Verbose will disable all logging
Verbose bool
// ApiKey is the key used to authenticate with the service
ApiKey string
// ForcedDomain don't edit unless you know what you're doing.
// This is used to allow the user to change their captcha service to whatever
// they want as long as they share the same api methods.
ForcedDomain string
// service is the service that the solver is using. It's private because it's only used internally
service SolveService
// methods is the methods used to solve captchas. It's private because it's only used internally
methods *solveMethods
}
type SolveService = string