/
chengdu.go
166 lines (142 loc) · 3.69 KB
/
chengdu.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
package adapters
import (
"encoding/base64"
"encoding/json"
"fmt"
"io"
"io/ioutil"
"net/http"
"net/url"
"strconv"
"strings"
"time"
"github.com/arnie97/emu-log/common"
)
const (
chengduKey = "kyd@info"
chengduCode = "kyd_lyk0351"
chengduLogin = `["10.192.111.79","hhs","hhs"]`
)
type Chengdu struct{}
func init() {
Register(Chengdu{})
}
func (Chengdu) Code() string {
return "W"
}
func (Chengdu) Name() string {
return "川之味"
}
func (Chengdu) URL() (pattern string, mockValue interface{}) {
return "https://kyd.cd-rail.com?code=%s", nil
}
func (Chengdu) AlwaysOn() bool {
return false
}
func (a Chengdu) Info(serial string) (info JSONObject, err error) {
const api = "https://kyd.cd-rail.com/KYDMS_S/WeixinServlet"
var form url.Values
if form, err = a.SerialEncrypt(api, serial); err != nil {
return
}
var resp *http.Response
if resp, err = httpClient(a).PostForm(api, form); err != nil {
return
}
defer resp.Body.Close()
result := []*JSONObject{&info}
if err = a.InfoDecrypt(resp.Body, &result); err != nil {
return
}
return
}
// SerialEncrypt converts the QR code tuple to a form,
// and encrypts the form values in DES-ECB cipher mode.
func (Chengdu) SerialEncrypt(api, serial string) (ret url.Values, err error) {
components := strings.Split(serial, ",")
if len(components) != 6 {
err = fmt.Errorf("invalid serial number tuple: %s", serial)
return
}
var (
today = time.Now().Format("2006/01/02")
unitClass = components[1]
unitSerial = components[2]
seatCoach int
sqlParams []byte
)
if seatCoach, err = strconv.Atoi(components[3]); err != nil {
return
}
sqlParamsTuple := []interface{}{unitSerial, seatCoach, today, unitClass}
if sqlParams, err = json.Marshal(sqlParamsTuple); err != nil {
return
}
base64Encrypt := func(data string) string {
return base64.StdEncoding.EncodeToString(
common.DesEcbEncrypt([]byte(data), []byte(chengduKey)))
}
ret = url.Values{
"code": {base64Encrypt(chengduCode)},
"sql": {base64Encrypt(string(sqlParams))},
"url": {api},
"type": {"POST"},
"where": {base64Encrypt("[]")},
"order": {base64Encrypt("[]")},
"login": {base64Encrypt(chengduLogin)},
}
return
}
// InfoDecrypt decrypts the base64 encoded cipher text with DES-ECB,
// and unmarshals the plain text result into the given structure.
func (Chengdu) InfoDecrypt(src io.Reader, dest interface{}) (err error) {
defer common.Catch(&err)
var (
base64Encoded []byte
cipherText []byte
)
if base64Encoded, err = ioutil.ReadAll(src); err != nil {
return
}
base64Str := string(base64Encoded)
if cipherText, err = base64.StdEncoding.DecodeString(base64Str); err != nil {
return
}
plainText := common.DesEcbDecrypt(cipherText, []byte(chengduKey))
return json.Unmarshal(plainText, dest)
}
func (Chengdu) TrainNo(info JSONObject) (trains []TrainSchedule, err error) {
defer common.Catch(&err)
var (
trainListBytes = []byte(info["TRAIN_ORDER_TIME"].(string))
trainList [][2]string
)
if err = json.Unmarshal(trainListBytes, &trainList); err != nil {
return
}
for _, pair := range trainList {
trains = append(trains, TrainSchedule{
TrainNo: pair[0],
Date: strings.Replace(pair[1], "/", "-", 2),
})
}
return
}
func (Chengdu) UnitNo(qrTuple string, info JSONObject) (unitNo string, err error) {
retrievedUnitNo, _ := info["TRAIN_UNDER"].(string)
if len(retrievedUnitNo) == 0 {
return
}
var (
components = strings.Split(qrTuple, ",")
unitClass = components[1]
unitSerial = components[2]
)
unitNo = unitClass + unitSerial
if common.ApproxEqualUnitNo(unitNo, retrievedUnitNo) {
return
}
// the number in the QR code does not match the one in the HTTP response
unitNo = "CRH@" + retrievedUnitNo
return
}