mirror of
https://gitlab.com/MrFry/mrfrys-node-server
synced 2025-04-01 20:24:18 +02:00
Date string update, midnight timer update, other stuff
This commit is contained in:
parent
c764c4f402
commit
dbb23a6724
6 changed files with 92 additions and 61 deletions
|
@ -1,4 +1,5 @@
|
||||||
const logger = require('../utils/logger.js')
|
const logger = require('../utils/logger.js')
|
||||||
|
const utils = require('../utils/utils.js')
|
||||||
const dbtools = require('../utils/dbtools.js')
|
const dbtools = require('../utils/dbtools.js')
|
||||||
|
|
||||||
module.exports = function (options) {
|
module.exports = function (options) {
|
||||||
|
@ -52,14 +53,14 @@ module.exports = function (options) {
|
||||||
UpdateAccess(authDB, user, ip, sessionID)
|
UpdateAccess(authDB, user, ip, sessionID)
|
||||||
|
|
||||||
dbtools.Update(authDB, 'sessions', {
|
dbtools.Update(authDB, 'sessions', {
|
||||||
lastAccess: new Date().toString()
|
lastAccess: utils.GetDateString()
|
||||||
}, {
|
}, {
|
||||||
id: sessionID
|
id: sessionID
|
||||||
})
|
})
|
||||||
|
|
||||||
dbtools.Update(authDB, 'users', {
|
dbtools.Update(authDB, 'users', {
|
||||||
lastIP: ip,
|
lastIP: ip,
|
||||||
lastAccess: new Date().toString()
|
lastAccess: utils.GetDateString()
|
||||||
}, {
|
}, {
|
||||||
id: user.id
|
id: user.id
|
||||||
})
|
})
|
||||||
|
@ -79,7 +80,7 @@ function UpdateAccess (db, user, ip, sessionID) {
|
||||||
userID: user.id,
|
userID: user.id,
|
||||||
ip: ip,
|
ip: ip,
|
||||||
sessionID: sessionID,
|
sessionID: sessionID,
|
||||||
date: new Date().toString()
|
date: utils.GetDateString()
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -45,8 +45,12 @@ const passwordFile = 'data/dataEditorPasswords.json'
|
||||||
const dataEditsLog = 'stats/dataEdits'
|
const dataEditsLog = 'stats/dataEdits'
|
||||||
const dailyDataCountFile = 'stats/dailyDataCount'
|
const dailyDataCountFile = 'stats/dailyDataCount'
|
||||||
const usersDBPath = 'data/dbs/users.db'
|
const usersDBPath = 'data/dbs/users.db'
|
||||||
|
const usersDbBackupPath = 'data/dbs/backup'
|
||||||
|
|
||||||
const maxVeteranPwGetCount = 5
|
const maxVeteranPwGetCount = 5
|
||||||
|
const addPWPerDay = 3 // every x day a user can give a pw
|
||||||
|
const maxPWCount = 2 // maximum pw give opportunities a user can have at once
|
||||||
|
const daysAfterUserGetsPWs = 2 // days after user gets pw-s
|
||||||
|
|
||||||
if (!utils.FileExists(usersDBPath)) {
|
if (!utils.FileExists(usersDBPath)) {
|
||||||
throw new Error('No user DB exists yet! please run utils/dbSetup.js first!')
|
throw new Error('No user DB exists yet! please run utils/dbSetup.js first!')
|
||||||
|
@ -137,7 +141,7 @@ app.post('/getpw', function (req, res) {
|
||||||
const pw = uuidv4()
|
const pw = uuidv4()
|
||||||
const insertRes = dbtools.Insert(authDB, 'users', {
|
const insertRes = dbtools.Insert(authDB, 'users', {
|
||||||
pw: pw,
|
pw: pw,
|
||||||
created: new Date().toString()
|
created: utils.GetDateString()
|
||||||
})
|
})
|
||||||
|
|
||||||
logger.Log(`User #${requestingUser.id} creted new user #${insertRes.lastInsertRowid}`, logger.GetColor('cyan'))
|
logger.Log(`User #${requestingUser.id} creted new user #${insertRes.lastInsertRowid}`, logger.GetColor('cyan'))
|
||||||
|
@ -170,7 +174,7 @@ app.post('/getveteranpw', function (req, res) {
|
||||||
} else {
|
} else {
|
||||||
dbtools.Update(authDB, 'veteranPWRequests', {
|
dbtools.Update(authDB, 'veteranPWRequests', {
|
||||||
count: tries.count + 1,
|
count: tries.count + 1,
|
||||||
lastDate: new Date().toString()
|
lastDate: utils.GetDateString()
|
||||||
}, {
|
}, {
|
||||||
id: tries.id
|
id: tries.id
|
||||||
})
|
})
|
||||||
|
@ -178,7 +182,7 @@ app.post('/getveteranpw', function (req, res) {
|
||||||
} else {
|
} else {
|
||||||
dbtools.Insert(authDB, 'veteranPWRequests', {
|
dbtools.Insert(authDB, 'veteranPWRequests', {
|
||||||
ip: ip,
|
ip: ip,
|
||||||
lastDate: new Date().toString()
|
lastDate: utils.GetDateString()
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -253,7 +257,7 @@ app.post('/login', (req, res) => {
|
||||||
dbtools.Update(authDB, 'users', {
|
dbtools.Update(authDB, 'users', {
|
||||||
loginCount: user.loginCount + 1,
|
loginCount: user.loginCount + 1,
|
||||||
lastIP: ip,
|
lastIP: ip,
|
||||||
lastLogin: new Date().toString()
|
lastLogin: utils.GetDateString()
|
||||||
}, {
|
}, {
|
||||||
id: user.id
|
id: user.id
|
||||||
})
|
})
|
||||||
|
@ -262,7 +266,7 @@ app.post('/login', (req, res) => {
|
||||||
id: sessionID,
|
id: sessionID,
|
||||||
ip: ip,
|
ip: ip,
|
||||||
userID: user.id,
|
userID: user.id,
|
||||||
createDate: new Date().toString()
|
createDate: utils.GetDateString()
|
||||||
})
|
})
|
||||||
|
|
||||||
// TODO: cookie age
|
// TODO: cookie age
|
||||||
|
@ -315,7 +319,7 @@ app.post('/postfeedbackfile', function (req, res) {
|
||||||
app.post('/postfeedback', function (req, res) {
|
app.post('/postfeedback', function (req, res) {
|
||||||
logger.LogReq(req)
|
logger.LogReq(req)
|
||||||
logger.Log('New feedback message', logger.GetColor('bluebg'), true)
|
logger.Log('New feedback message', logger.GetColor('bluebg'), true)
|
||||||
utils.AppendToFile(logger.GetDateString() + ':\n' + JSON.stringify(req.body), msgFile)
|
utils.AppendToFile(utils.GetDateString() + ':\n' + JSON.stringify(req.body), msgFile)
|
||||||
res.json({ success: true })
|
res.json({ success: true })
|
||||||
})
|
})
|
||||||
|
|
||||||
|
@ -403,7 +407,7 @@ app.post('/uploaddata', (req, res) => {
|
||||||
// returning if user password is not ok
|
// returning if user password is not ok
|
||||||
if (!user) {
|
if (!user) {
|
||||||
logger.Log(`Data upload: invalid password ${password}`, logger.GetColor('red'))
|
logger.Log(`Data upload: invalid password ${password}`, logger.GetColor('red'))
|
||||||
utils.AppendToFile(logger.GetDateString() + '\n' + password + '(FAILED PASSWORD)\n' + JSON.stringify(editedQuestions) + '\n\n', dataEditsLog)
|
utils.AppendToFile(utils.GetDateString() + '\n' + password + '(FAILED PASSWORD)\n' + JSON.stringify(editedQuestions) + '\n\n', dataEditsLog)
|
||||||
res.json({ status: respStatuses.invalidPass })
|
res.json({ status: respStatuses.invalidPass })
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
@ -411,10 +415,10 @@ app.post('/uploaddata', (req, res) => {
|
||||||
logger.Log(`Password accepted for ${user.name}`, logger.GetColor('bluebg'))
|
logger.Log(`Password accepted for ${user.name}`, logger.GetColor('bluebg'))
|
||||||
logger.Log(`Old Subjects/Questions: ${initialCount.subjectCount} / ${initialCount.questionCount} | New: ${count.subjectCount} / ${count.questionCount} | Edited question count: ${Object.keys(editedQuestions).length}`, logger.GetColor('bluebg'))
|
logger.Log(`Old Subjects/Questions: ${initialCount.subjectCount} / ${initialCount.questionCount} | New: ${count.subjectCount} / ${count.questionCount} | Edited question count: ${Object.keys(editedQuestions).length}`, logger.GetColor('bluebg'))
|
||||||
// saving detailed editedCount
|
// saving detailed editedCount
|
||||||
utils.AppendToFile(logger.GetDateString() + '\n' + JSON.stringify(user) + '\n' + JSON.stringify(editedQuestions) + '\n\n', dataEditsLog)
|
utils.AppendToFile(utils.GetDateString() + '\n' + JSON.stringify(user) + '\n' + JSON.stringify(editedQuestions) + '\n\n', dataEditsLog)
|
||||||
|
|
||||||
// making backup
|
// making backup
|
||||||
utils.CopyFile('./' + dataFile, `./public/backs/data_before_${user.name}_${new Date().toString().replace(/ /g, '_')}`)
|
utils.CopyFile('./' + dataFile, `./public/backs/data_before_${user.name}_${utils.GetDateString().replace(/ /g, '_')}`)
|
||||||
logger.Log('Backup made')
|
logger.Log('Backup made')
|
||||||
// writing data
|
// writing data
|
||||||
utils.WriteFile(JSON.stringify(newData), dataFile)
|
utils.WriteFile(JSON.stringify(newData), dataFile)
|
||||||
|
@ -550,8 +554,9 @@ app.post('*', function (req, res) {
|
||||||
})
|
})
|
||||||
|
|
||||||
function ExportDailyDataCount () {
|
function ExportDailyDataCount () {
|
||||||
|
logger.Log('Saving daily data count ...')
|
||||||
utils.AppendToFile(JSON.stringify({
|
utils.AppendToFile(JSON.stringify({
|
||||||
date: new Date(),
|
date: utils.GetDateString(),
|
||||||
subjectCount: data.Subjects.length,
|
subjectCount: data.Subjects.length,
|
||||||
questionCOunt: data.Subjects.reduce((acc, subj) => {
|
questionCOunt: data.Subjects.reduce((acc, subj) => {
|
||||||
return acc + subj.Questions.length
|
return acc + subj.Questions.length
|
||||||
|
@ -560,6 +565,47 @@ function ExportDailyDataCount () {
|
||||||
}), dailyDataCountFile)
|
}), dailyDataCountFile)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function BackupDB () {
|
||||||
|
logger.Log('Backing up auth DB ...')
|
||||||
|
utils.CreatePath(usersDbBackupPath, true)
|
||||||
|
authDB.backup(`${usersDbBackupPath}/users.${utils.GetDateString().replace(/ /g, '_')}.db`)
|
||||||
|
.then(() => {
|
||||||
|
logger.Log('Auth DB backup complete!')
|
||||||
|
})
|
||||||
|
.catch((err) => {
|
||||||
|
logger.Log('Auth DB backup failed!', logger.GetColor('redbg'))
|
||||||
|
console.error(err)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
function IncrementAvaiblePWs () {
|
||||||
|
const users = dbtools.SelectAll(authDB, 'users')
|
||||||
|
const today = new Date()
|
||||||
|
const getDayDiff = (dateString) => {
|
||||||
|
let msdiff = today - new Date(dateString)
|
||||||
|
return Math.floor(msdiff / (1000 * 3600 * 24))
|
||||||
|
}
|
||||||
|
|
||||||
|
users.forEach((u) => {
|
||||||
|
if (u.avaiblePWRequests >= maxPWCount) {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
const dayDiff = getDayDiff(u.created)
|
||||||
|
if (dayDiff < daysAfterUserGetsPWs) {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
if (dayDiff % addPWPerDay === 0) {
|
||||||
|
dbtools.Update(authDB, 'users', {
|
||||||
|
avaiblePWRequests: u.avaiblePWRequests + 1
|
||||||
|
}, {
|
||||||
|
id: u.id
|
||||||
|
})
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
exports.app = app
|
exports.app = app
|
||||||
exports.cleanup = () => {
|
exports.cleanup = () => {
|
||||||
logger.Log('Closing Auth DB')
|
logger.Log('Closing Auth DB')
|
||||||
|
@ -567,8 +613,8 @@ exports.cleanup = () => {
|
||||||
}
|
}
|
||||||
exports.dailyAction = () => {
|
exports.dailyAction = () => {
|
||||||
ExportDailyDataCount()
|
ExportDailyDataCount()
|
||||||
|
BackupDB()
|
||||||
// TODO: selectAll from users, check if date is more than x, and increment every y
|
IncrementAvaiblePWs()
|
||||||
}
|
}
|
||||||
|
|
||||||
logger.Log('API module started', logger.GetColor('yellow'))
|
logger.Log('API module started', logger.GetColor('yellow'))
|
||||||
|
|
30
server.js
30
server.js
|
@ -61,6 +61,7 @@ process.on('SIGTERM', () => exit('SIGTERM'))
|
||||||
process.on('SIGTERM', () => exit('SIGTERM'))
|
process.on('SIGTERM', () => exit('SIGTERM'))
|
||||||
|
|
||||||
function exit (reason) {
|
function exit (reason) {
|
||||||
|
console.log()
|
||||||
logger.Log(`Exiting, reason: ${reason}`)
|
logger.Log(`Exiting, reason: ${reason}`)
|
||||||
Object.keys(modules).forEach((k, i) => {
|
Object.keys(modules).forEach((k, i) => {
|
||||||
const x = modules[k]
|
const x = modules[k]
|
||||||
|
@ -134,25 +135,24 @@ if (startHTTPS && utils.FileExists(privkeyFile) && utils.FileExists(fullchainFil
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
setLogTimer()
|
||||||
function setLogTimer () {
|
function setLogTimer () {
|
||||||
const d = new Date()
|
const now = new Date()
|
||||||
const h = new Date(
|
const night = new Date(
|
||||||
d.getFullYear(),
|
now.getFullYear(),
|
||||||
d.getMonth(),
|
now.getMonth(),
|
||||||
d.getDate() + 1,
|
now.getDate() + 1, // the next day, ...
|
||||||
0,
|
0, 0, 0 // ...at 00:00:00 hours
|
||||||
0,
|
|
||||||
0,
|
|
||||||
0
|
|
||||||
)
|
)
|
||||||
const e = h - d
|
const msToMidnight = night.getTime() - now.getTime()
|
||||||
|
|
||||||
if (e > 100) {
|
setTimeout(function () {
|
||||||
setTimeout(setLogTimer, e)
|
LogTimerAction()
|
||||||
} else {
|
setLogTimer()
|
||||||
logger.Log('Log timer malfunction :/', logger.GetColor('redbg'))
|
}, msToMidnight)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function LogTimerAction () {
|
||||||
Object.keys(modules).forEach((k, i) => {
|
Object.keys(modules).forEach((k, i) => {
|
||||||
const x = modules[k]
|
const x = modules[k]
|
||||||
if (x.dailyAction) {
|
if (x.dailyAction) {
|
||||||
|
@ -171,8 +171,6 @@ function setLogTimer () {
|
||||||
utils.AppendToFile(line, allLogFile)
|
utils.AppendToFile(line, allLogFile)
|
||||||
}
|
}
|
||||||
|
|
||||||
setLogTimer()
|
|
||||||
|
|
||||||
logger.Log('Node version: ' + process.version)
|
logger.Log('Node version: ' + process.version)
|
||||||
logger.Log('Current working directory: ' + process.cwd())
|
logger.Log('Current working directory: ' + process.cwd())
|
||||||
logger.Log('Listening on port: ' + port)
|
logger.Log('Listening on port: ' + port)
|
||||||
|
|
|
@ -11,15 +11,6 @@ let authDB
|
||||||
console.clear()
|
console.clear()
|
||||||
CreateDB()
|
CreateDB()
|
||||||
|
|
||||||
// authDB.backup(usersDBPath)
|
|
||||||
// .then(() => {
|
|
||||||
// logger.Log('backup complete!')
|
|
||||||
// })
|
|
||||||
// .catch((err) => {
|
|
||||||
// logger.Log('backup failed!', logger.GetColor('redbg'))
|
|
||||||
// console.log(err)
|
|
||||||
// })
|
|
||||||
|
|
||||||
authDB.close()
|
authDB.close()
|
||||||
|
|
||||||
function CreateDB () {
|
function CreateDB () {
|
||||||
|
@ -44,7 +35,7 @@ function CreateDB () {
|
||||||
pw: uuidv4(),
|
pw: uuidv4(),
|
||||||
oldCID: cid,
|
oldCID: cid,
|
||||||
avaiblePWRequests: 4,
|
avaiblePWRequests: 4,
|
||||||
created: new Date().toString()
|
created: utils.GetDateString()
|
||||||
})
|
})
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
logger.Log('Error during inserting', logger.GetColor('redbg'))
|
logger.Log('Error during inserting', logger.GetColor('redbg'))
|
||||||
|
@ -55,7 +46,7 @@ function CreateDB () {
|
||||||
console.error(e)
|
console.error(e)
|
||||||
}
|
}
|
||||||
|
|
||||||
const dir = `./dbSetupResult/${GetDateString().replace(/ /g, '_')}`
|
const dir = `./dbSetupResult/${utils.GetDateString().replace(/ /g, '_')}`
|
||||||
utils.CreatePath(dir)
|
utils.CreatePath(dir)
|
||||||
Object.keys(dbStruct).forEach((key) => {
|
Object.keys(dbStruct).forEach((key) => {
|
||||||
const path = `${dir}/${key}.json`
|
const path = `${dir}/${key}.json`
|
||||||
|
@ -68,14 +59,3 @@ function CreateDB () {
|
||||||
|
|
||||||
logger.Log('Done')
|
logger.Log('Done')
|
||||||
}
|
}
|
||||||
|
|
||||||
function GetDateString () {
|
|
||||||
const m = new Date()
|
|
||||||
const d = m.getFullYear() + '-' +
|
|
||||||
('0' + (m.getMonth() + 1)).slice(-2) + '-' +
|
|
||||||
('0' + m.getDate()).slice(-2) + ' ' +
|
|
||||||
('0' + m.getHours()).slice(-2) + ':' +
|
|
||||||
('0' + m.getMinutes()).slice(-2) + ':' +
|
|
||||||
('0' + m.getSeconds()).slice(-2)
|
|
||||||
return d
|
|
||||||
}
|
|
||||||
|
|
|
@ -64,12 +64,7 @@ let noLogips = []
|
||||||
|
|
||||||
function GetDateString () {
|
function GetDateString () {
|
||||||
const m = new Date()
|
const m = new Date()
|
||||||
const d = m.getFullYear() + '/' +
|
const d = utils.GetDateString()
|
||||||
('0' + (m.getMonth() + 1)).slice(-2) + '/' +
|
|
||||||
('0' + m.getDate()).slice(-2) + ' ' +
|
|
||||||
('0' + m.getHours()).slice(-2) + ':' +
|
|
||||||
('0' + m.getMinutes()).slice(-2) + ':' +
|
|
||||||
('0' + m.getSeconds()).slice(-2)
|
|
||||||
return GetRandomColor(m.getHours().toString()) + d + C()
|
return GetRandomColor(m.getHours().toString()) + d + C()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -10,7 +10,8 @@ module.exports = {
|
||||||
CreatePath: CreatePath,
|
CreatePath: CreatePath,
|
||||||
WatchFile: WatchFile,
|
WatchFile: WatchFile,
|
||||||
ReadDir: ReadDir,
|
ReadDir: ReadDir,
|
||||||
CopyFile: CopyFile
|
CopyFile: CopyFile,
|
||||||
|
GetDateString: GetDateString
|
||||||
}
|
}
|
||||||
|
|
||||||
var fs = require('fs')
|
var fs = require('fs')
|
||||||
|
@ -19,6 +20,16 @@ var logger = require('../utils/logger.js')
|
||||||
|
|
||||||
const dataFile = './public/data.json'
|
const dataFile = './public/data.json'
|
||||||
|
|
||||||
|
function GetDateString () {
|
||||||
|
const m = new Date()
|
||||||
|
return m.getFullYear() + '-' +
|
||||||
|
('0' + (m.getMonth() + 1)).slice(-2) + '-' +
|
||||||
|
('0' + m.getDate()).slice(-2) + ' ' +
|
||||||
|
('0' + m.getHours()).slice(-2) + ':' +
|
||||||
|
('0' + m.getMinutes()).slice(-2) + ':' +
|
||||||
|
('0' + m.getSeconds()).slice(-2)
|
||||||
|
}
|
||||||
|
|
||||||
function CopyFile (from, to) {
|
function CopyFile (from, to) {
|
||||||
CreatePath(to)
|
CreatePath(to)
|
||||||
fs.copyFileSync(from, to)
|
fs.copyFileSync(from, to)
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue