diff options
| -rw-r--r-- | deno.json | 10 | ||||
| -rw-r--r-- | deno.lock | 67 | ||||
| -rw-r--r-- | index.js | 211 |
3 files changed, 138 insertions, 150 deletions
@@ -1,12 +1,12 @@ { "imports": { "lodash": "https://esm.sh/lodash@4.17.21", - "flags/": "https://deno.land/std/flags/", - "path/": "https://deno.land/std/path/", + "path": "jsr:@std/path", + "parse-args": "jsr:@std/cli/parse-args", "./": "./" - } + }, "tasks": { - "start": "deno run --allow-net --allow-read --allow-sys index.js" - "compile": "deno compile --allow-net --allow-read --allow-sys index.js -- --is_compiled_binary" + "start": "deno run --allow-net --allow-read --allow-sys index.js", + "compile": "deno compile --allow-net --allow-read --allow-sys index.js --is_compiled_binary" } } @@ -1,46 +1,29 @@ { - "version": "2", + "version": "5", + "specifiers": { + "jsr:@std/cli@*": "1.0.15", + "jsr:@std/path@*": "1.0.8" + }, + "jsr": { + "@std/cli@1.0.15": { + "integrity": "e79ba3272ec710ca44d8342a7688e6288b0b88802703f3264184b52893d5e93f" + }, + "@std/path@1.0.8": { + "integrity": "548fa456bb6a04d3c1a1e7477986b6cffbce95102d0bb447c67c4ee70e0364be" + } + }, + "redirects": { + "https://esm.sh/v135/@types/lodash@4.17.13/index": "https://esm.sh/v135/@types/lodash@4.17.13/index~.d.ts", + "https://esm.sh/v135/@types/lodash@~4.17/index.d.ts": "https://esm.sh/v135/@types/lodash@4.17.13/index.d.ts" + }, "remote": { - "https://deno.land/std@0.201.0/assert/assert.ts": "9a97dad6d98c238938e7540736b826440ad8c1c1e54430ca4c4e623e585607ee", - "https://deno.land/std@0.201.0/assert/assertion_error.ts": "4d0bde9b374dfbcbe8ac23f54f567b77024fb67dbb1906a852d67fe050d42f56", - "https://deno.land/std@0.201.0/flags/mod.ts": "0948466fc437f017f00c0b972a422b3dc3317a790bcf326429d23182977eaf9f", - "https://deno.land/std@0.201.0/path/_basename.ts": "057d420c9049821f983f784fd87fa73ac471901fb628920b67972b0f44319343", - "https://deno.land/std@0.201.0/path/_constants.ts": "e49961f6f4f48039c0dfed3c3f93e963ca3d92791c9d478ac5b43183413136e0", - "https://deno.land/std@0.201.0/path/_dirname.ts": "355e297236b2218600aee7a5301b937204c62e12da9db4b0b044993d9e658395", - "https://deno.land/std@0.201.0/path/_extname.ts": "eaaa5aae1acf1f03254d681bd6a8ce42a9cb5b7ff2213a9d4740e8ab31283664", - "https://deno.land/std@0.201.0/path/_format.ts": "4a99270d6810f082e614309164fad75d6f1a483b68eed97c830a506cc589f8b4", - "https://deno.land/std@0.201.0/path/_from_file_url.ts": "6eadfae2e6f63ad9ee46b26db4a1b16583055c0392acedfb50ed2fc694b6f581", - "https://deno.land/std@0.201.0/path/_interface.ts": "6471159dfbbc357e03882c2266d21ef9afdb1e4aa771b0545e90db58a0ba314b", - "https://deno.land/std@0.201.0/path/_is_absolute.ts": "05dac10b5e93c63198b92e3687baa2be178df5321c527dc555266c0f4f51558c", - "https://deno.land/std@0.201.0/path/_join.ts": "815f5e85b042285175b1492dd5781240ce126c23bd97bad6b8211fe7129c538e", - "https://deno.land/std@0.201.0/path/_normalize.ts": "a19ec8706b2707f9dd974662a5cd89fad438e62ab1857e08b314a8eb49a34d81", - "https://deno.land/std@0.201.0/path/_os.ts": "d932f56d41e4f6a6093d56044e29ce637f8dcc43c5a90af43504a889cf1775e3", - "https://deno.land/std@0.201.0/path/_parse.ts": "0f9b0ff43682dd9964eb1c4398610c4e165d8db9d3ac9d594220217adf480cfa", - "https://deno.land/std@0.201.0/path/_relative.ts": "27bdeffb5311a47d85be26d37ad1969979359f7636c5cd9fcf05dcd0d5099dc5", - "https://deno.land/std@0.201.0/path/_resolve.ts": "7a3616f1093735ed327e758313b79c3c04ea921808ca5f19ddf240cb68d0adf6", - "https://deno.land/std@0.201.0/path/_to_file_url.ts": "a141e4a525303e1a3a0c0571fd024552b5f3553a2af7d75d1ff3a503dcbb66d8", - "https://deno.land/std@0.201.0/path/_to_namespaced_path.ts": "0d5f4caa2ed98ef7a8786286df6af804b50e38859ae897b5b5b4c8c5930a75c8", - "https://deno.land/std@0.201.0/path/_util.ts": "4e191b1bac6b3bf0c31aab42e5ca2e01a86ab5a0d2e08b75acf8585047a86221", - "https://deno.land/std@0.201.0/path/basename.ts": "bdfa5a624c6a45564dc6758ef2077f2822978a6dbe77b0a3514f7d1f81362930", - "https://deno.land/std@0.201.0/path/common.ts": "ee7505ab01fd22de3963b64e46cff31f40de34f9f8de1fff6a1bd2fe79380000", - "https://deno.land/std@0.201.0/path/dirname.ts": "b6533f4ee4174a526dec50c279534df5345836dfdc15318400b08c62a62a39dd", - "https://deno.land/std@0.201.0/path/extname.ts": "62c4b376300795342fe1e4746c0de518b4dc9c4b0b4617bfee62a2973a9555cf", - "https://deno.land/std@0.201.0/path/format.ts": "110270b238514dd68455a4c54956215a1aff7e37e22e4427b7771cefe1920aa5", - "https://deno.land/std@0.201.0/path/from_file_url.ts": "9f5cb58d58be14c775ec2e57fc70029ac8b17ed3bd7fe93e475b07280adde0ac", - "https://deno.land/std@0.201.0/path/glob.ts": "593e2c3573883225c25c5a21aaa8e9382a696b8e175ea20a3b6a1471ad17aaed", - "https://deno.land/std@0.201.0/path/is_absolute.ts": "0b92eb35a0a8780e9f16f16bb23655b67dace6a8e0d92d42039e518ee38103c1", - "https://deno.land/std@0.201.0/path/join.ts": "31c5419f23d91655b08ec7aec403f4e4cd1a63d39e28f6e42642ea207c2734f8", - "https://deno.land/std@0.201.0/path/mod.ts": "6e1efb0b13121463aedb53ea51dabf5639a3172ab58c89900bbb72b486872532", - "https://deno.land/std@0.201.0/path/normalize.ts": "6ea523e0040979dd7ae2f1be5bf2083941881a252554c0f32566a18b03021955", - "https://deno.land/std@0.201.0/path/parse.ts": "be8de342bb9e1924d78dc4d93c45215c152db7bf738ec32475560424b119b394", - "https://deno.land/std@0.201.0/path/posix.ts": "0a1c1952d132323a88736d03e92bd236f3ed5f9f079e5823fae07c8d978ee61b", - "https://deno.land/std@0.201.0/path/relative.ts": "8bedac226afd360afc45d451a6c29fabceaf32978526bcb38e0c852661f66c61", - "https://deno.land/std@0.201.0/path/resolve.ts": "133161e4949fc97f9ca67988d51376b0f5eef8968a6372325ab84d39d30b80dc", - "https://deno.land/std@0.201.0/path/separator.ts": "40a3e9a4ad10bef23bc2cd6c610291b6c502a06237c2c4cd034a15ca78dedc1f", - "https://deno.land/std@0.201.0/path/to_file_url.ts": "00e6322373dd51ad109956b775e4e72e5f9fa68ce2c6b04e4af2a6eed3825d31", - "https://deno.land/std@0.201.0/path/to_namespaced_path.ts": "1b1db3055c343ab389901adfbda34e82b7386bcd1c744d54f9c1496ee0fd0c3d", - "https://deno.land/std@0.201.0/path/win32.ts": "8b3f80ef7a462511d5e8020ff490edcaa0a0d118f1b1e9da50e2916bdd73f9dd", - "https://esm.sh/lodash@4.17.21": "ae454206d9631a28c229a2ce9920262cc5cfaa2a23e46b3c03069daa4e94cf10", - "https://esm.sh/v131/lodash@4.17.21/denonext/lodash.mjs": "03507ec58790b76db2d03270878c70b943b83099cdfdcad2b0fa8759abc61057" + "https://esm.sh/lodash@4.17.21": "cf3544d5159a7648b25ad21fcf8dbf08a1fbfb1415b70b4163da646ef83eec4a", + "https://esm.sh/v135/lodash@4.17.21/denonext/lodash.mjs": "f04a5db09228738fd8cd06b6d1eaf3463b1b639d1529cf11673c3ac7bda1b1a8" + }, + "workspace": { + "dependencies": [ + "jsr:@std/cli@*", + "jsr:@std/path@*" + ] } } @@ -1,133 +1,138 @@ import lodash from "lodash"; -import { parse } from "flags/mod.ts"; -import { dirname, fromFileUrl, SEP } from "path/mod.ts"; +import { parseArgs } from "parse-args"; +import { dirname, fromFileUrl, SEPARATOR } from "path"; 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", + "User-Agent": + "Mozilla/5.0 (X11; Linux x86_64; rv:109.0) Gecko/20100101 Firefox/117.0", }; const PROG_PATH = Deno.args.includes("--is_compiled_binary") - ? Deno.execPath() - : fromFileUrl(Deno.mainModule); -const CONFIG_FILE = `${dirname(PROG_PATH)}${SEP}credentials.json`; + ? Deno.execPath() + : fromFileUrl(Deno.mainModule); +const CONFIG_FILE = `${dirname(PROG_PATH)}${SEPARATOR}credentials.json`; -(async () => { - - const flags = parse(Deno.args, { - boolean: ["login", "logout"], - string: ["config"], - default: { login: true, config: CONFIG_FILE } - }); - - try { - const credentials = JSON.parse(await Deno.readTextFile(flags.config)); +const flags = parseArgs(Deno.args, { + boolean: ["login", "logout"], + string: ["config", "interface"], + default: { login: true, config: CONFIG_FILE }, +}); - if (lodash.isEmpty(credentials.username) || lodash.isEmpty(credentials.password)) { - throw "Invalid credentials! "; - } - - const { ipv4, ipv6, mac } = getNetIfInfo(); +(async () => { + try { + const credentials = JSON.parse(await Deno.readTextFile(flags.config)); - 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); - }; + if ( + lodash.isEmpty(credentials.username) || + lodash.isEmpty(credentials.password) + ) { + throw "Invalid credentials! "; + } - } catch (err) { - if (err instanceof SyntaxError) { - exitWithError("Parse json failed!"); - } else if (err instanceof Deno.errors.NotFound) { - exitWithError(`${CONFIG_FILE} not found!`) - } - else { - console.error(`[ERROR]: ${err.toString()}`); - exitWithError(''); - } + const { ipv4, ipv6, mac } = getNetIfInfo(); + 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); } + } catch (err) { + if (err instanceof SyntaxError) { + exitWithError("Parse json failed!"); + } else if (err instanceof Deno.errors.NotFound) { + exitWithError(`${CONFIG_FILE} not found!`); + } else { + console.error(`[ERROR]: ${err.toString()}`); + exitWithError(""); + } + } })(); function exitWithError(errstr) { - if (!lodash.isEmpty(errstr)) { console.error(errstr) }; - Deno.exit(1); + if (!lodash.isEmpty(errstr)) { + console.error(errstr); + } + Deno.exit(1); } function getNetIfInfo() { - let ipv4, ipv6, mac; - const netIf = networkInterfaces(); - const wlanIf = netIf.wlan0 || netIf.WLAN; - if (lodash.isEmpty(wlanIf)) { - exitWithError("No valid wireless network interface Found!"); - } + let ipv4, ipv6, mac; + const netIf = networkInterfaces(); + const wlanIf = + netIf[flags.interface] || netIf.wlan0 || netIf.WLAN || netIf.em4; + 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; - } - } - mac = alias.mac; + 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, mac }; + 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}`}`; - } + 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}`}`; - } + 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}`}`; } -}
\ No newline at end of file + } +} |
