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

Commit

Permalink
feat: support user provided cookie to sign in
Browse files Browse the repository at this point in the history
  • Loading branch information
beetcb committed Dec 19, 2020
1 parent c657b7c commit 405dfdd
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 33 deletions.
45 changes: 26 additions & 19 deletions campusphere/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ class campusphereApp {
exports.signApp = class signApp extends (
campusphereApp
) {
constructor(school, cookie) {
constructor(school, cookie, user) {
super(school)
this.headers = {
'user-agent':
Expand All @@ -26,6 +26,7 @@ exports.signApp = class signApp extends (
'content-type': 'application/json',
connection: 'keep-alive',
}
this.user = user
}

async signInfo() {
Expand Down Expand Up @@ -62,46 +63,52 @@ exports.signApp = class signApp extends (
isNeedExtra,
signedStuInfo,
} = signDetails.datas

// format coordinates length
;[longitude, latitude] = this.randomLocale(signPlaceSelected[2]).map(
e => e.toString().slice(0, 9) - 0
;[longitude, latitude] = this.randomLocale(signPlaceSelected[0]).map(e =>
Number(e.toFixed(6))
)

const extraFieldItems = this.fillExtra(extraField)
const { address } = signPlaceSelected[2]
const { address } = signPlaceSelected[0]

const form = {
signInstanceWid,
longitude,
latitude,
isMalposition,
abnormalReason: '',
signPhotoUrl: '',
position: address,
isNeedExtra,
extraFieldItems,
}

headers['Cpdaily-Extension'] = this.extention()
// log.object(form)
headers['Cpdaily-Extension'] = this.extention(form)

res = await fetch(signApi.sign, {
headers,
method: 'POST',
body: JSON.stringify(form),
})
res = await res.json()
log.warning(`${signedStuInfo.userName}的签到结果: ${res.message}`)

res.code === '0' ? process.exit(0) : process.exit(1)
log.warning(
`${this.user.alias || this.user.username} 的签到结果: ${res.message}`
)
}

// construct random coordinates
randomLocale({ longitude, latitude, radius }) {
const [perMeterLat, perMeterLon] = [
360 / (Math.cos(latitude) * 40075016.68557849),
360 / (Math.cos(latitude) * 40076000),
0.000008983,
]
const [randomLon, randomLat] = [Math.random(), Math.random()]
longitude -= radius * perMeterLon * (randomLon - 0.5) * -1 * randomLon
latitude -= radius * perMeterLat * (randomLat - 0.5) * -1 * randomLat
const { PI, cos, sin } = Math
const [randomLon, randomLat] = [
cos(Math.random() * PI),
sin(Math.random() * PI),
]
longitude -= radius * perMeterLon * randomLon
latitude -= radius * perMeterLat * randomLat
return [longitude, latitude]
}

Expand All @@ -121,15 +128,15 @@ exports.signApp = class signApp extends (
}

// construct and encrypte Cpdaily_Extension for header
extention() {
extention(form) {
const Cpdaily_Extension = {
lon: 0,
model: 'PCRT00',
lon: form.longitude,
model: 'One Plus 7 Pro',
appVersion: '8.0.8',
systemVersion: '4.4.4',
userId: '',
userId: this.user.username,
systemName: 'android',
lat: 0,
lat: form.latitude,
deviceId: v1(),
}
return this.encrypt(Cpdaily_Extension)
Expand Down
2 changes: 1 addition & 1 deletion crawler/casLogIn.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ const cookie = {
* @param {Object} user user info for login
* @return {Object} cookie for cas and campusphere
*/
module.exports = async function login(school, user) {
module.exports = async (school, user) => {
headers.referer = school.login
// get acw_tc
let res = await fetch(school.campusphere, { headers, redirect: 'manual' })
Expand Down
22 changes: 15 additions & 7 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,10 @@ if (!users) {
* @compusphere something about cp daliy's app
* @swms continuing log into your school's swms [stu work magagement system]
*/
let cookie, storeCookiePath
let cookie
let storeCookiePath

// Hack: concurrent processing users using forEach
users.forEach(async i => {
storeCookiePath = `cookie.${i.alias || i.username}`

Expand All @@ -30,21 +32,27 @@ users.forEach(async i => {
storeCookie(storeCookiePath)
}

let sign = new signApp(school, cookie)
let sign = new signApp(school, cookie, i)

const isNeedLogIn = await sign.signInfo()
if (isNeedLogIn) {
await reLogin(i)
sign = new signApp(school, cookie)
sign = new signApp(school, cookie, i)
await sign.signInfo()
}

await sign.signWithForm()
process.exit(0)
})

async function reLogin(i) {
cookie = await login(school, i)
conf.set(storeCookiePath, cookie)
log.success('Cookie stored to local storage')
cookie = i.cookie
if (!cookie) {
cookie = await login(school, i)
conf.set(storeCookiePath, cookie)
log.success('Cookie stored to local storage')
} else {
log.success('Using user provided cookie')
}
}

function storeCookie(path) {
Expand Down
17 changes: 11 additions & 6 deletions init.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ const fs = require('fs')
const log = require('./interface/colorLog')
const { prompt } = require('inquirer')
const conf = new Conf()
module.exports = conf

let school = conf.get('school')

Expand All @@ -15,7 +16,7 @@ class User {
this.selectType = null
}

async loadUserFormFile(path) {
loadUserFormFile(path) {
let users = this.conf.get('users') || []
let loadedUsers
try {
Expand Down Expand Up @@ -54,7 +55,6 @@ class User {
value: 2,
name: '删除用户',
},

{
value: -1,
name: '取消',
Expand Down Expand Up @@ -85,6 +85,11 @@ class User {
name: 'alias',
message: '(可选)请输入用户别名',
},
{
type: 'input',
name: 'cookie',
message: '(可选,将省去登录操作)抓包到的 Cookie',
},
]

const res = await prompt(questions)
Expand All @@ -94,6 +99,7 @@ class User {
username: res.username,
password: res.password,
alias: res.alias || null,
cookie: res.cookie,
}
this.conf.set('users', [addUser, ...users])
log.success('🎉 成功添加用户', addUser)
Expand Down Expand Up @@ -186,10 +192,9 @@ class School {
const type = userUlti.selectType
if (type === 1) userUlti.createUser()
if (type === 2) userUlti.deleteUser()
}
if (process.argv[2].match(/(-s|--school)/)) {
} else if (process.argv[2].match(/(-s|--school)/)) {
school = new School(conf).init()
} else {
process.exitCode = 0
}
})()

module.exports = conf

0 comments on commit 405dfdd

Please sign in to comment.