1
|
|
|
'use strict';
|
2
|
|
|
|
3
|
|
|
var superagent = require('superagent'),
|
4
|
|
|
colors = require('colors'),
|
5
|
|
|
moment = require('moment');
|
6
|
|
|
|
7
|
|
|
const timeStamp = () => moment().format('[[]YY-MM-DD HH:mm:ss[]]');
|
8
|
|
|
|
9
|
|
|
// 直接从主页上扒下来的,加密算法
|
10
|
|
|
const encodeInp = (input) => {
|
11
|
|
|
const keyStr = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=';
|
12
|
|
|
let output = '';
|
13
|
|
|
let chr1, chr2, chr3 = '';
|
14
|
|
|
let enc1, enc2, enc3, enc4 = '';
|
15
|
|
|
let i = 0;
|
16
|
|
|
do {
|
17
|
|
|
chr1 = input.charCodeAt(i++);
|
18
|
|
|
chr2 = input.charCodeAt(i++);
|
19
|
|
|
chr3 = input.charCodeAt(i++);
|
20
|
|
|
enc1 = chr1 >> 2;
|
21
|
|
|
enc2 = ((chr1 & 3) << 4) | (chr2 >> 4);
|
22
|
|
|
enc3 = ((chr2 & 15) << 2) | (chr3 >> 6);
|
23
|
|
|
enc4 = chr3 & 63;
|
24
|
|
|
if (isNaN(chr2)) {
|
25
|
|
|
enc3 = enc4 = 64;
|
26
|
|
|
} else if (isNaN(chr3)) {
|
27
|
|
|
enc4 = 64;
|
28
|
|
|
}
|
29
|
|
|
output += keyStr.charAt(enc1) + keyStr.charAt(enc2) + keyStr.charAt(enc3) + keyStr.charAt(enc4);
|
30
|
|
|
chr1 = chr2 = chr3 = '';
|
|
|
|
|
31
|
|
|
enc1 = enc2 = enc3 = enc4 = '';
|
|
|
|
|
32
|
|
|
} while (i < input.length);
|
33
|
|
|
return output;
|
34
|
|
|
};
|
35
|
|
|
|
36
|
|
|
module.exports = {
|
37
|
|
|
// 登入模块
|
38
|
|
|
login(id, pwd, res, callback) {
|
39
|
|
|
// 通过GET首页,来获取cookie
|
40
|
|
|
superagent
|
41
|
|
|
.get('http://csujwc.its.csu.edu.cn/jsxsd')
|
42
|
|
|
.end(function (err, ires) {
|
43
|
|
|
if (err) {
|
44
|
|
|
console.log(`${timeStamp()} Failed to get the Cookie.\n${err.stack}`.red);
|
45
|
|
|
res.status(404).send({ error: '获取Cookie失败' });
|
46
|
|
|
return;
|
47
|
|
|
}
|
48
|
|
|
|
49
|
|
|
// 登录POST请求的headers,是我抓包来的
|
50
|
|
|
let headers = {
|
51
|
|
|
Host: 'csujwc.its.csu.edu.cn',
|
52
|
|
|
Connection: 'keep-alive',
|
53
|
|
|
'Cache-Control': 'max-age=0',
|
54
|
|
|
Accept: 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8',
|
55
|
|
|
Origin: 'http://csujwc.its.csu.edu.cn',
|
56
|
|
|
'Upgrade-Insecure-Requests': 1,
|
57
|
|
|
'User-Agent': 'Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/55.0.2883.87 Safari/537.36',
|
58
|
|
|
'Content-Type': 'application/x-www-form-urlencoded',
|
59
|
|
|
Referer: 'http://csujwc.its.csu.edu.cn/jsxsd/',
|
60
|
|
|
'Accept-Encoding': 'gzip, deflate',
|
61
|
|
|
'Accept-Language': 'zh-CN,zh;q=0.8',
|
62
|
|
|
Cookie: ires.headers['set-cookie'] // 猜测:感觉这个cookie也是不变的,不过出于稳定性,还是动态获取了
|
63
|
|
|
};
|
64
|
|
|
|
65
|
|
|
// POST登录
|
66
|
|
|
superagent
|
67
|
|
|
.post('http://csujwc.its.csu.edu.cn/jsxsd/xk/LoginToXk')
|
68
|
|
|
.set(headers)
|
69
|
|
|
.type('form')
|
70
|
|
|
.send({ encoded: `${encodeInp(id)}%%%${encodeInp(pwd)}` })
|
71
|
|
|
.end(function (err, iires) {
|
72
|
|
|
// 如果登录信息正确,这里是对http://csujwc.its.csu.edu.cn/jsxsd/framework/xsMain.jsp的GET请求
|
73
|
|
|
// 如果错误,会变成对http://csujwc.its.csu.edu.cn/jsxsd/xk/LoginToXk的POST请求
|
74
|
|
|
if (err) {
|
75
|
|
|
console.log(`${timeStamp()} Failed to login\n${err.stack}`.red);
|
76
|
|
|
res.status(404).send({ error: '登录失败' });
|
77
|
|
|
return;
|
78
|
|
|
}
|
79
|
|
|
if (/POST/i.test(iires.req.method)) {
|
80
|
|
|
console.log(`${timeStamp()} Failed to login\nPossibily id or password provided were wrong`.red);
|
81
|
|
|
res.status(404).send({ error: '登录失败,可能是用户名或密码错误,请确认参数已URL转义' });
|
82
|
|
|
return;
|
83
|
|
|
}
|
84
|
|
|
// 将相应的headers传入callback
|
85
|
|
|
callback(headers);
|
86
|
|
|
});
|
87
|
|
|
});
|
88
|
|
|
},
|
89
|
|
|
// 登出模块
|
90
|
|
|
logout(headers, res, callback) {
|
91
|
|
|
//console.log(`${timeStamp()} Started to logout`.green);
|
92
|
|
|
superagent
|
93
|
|
|
.get(`http://csujwc.its.csu.edu.cn/jsxsd/xk/LoginToXk?method=exit&tktime=${new Date().getTime()}`)
|
94
|
|
|
.set(headers)
|
95
|
|
|
.end(function (err) {
|
96
|
|
|
if (err) {
|
97
|
|
|
console.log(`${timeStamp()} Failed to logout\n${err.stack}`.red);
|
98
|
|
|
res.status(404).send({ error: '登出失败' });
|
99
|
|
|
return;
|
100
|
|
|
}
|
101
|
|
|
callback();
|
102
|
|
|
});
|
103
|
|
|
}
|
104
|
|
|
};
|
105
|
|
|
|