-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient.go
411 lines (336 loc) · 8.36 KB
/
client.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
package anticaptcha
import (
"bytes"
"context"
"encoding/json"
"errors"
"fmt"
"io"
"io/ioutil"
"net/http"
"net/url"
"strconv"
"time"
)
const softId = 948
//Client allows programmatic access to the anti-captcha api
type Client struct {
key string
host string
client *http.Client
delay time.Duration
checkInterval time.Duration
}
//ClientOption is an option used to modify the anti-captcha client
type ClientOption func(c *Client)
//OptionalValue is an option used to add values to an api request
type OptionalValue func(map[string]interface{})
//WithOptional is an option that adds values to an api request
func WithOptional(key string, value interface{}) OptionalValue {
return func(m map[string]interface{}) {
m[key] = value
}
}
//NewClient returns a new Client with the applied options
func NewClient(key string, opts ...ClientOption) (client *Client) {
client = &Client{
key: key,
client: http.DefaultClient,
delay: time.Second * 10,
checkInterval: time.Second * 3,
}
client.host = "api.anti-captcha.com"
for _, v := range opts {
v(client)
}
return
}
//WithDelay is an option that makes the Client use the provided delay
func WithDelay(duration time.Duration) ClientOption {
return func(c *Client) {
c.delay = duration
}
}
//WithHost is an option that makes the Client use the provided host
func WithHost(host string) ClientOption {
return func(c *Client) {
c.host = host
}
}
//WithCheckInterval is an option that makes the Client use the provided check interval
func WithCheckInterval(duration time.Duration) ClientOption {
return func(c *Client) {
c.checkInterval = duration
}
}
func (c *Client) createTask(ctx context.Context, task interface{}) (taskId int64, err error) {
data := map[string]interface{}{
"clientKey": c.key,
"softId": softId,
"task": task,
}
sendBytes, err := json.Marshal(data)
if err != nil {
return
}
req, err := createRequest(ctx, http.MethodPost, fmt.Sprintf("https://%s/createTask", c.host), bytes.NewReader(sendBytes))
if err != nil {
return
}
resp, err := c.client.Do(req)
if err != nil {
return
}
respBody, err := ioutil.ReadAll(resp.Body)
if err != nil {
return
}
err = resp.Body.Close()
if err != nil {
return
}
var response struct {
ErrorId int64 `json:"errorId"`
ErrorCode string `json:"errorCode"`
ErrorDescription string `json:"errorDescription"`
TaskId int64 `json:"taskId"`
}
err = json.Unmarshal(respBody, &response)
if err != nil {
return
}
if response.ErrorId != 0 {
err = errors.New("anticaptcha: " + response.ErrorDescription)
return
}
taskId = response.TaskId
return
}
func (c *Client) getTaskResult(ctx context.Context, taskId int64, dst interface{}) (ready bool, err error) {
data := map[string]interface{}{
"clientKey": c.key,
"taskId": taskId,
}
sendBytes, err := json.Marshal(data)
if err != nil {
return
}
req, err := createRequest(ctx, http.MethodPost, fmt.Sprintf("https://%s/getTaskResult", c.host), bytes.NewReader(sendBytes))
if err != nil {
return
}
resp, err := c.client.Do(req)
if err != nil {
return
}
respBody, err := ioutil.ReadAll(resp.Body)
if err != nil {
return
}
err = resp.Body.Close()
if err != nil {
return
}
var response struct {
ErrorId int64 `json:"errorId"`
ErrorCode string `json:"errorCode"`
ErrorDescription string `json:"errorDescription"`
Status string `json:"status"`
Solution json.RawMessage `json:"solution"`
Cost string `json:"cost"`
IP string `json:"ip"`
CreateTime int64 `json:"createTime"`
EndTime int64 `json:"endTime"`
SolveCount int64 `json:"solveCount"`
}
err = json.Unmarshal(respBody, &response)
if err != nil {
return
}
if response.ErrorId != 0 {
ready = false
err = errors.New("anticaptcha: " + response.ErrorDescription)
return
}
switch response.Status {
case "ready":
ready = true
err = json.Unmarshal(response.Solution, dst)
return
case "processing":
ready = false
return
}
return
}
func (c *Client) fetchTask(ctx context.Context, taskId int64, dst interface{}) (err error) {
ticker := time.NewTicker(c.checkInterval)
time.Sleep(c.delay)
for {
select {
case <-ticker.C:
ready, err := c.getTaskResult(ctx, taskId, dst)
if err != nil {
return err
}
if ready {
return nil
}
case <-ctx.Done():
return ctx.Err()
}
}
}
//GetBalance retrieves the current account balance
func (c *Client) GetBalance(ctx context.Context) (balance float64, err error) {
data := map[string]interface{}{
"clientKey": c.key,
}
sendBytes, err := json.Marshal(data)
if err != nil {
return
}
req, err := createRequest(ctx, http.MethodPost, fmt.Sprintf("https://%s/getBalance", c.host), bytes.NewReader(sendBytes))
if err != nil {
return
}
resp, err := c.client.Do(req)
if err != nil {
return
}
respBody, err := ioutil.ReadAll(resp.Body)
if err != nil {
return
}
err = resp.Body.Close()
if err != nil {
return
}
var response struct {
ErrorId int64 `json:"errorId"`
ErrorCode string `json:"errorCode"`
ErrorDescription string `json:"errorDescription"`
Balance float64 `json:"balance"`
}
err = json.Unmarshal(respBody, &response)
if err != nil {
return
}
if response.ErrorId != 0 {
err = errors.New("anticaptcha: " + response.ErrorDescription)
return
}
balance = response.Balance
return
}
//ReportIncorrectImageCaptcha reports an incorrect captcha for a refund
func (c *Client) ReportIncorrectImageCaptcha(ctx context.Context, taskId int64) (err error) {
data := map[string]interface{}{
"clientKey": c.key,
"taskId": taskId,
}
sendBytes, err := json.Marshal(data)
if err != nil {
return
}
req, err := createRequest(ctx, http.MethodPost, fmt.Sprintf("https://%s/reportIncorrectImageCaptcha", c.host), bytes.NewReader(sendBytes))
if err != nil {
return
}
resp, err := c.client.Do(req)
if err != nil {
return
}
respBody, err := ioutil.ReadAll(resp.Body)
if err != nil {
return
}
err = resp.Body.Close()
if err != nil {
return
}
var response struct {
ErrorId int64 `json:"errorId"`
ErrorCode string `json:"errorCode"`
ErrorDescription string `json:"errorDescription"`
Status string `json:"status"`
}
err = json.Unmarshal(respBody, &response)
if err != nil {
return
}
if response.ErrorId != 0 {
err = errors.New("anticaptcha: " + response.ErrorDescription)
return
}
return
}
//ReportIncorrectRecaptcha reports an incorrect captcha for a refund
func (c *Client) ReportIncorrectRecaptcha(ctx context.Context, taskId int64) (err error) {
data := map[string]interface{}{
"clientKey": c.key,
"taskId": taskId,
}
sendBytes, err := json.Marshal(data)
if err != nil {
return
}
req, err := createRequest(ctx, http.MethodPost, fmt.Sprintf("https://%s/reportIncorrectRecaptcha", c.host), bytes.NewReader(sendBytes))
if err != nil {
return
}
resp, err := c.client.Do(req)
if err != nil {
return
}
respBody, err := ioutil.ReadAll(resp.Body)
if err != nil {
return
}
err = resp.Body.Close()
if err != nil {
return
}
var response struct {
ErrorId int64 `json:"errorId"`
ErrorCode string `json:"errorCode"`
ErrorDescription string `json:"errorDescription"`
Status string `json:"status"`
}
err = json.Unmarshal(respBody, &response)
if err != nil {
return
}
if response.ErrorId != 0 {
err = errors.New("anticaptcha: " + response.ErrorDescription)
return
}
return
}
func addProxyInfo(proxy *url.URL, to map[string]interface{}) error {
p, err := strconv.Atoi(proxy.Port())
if err != nil {
return err
}
to["proxyPort"] = p
to["proxyType"] = proxy.Scheme
to["proxyAddress"] = proxy.Hostname()
pp, hasPassword := proxy.User.Password()
pu := proxy.User.Username()
if pu != "" {
to["proxyLogin"] = pu
if hasPassword {
to["proxyPassword"] = pp
}
}
return nil
}
func createRequest(ctx context.Context, method, url string, body io.Reader) (r *http.Request, err error) {
r, err = http.NewRequestWithContext(ctx, method, url, body)
if err != nil {
return
}
r.Header.Set("User-Agent", "anticaptcha (github.com/aidenesco/anticaptcha)")
r.Header.Set("Content-Type", "application/json")
return
}