2022-03-31 08:01:00 -04:00
|
|
|
const RedisWrapper = require('@overleaf/redis-wrapper')
|
|
|
|
const Settings = require('@overleaf/settings')
|
|
|
|
const SessionManager = require('../app/src/Features/Authentication/SessionManager')
|
|
|
|
const async = require('async')
|
|
|
|
const _ = require('lodash')
|
|
|
|
|
2023-07-25 04:19:18 -04:00
|
|
|
const redis = RedisWrapper.createClient(Settings.redis.websessions)
|
2022-03-31 08:01:00 -04:00
|
|
|
|
|
|
|
let totalDeletedSessions = 0
|
|
|
|
|
|
|
|
const queue = async.queue(function (sessKey, callback) {
|
|
|
|
const cb = _.once(callback)
|
|
|
|
redis.get(sessKey, (_err, session) => {
|
|
|
|
try {
|
|
|
|
if (SessionManager.isUserLoggedIn(JSON.parse(session))) {
|
|
|
|
cb()
|
|
|
|
} else {
|
|
|
|
redis.del(sessKey, () => {
|
|
|
|
totalDeletedSessions++
|
2022-04-05 06:21:24 -04:00
|
|
|
if (totalDeletedSessions % 1000 === 0) {
|
|
|
|
console.log(`Keys deleted so far: ${totalDeletedSessions}`)
|
|
|
|
}
|
2022-03-31 08:01:00 -04:00
|
|
|
cb()
|
|
|
|
})
|
|
|
|
}
|
|
|
|
} catch (err) {
|
|
|
|
console.log(`${sessKey} couldn't parse`)
|
|
|
|
cb()
|
|
|
|
}
|
|
|
|
})
|
2022-04-05 06:21:24 -04:00
|
|
|
}, 20)
|
2022-03-31 08:01:00 -04:00
|
|
|
|
|
|
|
function scanAndPurge(cb) {
|
|
|
|
const stream = redis.scanStream({
|
|
|
|
match: 'sess:*',
|
2022-04-05 06:21:24 -04:00
|
|
|
count: 1000,
|
2022-03-31 08:01:00 -04:00
|
|
|
})
|
|
|
|
console.log('starting scan')
|
|
|
|
|
|
|
|
stream.on('data', resultKeys => {
|
|
|
|
console.log(`Keys found, count:${resultKeys.length}`)
|
|
|
|
queue.push(resultKeys)
|
|
|
|
})
|
|
|
|
|
|
|
|
stream.on('end', () => {
|
2022-06-21 04:07:14 -04:00
|
|
|
queue.drain(() => {
|
2022-03-31 08:01:00 -04:00
|
|
|
console.log(
|
|
|
|
`All sessions have been checked, ${totalDeletedSessions} deleted`
|
|
|
|
)
|
|
|
|
cb()
|
2022-06-21 04:07:14 -04:00
|
|
|
})
|
2022-03-31 08:01:00 -04:00
|
|
|
})
|
|
|
|
|
|
|
|
stream.on('error', err => {
|
|
|
|
console.log(err)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
scanAndPurge(err => {
|
|
|
|
console.error(err)
|
|
|
|
process.exit()
|
|
|
|
})
|