Completed
Push — master ( 5e0f69...985619 )
by Equim
01:05
created

co.wrap   B

Complexity

Conditions 4
Paths 1

Size

Total Lines 50

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
nc 1
nop 3
dl 0
loc 50
rs 8.8571
c 0
b 0
f 0
1
'use strict';
2
3
const
4
    superagent = require('superagent'),
5
    colors     = require('colors'),
0 ignored issues
show
Unused Code introduced by
The constant colors seems to be never used. Consider removing it.
Loading history...
6
    co         = require('co'),
7
    thunkify   = require('thunkify'),
8
    moment     = require('moment');
9
10
superagent.Request.prototype.endThunk = thunkify(superagent.Request.prototype.end);
11
12
const timeStamp = () => moment().format('[[]YY-MM-DD HH:mm:ss[]]');
13
14
// 直接从主页上扒下来的,加密算法
15
const encodeInp = input => {
16
    const keyStr = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=';
17
    let output = '';
18
    let chr1, chr2, chr3 = '';
19
    let enc1, enc2, enc3, enc4 = '';
20
    let i = 0;
21
    do {
22
        chr1 = input.charCodeAt(i++);
23
        chr2 = input.charCodeAt(i++);
24
        chr3 = input.charCodeAt(i++);
25
        enc1 = chr1 >> 2;
26
        enc2 = ((chr1 & 3) << 4) | (chr2 >> 4);
27
        enc3 = ((chr2 & 15) << 2) | (chr3 >> 6);
28
        enc4 = chr3 & 63;
29
        if (isNaN(chr2)) {
30
            enc3 = enc4 = 64;
31
        } else if (isNaN(chr3)) {
32
            enc4 = 64;
33
        }
34
        output += keyStr.charAt(enc1) + keyStr.charAt(enc2) + keyStr.charAt(enc3) + keyStr.charAt(enc4);
35
        chr1 = chr2 = chr3 = '';
0 ignored issues
show
Unused Code introduced by
The assignment to variable chr3 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 chr1 seems to be never used. Consider removing it.
Loading history...
36
        enc1 = enc2 = enc3 = enc4 = '';
0 ignored issues
show
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...
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 enc4 seems to be never used. Consider removing it.
Loading history...
37
    } while (i < input.length);
38
    return output;
39
};
40
41
module.exports = {
42
    // 登入模块
43
    login: co.wrap(function* (id, pwd, res) {
44
        let ires, iires;
45
46
        // 通过GET首页,来获取cookie
47
        try {
48
            ires = yield superagent
49
                .get('http://csujwc.its.csu.edu.cn/jsxsd')
50
                .endThunk();
51
        } catch (err) {
52
            console.log(timeStamp() + ` Failed to get the Cookie.\n${err.stack}`.red);
53
            res.status(404).send({ error: '获取Cookie失败' });
54
            throw err;
55
        }
56
57
        const headers = {
58
            Host: 'csujwc.its.csu.edu.cn',
59
            Connection: 'keep-alive',
60
            'Cache-Control': 'max-age=0',
61
            Accept: 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8',
62
            Origin: 'http://csujwc.its.csu.edu.cn',
63
            'Upgrade-Insecure-Requests': 1,
64
            'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/55.0.2883.87 Safari/537.36',
65
            'Content-Type': 'application/x-www-form-urlencoded',
66
            Referer: 'http://csujwc.its.csu.edu.cn/jsxsd/',
67
            'Accept-Encoding': 'gzip, deflate',
68
            'Accept-Language': 'zh-CN,zh;q=0.8',
69
            Cookie: ires.headers['set-cookie']
70
        };
71
72
        try {
73
            iires = yield superagent
74
                .post('http://csujwc.its.csu.edu.cn/jsxsd/xk/LoginToXk')
75
                .set(headers)
76
                .type('form')
77
                .send({ encoded: `${encodeInp(id)}%%%${encodeInp(pwd)}` })
78
                .endThunk();
79
        } catch (err) {
80
            console.log(timeStamp() + ` Failed to login\n${err.stack}`.red);
81
            res.status(404).send({ error: '登录失败' });
82
            throw err;
83
        }
84
85
        if (/POST/i.test(iires.req.method)) {
86
            console.log(timeStamp() + ` Failed to login\nPossibily id or password provided were wrong`.red);
87
            res.status(404).send({ error: '登录失败,可能是用户名或密码错误,请确认参数已URL转义' });
88
            throw new Error();
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
            console.log(timeStamp() + ` Failed to logout\n${err.stack}`.red);
102
        }
103
    })
104
};
105