-
Notifications
You must be signed in to change notification settings - Fork 1
/
wechat.go
333 lines (313 loc) · 8.41 KB
/
wechat.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
package wwdk
import (
"encoding/json"
"fmt"
"github.com/ikuiki/wwdk/storer"
"log"
// "crypto/tls"
"errors"
"net"
"net/http"
"net/http/cookiejar"
"github.com/ikuiki/wwdk/datastruct"
"github.com/ikuiki/wwdk/tool"
"net/url"
"time"
)
// wechatCookie 微信登陆后的cookie凭据,登陆后的消息同步等操作需要此凭据
type wechatCookie struct {
Wxsid string
Wxuin string // 应该是用户的唯一识别号,同一个用户每次登陆此字段都相同
Uvid string
DataTicket string
AuthTicket string
// loadTime string // 登陆时间(10位时间戳字符串)
}
type wechatLoginInfo struct {
cookie wechatCookie
syncKey *datastruct.SyncKey
sKey string
PassTicket string
}
// storeLoginInfo 用于储存的登录信息
type storeLoginInfo struct {
Cookies map[string][]*http.Cookie
Cookie wechatCookie
SyncKey *datastruct.SyncKey
SKey string
PassTicket string
RunInfo WechatRunInfo // 运行统计信息
DeviceID string // 由客户端生成,为e+15位随机数
User *datastruct.User
}
// WechatRunInfo 微信运行信息
type WechatRunInfo struct {
// StartAt 程序启动的时间
StartAt time.Time
// LoginAt 程序登陆的时间
LoginAt time.Time
// SyncCount 同步次数
SyncCount uint64
// ContactModifyCount 联系人修改计数器
ContactModifyCount uint64
// MessageCount 消息计数器
MessageCount uint64
// MessageRecivedCount 收到消息计数器
MessageRecivedCount uint64
// MessageSentCount 发送消息计数器
MessageSentCount uint64
// MessageRevokeCount 撤回消息计数器
MessageRevokeCount uint64
// MessageRevokeRecivedCount 收到撤回消息计数器
MessageRevokeRecivedCount uint64
// MessageRevokeSentCount 发送撤回消息计数器
MessageRevokeSentCount uint64
// PanicCount panic计数器
PanicCount uint64
}
// WechatWeb 微信网页版客户端实例
type WechatWeb struct {
userAgent string
contactList map[string]datastruct.Contact
user *datastruct.User
syncHost string
messageHook map[datastruct.MessageType][]interface{}
modContactHook []interface{}
client *http.Client
loginInfo wechatLoginInfo // 登陆信息
runInfo WechatRunInfo // 运行统计信息
deviceID string // 由客户端生成,为e+15位随机数
loginStorer storer.Storer // 存储器,如果有赋值,则用于记录登录信息
}
// NewWechatWeb 生成微信网页版客户端实例
func NewWechatWeb(configs ...interface{}) (wxweb *WechatWeb, err error) {
jar, err := cookiejar.New(nil)
if err != nil {
return &WechatWeb{}, err
}
w := &WechatWeb{
contactList: make(map[string]datastruct.Contact),
userAgent: "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/56.0.2924.87 Safari/537.36",
deviceID: "e" + tool.GetRandomStringFromNum(15),
messageHook: make(map[datastruct.MessageType][]interface{}),
client: &http.Client{
Transport: &http.Transport{
Dial: (&net.Dialer{
Timeout: 1 * time.Minute,
}).Dial,
TLSHandshakeTimeout: 1 * time.Minute,
// TLSClientConfig: &tls.Config{
// InsecureSkipVerify: true,
// },
// Proxy: func(_ *http.Request) (*url.URL, error) {
// return url.Parse("http://127.0.0.1:8888") //根据定义Proxy func(*Request) (*url.URL, error)这里要返回url.URL
// },
},
Jar: jar,
Timeout: 1 * time.Minute,
},
runInfo: WechatRunInfo{
StartAt: time.Now(),
},
}
for _, c := range configs {
switch c.(type) {
case storer.Storer:
w.loginStorer = c.(storer.Storer)
default:
return &WechatWeb{}, fmt.Errorf("unknown config: %#v", c)
}
}
return w, nil
}
func (wxwb *WechatWeb) baseRequest() (baseRequest *datastruct.BaseRequest) {
return &datastruct.BaseRequest{
Uin: wxwb.loginInfo.cookie.Wxuin,
Sid: wxwb.loginInfo.cookie.Wxsid,
Skey: wxwb.loginInfo.sKey,
DeviceID: wxwb.deviceID,
}
}
// GetContact 根据username获取联系人
func (wxwb *WechatWeb) GetContact(username string) (contact datastruct.Contact, err error) {
contact, ok := wxwb.contactList[username]
if !ok {
err = errors.New("User not found")
}
return
}
// GetContactByAlias 根据Alias获取联系人
func (wxwb *WechatWeb) GetContactByAlias(alias string) (contact datastruct.Contact, err error) {
found := false
for _, v := range wxwb.contactList {
if v.Alias == alias {
contact = v
found = true
}
}
if !found {
err = errors.New("User not found")
}
return
}
// GetContactByNickname 根据昵称获取用户名
func (wxwb *WechatWeb) GetContactByNickname(nickname string) (contact datastruct.Contact, err error) {
found := false
for _, v := range wxwb.contactList {
if v.NickName == nickname {
contact = v
found = true
}
}
if !found {
err = errors.New("User not found")
}
return
}
// GetContactByRemarkName 根据备注获取用户名
func (wxwb *WechatWeb) GetContactByRemarkName(remarkName string) (contact datastruct.Contact, err error) {
found := false
for _, v := range wxwb.contactList {
if v.RemarkName == remarkName {
contact = v
found = true
}
}
if !found {
err = errors.New("User not found")
}
return
}
// GetContactList 获取联系人列表
func (wxwb *WechatWeb) GetContactList() (contacts []datastruct.Contact) {
for _, v := range wxwb.contactList {
contacts = append(contacts, v)
}
return
}
// GetRunInfo 获取运行计数器信息
func (wxwb *WechatWeb) GetRunInfo() (runinfo WechatRunInfo) {
return wxwb.runInfo
}
// refreshCookie 根据response更新cookie
func (wxwb *WechatWeb) refreshCookie(cookies []*http.Cookie) {
for _, c := range cookies {
switch c.Name {
case "wxuin":
wxwb.loginInfo.cookie.Wxuin = c.Value
case "wxsid":
wxwb.loginInfo.cookie.Wxsid = c.Value
case "webwxuvid":
wxwb.loginInfo.cookie.Uvid = c.Value
case "webwx_data_ticket":
wxwb.loginInfo.cookie.DataTicket = c.Value
case "webwx_auth_ticket":
wxwb.loginInfo.cookie.AuthTicket = c.Value
}
}
// 如有必要,记录login信息到storer
wxwb.writeLoginInfo()
}
// 重置登录信息
func (wxwb *WechatWeb) resetLoginInfo() error {
func() {
defer func() {
if r := recover(); r != nil {
log.Println("Recovered in resetLoginInfo: ", r)
wxwb.runInfo.PanicCount++
}
}()
}()
if wxwb.loginStorer != nil {
wxwb.loginStorer.Truncate()
}
jar, err := cookiejar.New(nil)
if err != nil {
return err
}
wxwb.client.Jar = jar
wxwb.loginInfo = wechatLoginInfo{}
return nil
}
// 往storer中写入信息
func (wxwb *WechatWeb) writeLoginInfo() error {
func() {
defer func() {
if r := recover(); r != nil {
log.Println("Recovered in writeLoginInfo: ", r)
wxwb.runInfo.PanicCount++
}
}()
}()
cookieMap := make(map[string][]*http.Cookie)
for _, host := range syncHosts {
u, _ := url.Parse("https://" + host)
cookieMap[host] = wxwb.client.Jar.Cookies(u)
}
if wxwb.loginStorer != nil {
storeInfo := storeLoginInfo{
Cookies: cookieMap,
Cookie: wxwb.loginInfo.cookie,
SyncKey: wxwb.loginInfo.syncKey,
SKey: wxwb.loginInfo.sKey,
PassTicket: wxwb.loginInfo.PassTicket,
User: wxwb.user,
DeviceID: wxwb.deviceID,
RunInfo: wxwb.runInfo,
}
data, err := json.Marshal(storeInfo)
if err != nil {
return err
}
err = wxwb.loginStorer.Writer(data)
return err
}
return nil
}
// 从storer中读取信息
func (wxwb *WechatWeb) readLoginInfo() error {
func() {
defer func() {
if r := recover(); r != nil {
log.Println("Recovered in readLoginInfo: ", r)
wxwb.runInfo.PanicCount++
}
}()
}()
if wxwb.loginStorer != nil {
data, err := wxwb.loginStorer.Read()
if err != nil {
return err
}
var storeInfo storeLoginInfo
err = json.Unmarshal(data, &storeInfo)
if err != nil {
return err
}
for _, host := range syncHosts {
u, _ := url.Parse("https://" + host)
wxwb.client.Jar.SetCookies(u, storeInfo.Cookies[host])
}
wxwb.loginInfo = wechatLoginInfo{
cookie: storeInfo.Cookie,
syncKey: storeInfo.SyncKey,
sKey: storeInfo.SKey,
PassTicket: storeInfo.PassTicket,
}
wxwb.runInfo = storeInfo.RunInfo
wxwb.user = storeInfo.User
wxwb.deviceID = storeInfo.DeviceID
}
return nil
}
// 统一请求
func (wxwb *WechatWeb) request(req *http.Request) (resp *http.Response, err error) {
if req == nil {
return nil, errors.New("request is nil")
}
resp, err = wxwb.client.Do(req)
if err == nil {
wxwb.refreshCookie(resp.Cookies())
}
return
}