forked from xhan/qqbot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathqqauth_qrcode.js
311 lines (280 loc) · 10.6 KB
/
qqauth_qrcode.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
// Generated by CoffeeScript 1.10.0
(function() {
var Log, Path, Url, all_cookies, create_img_server, crypto, encryptPass, http, https, img_server, int, log, login_next, md5, querystring, stop_img_server;
https = require("https");
http = require('http');
crypto = require('crypto');
querystring = require('querystring');
Url = require('url');
all_cookies = [];
int = function(v) {
return parseInt(v);
};
Path = require('path');
Log = require('log');
log = new Log('debug');
encryptPass = require('./encrypt');
md5 = function(str) {
var md5sum;
md5sum = crypto.createHash('md5');
return md5sum.update(str.toString()).digest('hex');
};
var all_cookies_str = function() {
var str = "";
for(var i=0; i<all_cookies.length; i++) {
str += all_cookies[i].split(' ')[0];
}
return str;
};
exports.cookies = function(cookies) {
if (cookies) {
all_cookies = cookies;
}
return all_cookies;
};
exports.prepare_login = function(callback) {
var url = 'https://ui.ptlogin2.qq.com/cgi-bin/login?daid=164&target=self&style=16&mibao_css=m_webqq&appid=501004106&enable_qlogin=0&no_verifyimg=1&s_url=http%3A%2F%2Fw.qq.com%2Fproxy.html&f_url=loginerroralert&strong_login=1&login_state=10&t=20131024001';
body = '';
body = '';
return https.get(url, function(resp) {
all_cookies = all_cookies.concat(resp.headers['set-cookie']);
resp.on('data', function(chunk) {
return body += chunk;
});
return resp.on('end', function() {
return callback([]);
});
}).on("error", function(e) {
return log.error(e);
});
};
exports.check_qq_verify = function(qq, callback) {
var body, options;
options = {
host: 'ssl.ptlogin2.qq.com',
path: '/ptqrlogin?webqq_type=10&remember_uin=1&login2qq=1&aid=501004106&u1=http%3A%2F%2Fw.qq.com%2Fproxy.html%3Flogin2qq%3D1%26webqq_type%3D10&ptredirect=0&ptlang=2052&daid=164&from_ui=1&pttype=1&dumy=&fp=loginerroralert&action=0-0-' + (Math.random() * 900000 + 1000000) +'&mibao_css=m_webqq&t=undefined&g=1&js_type=0&js_ver=10138&login_sig=&pt_randsalt=0',
headers: {
'Cookie': all_cookies_str() + 'RK=OfeLBai4FB; ptcz=ad3bf14f9da2738e09e498bfeb93dd9da7540dea2b7a71acfb97ed4d3da4e277; pgv_pvi=911366144; ETK=; superuin=o3136623241; superkey=GEjp3*qREn3dtFlOtraYOb-0JwYT8Yh776kQCIBe*wU_; supertoken=2909781602; ptisp=ctc; ptnick_3136623241=e88194e8908ce69cbae599a8e4baba; pgv_info=ssid=s2810019118; pgv_pvid=1051433466; o_cookie=3136623241; qrsig=hJ9GvNx*oIvLjP5I5dQ19KPa3zwxNI62eALLO*g2JLbKPYsZIRsnbJIxNe74NzQQ',
'Referer':'https://ui.ptlogin2.qq.com/cgi-bin/login?daid=164&target=self&style=16&mibao_css=m_webqq&appid=501004106&enable_qlogin=0&no_verifyimg=1&s_url=http%3A%2F%2Fw.qq.com%2Fproxy.html&f_url=loginerroralert&strong_login=1&login_state=10&t=20131024001'
}
};
body = '';
return https.get(options, function(resp) {
//all_cookies = resp.headers['set-cookie'];
for(var k in resp.headers) {
//console.log(k + " = " + resp.headers[k]);
if(k == 'set-cookie') {
all_cookies = resp.headers[k];
}
}
resp.on('data', function(chunk) {
return body += chunk;
});
return resp.on('end', function() {
var ret;
//console.log( body );
ret = body.match(/\'(.*?)\'/g).map(function(i) {
var last;
last = i.length - 2;
return i.substr(1, last);
});
return callback(ret);
});
}).on("error", function(e) {
return log.error(e);
});
};
exports.get_qr_code = function(qq, host, port, cap_cd, callback) {
var body, url;
url = "https://ssl.ptlogin2.qq.com/ptqrshow?appid=501004106&e=0&l=M&s=5&d=72&v=4&t=" + Math.random();
body = '';
return https.get(url, function(resp) {
all_cookies = all_cookies.concat(resp.headers['set-cookie']);
resp.setEncoding('binary');
resp.on('data', function(chunk) {
return body += chunk;
});
return resp.on('end', function() {
create_img_server(host, port, body, resp.headers);
return callback();
});
}).on("error", function(e) {
log.error(e);
return callback(e);
});
};
exports.finish_verify_code = function() {
return stop_img_server();
};
img_server = null;
create_img_server = function(host, port, body, origin_headers) {
var file_path, fs;
if (img_server) {
return;
}
fs = require('fs');
file_path = Path.join(__dirname, "..", "tmp", "qrcode.jpg");
fs.writeFileSync(file_path, body, 'binary');
if (process.platform !== 'darwin') {
img_server = http.createServer(function(req, res) {
res.writeHead(200, origin_headers);
return res.end(body, 'binary');
});
return img_server.listen(port);
} else {
return;
}
};
stop_img_server = function() {
if (img_server) {
img_server.close();
}
return img_server = null;
};
exports.check_sig = function(url, callback) {
var body = '';
return http.get(url, function(resp) {
all_cookies = all_cookies.concat(resp.headers['set-cookie']);
resp.on('data', function(chunk) {
return body += chunk;
});
return resp.on('end', function() {
return callback(body);
});
}).on("error", function(e) {
log.error(e);
return callback(e);
});
};
exports.login_token = function(client_id, psessionid, callback) {
var body, data, options, ptwebqq, r, req;
if (client_id == null) {
client_id = null;
}
if (psessionid == null) {
psessionid = null;
}
client_id || (client_id = parseInt(Math.random() * 89999999) + 10000000);
client_id = parseInt(client_id);
ptwebqq = all_cookies.filter(function(item) {
return item.match(/ptwebqq/);
}).pop().replace(/ptwebqq\=(.*?);.*/, '$1');
r = {
status: "online",
ptwebqq: ptwebqq,
clientid: "" + client_id,
psessionid: psessionid
};
r = JSON.stringify(r);
data = querystring.stringify({
r: r
});
body = '';
options = {
host: 'd.web2.qq.com',
path: '/channel/login2',
method: 'POST',
headers: {
'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10.9; rv:27.0) Gecko/20100101 Firefox/27.0',
'Referer': 'http://d.web2.qq.com/proxy.html?v=20110331002&callback=1&id=3',
'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8',
'Content-Length': Buffer.byteLength(data),
'Cookie': all_cookies
}
};
req = http.request(options, function(resp) {
log.debug("login token response: " + resp.statusCode);
resp.on('data', function(chunk) {
return body += chunk;
});
return resp.on('end', function() {
var ret;
ret = JSON.parse(body);
return callback(ret, client_id, ptwebqq);
});
});
req.write(data);
return req.end();
};
/*
全局登录函数,如果有验证码会建立一个 http-server ,同时写入 tmp/*.jpg (osx + open. 操作)
http-server 的端口和显示地址可配置
@param options {account,password,port,host}
@callback( cookies , auth_options ) if login success
*/
exports.login = function(options, callback) {
var auth, opt, pass, qq, ref, ref1;
ref = [exports, options], auth = ref[0], opt = ref[1];
ref1 = [opt.account, opt.password], qq = ref1[0], pass = ref1[1];
return auth.prepare_login(function(result){
log.info('登录 step0 二维码检测');
return auth.check_qq_verify(qq, function(result) {
var bits, need_verify, pass_encrypted, verifySession, verify_code;
need_verify = result[0], verify_code = result[1], bits = result[2], verifySession = result[3];
if (int(need_verify) == 65) {
log.info("登录 step0.5 获取二维码");
return auth.get_qr_code(qq, opt.host, opt.port, verify_code, function(error) {
if (process.platform === 'darwin') {
require('child_process').exec('open tmp/qrcode.jpg');
log.notice("请用 手机QQ 扫描该二维码");
} else {
log.notice("打开该地址->", "http://" + opt.host + ":" + opt.port);
}
return auth.prompt("手机QQ扫描二维码后, 回车继续: ", function(code) {
log.info("登录 step1 等待二维码校验结果");
return auth.check_qq_verify(qq, function(ret) {
if( int(ret[0]) == 0 && ret[2].match(/^http/)) {
console.log( ret[5] + ret[4] );
log.info("登录 step2 cookie获取");
return auth.check_sig(ret[2], function(ret){
log.info("登录 step3 token 获取");
return auth.login_token(null, null, function(ret, client_id, ptwebqq) {
var auth_options;
if (ret.retcode === 0) {
log.info('登录 token 获取成功');
auth_options = {
psessionid: ret.result.psessionid,
clientid: client_id,
ptwebqq: ptwebqq,
uin: ret.result.uin,
vfwebqq: ret.result.vfwebqq
};
console.log( auth_options );
return callback(all_cookies, auth_options);
} else {
log.info("登录失败");
return log.error(ret);
}
});
});
} else {
log.error("登录 step1 failed", ret);
return;
}
});
});
});
} else {
console.log(result);
}
});
});
};
exports.prompt = function(title, callback) {
process.stdin.resume();
process.stdout.write(title);
process.on("data", function(data) {
callback(data);
return process.stdin.pause();
});
process.stdin.on("data", function(data) {
data = data.toString().trim();
callback(data);
return process.stdin.pause();
});
return process.stdin.on('end', function() {
process.stdout.write('end');
return callback();
});
};
}).call(this);