-
Notifications
You must be signed in to change notification settings - Fork 3
/
solver.go
186 lines (146 loc) · 3.39 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
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
package rucaptcha
import (
"fmt"
"io/ioutil"
"net/http"
"net/url"
"regexp"
"strings"
"time"
)
// CaptchaSolver structure
type CaptchaSolver struct {
IsPhrase bool
IsRegsence bool
IsNumeric int
MinLength int
MaxLength int
Language int
ImagePath string
APIKey string
RequestURL string
ResultURL string
CheckResultTimeout time.Duration
}
// New creates instance of solver
func New(key string) *CaptchaSolver {
return &CaptchaSolver{
RequestURL: "http://rucaptcha.com/in.php",
ResultURL: "http://rucaptcha.com/res.php",
APIKey: key,
}
}
// Solve get image by path and redn request to rucaptcha service
// Returns captcha code, captchaID and error if errors occured
func (solver *CaptchaSolver) Solve(path string) (*string, *string, error) {
solver.ImagePath = path
file, err := solver.loadCaptchaImage()
if err != nil {
return nil, nil, err
}
captchaID, err := solver.getCaptchaID(*file)
if err != nil {
return nil, nil, err
}
answer, err := solver.waitForReady(*captchaID)
if err != nil {
return nil, captchaID, err
}
return answer, captchaID, nil
}
// Complain send complain request to service
func (solver *CaptchaSolver) Complain(captchaID string) error {
return solver.complainRequest(captchaID)
}
func (solver *CaptchaSolver) getCaptchaID(file []byte) (*string, error) {
response, err := solver.sendRequest(file)
if err != nil {
return nil, err
}
body, err := ioutil.ReadAll(response)
if err != nil {
return nil, err
}
hasError := regexp.
MustCompile(`ERROR`).
MatchString(string(body))
if hasError {
return nil, fmt.Errorf("Captcha service error: %s\n", string(body))
}
isOk := regexp.
MustCompile(`OK`).
MatchString(string(body))
if !isOk {
return nil, fmt.Errorf("Unknown response: %s\n", string(body))
}
results := strings.Split(string(body), "|")
return &results[1], nil
}
func (solver *CaptchaSolver) waitForReady(captchaID string) (*string, error) {
data := url.Values{}
data.Add("key", solver.APIKey)
data.Add("action", "get")
data.Add("id", captchaID)
url := solver.ResultURL + "?"
var response *http.Response
defer func() {
if response != nil {
response.Body.Close()
}
}()
var answer *string
for {
time.Sleep(solver.CheckResultTimeout * time.Second)
response, err := http.Get(url + data.Encode())
if err != nil {
return nil, err
}
body, err := ioutil.ReadAll(response.Body)
if err != nil {
return nil, err
}
isOk := regexp.
MustCompile(`OK`).
MatchString(string(body))
if isOk {
results := strings.Split(string(body), "|")
answer = &results[1]
break
}
notReady := regexp.
MustCompile(`CAPCHA_NOT_READY`).
MatchString(string(body))
if notReady {
continue
}
hasError := regexp.
MustCompile(`ERROR`).
MatchString(string(body))
if hasError {
return nil, fmt.Errorf("Error response: %s", string(body))
}
}
return answer, nil
}
func (solver *CaptchaSolver) loadCaptchaImage() (*[]byte, error) {
isHTTP := regexp.
MustCompile(`(http://|https://)`).
MatchString(solver.ImagePath)
if !isHTTP {
body, err := ioutil.ReadFile(solver.ImagePath)
if err != nil {
return nil, err
}
return &body, nil
}
response, err := http.Get(solver.ImagePath)
if err != nil {
return nil, err
}
defer response.Body.Close()
body, err := ioutil.ReadAll(response.Body)
if err != nil {
return nil, err
}
return &body, nil
}