From 405dfdd8b78207d4cf9b4a0a8576f36a65e40240 Mon Sep 17 00:00:00 2001 From: beet <63141491+beetcb@users.noreply.github.com> Date: Sat, 19 Dec 2020 15:45:20 +0800 Subject: [PATCH] feat: support user provided cookie to sign in --- campusphere/app.js | 45 ++++++++++++++++++++++++++------------------- crawler/casLogIn.js | 2 +- index.js | 22 +++++++++++++++------- init.js | 17 +++++++++++------ 4 files changed, 53 insertions(+), 33 deletions(-) diff --git a/campusphere/app.js b/campusphere/app.js index d2794d2..4bdc89e 100644 --- a/campusphere/app.js +++ b/campusphere/app.js @@ -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': @@ -26,6 +26,7 @@ exports.signApp = class signApp extends ( 'content-type': 'application/json', connection: 'keep-alive', } + this.user = user } async signInfo() { @@ -62,25 +63,27 @@ 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, @@ -88,20 +91,24 @@ exports.signApp = class signApp extends ( 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] } @@ -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) diff --git a/crawler/casLogIn.js b/crawler/casLogIn.js index ae4cf63..c38c48b 100644 --- a/crawler/casLogIn.js +++ b/crawler/casLogIn.js @@ -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' }) diff --git a/index.js b/index.js index 31865fb..a0e9805 100644 --- a/index.js +++ b/index.js @@ -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}` @@ -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) { diff --git a/init.js b/init.js index 4a87ae3..f4313af 100755 --- a/init.js +++ b/init.js @@ -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') @@ -15,7 +16,7 @@ class User { this.selectType = null } - async loadUserFormFile(path) { + loadUserFormFile(path) { let users = this.conf.get('users') || [] let loadedUsers try { @@ -54,7 +55,6 @@ class User { value: 2, name: '删除用户', }, - { value: -1, name: '取消', @@ -85,6 +85,11 @@ class User { name: 'alias', message: '(可选)请输入用户别名', }, + { + type: 'input', + name: 'cookie', + message: '(可选,将省去登录操作)抓包到的 Cookie', + }, ] const res = await prompt(questions) @@ -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) @@ -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