diff --git a/src/includes.ts b/src/includes.ts index 5f6e3d4..c05847c 100644 --- a/src/includes.ts +++ b/src/includes.ts @@ -80,8 +80,8 @@ export async function acquireCourse(course: IJsonCourse, channel: TChannel): Pro return true } else { logAndNotifyUser(resp.data.message) - if(resp.data.message.includes('当前账号已在别处登录')){ - await resetCookie() + if (resp.data.message.includes('当前账号已在别处登录')) { + await resetCookie()// TODO 加锁、优化 } return false } @@ -97,7 +97,7 @@ export async function exitCourse(course: IJsonCourse): Promise { return true } else { logAndNotifyUser('[exitCourse]退课失败:' + resp.data.message) - if(resp.data.message.includes('当前账号已在别处登录')){ + if (resp.data.message.includes('当前账号已在别处登录')) { await resetCookie() } return false @@ -108,42 +108,55 @@ export async function exitCourse(course: IJsonCourse): Promise { } } +let resetCookieLock = false + export async function resetCookie() { - let cookieJar = new tough.CookieJar() - let cookieAxios = axios.create({ - headers: { - 'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8', - 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:92.0) Gecko/20100101 Firefox/92.0' - }, - timeout: 6000, - maxRedirects: 10 - }) - axiosCookieJarSupport(cookieAxios) - cookieAxios.defaults.jar = cookieJar - cookieAxios.defaults.withCredentials = true - await cookieAxios.get('http://bkzhjx.wh.sdu.edu.cn/sso.jsp') - let casUrl = 'https://pass.sdu.edu.cn/cas/login?service=http%3A%2F%2Fbkzhjx.wh.sdu.edu.cn%2Fsso.jsp' - let resp = await cookieAxios.get(casUrl) - let ticket = /name="lt" value="(.*?)"/.exec(resp.data)?.[1] || null - let rsa = strEnc(appConfig.username + appConfig.password + ticket, '1', '2', '3') - resp = await cookieAxios.post(casUrl, qs.stringify({ - rsa, - ul: appConfig.username.length, - pl: appConfig.password.length, - lt: ticket, - execution: 'e1s1', - _eventId: 'submit' - })) - let nextUrl = 'https://bkzhjx.wh.sdu.edu.cn' + /进入选课/.exec(resp.data)![1] - resp = await cookieAxios.get(nextUrl) - let xkId = /jrxk\('1','(.*?)'/.exec(resp.data)![1] - nextUrl = 'https://bkzhjx.wh.sdu.edu.cn/jsxsd/xsxk/newXsxkzx?jx0502zbid=' + xkId - await cookieAxios.get(nextUrl) - await cookieAxios.get('https://bkzhjx.wh.sdu.edu.cn/jsxsd/xsxk/selectBottom?jx0502zbid=' + xkId) - let cookieString = cookieJar.getCookieStringSync('https://bkzhjx.wh.sdu.edu.cn/jsxsd/xsxk') - console.log('[cookie]取得Cookie:' + cookieString) - appConfig.cookie = cookieString - sduAxios.defaults.headers['Cookie'] = cookieString + if (resetCookieLock) { + console.log('resetCookie 已被锁定') + return + } + resetCookieLock = true + try { + let cookieJar = new tough.CookieJar() + let cookieAxios = axios.create({ + headers: { + 'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8', + 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:92.0) Gecko/20100101 Firefox/92.0' + }, + timeout: 6000, + maxRedirects: 10 + }) + axiosCookieJarSupport(cookieAxios) + cookieAxios.defaults.jar = cookieJar + cookieAxios.defaults.withCredentials = true + await cookieAxios.get('http://bkzhjx.wh.sdu.edu.cn/sso.jsp') + let casUrl = 'https://pass.sdu.edu.cn/cas/login?service=http%3A%2F%2Fbkzhjx.wh.sdu.edu.cn%2Fsso.jsp' + let resp = await cookieAxios.get(casUrl) + let ticket = /name="lt" value="(.*?)"/.exec(resp.data)?.[1] || null + let rsa = strEnc(appConfig.username + appConfig.password + ticket, '1', '2', '3') + resp = await cookieAxios.post(casUrl, qs.stringify({ + rsa, + ul: appConfig.username.length, + pl: appConfig.password.length, + lt: ticket, + execution: 'e1s1', + _eventId: 'submit' + })) + let nextUrl = 'https://bkzhjx.wh.sdu.edu.cn' + /进入选课/.exec(resp.data)![1] + resp = await cookieAxios.get(nextUrl) + let xkId = /jrxk\('1','(.*?)'/.exec(resp.data)![1] + nextUrl = 'https://bkzhjx.wh.sdu.edu.cn/jsxsd/xsxk/newXsxkzx?jx0502zbid=' + xkId + await cookieAxios.get(nextUrl) + await cookieAxios.get('https://bkzhjx.wh.sdu.edu.cn/jsxsd/xsxk/selectBottom?jx0502zbid=' + xkId) + let cookieString = cookieJar.getCookieStringSync('https://bkzhjx.wh.sdu.edu.cn/jsxsd/xsxk') + console.log('[cookie]取得Cookie:' + cookieString) + appConfig.cookie = cookieString + sduAxios.defaults.headers['Cookie'] = cookieString + } catch (e) { + throw e + } finally { + resetCookieLock = false + } } export function countStr(str: string, word: string) {