-
Notifications
You must be signed in to change notification settings - Fork 1
/
app.js
340 lines (337 loc) · 9.87 KB
/
app.js
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
/**
* @file 微信核心模块
* @author Yaozong Sun <[email protected]>
*/
const axios = require('axios')
const opn = require('opn')
axios.defaults.headers.common['User-Agent'] = 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/59.0.3071.115 Safari/537.36'
class WeChat {
constructor () {
this.loginURL = 'https://login.weixin.qq.com/'
this.baseURL = 'https://wx2.qq.com/'
this.uuid = ''
this.tip = 1
this.ticket = ''
this.skey = ''
this.sid = ''
this.uin = ''
this.passTicket = ''
this.syncKey = null
this.user = null
this.memberCount = 0
this.memberList = []
this.cookie = []
}
/**
* 程序入口登录与获取信息相关
*/
async run () {
await this.getUUID()
this.showQR()
let loginFlag = await this.waitLogin()
while (!loginFlag) {
loginFlag = await this.waitLogin()
}
await this.getCookie()
await this.wxInit()
await this.wxStatusNotifiy()
this.wxSync()
await this.wxGetContcat()
this.getMessage()
}
async getMessage () {
let messageJson = null
while (1) {
messageJson = await this.wxSyncCheck()
if (parseInt(messageJson.retcode, 10) === 1102) {
console.log('[Error] 消息同步出现异常 1102 ...')
break
}
if (parseInt(messageJson.selector, 10) === 2) {
await this.wxSync()
}
}
}
getTime () {
return new Date().valueOf()
}
genBaseRequest () {
let random = (Math.random() + '').substr(2, 15)
return {'Uin': this.uin, 'Sid': this.sid, 'Skey': this.skey, 'DeviceID': 'e' + random}
}
getUUID () {
let reg = new RegExp('window.QRLogin.code = 200; window.QRLogin.uuid = "([a-zA-Z0-9-=]*)";')
return axios.get(`${this.loginURL}jslogin`, {
params: {
appid: 'wx782c26e4c19acffb',
fun: 'new',
lang: 'zh_CN',
_: this.getTime()
}
}).then(res => {
this.uuid = reg.exec(res.data)[1]
console.log('已获取uuid...')
return reg.exec(res.data)[1]
}).catch(err => {
this.uuid = ''
console.log('获取uuid异常...' + err)
})
}
showQR () {
console.log('已获取登录二维码...')
this.uuid && opn(`${this.loginURL}qrcode/${this.uuid}`)
}
waitLogin () {
return axios.get(`${this.loginURL}cgi-bin/mmwebwx-bin/login`, {
params: {
loginicon: true,
uuid: this.uuid,
tip: this.tip,
r: -907243799,
_: this.getTime()
}
}).then(res => {
let codeReg = new RegExp('window.code=([0-9]*);')
let code = parseInt(codeReg.exec(res.data)[1], 10)
if (code === 408) {
this.tip = 1
console.log('[Warning] 二维码等待超时...')
return false
}
if (code === 201) {
console.log('[OK] 已扫描二维码等待确认...')
this.tip = 0
return false
}
if (code === 200) {
let ticketReg = new RegExp('ticket=([a-zA-Z0-9_@]*)')
let scanReg = new RegExp('scan=([0-9]*)')
this.ticket = ticketReg.exec(res.data)[1]
this.scan = scanReg.exec(res.data)[1]
console.log('[OK] 登录成功...')
return true
}
}).catch(err => {
console.log('等待登录异常...' + err)
})
}
getCookie () {
return axios.get(`${this.baseURL}cgi-bin/mmwebwx-bin/webwxnewloginpage`, {
params: {
ticket: this.ticket,
uuid: this.uuid,
scan: this.scan,
fun: 'new',
version: 'v2',
lang: 'zh_CN'
}
}).then(res => {
let xmlString = res.data
let retReg = new RegExp('<ret>([0-9]*)</ret>')
let skeyReg = new RegExp('<skey>([a-zA-Z0-9/_%@]*)</skey>')
let sidReg = new RegExp('<wxsid>([a-zA-Z0-9/_%@+]*)</wxsid>')
let uinReg = new RegExp('<wxuin>([a-zA-Z0-9/_%@]*)</wxuin>')
let passTicketReg = new RegExp('<pass_ticket>([a-zA-Z0-9/_%@]*)</pass_ticket>')
let ret = retReg.exec(xmlString)[1]
if (parseInt(ret, 10)) {
throw new Error('[Error] Cooike 获取出现异常')
}
this.skey = skeyReg.exec(xmlString)[1]
this.sid = sidReg.exec(xmlString)[1]
this.uin = uinReg.exec(xmlString)[1]
this.passTicket = passTicketReg.exec(xmlString)[1]
this.cookie = res.headers['set-cookie']
console.log('[OK] 获取Cookie成功...')
return true
}).catch(err => {
console.log('[Error] 获取Cookie出现异常...', err)
return false
})
}
wxInit () {
let r = ~(new Date().valueOf())
return axios({
method: 'post',
url: `${this.baseURL}cgi-bin/mmwebwx-bin/webwxinit?lang=zh_CN&pass_ticket=${this.passTicket}&r=${r}`,
headers: {
'Content-Type': 'application/json'
},
data: {
'BaseRequest': this.genBaseRequest()
}
}).then(res => {
let json = res.data
if (!json.BaseResponse.Ret) {
this.syncKey = json.SyncKey
this.user = json.User
console.log('[OK] 微信初始化成功...')
return true
} else {
console.log('[Error] 微信初始化出现异常...参数异常')
return false
}
}).catch(err => {
console.log('[Error] 微信初始化出现异常...', err)
return false
})
}
wxStatusNotifiy () {
return axios({
method: 'post',
url: `${this.baseURL}cgi-bin/mmwebwx-bin/webwxstatusnotify?lang=zh_CN&pass_ticket=${this.passTicket}`,
headers: {
'Content-Type': 'application/json'
},
data: {
'BaseRequest': this.genBaseRequest(),
'ClientMsgId': this.getTime(),
'Code': 3,
'FromUserName': this.user.UserName,
'ToUserName': this.user.UserName
}
}).then(res => {
let json = res.data
if (!json.BaseResponse.Ret) {
console.log('[OK] 消息通知开启成功...')
return true
} else {
console.log('[Error] 消息通知开启失败...')
return false
}
}).catch(err => {
console.log('[Error] 消息通知开启失败...', err)
return false
})
}
parseCookie () {
return this.cookie.map(el => el.split(';')[0]).join('; ')
}
wxGetContcat () {
return axios.get(`${this.baseURL}cgi-bin/mmwebwx-bin/webwxgetcontact`, {
params: {
lang: 'zh_CN',
pass_ticket: this.passTicket,
r: this.getTime(),
seq: 0,
skey: this.skey
},
headers: {
'Cookie': this.parseCookie()
}
}).then(res => {
let json = res.data
if (!json.BaseResponse.Ret) {
this.memberCount = json.MemberCount
this.memberList = json.MemberList
console.log(`[OK] 获取联系人列表成功...共计 ${this.memberCount} 人`)
return true
} else {
console.log('[Error] 获取联系人列表失败...')
return false
}
}).catch(err => {
console.log('[Error] 获取联系人列表失败...', err)
return false
})
}
wxSync () {
return axios({
method: 'post',
url: `${this.baseURL}cgi-bin/mmwebwx-bin/webwxsync`,
params: {
sid: this.sid,
skey: this.skey,
lang: 'zh_CN',
pass_ticket: this.passTicket
},
data: {
'BaseRequest': this.genBaseRequest(),
'SyncKey': this.syncKey,
'rr': ~(this.getTime)
},
headers: {
'Cookie': this.parseCookie()
}
}).then(res => {
let json = res.data
if (!json.BaseResponse.Ret) {
this.syncKey = json.SyncKey
console.log('[OK] 获取到新消息...')
if (json.AddMsgList.length) {
let content = json.AddMsgList[0].Content
let sentence = ['草船借箭', '三打白骨精', '壮徒恒贾勇,拔拒抵长河', '战伐有功业,焉能守旧丘']
if (content.indexOf('小山') > -1) {
this.wxSendMessage(this.user.UserName, json.AddMsgList[0].FromUserName, '此人' + sentence[Math.round(Math.random() * 100) % 4])
}
console.log(json.AddMsgList[0].Content)
}
}
return true
}).catch(err => {
console.log('[ERROR] 同步机制错误...', err)
return false
})
}
genSyncKey () {
return this.syncKey.List.map(el => el.Key + '_' + el.Val).join('|')
}
wxSyncCheck () {
let random = (Math.random() + '').substr(2, 15)
return axios.get('https://webpush.wx2.qq.com/cgi-bin/mmwebwx-bin/synccheck', {
params: {
r: this.getTime(),
skey: this.skey,
sid: this.sid,
uin: this.uin,
deviceid: 'e' + random,
synckey: this.genSyncKey()
},
headers: {
'Cookie': this.parseCookie()
}
}).then(res => {
let reg = new RegExp('window.synccheck={retcode:"([0-9]+)",selector:"([0-9]+)"}')
let exec = reg.exec(res.data)
// 返回数据为 window.synccheck={retcode:"1102",selector:"0"}
let info = {
retcode: exec[1],
selector: exec[2]
}
return info
}).catch(err => {
console.log('[Error] 消息同步轮询发生异常...', err)
})
}
wxSendMessage (from, to, message) {
let random = (Math.random() + '').substr(4, 4)
axios({
method: 'post',
url: 'https://wx2.qq.com/cgi-bin/mmwebwx-bin/webwxsendmsg?lang=zh_CN',
data: {
'BaseRequest': this.genBaseRequest(),
'Msg': {
'ClientMsgId': this.getTime() + random,
'Content': message,
'FromUserName': from,
'ToUserName': to,
'Type': 1
},
'Scene': 0
},
headers: {
'Cookie': this.parseCookie()
}
}).then(res => {
let json = res.data
if (!json.BaseResponse.Ret) {
console.log(`[OK] 发送消息 [${message}] 成功...`)
} else {
console.log(`[Waring] 发送消息失败`)
}
}).catch(err => {
console.log(`[Waring] 发送消息失败`, err)
})
}
}
const wechat = new WeChat()
wechat.run()