/
login.go
180 lines (146 loc) · 4.39 KB
/
login.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
package gochat
import (
"time"
"strconv"
"regexp"
"errors"
"strings"
"gochat/utils"
"math/rand"
"encoding/json"
)
type pushLoginResponse struct {
Msg string
Ret string
Uuid string
}
func (weChat *WeChat) beginLogin() error {
var err error
weChat.Uuid, weChat.baseRequest, weChat.passTicket, weChat.httpClient.Cookies, weChat.host, err = weChat.storage.getData()
if err != nil {
weChat.Uuid, err = weChat.getUuid()
if err != nil {
return err
}
weChat.triggerGenUuidEvent(weChat.Uuid)
weChat.logger.Println("[Info] Uuid=" + weChat.Uuid)
tip := 1
redirectUrl := ""
for {
status, result, err := weChat.isAuth(tip)
if err != nil {
weChat.logger.Println("[Error] GetRedirectUrl Error :" + err.Error())
time.Sleep(time.Second * time.Duration(1))
continue
}
if 200 == status {
redirectUrl = result
weChat.logger.Println("[Info] Redirect=" + redirectUrl)
weChat.triggerConfirmAuthEvent(redirectUrl)
break
}
if 201 == status {
tip = 0
weChat.logger.Println("[Info] Scan Code")
weChat.triggerScanCodeEvent(result)
}
}
weChat.host = utils.GetHostByUrl(redirectUrl)
err = weChat.doLogin(redirectUrl)
if err != nil {
return err
}
weChat.storage.setData(weChat.Uuid, weChat.baseRequest, weChat.passTicket, weChat.httpClient.Cookies, weChat.host)
}
weChat.logger.Println("[Info] Login.")
weChat.triggerLoginEvent(weChat.baseRequest.DeviceID)
return nil
}
// 获取Uuid
func (weChat *WeChat) getUuid() (string, error) {
getUuidApiUrl := weChatApi["getUuidApi"] + utils.GetUnixMsTime()
content, err := weChat.httpClient.get(getUuidApiUrl, time.Second * 5, &httpHeader{
Host: "login.wx2.qq.com",
Referer: "https://wx2.qq.com/?&lang=zh_CN",
})
if err != nil {
return "", err
}
reg, err := regexp.Compile(`window.QRLogin.code = 200; window.QRLogin.uuid = "(.+)"`)
if err != nil {
return "", err
}
uuidArr := reg.FindSubmatch([]byte(content))
if len(uuidArr) != 2 {
return "", errors.New("Uuid get failed")
}
return string(uuidArr[1]), nil
}
// 判断是否已授权登陆,获取redirectUrl
func (weChat *WeChat) isAuth(tip int) (int, string, error) {
loginPollApi := strings.Replace(weChatApi["loginApi"], "{uuid}", weChat.Uuid, 1)
loginPollApi = strings.Replace(loginPollApi, "{tip}", strconv.Itoa(tip), 1)
loginPollApi = strings.Replace(loginPollApi, "{time}", utils.GetUnixMsTime(), 1)
content, err := weChat.httpClient.get(loginPollApi, time.Second * 30, &httpHeader{
Host: "login.wx2.qq.com",
Referer: "https://wx2.qq.com/?&lang=zh_CN",
})
if err != nil {
return 0, "", err
}
regRedirectUri, err := regexp.Compile(`window.redirect_uri="(.+)";`)
if err != nil {
return 0, "", err
}
redirectUriArr := regRedirectUri.FindSubmatch([]byte(content))
if len(redirectUriArr) == 2 {
return 200, string(redirectUriArr[1]), nil
}
regScanCode, err := regexp.Compile(`window.code=201;window.userAvatar = '(.+)';`)
if err != nil {
return 0, "", err
}
userAvatarArr := regScanCode.FindSubmatch([]byte(content))
if len(userAvatarArr) == 2 {
return 201, string(userAvatarArr[1]), nil
}
return 0, "", nil
}
// 请求redirectUrl 登录
func (weChat *WeChat) doLogin(redirectUrl string) error {
content, err := weChat.httpClient.get(redirectUrl + "&fun=new&version=v2&lang=zh_CN", time.Second * 5, &httpHeader{
Host: weChat.host,
Referer: "https://"+ weChat.host +"/?&lang=zh_CN",
})
if err != nil {
return err
}
var max int64 = 999999999999999
var min int64 = 100000000000000
weChat.baseRequest.DeviceID = "e" + strconv.Itoa(int(rand.Int63n(max-min) + min))
weChat.baseRequest.Sid, weChat.baseRequest.Uin, weChat.baseRequest.Skey, weChat.passTicket, err = utils.AnalysisLoginXml(content)
return err
}
// 绑定登录
func (weChat *WeChat) pushLogin() (string, error) {
pushLoginApi := strings.Replace(weChatApi["pushLoginApi"], "{uin}", weChat.baseRequest.Uin, 1)
content, err := weChat.httpClient.get(pushLoginApi, time.Second * 5, &httpHeader{
Host: weChat.host,
Referer: "https://"+ weChat.host +"/?&lang=zh_CN",
})
if err != nil {
return "", err
}
var pushLoginResp pushLoginResponse
err = json.Unmarshal([]byte(content), &pushLoginResp)
if err != nil {
return "", err
}
if pushLoginResp.Ret != "0" || "" == pushLoginResp.Uuid{
return "", errors.New("Push Login Failed")
}
return pushLoginResp.Uuid, nil
}
// 退出
func (weChat *WeChat) Logout() {
}