Completed
Push — master ( 9f3995...f59160 )
by Equim
01:09
created

access.js ➔ ... ➔ superagent.end   B

Complexity

Conditions 2
Paths 5

Size

Total Lines 45

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
nc 5
nop 2
dl 0
loc 45
rs 8.8571
c 0
b 0
f 0

1 Function

Rating   Name   Duplication   Size   Complexity  
A end 0 12 4
1
var superagent = require('superagent'),
2
    escaper = require('true-html-escape'),
3
    colors = require('colors'),
4
    Date = require('./Date.js');
0 ignored issues
show
Comprehensibility introduced by
You are shadowing the built-in type Date. This makes code hard to read, consider using a different name.
Loading history...
5
6
const timeStamp = () => new Date().format('[MM-dd hh:mm:ss] ');
7
8
// 直接从主页上扒下来的,哈希算法
9
const encodeInp = (input) => {
10
    let keyStr = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";
11
    let output = "";
12
    let chr1, chr2, chr3 = "";
13
    let enc1, enc2, enc3, enc4 = "";
14
    let i = 0;
15
    do {
16
        chr1 = input.charCodeAt(i++);
17
        chr2 = input.charCodeAt(i++);
18
        chr3 = input.charCodeAt(i++);
19
        enc1 = chr1 >> 2;
20
        enc2 = ((chr1 & 3) << 4) | (chr2 >> 4);
21
        enc3 = ((chr2 & 15) << 2) | (chr3 >> 6);
22
        enc4 = chr3 & 63;
23
        if (isNaN(chr2)) {
24
            enc3 = enc4 = 64
25
        } else if (isNaN(chr3)) {
26
            enc4 = 64
27
        }
28
        output = output + keyStr.charAt(enc1) + keyStr.charAt(enc2) + keyStr.charAt(enc3) + keyStr.charAt(enc4);
29
        chr1 = chr2 = chr3 = "";
0 ignored issues
show
Unused Code introduced by
The assignment to variable chr1 seems to be never used. Consider removing it.
Loading history...
Unused Code introduced by
The assignment to variable chr2 seems to be never used. Consider removing it.
Loading history...
Unused Code introduced by
The assignment to variable chr3 seems to be never used. Consider removing it.
Loading history...
30
        enc1 = enc2 = enc3 = enc4 = ""
0 ignored issues
show
Unused Code introduced by
The assignment to variable enc4 seems to be never used. Consider removing it.
Loading history...
Unused Code introduced by
The assignment to variable enc1 seems to be never used. Consider removing it.
Loading history...
Unused Code introduced by
The assignment to variable enc2 seems to be never used. Consider removing it.
Loading history...
Unused Code introduced by
The assignment to variable enc3 seems to be never used. Consider removing it.
Loading history...
31
    } while (i < input.length);
32
    return output;
33
};
34
35
// 登出时用到的函数,一样是从原网页扒下来的
36
const getRandomUrl = (htmlurl) => {
37
    let count = htmlurl.indexOf("?");
38
    let date = new Date();
39
    let t = Date.parse(date);    
40
    if (count < 0) {
41
        htmlurl = htmlurl + "?tktime=" + t;
42
    } else {
43
        htmlurl = htmlurl + "&tktime=" + t;
44
    }
45
    return htmlurl;
46
}
47
48
// 登录模块
49
const login = (id, pwd, res, callback) => {
50
    // 通过GET首页,来获取cookie
51
    superagent.get('http://csujwc.its.csu.edu.cn/jsxsd')
52
        .end(function (err, ires) {
53
            if (err) {
54
                console.log((timeStamp() + 'Failed to get the Cookie.\n' + err.stack).red);
1 ignored issue
show
Debugging Code introduced by
console.log looks like debug code. Are you sure you do not want to remove it?
Loading history...
55
                res.send({ error: '获取Cookie失败' });
56
                return;
57
            }
58
59
            // 登录POST请求的headers,是我抓包来的
60
            let headers = {
61
                Host: 'csujwc.its.csu.edu.cn',
62
                Connection: 'keep-alive',
63
                'Cache-Control': 'max-age=0',
64
                Accept: 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8',
65
                Origin: 'http://csujwc.its.csu.edu.cn',
66
                'Upgrade-Insecure-Requests': 1,
67
                'User-Agent': 'Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/55.0.2883.87 Safari/537.36',
68
                'Content-Type': 'application/x-www-form-urlencoded',
69
                Referer: 'http://csujwc.its.csu.edu.cn/jsxsd/',
70
                'Accept-Encoding': 'gzip, deflate',
71
                'Accept-Language': 'zh-CN,zh;q=0.8',
72
                Cookie: ires.headers['set-cookie']     // 猜测:感觉这个cookie也是不变的,不过出于稳定性,还是动态获取了
73
            }
74
75
            let account = encodeInp(id);
76
            let passwd = encodeInp(pwd);
77
            let encoded = escaper.escape(account + "%%%" + passwd);
78
79
            // POST登录
80
            superagent.post('http://csujwc.its.csu.edu.cn/jsxsd/xk/LoginToXk')
81
                .set(headers)
82
                .type('form')
83
                .send({ encoded: encoded })
84
                .end(function (err, iires) {
85
                    // 如果登录信息正确,这里是对http://csujwc.its.csu.edu.cn/jsxsd/framework/xsMain.jsp的GET请求
86
                    // 如果错误,会变成对http://csujwc.its.csu.edu.cn/jsxsd/xk/LoginToXk的POST请求
87
                    if (err || /POST/i.test(iires.req.method)) {
88
                        console.log((timeStamp() + 'Fail to login\n' + (err ? err.stack : 'Possibily id or password provided were wrong')).red);
1 ignored issue
show
Debugging Code introduced by
console.log looks like debug code. Are you sure you do not want to remove it?
Loading history...
89
                        res.send({ error: '登录失败,可能是用户名或密码错误' });
90
                        return;
91
                    }
92
                    
93
                    // 将相应的headers和返回的response(刚进去的首页)传入callback
94
                    callback(headers, iires);
95
                });
96
        });
97
};
98
99
// 登出模块
100
const logout = (headers, res, callback) => {
101
    superagent.get(getRandomUrl('http://csujwc.its.csu.edu.cn/jsxsd/xk/LoginToXk?method=exit'))
102
        .set(headers)
103
        .end(function (err, ires) {
0 ignored issues
show
Unused Code introduced by
The parameter ires is not used and could be removed.

This check looks for parameters in functions that are not used in the function body and are not followed by other parameters which are used inside the function.

Loading history...
104
            if (err) {
105
                console.log((timeStamp() + 'Fail to logout\n' + err.stack).red);
1 ignored issue
show
Debugging Code introduced by
console.log looks like debug code. Are you sure you do not want to remove it?
Loading history...
106
                res.send({ error: '操作失败' });
107
                return;
108
            }
109
            callback();
110
        });
111
};
112
113
module.exports.login = login;
114
module.exports.logout = logout;