This repository has been archived by the owner on Apr 17, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 252
/
cookies.js
94 lines (83 loc) · 2.59 KB
/
cookies.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
/**
* Created by sulian on 13-11-4.
* 用于读取、写入或返回可用cookie
*/
var cheerio = require("cheerio");
var tools = require("./tools");
var config = require("./config");
var db = require("./db");
var logger = require("./logger");
var maxretry = 5;//最大重试次数,cookie无效达到此数则发警告邮件
var cookieuser = null;//用于访问的用户信息
//获取一个可用的cookie,附加xsrf(session标记)
exports.getCookie = function (callback) {
getcookieuser(function (err) {
if (err) {
callback(err);
return;
}
if (!cookieuser.cookie) {
callback("Cookie string is null.");
return;
}
trygetcookie(0, callback);
})
}
//尝试读取cookie,如果多次失败则报错
function trygetcookie(retry, callback) {
tools.get(config.urlpre, cookieuser.cookie, function (err, data) {
if (err) {
if (retry >= maxretry)
callback("Get homepage error.<br/>" + err);
else
trygetcookie(retry + 1, callback);
return;
}
var $ = cheerio.load(data, {decodeEntities: false});
var findname = $(".zu-top-nav-userinfo .name").html();//寻找已登录用户名,如果找不到说明cookie失效,登录失败
if (findname != cookieuser.name) {
if (retry >= maxretry)
callback("Invalid cookie.");
else
trygetcookie(retry + 1, callback);
return;
}
var xsrf = $("input[name='_xsrf']").val();
if (!xsrf) {
if (retry >= maxretry)
callback("Cannot read xsrf.");
else
trygetcookie(retry + 1, callback);
return
}
var fullcookie = cookieuser.cookie + '_xsrf=' + xsrf;//带sessionid的cookie
callback(null, fullcookie, xsrf);
})
}
//获取hash
exports.getHash = function () {
if (cookieuser) return cookieuser.hash;
else return null;
}
//获取用户名
exports.getName = function () {
if (cookieuser) return cookieuser.name;
else return null;
}
//从数据库读取用户信息
function getcookieuser(callback) {
db.query("select email, password, name, hash, cookie from cookies", function (err, rows) {
if (err) {
cookieuser = null;
callback(err);
}
else if (rows.length == 0) {
cookieuser = null;
callback("No cookie user in database.");
}
else {
cookieuser = rows[0];
callback();
}
})
}