-
Notifications
You must be signed in to change notification settings - Fork 0
/
common.go
363 lines (334 loc) · 8.87 KB
/
common.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
package main
import (
"compress/gzip"
"crypto/tls"
"encoding/json"
"fmt"
"image"
"io"
"io/ioutil"
"net"
"net/http"
"net/http/httputil"
"strconv"
"strings"
"time"
)
var (
passenger = &PassengerDTO{}
Client = &http.Client{}
)
//获取联系人
func getPassengerDTO() {
for _, cdn := range Conf.CDN {
Info("开始获取联系人!")
body, err := DoForWardRequest(cdn, "POST", URLGetPassengerDTOs, nil)
if err != nil {
Error("getPassengerDTO DoForWardRequest error:", err)
continue
}
Debug("getPassengerDTO body:", body)
if !strings.Contains(body, "passenger_name") {
Error("获取联系人出错!!!!!!返回:", body)
continue
}
if err := json.Unmarshal([]byte(body), &passenger); err != nil {
Error("getPassengerDTO", cdn, err)
continue
} else {
Info(cdn, "获取成功!")
break
}
}
Debug(passenger)
}
func dyLoginJs(cdn string) error {
body, err := DoForWardRequest(cdn, "GET", URLLoginJs, nil)
if err != nil {
Error("dyLoginJs DoForWardRequest error:", err)
return err
}
Debug("dyLoginJs body:", body)
return nil
}
//获取新的cookie
func setNewCookie(cdn string) error {
req, err := http.NewRequest("GET", URLLoginJs, nil)
if err != nil {
Error("setNewCookie http.NewRequest error:", err)
return nil
}
con, err := NewForwardClientConn(cdn, req.URL.Scheme)
if err != nil {
Error("setNewCookie newForwardClientConn error:", err)
return nil
}
defer con.Close()
resp, err := con.Do(req)
if err != nil {
Error("setNewCookie con.Do error:", err)
return nil
}
defer resp.Body.Close()
login.Cookie = GetCookieFromRespHeader(resp)
Info("==" + login.Cookie + "==")
return nil
}
func dyQueryJs(cdn string) error {
body, err := DoForWardRequest(cdn, "GET", URLQueryJs, nil)
if err != nil {
Error("dyQueryJs DoForWardRequest error:", err)
return err
}
Debug("dyQueryJs body:", body)
return nil
}
func initQueryUserInfo(cdn string) error {
body, err := DoForWardRequest(cdn, "GET", URLInitQueryUserInfo, nil)
if err != nil {
Error("initQueryUserInfo DoForWardRequest error:", err)
return err
}
Debug("initQueryUserInfo body:", body)
return nil
}
func checkUser(cdn string) error {
body, err := DoForWardRequest(cdn, "POST", URLCheckUser, nil)
if err != nil {
Error("checkUser DoForWardRequest error:", err)
return err
}
Debug("checkUser body:", body)
return nil
}
func leftTicketInit(cdn string) error {
body, err := DoForWardRequest(cdn, "GET", URLInit, nil)
if err != nil {
Error("leftTicketInit DoForWardRequest error:", err)
return err
}
Debug("leftTicketInit body:", body)
return nil
}
//获取新的验证码图片
func GetLoginImage(cdn string) image.Image {
req, err := http.NewRequest("GET", URLLoginPassCode, nil)
if err != nil {
Error("GetImage http.NewRequest error:", err)
return nil
}
AddReqestHeader(req, "GET", map[string]string{"Accept": "image/webp,*/*;q=0.8"})
req.Header.Del("X-Requested-With")
con, err := NewForwardClientConn(cdn, req.URL.Scheme)
if err != nil {
Error("GetImage newForwardClientConn error:", err)
return nil
}
defer con.Close()
resp, err := con.Do(req)
if err != nil {
Error("GetImage con.Do error:", err)
return nil
}
defer resp.Body.Close()
img, s, err := image.Decode(resp.Body)
Debug("image type:", s)
if err != nil {
Error("GetImage image.Decode:", err)
return nil
}
return img
}
//从响应消息头里面获取cookie
func GetCookieFromRespHeader(resp *http.Response) (cookie string) {
cookies := []string{}
for k, v := range resp.Header {
Debug("k=", k, "v=", v)
if k == "Set-Cookie" {
for _, b := range v {
v := strings.Split(b, ";")[0]
cookies = append(cookies, v)
cookies = append(cookies, "; ")
}
}
}
d := strings.Join(cookies, "")
if len(d) < 2 {
return ""
}
cookie = d[:len(d)-2]
return
}
//转发
func DoForWardRequest(cdn, method, requestUrl string, body io.Reader) (string, error) {
return DoForWardRequestHeader(cdn, method, requestUrl, body, nil)
}
func PrepareClient() {
Client = &http.Client{
Transport: &http.Transport{
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
Dial: func(netw, addr string) (net.Conn, error) {
// deadline := time.Now().Add(25 * time.Second)
c, err := net.DialTimeout(netw, addr, time.Second*20)
// c, err := tls.Dial("tcp", Conf.CDN[0]+":443", &tls.Config{
// InsecureSkipVerify: true,
// })
if err != nil {
return nil, err
}
// c.SetDeadline(deadline)
return c, nil
},
},
}
}
func DoForWardRequestHeader(cdn, method, requestUrl string, body io.Reader, customHeader map[string]string) (string, error) {
req, err := http.NewRequest(method, requestUrl, body)
if err != nil {
Error("DoForWardRequest http.NewRequest error:", err)
return "", err
}
AddReqestHeader(req, method, customHeader)
// con, err := NewForwardClientConn(cdn, req.URL.Scheme)
// if err != nil {
// Error("DoForWardRequest newForwardClientConn error:", err)
// return "", err
// }
// defer con.Close()
// resp, err := con.Do(req)
resp, err := Client.Do(req)
if err != nil {
Error("DoForWardRequest con.Do error:", err)
return "", err
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
Error("DoForWardRequest StatusCode:", resp.StatusCode)
for k, v := range resp.Header {
Error("k=", k, "v=", v)
}
return "", err
}
content := ParseResponseBody(resp)
Debug("DoForWardRequest content:", content)
return content, nil
}
//添加头
func AddReqestHeader(request *http.Request, method string, customHeader map[string]string) {
// request.Header.Set("Connection", "Keep-Alive")
request.Header.Set("Host", "kyfw.12306.cn")
request.Header.Set("Accept-Encoding", "gzip,deflate")
request.Header.Set("Accept-Language", "zh-CN")
request.Header.Set("Accept", "*/*")
request.Header.Set("X-Requested-With", "XMLHttpRequest")
request.Header.Set("Referer", "https://kyfw.12306.cn/otn/confirmPassenger/initDc")
request.Header.Set("User-Agent", UserAgent)
if login.Cookie != "" {
request.Header.Set("Cookie", login.Cookie)
}
if method == "POST" {
request.Header.Set("Content-Length", fmt.Sprintf("%d", request.ContentLength))
request.Header.Set("Content-Type", "application/x-www-form-urlencoded")
}
for k, v := range customHeader {
request.Header.Set(k, v)
}
Info(request.Header)
}
//读取响应
func ParseResponseBody(resp *http.Response) string {
var body []byte
switch resp.Header.Get("Content-Encoding") {
case "gzip":
reader, err := gzip.NewReader(resp.Body)
if err != nil {
Error("gzip.NewReader:", err)
return ""
}
defer reader.Close()
bodyByte, err := ioutil.ReadAll(reader)
if err != nil {
Error("gzip ioutil.ReadAll:", err)
return ""
}
body = bodyByte
default:
bodyByte, err := ioutil.ReadAll(resp.Body)
if err != nil {
Error("ioutil.ReadAll:", err)
return ""
}
body = bodyByte
}
return string(body)
}
// clientConn, _ := newForwardClientConn("www.google.com","https")
// resp, err := clientConn.Do(req)
func NewForwardClientConn(forwardAddress, scheme string) (*httputil.ClientConn, error) {
if "http" == scheme {
conn, err := net.Dial("tcp", forwardAddress+":80")
if err != nil {
Error("newForwardClientConn net.Dial error:", err)
return nil, err
}
return httputil.NewProxyClientConn(conn, nil), nil
}
conn, err := tls.Dial("tcp", forwardAddress+":443", &tls.Config{
InsecureSkipVerify: true,
})
if err != nil {
Error("newForwardClientConn tls.Dial error:", err)
return nil, err
}
return httputil.NewProxyClientConn(conn, nil), nil
}
//获取车票余票信息
//getTicketNum("O008450822M010250252O008453240", "O0M0O0")
func GetTicketNum(yupiaoInfo, seat_types string) (ticketNum map[string]int) {
ticketNum = make(map[string]int)
//去除第一个类型,因为第一类型比较特殊,下面的str同样去掉
types := strings.Split(seat_types[2:len(seat_types)-1], "0")
//判断第一个类型
if strings.HasPrefix(yupiaoInfo, "10") {
num, _ := strconv.Atoi(yupiaoInfo[7:10])
ticketNum["无座"] = num
} else if strings.HasPrefix(yupiaoInfo, "O0") {
num, _ := strconv.Atoi(yupiaoInfo[7:10])
ticketNum["二等座"] = num
} else if strings.HasPrefix(yupiaoInfo, "60") {
num, _ := strconv.Atoi(yupiaoInfo[7:10])
ticketNum["高级软卧"] = num
} else {
num, _ := strconv.Atoi(yupiaoInfo[7:10])
ticketNum[yupiaoInfo[0:2]] = num
}
yupiaoInfo = yupiaoInfo[10:]
for _, v := range types {
key := v + "0"
start := strings.Index(yupiaoInfo, key) + 7
end := start + 3
num, _ := strconv.Atoi(yupiaoInfo[start:end])
switch key {
case "10":
ticketNum["硬座"] = num
case "20":
ticketNum["软座"] = num
case "30":
ticketNum["硬卧"] = num
case "40":
ticketNum["软卧"] = num
case "O0":
ticketNum["高铁无座"] = num
case "M0":
ticketNum["一等座"] = num
case "90":
ticketNum["商务座"] = num
case "P0":
ticketNum["特等座"] = num
default:
ticketNum[key] = num
}
yupiaoInfo = yupiaoInfo[end:]
}
return
}