-
Notifications
You must be signed in to change notification settings - Fork 1
/
wx.go
471 lines (420 loc) · 11.2 KB
/
wx.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
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
package webwx
import (
"bytes"
"encoding/xml"
"errors"
"fmt"
"io/ioutil"
"math/rand"
"net/http"
"net/http/cookiejar"
"net/url"
"regexp"
"strings"
"time"
jsoniter "github.com/json-iterator/go"
"github.com/golang/glog"
)
var json = jsoniter.ConfigCompatibleWithStandardLibrary
const (
httpUserAgent = "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/47.0.2526.111 Safari/537.36"
httpReferer = "https://wx.qq.com/"
cgipath = "https://wx.qq.com/cgi-bin/mmwebwx-bin"
syncHost = "webpush.wx.qq.com"
)
const (
cmdNewLogin = "uuid"
cmdLoginOK = "loginok"
cmdRunning = "runingok"
)
var (
cmdChan = make(chan Cmd)
wechats = make(map[string]*Wechat)
)
type Cmd struct {
command string
content string
}
// User user info
type User struct {
Uin int `json:"Uin"`
UserName string `json:"UserName"`
NickName string `json:"NickName"`
HeadImgURL string `json:"HeadImgUrl"`
ContactFlag int `json:"ContactFlag"`
RemarkName string `json:"RemarkName"`
Sex int `json:"Sex"`
Status int `json:"Status"`
AttrStatus int `json:"AttrStatus"`
Province string `json:"Province"`
City string `json:"City"`
}
// Wechat wechat context
type Wechat struct {
Skey string `json:"skey"`
Uin string `json:"uid"`
Sid string `json:"sid"`
DeviceID string `json:"device_id"`
PassTicket string `json:"pass_ticket"`
UUID string `json:"uuid"`
Name string
baseRequest map[string]interface{}
contacts map[string]User
client *http.Client
}
// Session wechat login user session
// Msg wechat msg struct
type Msg struct {
MsgID string `json:"MsgId"`
FromUserName string `json:"FromUserName"`
ToUserName string `json:"ToUserName"`
MsgType int `json:"MsgType"`
Content string `json:"Content"`
Status string `json:"Status"`
}
type kv struct {
Key int `json:"Key"`
Val int `json:"Val"`
}
type syncKey struct {
Count int `json:"Count"`
List []kv `json:"List"`
}
type getContactRes struct {
MemberCount int `json:"MemberCount"`
MemberList []User `json:"MemberList"`
}
func packSyncKey(syncKeys []kv) string {
var keys []string
for _, e := range syncKeys {
keys = append(keys, fmt.Sprintf("%d_%d", e.Key, e.Val))
}
return strings.Join(keys, "|")
}
// MsgHandleFunc webwx recv msg will call
type MsgHandleFunc func(wx *Wechat, msgs []*Msg) error
// SendMsg send msg
func (w *Wechat) SendMsg(toUserName, content string) (err error) {
return w.sendmsg(toUserName, content)
}
func (w *Wechat) GetUser(username string) *User {
u, ok := w.contacts[username]
if !ok {
return nil
}
return &u
}
func (w *Wechat) msgRecv(sk *syncKey, handle MsgHandleFunc, argv interface{}) (err error) {
var (
retcode, selector string
tryTimes int
msgs []*Msg
res []byte
)
for {
if retcode, selector, err = w.synccheck(sk); err != nil || retcode != "0" {
if tryTimes > 3 {
break
}
time.Sleep(time.Second)
tryTimes++
continue
}
tryTimes = 0
if res, err = w.sync(sk); err != nil {
break
}
json.Get(res, "SyncKey").ToVal(sk)
json.Get(res, "AddMsgList").ToVal(&msgs)
if selector == "2" && err == nil {
if err = handle(w, msgs); err != nil {
break
}
} else if selector == "0" {
time.Sleep(time.Millisecond * 1000)
} else {
err = fmt.Errorf("unknow selector: %s", selector)
break
}
}
glog.Errorf("stop recv msg, err:%s", err.Error())
return
}
func (w *Wechat) getcontact() (err error) {
r := time.Now().UnixNano() / int64(time.Millisecond)
url := fmt.Sprintf("%s/webwxgetcontact?r=%d&seq=0&skey=%s", cgipath, r, w.Skey)
params := map[string]interface{}{
"BaseRequest": w.baseRequest,
}
body, err := w.wxpost(url, params)
if err != nil {
return
}
res := new(getContactRes)
if err = json.Unmarshal(body, res); err == nil {
for _, user := range res.MemberList {
w.contacts[user.UserName] = user
}
}
return
}
func (w *Wechat) synccheck(sk *syncKey) (string, string, error) {
skstr := packSyncKey(sk.List)
r := time.Now().UnixNano() / int64(time.Millisecond)
url := fmt.Sprintf("https://%s/cgi-bin/mmwebwx-bin/synccheck?r=%d&skey=%s&sid=%s&uin=%s&deviceid=%s&synckey=%s&_=%d",
syncHost, r, w.Skey, url.QueryEscape(w.Sid), w.Uin, w.DeviceID, skstr, r)
res, err := w.wxget(url)
if err != nil {
return "", "", err
}
// resp: window.synccheck={retcode:"0",selector:"0"}
re := regexp.MustCompile(`window.synccheck={retcode:"(\d+)",selector:"(\d+)"}`)
find := re.FindStringSubmatch(res)
return find[1], find[2], err
}
func (w *Wechat) sync(sk *syncKey) ([]byte, error) {
url := fmt.Sprintf("%s/webwxsync?sid=%s&skey=%s&pass_ticket=%s", cgipath, w.Sid, w.Skey, w.PassTicket)
params := map[string]interface{}{
"BaseRequest": w.baseRequest,
"SyncKey": sk,
"rr": ^int(time.Now().Unix()),
}
return w.wxpost(url, params)
}
func (w *Wechat) sendmsg(toUserName, content string) (err error) {
r := time.Now().UnixNano() / int64(time.Millisecond)
msgid := fmt.Sprintf("%d%04d", r, rand.Intn(10000))
params := map[string]interface{}{
"BaseRequest": w.baseRequest,
"Msg": map[string]interface{}{
"Type": 1,
"Content": content,
"FromUserName": w.Name,
"ToUserName": toUserName,
"LocalID": msgid,
"ClientMsgID": msgid,
},
}
urlstr := fmt.Sprintf("%s/webwxsendmsg?pass_ticket=%s", cgipath, w.PassTicket)
_, err = w.wxpost(urlstr, params)
return
}
func (w *Wechat) wxget(urlstr string) (result string, err error) {
return httpClientGet(w.client, urlstr)
}
func (w *Wechat) wxpost(urlstr string, params map[string]interface{}) (result []byte, err error) {
glog.V(2).Infof("url:%s", urlstr)
var (
jsonb []byte
request *http.Request
)
if jsonb, err = json.Marshal(params); err != nil {
return
}
requestBody := bytes.NewBuffer([]byte(jsonb))
if request, err = http.NewRequest("POST", urlstr, requestBody); err != nil {
return
}
request.Header.Set("Content-Type", "application/json;charset=utf-8")
request.Header.Add("Referer", httpReferer)
request.Header.Add("User-agent", httpUserAgent)
resp, err := w.client.Do(request)
if err != nil {
return
}
defer resp.Body.Close()
if result, err = ioutil.ReadAll(resp.Body); err != nil {
return
}
glog.V(2).Infof("result:%s", string(result))
return
}
func waitLogin(uuid string) error {
var (
res, url, code string
err error
r int64
tip = -1
)
for {
tip++
r = time.Now().UnixNano() / int64(time.Millisecond)
url = fmt.Sprintf("%s/login?loginicon=true&tip=%d&uuid=%s&_=%d", cgipath, tip, uuid, r)
res, err = httpGet(url)
if err == nil {
code = res[12:15]
}
if code == "200" {
idx := strings.Index(res, "window.redirect_uri=")
if idx == -1 {
err = errors.New("wait confirm login invalid response")
return err
}
idx += len("window.redirect_uri=") + 1
loginCodeURL := res[idx : len(res)-2]
cookieJar, _ := cookiejar.New(nil)
client := &http.Client{
Jar: cookieJar,
}
if res, err = httpClientGet(client, loginCodeURL+"&fun=new&version=v2"); err != nil {
return err
}
session := struct {
Skey string `xml:"skey"`
Sid string `xml:"wxsid"`
Uin string `xml:"wxuin"`
PassTicket string `xml:"pass_ticket"`
}{}
if err = xml.Unmarshal([]byte(res), &session); err != nil {
glog.Errorf("invalid session data: %s err: %s", res, err.Error())
return err
}
w := &Wechat{
Skey: session.Skey,
Sid: session.Sid,
Uin: session.Uin,
PassTicket: session.PassTicket,
UUID: uuid,
}
w.Init(client)
return nil
} else if code == "408" {
return errors.New("login timeout")
} else if code == "201" {
time.Sleep(time.Second * 1)
} else {
time.Sleep(time.Second * 1)
}
}
}
/*
func NewWechatFromSessionData(data string) (*Wechat, error) {
w := new(Wechat)
var err error
if err = json.Unmarshal([]byte(data), w); err != nil {
return nil, err
}
w.Init()
return w, nil
}
*/
func (w *Wechat) SaveSession() string {
return ""
}
func (w *Wechat) Init(client *http.Client) {
if w.DeviceID == "" {
w.DeviceID = "e786342593651293"
}
w.baseRequest = map[string]interface{}{
"Uin": w.Uin,
"Sid": w.Sid,
"Skey": w.Skey,
"DeviceID": w.DeviceID,
}
w.contacts = make(map[string]User)
w.client = client
wechats[w.Uin] = w
sendCmd(cmdLoginOK, w.Uin)
}
func (w *Wechat) start(handle MsgHandleFunc, argv string) error {
var (
res []byte
err error
)
// 1. webwxinit
params := map[string]interface{}{
"BaseRequest": w.baseRequest,
}
r := time.Now().UnixNano() / int64(time.Millisecond)
url := fmt.Sprintf("%s/webwxinit?pass_ticket=%s&r=%d", cgipath, w.PassTicket, r)
if res, err = w.wxpost(url, params); err != nil {
return err
}
w.Name = json.Get(res, "User", "UserName").ToString()
sk := new(syncKey)
json.Get(res, "SyncKey").ToVal(sk)
// 2. webwxstatusnotify
params = map[string]interface{}{
"BaseRequest": w.baseRequest,
"Code": 3,
"FromUserName": w.Name,
"ToUserName": w.Name,
"ClientMsgId": int(time.Now().Unix()),
}
urlstr := fmt.Sprintf("%s/webwxstatusnotify?lang=zh_CN&pass_ticket=%s", cgipath, w.PassTicket)
if res, err = w.wxpost(urlstr, params); err != nil {
return err
}
retcode := json.Get(res, "BaseResponse", "Ret").ToInt()
if retcode != 0 {
err = fmt.Errorf("statusnotify retcode:%d", retcode)
}
// 3. getcontact
r = time.Now().UnixNano() / int64(time.Millisecond)
url = fmt.Sprintf("%s/webwxgetcontact?r=%d&seq=0&skey=%s", cgipath, r, w.Skey)
params = map[string]interface{}{
"BaseRequest": w.baseRequest,
}
if res, err = w.wxpost(url, params); err != nil {
return err
}
contacts := new(getContactRes)
if err = json.Unmarshal(res, contacts); err == nil {
for _, user := range contacts.MemberList {
w.contacts[user.UserName] = user
}
}
// 4. handleMessage
return w.msgRecv(sk, handle, argv)
}
func sendCmd(cmd, content string) {
cmdChan <- Cmd{cmd, content}
}
func Run(handle MsgHandleFunc, argv string) error {
for {
select {
case cmd := <-cmdChan:
switch cmd.command {
case cmdNewLogin: // uuid
go waitLogin(cmd.content)
case cmdLoginOK: // wechat uin
w, _ := wechats[cmd.content]
go w.start(handle, argv)
case cmdRunning:
}
}
}
return nil
}
func NewLoginCodeURL() (string, error) {
r := time.Now().UnixNano() / int64(time.Millisecond)
url := fmt.Sprintf("https://login.weixin.qq.com/jslogin?appid=wx782c26e4c19acffb&redirect_uri=%s&fun=new&lang=zh_CN&_=%d", "", r)
res, err := httpGet(url)
if err != nil {
return "", err
}
begin := strings.Index(res, "\"")
end := strings.Index(res[begin+1:], "\"")
uuid := res[begin+1 : begin+end+1]
// add uuid to wait login queue
sendCmd(cmdNewLogin, uuid)
codeURL := "https://login.weixin.qq.com/l/" + uuid
return codeURL, nil
}
func httpClientGet(client *http.Client, url string) (string, error) {
resp, err := client.Get(url)
if err != nil {
glog.Errorf("http get fail err=%s", err.Error())
return "", err
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return "", err
}
glog.V(2).Infof("url: %s\n res: %s", url, string(body))
return string(body), nil
}
func httpGet(url string) (string, error) {
return httpClientGet(http.DefaultClient, url)
}