-
Notifications
You must be signed in to change notification settings - Fork 1
/
sync.go
222 lines (212 loc) · 5.62 KB
/
sync.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
package wwdk
import (
"errors"
"io/ioutil"
"log"
"net/http"
"net/url"
"strconv"
"strings"
"time"
"github.com/ikuiki/wwdk/datastruct"
"github.com/ikuiki/wwdk/tool"
)
var syncHosts = []string{
"wx2.qq.com",
"webpush.wx.qq.com",
"webpush.wx2.qq.com",
"wx8.qq.com",
"webpush.wx8.qq.com",
"qq.com",
"webpush.wx.qq.com",
"web2.wechat.com",
"webpush.web2.wechat.com",
"wechat.com",
"webpush.web.wechat.com",
"webpush.weixin.qq.com",
"webpush.wechat.com",
"webpush1.wechat.com",
"webpush2.wechat.com",
"webpush2.wx.qq.com",
}
// assembleSyncKey 组装synckey
// 将同步需要的synckey组装为请求字符串
func assembleSyncKey(syncKey *datastruct.SyncKey) string {
keys := make([]string, 0)
for _, v := range syncKey.List {
keys = append(keys, strconv.FormatInt(v.Key, 10)+"_"+strconv.FormatInt(v.Val, 10))
}
ret := strings.Join(keys, "|")
// return url.QueryEscape(ret)
return ret
}
// analysisSyncResp 解析同步状态返回值
// 同步状态返回的接口
func analysisSyncResp(syncResp string) (result datastruct.SyncCheckRespond) {
syncResp = strings.TrimPrefix(syncResp, "{")
syncResp = strings.TrimSuffix(syncResp, "}")
arr := strings.Split(syncResp, ",")
result = datastruct.SyncCheckRespond{}
for _, v := range arr {
if strings.HasPrefix(v, "retcode") {
result.Retcode = strings.TrimPrefix(strings.TrimSuffix(v, `"`), `retcode:"`)
}
if strings.HasPrefix(v, "selector") {
result.Selector = strings.TrimPrefix(strings.TrimSuffix(v, `"`), `selector:"`)
}
}
return result
}
func (wxwb *WechatWeb) chooseSyncHost() bool {
log.Println("choose sync host...")
for _, host := range syncHosts {
wxwb.syncHost = host
code, _, _ := wxwb.syncCheck()
if code == `0` {
log.Printf("sync host [%s] avaliable", host)
return true
}
}
return false
}
// syncCheck 同步状态
// 轮询微信服务器,如果有新的状态,会通过此接口返回需要同步的信息
func (wxwb *WechatWeb) syncCheck() (retCode, selector string, err error) {
if wxwb.syncHost == "" {
return "", "", errors.New("sync host empty")
}
params := url.Values{}
params.Set("r", tool.GetWxTimeStamp())
params.Set("sid", wxwb.loginInfo.cookie.Wxsid)
params.Set("uin", wxwb.loginInfo.cookie.Wxuin)
params.Set("deviceid", wxwb.deviceID)
params.Set("synckey", assembleSyncKey(wxwb.loginInfo.syncKey))
params.Set("_", tool.GetWxTimeStamp())
req, err := http.NewRequest("GET", "https://"+wxwb.syncHost+"/cgi-bin/mmwebwx-bin/synccheck?"+params.Encode(), nil)
if err != nil {
return "", "", errors.New("create request error: " + err.Error())
}
resp, err := wxwb.request(req)
if err != nil {
return "", "", errors.New("request error: " + err.Error())
}
defer resp.Body.Close()
body, _ := ioutil.ReadAll(resp.Body)
retArr := tool.ExtractWxWindowRespond(string(body))
ret := analysisSyncResp(retArr["window.synccheck"])
return ret.Retcode, ret.Selector, nil
// if ret.Retcode != "0" {
// if ret.Retcode == "1101" {
// return "Logout", nil
// }
// return "", errors.New("respond Retcode " + ret.Retcode)
// }
// return ret.Selector, nil
}
// StartServe 启动消息同步服务
func (wxwb *WechatWeb) StartServe() {
avaliable := wxwb.chooseSyncHost()
if !avaliable {
log.Println("all sync host unavaliable, exit...")
return
}
getMessage := func() {
gmResp, err := wxwb.getMessage()
if err != nil {
log.Printf("GetMessage error: %s\n", err.Error())
return
}
if gmResp.SyncCheckKey != nil {
wxwb.loginInfo.syncKey = gmResp.SyncCheckKey
} else {
wxwb.loginInfo.syncKey = gmResp.SyncKey
}
// 处理新增联系人
for _, contact := range gmResp.ModContactList {
wxwb.runInfo.ContactModifyCount++
log.Println("Modify contact: ", contact.NickName)
oldContact := wxwb.contactList[contact.UserName]
wxwb.contactProcesser(&oldContact, &contact)
wxwb.contactList[contact.UserName] = contact
}
// 新消息
for _, msg := range gmResp.AddMsgList {
if msg.MsgType == datastruct.RevokeMsg {
wxwb.runInfo.MessageRevokeCount++
} else {
wxwb.runInfo.MessageCount++
}
err = wxwb.messageProcesser(&msg)
if err != nil {
log.Printf("MessageProcesser error: %s\n", err.Error())
continue
}
}
}
for {
isBreaked := func() (isBreaked bool) {
defer func() {
if r := recover(); r != nil {
log.Println("Recovered in StartServe loop: ", r)
wxwb.runInfo.PanicCount++
}
}()
code, selector, err := wxwb.syncCheck()
if err != nil {
log.Printf("SyncCheck error: %s\n", err.Error())
return false
}
if code != "0" {
switch code {
case "1101":
log.Println("User has logout web wechat, exit...")
return true
case "1100":
log.Println("sync host unavaliable, choose a new one...")
avaliable = wxwb.chooseSyncHost()
if !avaliable {
log.Println("all sync host unavaliable, exit...")
return true
}
return false
}
}
// log.Println("selector: ", selector)
switch selector {
case "0":
// log.Println("SyncCheck 0")
// normal
// log.Println("no new message")
case "6":
log.Printf("selector is 6")
getMessage()
case "7":
log.Printf("selector is 7")
getMessage()
case "1":
log.Printf("selector is 1")
getMessage()
case "3":
log.Printf("selector is 3")
getMessage()
case "4":
log.Printf("selector is 4")
getMessage()
case "5":
log.Printf("selector is 5")
getMessage()
case "2":
// log.Println("SyncCheck 2")
getMessage()
default:
log.Printf("SyncCheck Unknow selector: %s\n", selector)
}
wxwb.runInfo.SyncCount++
time.Sleep(1000 * time.Millisecond)
return false
}()
if isBreaked {
break
}
}
}