From 00369e7730abd058914c214f36105cf3653516d7 Mon Sep 17 00:00:00 2001 From: be beet <63141491+beetcb@users.noreply.github.com> Date: Sat, 20 Feb 2021 12:45:03 +0800 Subject: [PATCH] fix(login): make use of the curTask we already have --- .github/workflows/cea.yml | 4 +-- .vscode/launch.json | 17 ++++++++++ Jenkinsfile | 4 +-- crawler/casLogIn.js | 68 +++++++++++++++++++-------------------- src/cookie.js | 16 +++++---- 5 files changed, 64 insertions(+), 45 deletions(-) create mode 100644 .vscode/launch.json diff --git a/.github/workflows/cea.yml b/.github/workflows/cea.yml index 6ad60c6..b85b2a6 100644 --- a/.github/workflows/cea.yml +++ b/.github/workflows/cea.yml @@ -31,10 +31,10 @@ jobs: - name: Init school and users run: | npm i - node ./cli.js load + node ./src/cli.js load env: # Or as an environment variable users: ${{ secrets.users }} school: ${{ secrets.school }} - name: 执行签到 - run: node ./cli.js sign + run: node ./src/cli.js sign diff --git a/.vscode/launch.json b/.vscode/launch.json new file mode 100644 index 0000000..5b0d369 --- /dev/null +++ b/.vscode/launch.json @@ -0,0 +1,17 @@ +{ + // Use IntelliSense to learn about possible attributes. + // Hover to view descriptions of existing attributes. + // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387 + "version": "0.2.0", + "configurations": [ + { + "type": "pwa-node", + "request": "launch", + "name": "Launch Program", + "skipFiles": [ + "/**" + ], + "program": "${workspaceFolder}\\TEST\\dcampus.js" + } + ] +} \ No newline at end of file diff --git a/Jenkinsfile b/Jenkinsfile index 5119878..b12c4d8 100644 --- a/Jenkinsfile +++ b/Jenkinsfile @@ -16,8 +16,8 @@ pipeline { stage('签到') { steps { sh 'npm i' - sh 'node ./cli.js load' - sh 'node ./cli.js sign' + sh 'node ./src/cli.js load' + sh 'node ./src/cli.js sign' sh 'date' } } diff --git a/crawler/casLogIn.js b/crawler/casLogIn.js index e59ee06..5e9c36a 100644 --- a/crawler/casLogIn.js +++ b/crawler/casLogIn.js @@ -5,15 +5,6 @@ const crypto = require('crypto') const log = require('../interface/colorLog') const ocr = require('./captcha') -const headers = { - 'Cache-control': 'max-age=0', - 'Accept-Encoding': 'gzip, deflate', - Connection: 'keep-alive', - 'Upgrade-Insecure-Requests': '1', - 'User-agent': - 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.88 Safari/537.36', -} - /** * login to SWMS(stu work magagement system) process * @@ -22,6 +13,15 @@ const headers = { * @return {Object} cookie for cas and campusphere */ module.exports = async (school, user) => { + const headers = { + 'Cache-control': 'max-age=0', + 'Accept-Encoding': 'gzip, deflate', + Connection: 'keep-alive', + 'Upgrade-Insecure-Requests': '1', + 'User-agent': + 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.88 Safari/537.36', + } + const cookie = { swms: '', campusphere: '', @@ -119,32 +119,32 @@ module.exports = async (school, user) => { return } - return cookie -} - -/** - * refresh cookie in headers (for next request) - * - * @param {Object} headers refresh target - * @param {Object} res response object - * @param {Object} cookie - */ -function reCook(res, isCas, cookie) { - let cook - try { - cook = res.headers.raw()['set-cookie'] - cook.forEach(e => { - if (e.includes('authserver')) { - cookie.swms += e.match(/^(\w|\d|\s)+\=(\w|\d|\s|\-)+;/)[0] - } else { - cookie.campusphere += e.match(/^(\w|\d|\s)+\=(\w|\d|\s|\-)+;/)[0] - } - }) - } catch (e) { - return false + /** + * refresh cookie in headers (for next request) + * + * @param {Object} headers refresh target + * @param {Object} res response object + * @param {Object} cookie + */ + function reCook(res, isCas) { + let cook + try { + cook = res.headers.raw()['set-cookie'] + cook.forEach(e => { + if (e.includes('authserver')) { + cookie.swms += e.match(/^(\w|\d|\s)+\=(\w|\d|\s|\-)+;/)[0] + } else { + cookie.campusphere += e.match(/^(\w|\d|\s)+\=(\w|\d|\s|\-)+;/)[0] + } + }) + } catch (e) { + return false + } + headers.cookie = isCas ? cookie.swms : cookie.campusphere + return true } - headers.cookie = isCas ? cookie.swms : cookie.campusphere - return true + + return cookie } class AES { diff --git a/src/cookie.js b/src/cookie.js index 24454aa..0ce2fa0 100644 --- a/src/cookie.js +++ b/src/cookie.js @@ -9,11 +9,13 @@ const conf = new Conf() conf.handleCookie = async () => { // Return users with curTask const usersWithTask = [] - for (const i of conf.get('users')) { - const storeCookiePath = `cookie.${i.alias}` - i.sign = await handleLogin(i, storeCookiePath) - usersWithTask.push(i) - } + await Promise.all( + conf.get('users').map(async i => { + const storeCookiePath = `cookie.${i.alias}` + i.sign = await handleLogin(i, storeCookiePath) + usersWithTask.push(i) + }) + ) return usersWithTask } @@ -38,6 +40,7 @@ async function handleLogin(i, storeCookiePath) { // Check if the cookie is eligible, if not, reLogin 1 more time const isNeedLogIn = await sign.signInfo(cookie) if (isNeedLogIn) { + log.warning(`用户${name}: Cookie 失效,正在重新获取`) cookie = await login(conf.get('school'), i) if (cookie) { conf.set(storeCookiePath, cookie) @@ -46,8 +49,7 @@ async function handleLogin(i, storeCookiePath) { } else { log.success(`用户${name}: 尝试使用缓存中的 Cookie`) } - - // Return sign instance, cause we already have cur task in our hand + // Make use of the cur task we already have return sign }