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

Commit

Permalink
fix(login): rewrite cookie logic
Browse files Browse the repository at this point in the history
  • Loading branch information
beetcb committed Mar 21, 2021
1 parent 78716eb commit 015562d
Showing 1 changed file with 5 additions and 6 deletions.
11 changes: 5 additions & 6 deletions crawler/casLogIn.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ module.exports = async (school, user) => {

// get base session -> cookie
res = await fetch(school.login, { headers })
reCook(res, 1, cookie)
reCook(res, 1)

// create document for crawling
const body = await res.text()
Expand Down Expand Up @@ -105,7 +105,7 @@ module.exports = async (school, user) => {
return
}

reCook(res, 1, cookie)
reCook(res, 0)

// set redirect headers
delete headers['Content-Type']
Expand All @@ -120,7 +120,6 @@ module.exports = async (school, user) => {

// get campus cookie
try {
reCook(null, 0, cookie)
res = await fetch(redirect, {
headers,
redirect: 'manual',
Expand All @@ -129,7 +128,7 @@ module.exports = async (school, user) => {
console.error(e)
return
}
if (/30(1|2)/.test(res.status + '') && reCook(res, 0, cookie)) {
if (/30(1|2)/.test(res.status + '') && reCook(res, 0)) {
log.success(`用户${name}: 登录成功`)
} else {
log.error(`用户${name}:登录失败,${res.statusText}`)
Expand All @@ -138,16 +137,16 @@ module.exports = async (school, user) => {

/**
* refresh cookie in headers (for next request)
* @param {object} headers refresh target
* @param {object} res response object
* @param {boolean | number} isCas is swms(only) cookie
* @return {boolean} true if set-cookie exists
*/
function reCook(res, isCas) {
try {
const setCookieList = res.headers.raw()['set-cookie']
setCookieList.forEach((e) => {
const content = e.split(';').shift()
if (e.includes('authserver')) {
if (isCas) {
cookie.swms += `${content}; `
} else {
cookie.campusphere += `${content}; `
Expand Down

0 comments on commit 015562d

Please sign in to comment.