Deleting old sessions fix

This commit is contained in:
mrfry 2020-12-16 18:04:34 +01:00
parent c8161e4dce
commit 771959ec2b

View file

@ -186,7 +186,6 @@ function GetApp(): ModuleType {
temp = temp[temp.length - 1] temp = temp[temp.length - 1]
version = temp version = temp
console.log(version)
} }
function LoadMOTD() { function LoadMOTD() {
@ -680,7 +679,6 @@ function GetApp(): ModuleType {
.replace(/"/g, '') .replace(/"/g, '')
.replace(/;/g, '') .replace(/;/g, '')
: false : false
const cid = req.body.cid
const isScript = req.body.script const isScript = req.body.script
const ip = req.headers['cf-connecting-ip'] || req.connection.remoteAddress const ip = req.headers['cf-connecting-ip'] || req.connection.remoteAddress
const user: User = dbtools.Select(userDB, 'users', { const user: User = dbtools.Select(userDB, 'users', {
@ -690,24 +688,32 @@ function GetApp(): ModuleType {
if (user) { if (user) {
const sessionID = uuidv4() const sessionID = uuidv4()
const existingSessions = dbtools.Select(userDB, 'sessions', { const existingSessions = dbtools
userID: user.id, .Select(userDB, 'sessions', {
isScript: isScript ? 1 : 0, userID: user.id,
}) isScript: isScript ? 1 : 0,
})
.sort((a, b) => {
return new Date(a).getTime() - new Date(b).getTime()
})
if (existingSessions.length >= minimumAlowwedSessions) { // TODO: check if sort works, and only the first few gets deleted.
const diff = existingSessions.length - minimumAlowwedSessions
if (diff > 0) {
logger.Log( logger.Log(
`Multiple ${isScript ? 'script' : 'website'} sessions ( ${ `Multiple ${isScript ? 'script' : 'website'} sessions ( ${
existingSessions.length existingSessions.length
} ) for #${user.id}, deleting olds`, } ) for #${user.id}, deleting olds`,
logger.GetColor('cyan') logger.GetColor('cyan')
) )
existingSessions.forEach((sess) => { for (let i = 0; i < diff; i++) {
const id = existingSessions[i].id
dbtools.Delete(userDB, 'sessions', { dbtools.Delete(userDB, 'sessions', {
id: sess.id, id: id,
isScript: isScript ? 1 : 0, isScript: isScript ? 1 : 0,
}) })
}) }
} }
dbtools.Update( dbtools.Update(
@ -763,7 +769,7 @@ function GetApp(): ModuleType {
logger.Log( logger.Log(
`Login attempt with invalid pw: ${pw} to ${ `Login attempt with invalid pw: ${pw} to ${
isScript ? 'script' : 'website' isScript ? 'script' : 'website'
}${cid ? ', CID:' + cid : ''}`, }`,
logger.GetColor('cyan') logger.GetColor('cyan')
) )
res.json({ res.json({