-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathclient.go
108 lines (101 loc) · 3.06 KB
/
client.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
package main
import (
"encoding/json"
"errors"
"fmt"
"io"
"io/ioutil"
"math"
"math/rand"
"net/http"
"regexp"
"strconv"
"time"
)
type smzdm interface {
getPostID() []int
smzdmSign() (bool, error)
smzdmCommit(int, string) (bool, error)
}
type cracker struct {
index int
account account
}
// NewCracker ...
func NewCracker(index int, account account) *cracker {
return &cracker{index: index, account: account}
}
func (c *cracker) handle(method, url, referer string, body *io.Reader) []byte {
client := &http.Client{}
req, err := http.NewRequest(method, url, nil)
checkError(err)
header := http.Header{}
if referer == "" {
referer = "https://www.smzdm.com"
}
header.Add("Referer", referer)
header.Add("User-Agent", c.account.UserAgent)
header.Add("Cookie", c.account.Cookies)
req.Header = header
resp, err := client.Do(req)
checkError(err)
defer resp.Body.Close()
content, err := ioutil.ReadAll(resp.Body)
checkError(err)
return content
}
func (c *cracker) getPostID() []int {
postID := make([]int, conf.PostCommentMax)
mtrand := rand.New(rand.NewSource(time.Now().UnixNano()))
randNum := int(30.0 + math.Round(mtrand.Float64()*40.0))
url := "https://faxian.smzdm.com/h1s0t0f37c0p" + strconv.Itoa(randNum)
content := c.handle("GET", url, "https://www.smzdm.com/jingxuan/", nil)
reg, err := regexp.Compile(`articleid="\d_(\d+)"`)
checkError(err)
postIDMatch := reg.FindAllSubmatch(content, conf.PostCommentMax)
for k, v := range postIDMatch {
for k2, v2 := range v {
if k2 == 1 {
id, err := strconv.Atoi(string(v2))
checkError(err)
postID[k] = id
}
}
}
return postID
}
func (c *cracker) smzdmSign() (bool, error) {
jsonData := &signJson{}
ts := time.Now().UnixNano()
url := fmt.Sprintf("https://zhiyou.smzdm.com/user/checkin/jsonp_checkin?callback=jQuery112409568846254764496_%d&_=%d", ts, ts)
content := c.handle("GET", url, "http://www.smzdm.com/qiandao/", nil)
reg := regexp.MustCompile(`^jQuery\d+_\d+\((.*?)\)$`)
jsonStr := reg.ReplaceAll(content, []byte(`$1`))
json.Unmarshal(jsonStr, jsonData)
if jsonData.ErrorCode == 0 {
jsonData.Index = c.index
jsonData.Account = c.account
jsonData.Time = time.Now()
signResult = append(signResult, *jsonData)
return true, nil
}
return false, errors.New(string(jsonStr))
}
func (c *cracker) smzdmCommit(postID int, comment string) (bool, error) {
jsonData := &commentJson{}
ts := time.Now().UnixNano()
url := fmt.Sprintf("https://zhiyou.smzdm.com/user/comment/ajax_set_comment?callback=jQuery111006551744323225079_%d&type=3&pid=%d&parentid=0&vote_id=0&vote_type=&vote_group=&content=%s&_=%d", ts, postID, comment, ts)
content := c.handle("GET", url, "https://zhiyou.smzdm.com/user/submit/", nil)
reg := regexp.MustCompile(`^jQuery\d+_\d+\((.*?)\)$`)
jsonStr := reg.ReplaceAll(content, []byte(`$1`))
json.Unmarshal(jsonStr, jsonData)
if jsonData.ErrorCode == 0 {
jsonData.Index = c.index
jsonData.Account = c.account
jsonData.PostID = postID
jsonData.Time = time.Now()
commentResult = append(commentResult, *jsonData)
return true, nil
}
return false, errors.New(string(jsonStr))
}