diff options
-rw-r--r-- | deno.json | 1 | ||||
-rw-r--r-- | deno.lock | 3 | ||||
-rw-r--r-- | index.js | 102 |
3 files changed, 76 insertions, 30 deletions
@@ -1,6 +1,7 @@ { "imports": { "lodash": "https://esm.sh/[email protected]", + "flags/": "https://deno.land/std/flags/", "./": "./" } "tasks": { @@ -1,6 +1,9 @@ { "version": "2", "remote": { + "https://deno.land/[email protected]/assert/assert.ts": "9a97dad6d98c238938e7540736b826440ad8c1c1e54430ca4c4e623e585607ee", + "https://deno.land/[email protected]/assert/assertion_error.ts": "4d0bde9b374dfbcbe8ac23f54f567b77024fb67dbb1906a852d67fe050d42f56", + "https://deno.land/[email protected]/flags/mod.ts": "0948466fc437f017f00c0b972a422b3dc3317a790bcf326429d23182977eaf9f", "https://esm.sh/[email protected]": "ae454206d9631a28c229a2ce9920262cc5cfaa2a23e46b3c03069daa4e94cf10", "https://esm.sh/v131/[email protected]/denonext/lodash.mjs": "03507ec58790b76db2d03270878c70b943b83099cdfdcad2b0fa8759abc61057" } @@ -1,10 +1,20 @@ import lodash from "lodash"; +import { parse } from "flags/mod.ts"; import { networkInterfaces } from "node:os"; const REFERER = "https://w.seu.edu.cn/"; const EPORTAL_API = "https://w.seu.edu.cn:802/eportal"; +const headers = { + "User-Agent": "Mozilla/5.0 (X11; Linux x86_64; rv:109.0) Gecko/20100101 Firefox/117.0", +}; (async () => { + + const flags = parse(Deno.args, { + boolean: ["login", "logout"], + default: { login: true } + }); + try { const credentials = JSON.parse(await Deno.readTextFile("./credentials.json")); @@ -12,42 +22,24 @@ const EPORTAL_API = "https://w.seu.edu.cn:802/eportal"; throw "Invalid credentials! "; } - const { ipv4, ipv6 } = getIP(); + const { ipv4, ipv6, mac } = getNetIfInfo(); - let headers = { - "User-Agent": "Mozilla/5.0 (X11; Linux x86_64; rv:109.0) Gecko/20100101 Firefox/117.0", + if (flags.logout) { + const LOGOUT_URL = `${EPORTAL_API}/?c=Portal&a=unbind_mac&callback=dr1004&user_account=&wlan_user_mac=${mac.replaceAll(":", "").toUpperCase()}&wlan_user_ip=${ipv4}&jsVersion=3.3.3`; + await logout(LOGOUT_URL); + } else { + const LOGIN_URL = `${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`; + await login(LOGIN_URL); }; - 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 == 1 || 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 { + console.error(`[ERROR]: ${err.toString()}`); exitWithError(''); } @@ -55,12 +47,12 @@ const EPORTAL_API = "https://w.seu.edu.cn:802/eportal"; })(); function exitWithError(errstr) { - console.error(errstr); + if (!lodash.isEmpty(errstr)) { console.error(errstr) }; Deno.exit(1); } -function getIP() { - let ipv4, ipv6; +function getNetIfInfo() { + let ipv4, ipv6, mac; const netIf = networkInterfaces(); const wlanIf = netIf.wlan0 || netIf.WLAN; if (lodash.isEmpty(wlanIf)) { @@ -80,6 +72,56 @@ function getIP() { ipv6 = alias.address; } } + mac = alias.mac; + } + return { ipv4, ipv6, mac }; +} + +async function login(LOGIN_URL) { + + const resp = await fetch(LOGIN_URL, { + "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 == 1 || parsedData.ret_code == 2)) { + console.log("Already logged in!") + } else { + throw `Login failed.${lodash.isEmpty(parsedData.msg) ? '' : `\nServer responds message: ${parsedData.msg}`}`; + } + } +} + +async function logout(LOGOUT_URL) { + const resp = await fetch(LOGOUT_URL, { + "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("Logout success!"); + } else { + throw `Logout failed.${lodash.isEmpty(parsedData.msg) ? '' : `\nServer responds message: ${parsedData.msg}`}`; + } } - return { ipv4, ipv6 }; }
\ No newline at end of file |