From f2217046864759033a5936762da8ded3eb8627ea Mon Sep 17 00:00:00 2001 From: Mole Shang <135e2@135e2.dev> Date: Sun, 3 Sep 2023 21:52:56 +0800 Subject: initial commit --- .gitignore | 172 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ deno.json | 6 ++ deno.lock | 7 +++ index.js | 85 +++++++++++++++++++++++++++++ jsconfig.json | 19 +++++++ 5 files changed, 289 insertions(+) create mode 100644 .gitignore create mode 100644 deno.json create mode 100644 deno.lock create mode 100644 index.js create mode 100644 jsconfig.json diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..f5a54ff --- /dev/null +++ b/.gitignore @@ -0,0 +1,172 @@ +# Based on https://raw.githubusercontent.com/github/gitignore/main/Node.gitignore + +# Logs + +logs +_.log +npm-debug.log_ +yarn-debug.log* +yarn-error.log* +lerna-debug.log* +.pnpm-debug.log* + +# Diagnostic reports (https://nodejs.org/api/report.html) + +report.[0-9]_.[0-9]_.[0-9]_.[0-9]_.json + +# Runtime data + +pids +_.pid +_.seed +\*.pid.lock + +# Directory for instrumented libs generated by jscoverage/JSCover + +lib-cov + +# Coverage directory used by tools like istanbul + +coverage +\*.lcov + +# nyc test coverage + +.nyc_output + +# Grunt intermediate storage (https://gruntjs.com/creating-plugins#storing-task-files) + +.grunt + +# Bower dependency directory (https://bower.io/) + +bower_components + +# node-waf configuration + +.lock-wscript + +# Compiled binary addons (https://nodejs.org/api/addons.html) + +build/Release + +# Dependency directories + +node_modules/ +jspm_packages/ + +# Snowpack dependency directory (https://snowpack.dev/) + +web_modules/ + +# TypeScript cache + +\*.tsbuildinfo + +# Optional npm cache directory + +.npm + +# Optional eslint cache + +.eslintcache + +# Optional stylelint cache + +.stylelintcache + +# Microbundle cache + +.rpt2_cache/ +.rts2_cache_cjs/ +.rts2_cache_es/ +.rts2_cache_umd/ + +# Optional REPL history + +.node_repl_history + +# Output of 'npm pack' + +\*.tgz + +# Yarn Integrity file + +.yarn-integrity + +# dotenv environment variable files + +.env +.env.development.local +.env.test.local +.env.production.local +.env.local + +# parcel-bundler cache (https://parceljs.org/) + +.cache +.parcel-cache + +# Next.js build output + +.next +out + +# Nuxt.js build / generate output + +.nuxt +dist + +# Gatsby files + +.cache/ + +# Comment in the public line in if your project uses Gatsby and not Next.js + +# https://nextjs.org/blog/next-9-1#public-directory-support + +# public + +# vuepress build output + +.vuepress/dist + +# vuepress v2.x temp and cache directory + +.temp +.cache + +# Docusaurus cache and generated files + +.docusaurus + +# Serverless directories + +.serverless/ + +# FuseBox cache + +.fusebox/ + +# DynamoDB Local files + +.dynamodb/ + +# TernJS port file + +.tern-port + +# Stores VSCode versions used for testing VSCode extensions + +.vscode-test + +# yarn v2 + +.yarn/cache +.yarn/unplugged +.yarn/build-state.yml +.yarn/install-state.gz +.pnp.\* + +# credentials +credentials.json diff --git a/deno.json b/deno.json new file mode 100644 index 0000000..657ca18 --- /dev/null +++ b/deno.json @@ -0,0 +1,6 @@ +{ + "imports": { + "lodash": "https://esm.sh/lodash@4.17.21", + "./": "./" + } +} diff --git a/deno.lock b/deno.lock new file mode 100644 index 0000000..7b78cfc --- /dev/null +++ b/deno.lock @@ -0,0 +1,7 @@ +{ + "version": "2", + "remote": { + "https://esm.sh/lodash@4.17.21": "ae454206d9631a28c229a2ce9920262cc5cfaa2a23e46b3c03069daa4e94cf10", + "https://esm.sh/v131/lodash@4.17.21/denonext/lodash.mjs": "03507ec58790b76db2d03270878c70b943b83099cdfdcad2b0fa8759abc61057" + } +} diff --git a/index.js b/index.js new file mode 100644 index 0000000..5a482e8 --- /dev/null +++ b/index.js @@ -0,0 +1,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 }; +} \ No newline at end of file diff --git a/jsconfig.json b/jsconfig.json new file mode 100644 index 0000000..7eff589 --- /dev/null +++ b/jsconfig.json @@ -0,0 +1,19 @@ +{ + "compilerOptions": { + "lib": ["ESNext"], + "module": "esnext", + "target": "esnext", + "moduleResolution": "bundler", + "moduleDetection": "force", + "allowImportingTsExtensions": true, + "noEmit": true, + "composite": true, + "strict": true, + "downlevelIteration": true, + "skipLibCheck": true, + "jsx": "preserve", + "allowSyntheticDefaultImports": true, + "forceConsistentCasingInFileNames": true, + "allowJs": true + } +} -- cgit v1.2.3