Skip to content
This repository has been archived by the owner on Jun 3, 2021. It is now read-only.

Commit

Permalink
fix(login): make use of the curTask we already have
Browse files Browse the repository at this point in the history
  • Loading branch information
beetcb committed Feb 20, 2021
1 parent a1e824f commit 00369e7
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 45 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/cea.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
17 changes: 17 additions & 0 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
@@ -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": [
"<node_internals>/**"
],
"program": "${workspaceFolder}\\TEST\\dcampus.js"
}
]
}
4 changes: 2 additions & 2 deletions Jenkinsfile
Original file line number Diff line number Diff line change
Expand Up @@ -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'
}
}
Expand Down
68 changes: 34 additions & 34 deletions crawler/casLogIn.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
*
Expand All @@ -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: '',
Expand Down Expand Up @@ -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 {
Expand Down
16 changes: 9 additions & 7 deletions src/cookie.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand All @@ -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)
Expand All @@ -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
}

Expand Down

0 comments on commit 00369e7

Please sign in to comment.