UNPKG

cea-core

Version:
135 lines 5.28 kB
import cheerio from 'cheerio'; import UserAgent from 'user-agents'; import getEdgeCases from '../compatibility/edge-case.js'; import FetchWithCookie from '../utils/fetch-helper.js'; import AES from '../utils/encrypt.js'; import log from '../utils/logger.js'; import { captchaHandler } from './captcha.js'; import { UnifiedLoginResultCodeMsg } from '../types/login.js'; export default async function login(school, user) { var _a, _b; const schoolEdgeCases = getEdgeCases(school.chineseName, school.isCloud); const headers = { 'User-agent': new UserAgent().toString(), 'Upgrade-Insecure-Requests': '1', }; const fetch = new FetchWithCookie(headers); const name = user.alias; let res = await fetch.get(school.preAuthURL); let authURL = school.authURL; if (!authURL) { res = await fetch.follow(); authURL = fetch.lastRedirectUrl; } const hiddenInputNameValueMap = {}; let pwdSalt, needCaptcha; if (!school.isCloud) { const body = await res.text(); const $ = cheerio.load(body); const form = $('form[method=post]').get(schoolEdgeCases.formIdx); const hiddenInputList = $('input[type=hidden]', form); hiddenInputList.each((e) => { const hiddenInputNode = hiddenInputList[e]; const [name, value] = [ hiddenInputNode.attribs.name, hiddenInputNode.attribs.value, ]; if (name && value) { hiddenInputNameValueMap[name] = value; } else if (value) { pwdSalt = value; } }); const addtionalParams = `?username=${user.username}&ltId=${hiddenInputNameValueMap.lt || ''}`; needCaptcha = (await (await fetch.get(`${school.auth}${schoolEdgeCases.checkCaptchaPath}${addtionalParams}`)).text()).includes('true'); } else { res = await fetch.get(res.headers.get('location')); const ltWrapper = (_a = res.headers.get('location')) === null || _a === void 0 ? void 0 : _a.split('_2lBepC=')[1]; res = await fetch.post(`${school.auth}${schoolEdgeCases.lt}`, { type: 'form', body: `lt=${ltWrapper}`, }); const { result } = (await res.json()); needCaptcha = false; Object.defineProperties(hiddenInputNameValueMap, { lt: { value: result._lt, enumerable: true }, dllt: { value: '', enumerable: true }, }); } const auth = new URLSearchParams({ username: user.username, password: pwdSalt ? new AES(user.password, pwdSalt).encrypt() : user.password, ...hiddenInputNameValueMap, rememberMe: schoolEdgeCases.rememberMe.toString(), }); while (true) { if (needCaptcha) { const captchaUrl = `${school.auth}${schoolEdgeCases.getCaptchaPath}?username=${user.username}&ltId=${(_b = hiddenInputNameValueMap .lt) !== null && _b !== void 0 ? _b : ''}`; log.warn({ message: '登录需要验证码', suffix: `@${name}`, }); let captchaCode = await captchaHandler(captchaUrl, fetch, user.captcha); if ((captchaCode === null || captchaCode === void 0 ? void 0 : captchaCode.length) >= 4) { log.warn({ message: `使用验证码 ${captchaCode} 登录`, suffix: `@${name}`, }); auth.append(schoolEdgeCases.submitCaptchakey, captchaCode); } else { log.error({ message: `验证码格式错误,结果为${captchaCode},长度错误`, suffix: `@${name}`, }); return; } } res = await fetch.post(authURL, { type: 'form', body: auth.toString(), }); const isRedirect = res.headers.get('location'); if (!isRedirect) { if (school.isCloud) { const result = (await res.json()); log.error({ message: UnifiedLoginResultCodeMsg[result.resultCode], suffix: `@${name}`, }); if (result.needCaptcha && !needCaptcha) { needCaptcha = true; continue; } } else { log.error({ message: `登录失败,${res.statusText}`, suffix: `@${name}`, }); } return; } else if (isRedirect.includes('.do')) { log.error({ message: `登录失败,密码安全等级低,需要修改`, suffix: `@${name}`, }); return; } else { log.success({ message: `登录成功`, suffix: `@${name}`, }); } await fetch.follow(); return fetch.getCookieObj(); } } //# sourceMappingURL=login.js.map