This repository has been archived by the owner on Feb 20, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 9
/
common.go
348 lines (324 loc) · 8.56 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
package main
import (
"bytes"
"encoding/gob"
"encoding/json"
"fmt"
"io/ioutil"
"math/rand"
"net/http"
"os"
"regexp"
"strconv"
"strings"
"time"
"github.com/Unknwon/goconfig"
"github.com/sirupsen/logrus"
"golang.org/x/text/encoding/simplifiedchinese"
)
// SkuMeta - Sku Meta
type SkuMeta struct {
ID string `json:"skuid"`
Name string `json:"name"`
VenderID string `json:"venderId"`
StockURL string `json:"stockurl"`
Cat string `json:"cat"`
Num int `json:"num"`
}
// WxMessage - WeChat Markdown Message
type WxMessage struct {
MsgType string `json:"msgtype"`
Markdown struct {
Content string `json:"content"`
} `json:"markdown"`
}
// FsMessage - WeChat Markdown Message
type FsMessage struct {
Title string `json:"title"`
Content string `json:"text"`
}
var (
// skuState map[string]bool
randSrc rand.Source
logger *logrus.Logger
tr *http.Transport
config map[string]string
skuMetas map[string]*SkuMeta
ch chan os.Signal
debug bool = false
refresh bool = false
verbose bool = false
stop bool = false
areaNext string = ""
waittime int = 60
speed float64 = 1
orderdelay int = 30
command string
justListen bool = false
)
// VkLogHook to send error logs via bot.
type VkLogHook struct{}
// Fire - VkLogHook::Fire
func (hook *VkLogHook) Fire(entry *logrus.Entry) error {
line, err := entry.String()
if err != nil {
fmt.Fprintf(os.Stderr, "Unable to read entry, %v", err)
return err
}
if entry.Level == logrus.ErrorLevel {
sendBotMsg(line)
}
return nil
}
// Levels - VkLogHook::Levels
func (hook *VkLogHook) Levels() []logrus.Level {
return logrus.AllLevels
}
// NewVkLogHook - Create a hook to be added to an instance of logger
func NewVkLogHook() *VkLogHook {
return &VkLogHook{}
}
func loadConf() (map[string]string, error) {
config = make(map[string]string, 5)
cfg, err := goconfig.LoadConfigFile("config.conf")
if err != nil {
logger.Errorln("[-] Load conf file 'config.conf' error ", err)
return config, err
}
// core
config["cookies"], err = cfg.GetValue("core", "cookies")
if err != nil {
logger.Warningln("[-] Load conf core.cookies error")
config["cookies"] = ""
}
config["wxBotKey"], err = cfg.GetValue("core", "wxbotkey")
if err != nil {
config["wxBotKey"] = ""
}
config["fsBotKey"], err = cfg.GetValue("core", "fsbotkey")
if err != nil {
config["fsBotKey"] = ""
}
config["paymentpwd"], err = cfg.GetValue("core", "paymentpwd")
if err != nil {
config["paymentpwd"] = ""
}
config["area"], err = cfg.GetValue("core", "area")
if err != nil {
logger.Fatalln("[-] Load conf core.area error ", err)
return config, err
}
config["webhook"], err = cfg.GetValue("core", "webhook")
if err != nil {
config["webhook"] = ""
}
config["waittime"], err = cfg.GetValue("core", "waittime")
if err != nil {
config["waittime"] = "60"
}
config["speed"], err = cfg.GetValue("core", "speed")
if err != nil {
config["speed"] = "1"
}
config["orderdelay"], err = cfg.GetValue("core", "orderdelay")
if err != nil {
config["orderdelay"] = "30"
}
config["command"], err = cfg.GetValue("core", "command")
if err != nil {
config["command"] = ""
}
areaNext = strings.Replace(config["area"], ",", "_", -1)
waittime, err = strconv.Atoi(config["waittime"])
if err != nil {
waittime = 60
}
speed, err = strconv.ParseFloat(config["speed"], 64)
if err != nil {
speed = 1
}
orderdelay, err = strconv.Atoi(config["orderdelay"])
if err != nil {
orderdelay = 30
}
return config, nil
}
func httpPostJSON(sendURL string, sendData []byte) bool {
client := &http.Client{Transport: tr}
req, err := http.NewRequest("POST", sendURL, bytes.NewBuffer(sendData))
if err != nil {
logger.Errorln("[-] HPJ::NewRequest:", err)
return false
}
req.Header.Set("Content-Type", "application/json")
req.Header.Set("Charset", "UTF-8")
resp, err := client.Do(req)
if err != nil {
logger.Errorln("[-] HPJ::DoRequest:", err)
return false
}
defer resp.Body.Close()
return true
}
func sendWeChatBotMsg(content string) {
msgJSON := WxMessage{
MsgType: "markdown",
Markdown: struct {
Content string `json:"content"`
}{Content: content},
}
msg, _ := json.Marshal(msgJSON)
sendURL := fmt.Sprintf("%s%s", "https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=", config["wxBotKey"])
httpPostJSON(sendURL, msg)
}
func sendFeishuBotMsg(content string) {
msgJSON := FsMessage{
Title: "JD Face Mask",
Content: content,
}
msg, _ := json.Marshal(msgJSON)
sendURL := fmt.Sprintf("%s%s", "https://open.feishu.cn/open-apis/bot/hook/", config["fsBotKey"])
httpPostJSON(sendURL, msg)
}
func sendBotMsg(content string) {
if len(config["fsBotKey"]) > 0 {
sendFeishuBotMsg(content)
}
if len(config["wxBotKey"]) > 0 {
sendWeChatBotMsg(content)
}
}
func getCallback() string {
return fmt.Sprintf("jQuery%07v", rand.New(randSrc).Int31n(1000000))
}
func getRandomTs() string {
return fmt.Sprintf("%d", time.Now().UnixNano()/1e6)
}
func setReqHeader(req *http.Request) {
req.Header.Set("User-Agent", "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_3) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/13.0.5 Safari/605.1.15")
req.Header.Set("Connection", "keep-alive")
}
func loadSkuMetaCache() bool {
file, err := os.Open(".skumeta")
if err != nil {
logger.Errorln("[-] LSMC::OpenFile", err.Error())
return false
}
dec := gob.NewDecoder(file)
skuMetas = make(map[string]*SkuMeta)
err = dec.Decode(&skuMetas)
if err != nil {
logger.Errorln("[-] LSMC::Decode", err.Error())
return false
}
logger.Infoln("[+] LSMC Success")
return true
}
func saveSkuMetaCache() bool {
file, err := os.Create(".skumeta")
if err != nil {
logger.Errorln("[-] SSMC::CreateFile", err.Error())
return false
}
enc := gob.NewEncoder(file)
err = enc.Encode(skuMetas)
if err != nil {
logger.Errorln("[-] SSMC::Enccode", err.Error())
return false
}
logger.Infoln("[+] SSMC Success")
return true
}
func loadMask() (map[string]int, error) {
masks := make(map[string]int)
f, err := ioutil.ReadFile("masks.json")
if err != nil {
logger.Fatalln("[-]", err.Error())
return masks, err
}
err = json.Unmarshal(f, &masks)
if err != nil {
logger.Errorln("[-]", err.Error())
return masks, err
}
return masks, nil
}
func getMetaByItem(metaClient *http.Client, skuid string) (*SkuMeta, error) {
var (
re *regexp.Regexp
res [][]string
)
meta := &SkuMeta{ID: skuid}
itemURL := fmt.Sprintf("https://item.jd.com/%s.html", skuid)
logger.Infoln("[+] ItemURL :", itemURL)
req, err := http.NewRequest("GET", itemURL, nil)
if err != nil {
logger.Errorln("[-] GMBI::NewRequest :", err.Error())
return meta, err
}
setReqHeader(req)
resp, err := metaClient.Do(req)
if err != nil {
logger.Errorln("[-] GMBI::DoRequest:: ", err.Error())
return meta, err
}
defer resp.Body.Close()
reader := simplifiedchinese.GB18030.NewDecoder().Reader(resp.Body)
body, err := ioutil.ReadAll(reader)
if err != nil {
logger.Errorln("[-] GMBI::ReadBody :", err.Error())
return meta, err
}
bodyStr := string(body)
// Cat
re = regexp.MustCompile(`(?m)cat:.\[(\d+),(\d+),(\d+)\]`)
res = re.FindAllStringSubmatch(bodyStr, -1)
if len(res) > 0 && len(res[0]) == 4 {
meta.Cat = fmt.Sprintf("%s,%s,%s", res[0][1], res[0][2], res[0][3])
logger.Infoln("[+] GMBI::Cat :", meta.Cat)
}
// VenderId
re = regexp.MustCompile(`(?m)venderId:(\d+),`)
res = re.FindAllStringSubmatch(bodyStr, -1)
if len(res) > 0 && len(res[0]) == 2 {
meta.VenderID = res[0][1]
logger.Infoln("[+] GMBI::VenderID :", meta.VenderID)
}
// Name/Title
re = regexp.MustCompile(`(?m)\<title\>(.*?)\<\/title\>`)
res = re.FindAllStringSubmatch(bodyStr, -1)
if len(res) > 0 && len(res[0]) == 2 {
meta.Name = res[0][1]
logger.Infoln("[+] GMBI::Name :", meta.Name)
}
// StockURL
meta.StockURL = fmt.Sprintf("https://c0.3.cn/stock?skuId=%s&area=%s&venderId=%s&buyNum=1&cat=%s&callback=%s", skuid, areaNext, meta.VenderID, meta.Cat, getCallback())
logger.Infoln("[+] GMBI::StockURL :", meta.StockURL)
return meta, nil
}
func getSkuMeta(masks map[string]int) bool {
metaClient := &http.Client{
Transport: tr,
}
skuMetas = make(map[string]*SkuMeta)
for skuid, num := range masks {
meta, err := getMetaByItem(metaClient, skuid)
if err != nil {
logger.Errorln("[-] Sku [", skuid, "] Error :", err.Error())
continue
}
meta.Num = num
skuMetas[skuid] = meta
}
return len(skuMetas) != 0
}
func getCallbackBody(body []byte, msg string) []byte {
if len(body) > 14 {
body = body[14 : len(body)-1]
} else {
msg = fmt.Sprintf("[!] %s::GetCallbackBody Error", msg)
logger.Errorln(msg, string(body))
return nil
}
return body
}