1
|
|
|
'use strict';
|
2
|
|
|
|
3
|
|
|
const superagent = require('superagent'),
|
4
|
|
|
co = require('co'),
|
5
|
|
|
thunkify = require('thunkify');
|
6
|
|
|
|
7
|
|
|
superagent.Request.prototype.endThunk = thunkify(superagent.Request.prototype.end);
|
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
|
|
|
} while (i < input.length);
|
31
|
|
|
return output;
|
32
|
|
|
};
|
33
|
|
|
|
34
|
|
|
exports = module.exports = {
|
35
|
|
|
// 登入模块
|
36
|
|
|
login: co.wrap(function *(id, pwd) {
|
37
|
|
|
let ires, iires;
|
38
|
|
|
|
39
|
|
|
// 通过GET首页,来获取cookie
|
40
|
|
|
try {
|
41
|
|
|
ires = yield superagent
|
42
|
|
|
.get('http://csujwc.its.csu.edu.cn/jsxsd')
|
43
|
|
|
.endThunk();
|
44
|
|
|
} catch (err) {
|
45
|
|
|
let message = {
|
46
|
|
|
inner: `Failed to get the Cookie for login.\n${err.stack}`,
|
47
|
|
|
public: '获取Cookie失败'
|
48
|
|
|
};
|
49
|
|
|
throw message;
|
50
|
|
|
}
|
51
|
|
|
|
52
|
|
|
const headers = {
|
53
|
|
|
Host: 'csujwc.its.csu.edu.cn',
|
54
|
|
|
Connection: 'keep-alive',
|
55
|
|
|
'Cache-Control': 'max-age=0',
|
56
|
|
|
Accept: 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8',
|
57
|
|
|
Origin: 'http://csujwc.its.csu.edu.cn',
|
58
|
|
|
'Upgrade-Insecure-Requests': 1,
|
59
|
|
|
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/55.0.2883.87 Safari/537.36',
|
60
|
|
|
'Content-Type': 'application/x-www-form-urlencoded',
|
61
|
|
|
Referer: 'http://csujwc.its.csu.edu.cn/jsxsd/',
|
62
|
|
|
'Accept-Encoding': 'gzip, deflate',
|
63
|
|
|
'Accept-Language': 'zh-CN,zh;q=0.8',
|
64
|
|
|
Cookie: ires.headers['set-cookie']
|
65
|
|
|
};
|
66
|
|
|
|
67
|
|
|
try {
|
68
|
|
|
iires = yield superagent
|
69
|
|
|
.post('http://csujwc.its.csu.edu.cn/jsxsd/xk/LoginToXk')
|
70
|
|
|
.set(headers)
|
71
|
|
|
.type('form')
|
72
|
|
|
.send({ encoded: `${encodeInp(id)}%%%${encodeInp(pwd)}` })
|
73
|
|
|
.endThunk();
|
74
|
|
|
} catch (err) {
|
75
|
|
|
let message = {
|
76
|
|
|
inner: `Failed to login\n${err.stack}`,
|
77
|
|
|
public: '登录失败'
|
78
|
|
|
};
|
79
|
|
|
throw message;
|
80
|
|
|
}
|
81
|
|
|
|
82
|
|
|
if (/POST/i.test(iires.req.method)) {
|
83
|
|
|
let err = new Error('The request method to the logged-in page is POST instead of GET');
|
84
|
|
|
let message = {
|
85
|
|
|
inner: `Failed to login (possibily id or password provided were wrong)\n${err.stack}`,
|
86
|
|
|
public: '登录失败,可能是用户名或密码错误,请确认参数已URL转义'
|
87
|
|
|
};
|
88
|
|
|
throw message;
|
89
|
|
|
}
|
90
|
|
|
|
91
|
|
|
return headers;
|
92
|
|
|
}),
|
93
|
|
|
// 登出模块
|
94
|
|
|
logout: co.wrap(function *(headers) {
|
95
|
|
|
try {
|
96
|
|
|
yield superagent
|
97
|
|
|
.get(`http://csujwc.its.csu.edu.cn/jsxsd/xk/LoginToXk?method=exit&tktime=${new Date().getTime()}`)
|
98
|
|
|
.set(headers)
|
99
|
|
|
.endThunk();
|
100
|
|
|
} catch (err) {
|
101
|
|
|
// res已经sent了,所以没有public的message
|
102
|
|
|
let message = {
|
103
|
|
|
inner: `Failed to logout\n${err.stack}`
|
104
|
|
|
};
|
105
|
|
|
throw message;
|
106
|
|
|
}
|
107
|
|
|
})
|
108
|
|
|
};
|
109
|
|
|
|