UNPKG

run-away-from-sdu

Version:

山大? 快润! 山东大学一键申请出校脚本

89 lines (88 loc) 4.47 kB
"use strict"; var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); const des_1 = __importDefault(require("./des")); const node_fetch_1 = __importDefault(require("node-fetch")); const cookie_1 = __importDefault(require("cookie")); /** * 模拟统一身份认证登录信息中心并获取登录态Cookie * * @param userID 统一身份认证学号 * @param password 统一身份认证密码 * @returns 服务大厅登录态Cookie */ const authScenterLogin = (userID, password) => __awaiter(void 0, void 0, void 0, function* () { const cookies = {}; const getOrigin = (url) => { const arr = url.split('/'); return arr[0] + '//' + arr[2]; }; const serialize = (cookieObj) => { return ((cookieObj && Object.keys(cookieObj).map((v) => cookie_1.default.serialize(v, cookieObj[v]))) || []); }; const fetch = (url, params) => __awaiter(void 0, void 0, void 0, function* () { const origin = getOrigin(url); const headers = Object.assign({}, params.headers); headers['cookie'] = serialize(cookies[origin]).join(';'); const res = yield (0, node_fetch_1.default)(url, Object.assign(Object.assign({}, params), { headers })); cookies[origin] = Object.assign(Object.assign({}, cookies[origin]), cookie_1.default.parse((res.headers.raw()['set-cookie'] || []).join(';'))); return res; }); // 获取统一验证登录表单数据 const getLoginData = (url) => __awaiter(void 0, void 0, void 0, function* () { var _a, _b, _c; const res = yield fetch(url, { method: 'get', }); const html = yield res.text(); return { lt: (_a = /name="lt" value="(.*)"/.exec(html)) === null || _a === void 0 ? void 0 : _a[1], _eventId: (_b = /name="_eventId" value="(.*)"/.exec(html)) === null || _b === void 0 ? void 0 : _b[1], execution: (_c = /name="execution" value="(.*)"/.exec(html)) === null || _c === void 0 ? void 0 : _c[1], }; }); const login = (username, password) => __awaiter(void 0, void 0, void 0, function* () { // 访问统一登录页面,获取登录表单数据和相关cookie const url = `https://pass.sdu.edu.cn/cas/login?service=https%3A%2F%2Fscenter.sdu.edu.cn%2Ftp_fp%2Fview%3Fm%3Dfp#from=hall&serveID=87dc6da9-9ad8-4458-9654-90823be0d5f6&act=fp/serveapply`; const { lt, _eventId, execution } = yield getLoginData(url); const rsa = (0, des_1.default)(username + password + lt, '1', '2', '3'); // 通过登录,拿取统一身份认证平台登录态cookie let res = yield fetch(url, { method: 'post', headers: { 'content-type': 'application/x-www-form-urlencoded', }, body: `rsa=${rsa}&ul=${username.length}&pl=${password.length}&lt=${lt}&execution=${execution}&_eventId=${_eventId}`, redirect: 'manual', }); const text = yield res.text(); const error = /id="errormsg".*?>(.*?)<\//.exec(text) || /<span class="authorise_title">(.+?)<\/span>/.exec(text); if (error != null) { throw new Error(error[1]); } // 跳转并拿取到新教务登录态Cookie while (res.status === 302) { res = yield fetch(res.headers.raw()['location'][0], { redirect: 'manual', }); } }); yield login(userID, password); const Cookie = `JSESSIONID=${cookies['https://scenter.sdu.edu.cn']['JSESSIONID']}`; return Cookie; }); exports.default = authScenterLogin;