-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcapbypass.go
102 lines (84 loc) · 2.28 KB
/
capbypass.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
package capbypass
import (
"bytes"
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"net/http"
"time"
)
const (
BASEURL = "https://capbypass.com/api"
TASK_TIMEOUT = 250
)
func New(apiKey string) *CapBypass {
return &CapBypass{apiKey: apiKey}
}
func (c *CapBypass) Balance() (*CapBypassResponse, error) {
jsonPayload := &CapBypassPayload{
ClientKey: c.apiKey,
}
b, _ := json.Marshal(jsonPayload)
resp, err := http.Post(BASEURL+"/getBalance", "application/json", bytes.NewReader(b))
if err != nil {
return nil, err
}
defer resp.Body.Close()
var response CapBypassResponse
if err := json.NewDecoder(resp.Body).Decode(&response); err != nil {
return nil, err
}
return &response, nil
}
func (c *CapBypass) Solve(task CapBypassPayload) (*CapBypassResponse, error) {
_ = checkTask(task)
payload := &CapBypassPayload{
ClientKey: c.apiKey,
Task: task.Task,
}
data, _ := json.Marshal(payload)
createTaskResp, err := http.Post(BASEURL+"/createTask", "application/json", bytes.NewReader(data))
if err != nil {
return nil, err
}
defer createTaskResp.Body.Close()
bodystring, err := ioutil.ReadAll(createTaskResp.Body)
if err != nil {
return nil, err
}
var createTaskResponse CapBypassResponse
if err := json.Unmarshal(bodystring, &createTaskResponse); err != nil {
return nil, err
}
if createTaskResponse.ErrorMessage == "Not enough credits" {
return nil, fmt.Errorf(createTaskResponse.ErrorMessage)
}
if createTaskResp.StatusCode != 200 {
return nil, fmt.Errorf(createTaskResponse.ErrorMessage)
}
for i := 0; i < TASK_TIMEOUT; i++ {
statusPayload := &CapBypassPayload{
ClientKey: c.apiKey,
TaskId: createTaskResponse.TaskId,
}
statusData, _ := json.Marshal(statusPayload)
statusResp, err := http.Post(BASEURL+"/getTaskResult", "application/json", bytes.NewReader(statusData))
time.Sleep(time.Second * 1)
if err != nil {
return nil, err
}
defer statusResp.Body.Close()
var statusResponse CapBypassResponse
if err := json.NewDecoder(statusResp.Body).Decode(&statusResponse); err != nil {
return nil, err
}
if statusResponse.Status == "DONE" {
return &statusResponse, nil
}
if statusResponse.ErrorId == 1 {
return nil, errors.New(statusResponse.Errordescription)
}
}
return nil, errors.New("could not solve")
}