summaryrefslogtreecommitdiff
path: root/index.js
blob: 5a482e8af762d3f0e0739803b0152b1cf03c7b07 (plain)
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
import lodash from "lodash";
import { networkInterfaces } from "node:os";

const REFERER = "https://w.seu.edu.cn/";
const EPORTAL_API = "https://w.seu.edu.cn:802/eportal";

(async () => {
    try {
        const credentials = JSON.parse(await Deno.readTextFile("./credentials.json"));

        if (lodash.isEmpty(credentials.username) || lodash.isEmpty(credentials.password)) {
            throw "Invalid credentials! ";
        }

        const { ipv4, ipv6 } = getIP();

        let headers = {
            "User-Agent": "Mozilla/5.0 (X11; Linux x86_64; rv:109.0) Gecko/20100101 Firefox/117.0",
        };
        const resp = await fetch(`${EPORTAL_API}/?c=Portal&a=login&callback=dr1004&login_method=1&user_account=%2C0%2C${credentials.username}&user_password=${credentials.password}&wlan_user_ip=${lodash.isEmpty(ipv4) ? '' : ipv4}&wlan_user_ipv6=${lodash.isEmpty(ipv6) ? '' : ipv6}&wlan_user_mac=000000000000&wlan_ac_ip=&wlan_ac_name=&jsVersion=3.3.3`, {
            "credentials": "include",
            headers,
            "referrer": REFERER,
            "mode": "cors"
        });
        if (resp.ok) {
            const resp_text = await resp.text();
            const re = /dr1004\((.*?)\)/g;
            const match = re.exec(resp_text);
            if (lodash.isEmpty(match) || match.length <= 1) {
                throw "No match in response!";
            }
            const parsedData = JSON.parse(match[1]);
            if (parsedData.result == 1) {
                console.log("Login success!");
            } else if (parsedData.result == 0 && parsedData.ret_code == 2) {
                console.log("Already logged in!")
            } else {
                throw `Login failed.\nresponse JSON: ${match[1]}`;
            }
        }

    } catch (err) {
        console.log(err);
        if (err instanceof SyntaxError) {
            exitWithError("Parse json failed!");
        } else if (err instanceof Deno.errors.NotFound) {
            exitWithError("./credentials.json not found!")
        }
        else {
            exitWithError('');
        }

    }
})();

function exitWithError(errstr) {
    console.error(errstr);
    Deno.exit(1);
}

function getIP() {
    let ipv4, ipv6;
    const netIf = networkInterfaces();
    const wlanIf = netIf.wlan0 || netIf.WLAN;
    if (lodash.isEmpty(wlanIf)) {
        exitWithError("No valid wireless network interface Found!");
    }

    for (let i = 0; i < wlanIf.length; i++) {
        var alias = wlanIf[i];
        if (alias.family === 'IPv4' && !alias.internal) {
            const re = /^169.254.*/g;
            if (lodash.isEmpty(alias.address.match(re))) {
                ipv4 = alias.address;
            }
        } else if (alias.family === 'IPv6' && !alias.internal) {
            const re = /^fe80::*/g;
            if (lodash.isEmpty(alias.address.match(re))) {
                ipv6 = alias.address;
            }
        }
    }
    return { ipv4, ipv6 };
}