This repository has been archived by the owner on Jun 24, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
wechat_login.js
347 lines (340 loc) · 10.5 KB
/
wechat_login.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
341
342
343
344
345
346
347
//var http=require('http');
//var https=require('https');
var request=require('request');
//var querystring= require('querystring');
var fs=require('fs');
function wx_login(){
var me=this;
var uuid;
var promise;
var j;
var user;
var message;
var contacter;
var untonn;//user name to nick name
var untogn;//predder to use RemarkName
var message_deal;
this.Init=()=>{
me.j=request.jar();
request = request.defaults({jar:j})
me.user=new Array();
me.message=new Array();
me.contacter=new Array();
me.untonn=new Array();
me.untogn=new Array();
me.user['DeviceID']="e" + ("" + Math.random().toFixed(15)).substring(2, 17);//copy from wechat's js which named index_e01fd8a.js
me.message_deal=me.message_deal||me._dealmsg_;
};
this.getuuid=(resolve,reject)=>{
var options={
url:'https://login.wx2.qq.com/jslogin?appid=wx782c26e4c19acffb&redirect_uri=https%3A%2F%2Fwx2.qq.com%2Fcgi-bin%2Fmmwebwx-bin%2Fwebwxnewloginpage&fun=new&lang=zh_CN',
jar:me.j,
}
request.get(options,(e,r,b)=>{
if(!e & r.statusCode==200){
let re=b.match(/\s=\s.*?(?=;)/g);
me.uuid=re[1].replace(' = ','').replace(/"/g,'');
//resolve(me.uuid);
request('https://login.weixin.qq.com/qrcode/'+me.uuid).pipe(fs.createWriteStream('test.jpg')).on('close',()=>{resolve()})
}
})
};
this.checklogin=(resolve,reject)=>{
var windowcode;
var windowrespone;
var options={
url:'https://wx2.qq.com/cgi-bin/mmwebwx-bin/webwxstatreport?fun=new&lang=zh_CN',
jar:me.j,
json:{
BaseRequest:{
Uin:"",
Sid:"",
DeviceID:me.user.DeviceID,
},
Count:0,
List:[],
}
}
request.post(options,(e,r,b)=>{
if(!e&r.statusCode==200)reqc();
})
//reqc();
function reqc(){
var options={
url:'https://login.wx2.qq.com/cgi-bin/mmwebwx-bin/login?loginicon=true&uuid='+me.uuid+'&tip=1&r='+~Date.now(),
jar:me.j
}
request.get(options,(e,r,b)=>{
if(!e & r.statusCode==200){
windowrespone=b;
windowcode=b.split(';')[0].split('=')[1];
if(windowcode!=201){
reqc();
}else{
resolve(b.match(/'.*?'/g)[0]);
};
}
})
}
};
this.checkloginconfirm=(resolve,reject)=>{
var windowcode;
var windowrespone;
reqc();
function reqc(){
var options={
url:'https://login.wx2.qq.com/cgi-bin/mmwebwx-bin/login?loginicon=true&uuid='+me.uuid+'&tip=0&r='+~Date.now(),
jar:me.j,
}
request.get(options,(e,r,b)=>{
if(!e & r.statusCode==200){
windowrespone=b;
windowcode=b.split(';')[0].split('=')[1];
if(windowcode!=200){
reqc();
}else{
var options={
url:b.match(/".*?"/g)[0].replace(/"/g,'')+'&fun=new&version=v2&lang=zh_CN',
jar:me.j,
}
request.get(options,(e,r,b)=>{
if(!e&r.statusCode==200){
//console.log(b);
me.user['skey']=b.match(/\<skey\>.*?(?=\<\/skey\>)/)[0].replace('<skey>','');
me.user['pass_ticket']=b.match(/\<pass_ticket\>.*?(?=\<\/pass_ticket\>)/)[0].replace('<pass_ticket>','');
me.user['wxsid']=b.match(/\<wxsid\>.*?(?=\<\/wxsid\>)/)[0].replace('<wxsid>','');
me.user['uin']=b.match(/\<wxuin\>.*?(?=\<\/wxuin\>)/)[0].replace('<wxuin>','');
resolve(me.user);
}
})
};
}
})
}
};
this.allinit=(resolve,reject)=>{
var options={
url:'https://wx2.qq.com/cgi-bin/mmwebwx-bin/webwxinit?r='+~Date.now()+'&lang=zh_CN&pass_ticket='+me.user.pass_ticket,
jar:me.j,
json:{BaseRequest:{
Uin:me.user.uin,
Sid:me.user.wxsid,
Skey:me.user.skey,
DeviceID:me.user.DeviceID,
}},
}
request.post(options,(e,r,b)=>{
if(!e & r.statusCode==200){
me.message['SyncKey']=b.SyncKey;
me.user['UserName']=b.User.UserName;
me.user['NickName']=b.User.NickName;
var options={
url:'https://wx2.qq.com/cgi-bin/mmwebwx-bin/webwxgetcontact?lang=zh_CN&pass_ticket='+me.user.pass_ticket+'&r='+~Date.now()+'&seq=0&skey='+me.user.skey,
jar:me.j,
};
request.get(options,(e,r,b)=>{
if(!e&r.statusCode==200){
b=JSON.parse(b);
me.contacter=b;
for(let num=0;num<b.MemberList.length;num++){
me.untonn[b.MemberList[num].UserName]=b.MemberList[num].NickName;
b.MemberList[num].RemarkName!=''?me.untogn[b.MemberList[num].UserName]=b.MemberList[num].RemarkName:me.untogn[b.MemberList[num].UserName]=b.MemberList[num].NickName;
}//初始化名单
//resolve('Finish');
var options={
url:'https://wx2.qq.com/cgi-bin/mmwebwx-bin/webwxstatusnotify?lang=zh_CN&pass_ticket='+me.user.pass_ticket,
jar:me.j,
json:{
BaseRequest:{
Uin:me.user.uin,
Sid:me.user.wxsid,
Skey:me.user.skey,
DeviceID:me.user.DeviceID,
},
ClientMsgId:Date.now(),
Code:3,
FromUserName:me.user.UserName,
ToUserName:me.user.UserName,
}
};
request.post(options,(e,r,b)=>{
if(!e&r.statusCode==200){
//console.log(b);
resolve(b);
}
})
}
})
}
})
};
this.waitforsync=(resolve,reject)=>{
reqc();
function reqc(){
var options={
url:'https://webpush.wx2.qq.com/cgi-bin/mmwebwx-bin/synccheck?r='+Date.now()+'&skey='+me.user.skey+'&sid='+me.user.wxsid+'&uin='+me.user.uin+'&deviceid='+me.user.DeviceID+'&synckey='+me.getsyncstring(),
//jar:nj,
jar:me.j,
}
request.get(options,(e,r,b)=>{
if(!e&r.statusCode==200){
if(me.message_deal==="null")return;
if(me.message_deal==="exit"){resolve(b);return;};
b=b.match(/\{.*?\}/g)[0].replace('{','').replace('}','').split(',');
if(b[0].split(':')[1]!='"0"'){console.log('异常退出');reject('reason:retcode='+b[0].split(':')[1]);return;};
if(b[0].split(':')[1]!='"0"' || b[1].split(':')[1]!='"0"'){deal();}else{reqc()};
//console.log(b);
//if(b.match(/"0"/g).length!=2){deal();}else{reqc();};
//reqc(options);
}
})
}
function deal(){
var options={
url:'https://wx2.qq.com/cgi-bin/mmwebwx-bin/webwxsync?sid='+me.user.wxsid+'&skey='+me.user.skey+'&lang=zh_CN&pass_ticket='+me.user.pass_ticket,
jar:me.j,
json:{
BaseRequest:{
Uin:me.user.uin,
Sid:me.user.wxsid,
Skey:me.user.skey,
DeviceID:me.user.DeviceID,
},
SyncKey:me.message.SyncKey,
rr:~Date.now(),
}
}
request.post(options,(e,r,b)=>{
if(!e&r.statusCode==200){
if(me.message_deal==="null")return;
if(me.message_deal==="exit"){resolve(b);return;};
me.message['SyncKey']=b.SyncKey;
//me.message_deal(b.AddMsgList);
for(let i=0;i<b.AddMsgList.length;i++)me.message_deal(b.AddMsgList[i]);
reqc();
}
})
}
};
this.search=(list,pass,doit)=>{
if(list==[]){
doit(pass);
}else{
var options={
url:'https://wx2.qq.com/cgi-bin/mmwebwx-bin/webwxbatchgetcontact?type=ex&r='+Date.now()+'&lang=zh_CN&pass_ticket='+me.user.pass_ticket,
jar:me.j,
json:{
BaseRequest:{
Uin:me.user.uin,
Sid:me.user.wxsid,
Skey:me.user.skey,
DeviceID:me.user.DeviceID,
},
Count:list.length,
List:list,
}
}
request.post(options,(e,r,b)=>{
if(!e&r.statusCode==200){
for(let i=0;i<b.ContactList.length;i++)me.untonn[b.ContactList[i].UserName]=b.ContactList[i].NickName;
doit(pass);
}
})
}
}
//base function
this.getsyncstring=()=>{
let io=[];
for(let i=0;i<me.message.SyncKey.List.length;i++)io.push(me.message.SyncKey.List[i].Key+'_'+me.message.SyncKey.List[i].Val);
return io.join('|');
//return encodeURIComponent(io.join('|'));
}
this._msglist_=(UserName,EncryChatRoomId)=>{EncryChatRoomId=EncryChatRoomId||'';return {UserName:UserName,EncryChatRoomId:EncryChatRoomId,}};
//the function of deal with new message
this._dealmsg_=(msg)=>{//默认仅支持文本,图片
switch(msg.MsgType){
case 1://text message
let ps=[];
let pass=new Array();
if(msg.FromUserName[1]=='@'||msg.ToUserName[1]=='@'){//群消息
if(msg.FromUserName!=me.user.UserName){
if(!me.untonn[msg.FromUserName]){//没有保存在联系库中的群名字
ps.push(me._msglist_(msg.FromUserName));
};
let qm=msg.Content.indexOf(':<br/>');
let postername=msg.Content.substring(0,qm);
let content=msg.Content.substring(qm+6);
if(!(me.untogn[postername]||me.untonn[postername])){//没有保存在联系库中的人名
ps.push(me._msglist_(postername,msg.FromUserName));
};
pass['postername']=postername;
pass['FromUserName']=msg.FromUserName;
pass['content']=content;
var doit=(pass)=>{
var postername=me.untogn[pass.postername]||me.untonn[pass.postername];
console.log('群【'+me.untonn[pass.FromUserName]+'】 【'+postername+'】:'+pass.content);
};
me.search(ps,pass,doit);
}else{
if(!(me.untogn[msg.ToUserName]||me.untonn[msg.ToUserName])){//没有保存在联系库中的群名字
ps.push(me._msglist_(msg.ToUserName));
};
pass['ToUserName']=msg.ToUserName;
pass['content']=msg.Content;
var doit=(pass)=>{
console.log('我 -> 群【'+me.untonn[pass.ToUserName]+'】:'+pass.content)
}
me.search(ps,pass,doit)
}
}else{//非群消息
if(msg.FromUserName!=me.user.UserName){
if(!(me.untogn[msg.FromUserName]||me.untonn[msg.FromUserName])){
ps.push(me._msglist_(msg.FromUserName));
};
pass['FromUserName']=msg.FromUserName;
pass['content']=msg.Content;
var doit=(pass)=>{
var postername=me.untogn[pass.FromUserName]||me.untonn[pass.FromUserName];
console.log('【'+postername+'】:'+pass.content);
};
me.search(ps,pass,doit);
}else{
if(!(me.untogn[msg.ToUserName]||me.untonn[msg.ToUserName])){
ps.push(me._msglist_(msg.ToUserName));
};
pass['ToUserName']=msg.ToUserName;
pass['content']=msg.Content;
var doit=(pass)=>{
var recevier=me.untogn[pass.ToUserName]||me.untonn[pass.ToUserName];
console.log('我 -> 【'+recevier+'】:'+pass.content);
};
me.search(ps,pass,doit);
}
};
break;
}
}
}
module.exports =wx_login;
//something you would want to konw
/*
msgtype of wechatmsg, you can deal with it to do what you want to do.
MSGTYPE_TEXT: 1,
MSGTYPE_IMAGE: 3,
MSGTYPE_VOICE: 34,
MSGTYPE_VIDEO: 43,
MSGTYPE_MICROVIDEO: 62,
MSGTYPE_EMOTICON: 47,
MSGTYPE_APP: 49,
MSGTYPE_VOIPMSG: 50,
MSGTYPE_VOIPNOTIFY: 52,
MSGTYPE_VOIPINVITE: 53,
MSGTYPE_LOCATION: 48,
MSGTYPE_STATUSNOTIFY: 51,
MSGTYPE_SYSNOTICE: 9999,
MSGTYPE_POSSIBLEFRIEND_MSG: 40,
MSGTYPE_VERIFYMSG: 37,
MSGTYPE_SHARECARD: 42,
MSGTYPE_SYS: 10000,
MSGTYPE_RECALLED: 10002, // 撤销消息
*/