-
Notifications
You must be signed in to change notification settings - Fork 0
/
chaojiying.go
272 lines (240 loc) · 6.27 KB
/
chaojiying.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
package chaojiying_sdk
import (
"crypto/tls"
"encoding/json"
"errors"
"io/ioutil"
"net/http"
"net/url"
"os"
"strconv"
"strings"
"time"
)
// IChaoJiYing ChaoJiYing sdk open interface
type ChaoJiYing interface {
UserInfo() (userInfo *userInfoResp, err error) // Get User info
IdentifyPic(
codeType int,
minLen int,
imgBase64 string,
) (
identifyPic *identifyPicResp,
err error,
) // Identify pictures
ReportError(picId string) (
reportError *reportErrorResp,
err error,
) // Error report and return to score
SetHttpsProxy(u string) // Set Https Proxy
SetTimeout(t time.Duration) // Set timeout (seconds)
SetUser(user string) // Set User account
SetPass(pass string) // Set User password
SetPass2(pass2 string) // Set DM5 value of user password (32-bit lower case)
SetSoftId(softId string) // Set Software ID: in the user center, the software ID can be generated
}
// chaoJiYing
type chaoJiYing struct {
c *http.Client
tr *http.Transport
httpsProxy string // HTTPS proxy
timeout time.Duration // Timeout (seconds)
user string // User account
pass string // User password
pass2 string // MD5 value of user password (32-bit lower case)
softId string // Software ID: in the user center, the software ID can be generated
}
// NewChaoJiYing
func NewChaoJiYing() ChaoJiYing {
// When using HTTPS, set no authentication
tr := &http.Transport{
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
}
var cjy ChaoJiYing = &chaoJiYing{
c: &http.Client{Transport: tr},
tr: tr,
user: os.Getenv("CHAOJIYING_USER"),
pass: os.Getenv("CHAOJIYING_PASS"),
pass2: os.Getenv("CHAOJIYING_PASS2"),
softId: os.Getenv("CHAOJIYING_SOFT_ID"),
}
cjy.SetTimeout(60) // Default 60 second timeout
return cjy
}
// SetHttpsProxy Set Https Proxy
func (c *chaoJiYing) SetHttpsProxy(u string) {
proxy, _ := url.Parse(u)
c.tr.Proxy = http.ProxyURL(proxy)
c.c.Transport = c.tr
}
// SetTimeout Set timeout (seconds)
func (c *chaoJiYing) SetTimeout(t time.Duration) {
s := t * time.Second
c.timeout = s
c.c.Timeout = s
}
// SetUser Set User account
func (c *chaoJiYing) SetUser(user string) {
c.user = user
}
// SetPass Set User password
func (c *chaoJiYing) SetPass(pass string) {
c.pass = pass
}
// SetPass2 Set DM5 value of user password (32-bit lower case)
func (c *chaoJiYing) SetPass2(pass2 string) {
c.pass2 = pass2
}
// SetSoftId Set Software ID: in the user center, the software ID can be generated
func (c *chaoJiYing) SetSoftId(softId string) {
c.softId = softId
}
// UserInfo Get User info
func (c *chaoJiYing) UserInfo() (userInfo *userInfoResp, err error) {
var req *http.Request
var resp *http.Response
var body []byte
params := url.Values{}
params.Add("user", c.user)
if c.pass != "" {
params.Add("pass", c.pass)
} else {
params.Add("pass2", c.pass2)
}
req, err = http.NewRequest(
"POST",
"http://upload.chaojiying.net/Upload/GetScore.php",
strings.NewReader(params.Encode()),
)
if err != nil {
return
}
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
req.Header.Set("User-Agent", "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.1; Trident/4.0)")
req.Header.Set("Connection", "Keep-Alive")
resp, err = c.c.Do(req)
if err != nil {
return
}
defer resp.Body.Close()
body, err = ioutil.ReadAll(resp.Body)
if err != nil {
return
}
if err = json.Unmarshal(body, &userInfo); err != nil {
return
}
if userInfo.ErrNo != 0 {
err = errors.New(userInfo.ErrStr)
return
}
return userInfo, nil
}
// IdentifyPic Identify pictures
func (c *chaoJiYing) IdentifyPic(codeType int, minLen int, imgBase64 string) (identifyPic *identifyPicResp, err error) {
var req *http.Request
var resp *http.Response
var body []byte
params := url.Values{}
params.Add("user", c.user)
if c.pass != "" {
params.Add("pass", c.pass)
} else {
params.Add("pass2", c.pass2)
}
params.Add("softid", c.softId)
params.Add("codetype", strconv.Itoa(codeType))
params.Add("len_min", strconv.Itoa(minLen))
params.Add("file_base64", imgBase64)
req, err = http.NewRequest(
"POST",
"http://upload.chaojiying.net/Upload/Processing.php",
strings.NewReader(params.Encode()),
)
if err != nil {
return
}
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
req.Header.Set("User-Agent", "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.1; Trident/4.0)")
req.Header.Set("Connection", "Keep-Alive")
resp, err = c.c.Do(req)
if err != nil {
return
}
defer resp.Body.Close()
body, err = ioutil.ReadAll(resp.Body)
if err != nil {
return
}
if err = json.Unmarshal(body, &identifyPic); err != nil {
return
}
if identifyPic.ErrNo != 0 {
err = errors.New(identifyPic.ErrStr)
return
}
return identifyPic, nil
}
// ReportError Error report and return to score
func (c *chaoJiYing) ReportError(picId string) (reportError *reportErrorResp, err error) {
var req *http.Request
var resp *http.Response
var body []byte
params := url.Values{}
params.Add("user", c.user)
if c.pass != "" {
params.Add("pass", c.pass)
} else {
params.Add("pass2", c.pass2)
}
params.Add("softid", c.softId)
params.Add("id", picId)
req, err = http.NewRequest(
"POST",
"http://upload.chaojiying.net/Upload/ReportError.php",
strings.NewReader(params.Encode()),
)
if err != nil {
return
}
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
req.Header.Set("User-Agent", "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.1; Trident/4.0)")
req.Header.Set("Connection", "Keep-Alive")
resp, err = c.c.Do(req)
if err != nil {
return
}
defer resp.Body.Close()
body, err = ioutil.ReadAll(resp.Body)
if err != nil {
return
}
if err = json.Unmarshal(body, &reportError); err != nil {
return
}
if reportError.ErrNo != 0 {
err = errors.New(reportError.ErrStr)
return
}
return reportError, nil
}
// identifyPicResp
type identifyPicResp struct {
ErrNo int `json:"err_no"`
ErrStr string `json:"err_str"`
PicId string `json:"pic_id"`
PicStr string `json:"pic_str"`
Md5 string `json:"md5"`
}
// reportErrorResp
type reportErrorResp struct {
ErrNo int `json:"err_no"`
ErrStr string `json:"err_str"`
}
// scoreResp
type userInfoResp struct {
ErrNo int `json:"err_no"`
ErrStr string `json:"err_str"`
TiFen int `json:"tifen"`
TiFenLock int `json:"tifen_lock"`
}