summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--deno.json9
-rw-r--r--deno.lock31
-rw-r--r--index.js208
3 files changed, 139 insertions, 109 deletions
diff --git a/deno.json b/deno.json
index 4eb6f8f..aaa93f8 100644
--- a/deno.json
+++ b/deno.json
@@ -1,11 +1,12 @@
{
"imports": {
"lodash": "https://esm.sh/lodash@4.17.21",
- "flags/": "https://deno.land/std/flags/",
+ "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"
+ "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"
}
}
diff --git a/deno.lock b/deno.lock
index 365bfca..45d26cc 100644
--- a/deno.lock
+++ b/deno.lock
@@ -1,10 +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://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@*"
+ ]
}
}
diff --git a/index.js b/index.js
index b02aedb..066c5fd 100644
--- a/index.js
+++ b/index.js
@@ -1,128 +1,138 @@
import lodash from "lodash";
-import { parse } from "flags/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)}${SEPARATOR}credentials.json`;
-(async () => {
-
- const flags = parse(Deno.args, {
- boolean: ["login", "logout"],
- string: ["config"],
- default: { login: true, config: "./credentials.json" }
- });
-
- 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("credentials.json 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
+ }
+}