High-level rank changes and bad attempts and good Remember-Me tokens now logged.
This commit is contained in:
parent
a34ece4374
commit
1bd9fcdc80
5
.gitignore
vendored
5
.gitignore
vendored
|
|
@ -1,9 +1,6 @@
|
||||||
node_modules/
|
node_modules/
|
||||||
log/crash/*
|
log/*
|
||||||
!log/crash
|
|
||||||
www/doc/*/*
|
www/doc/*/*
|
||||||
!www/doc/client
|
|
||||||
!www/doc/server
|
|
||||||
package-lock.json
|
package-lock.json
|
||||||
config.json
|
config.json
|
||||||
config.json.old
|
config.json.old
|
||||||
|
|
|
||||||
|
|
@ -128,14 +128,15 @@ rememberMeToken.statics.authenticate = async function(id, token, failLine = "Bad
|
||||||
badLogin();
|
badLogin();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//Populate the user field
|
||||||
|
await tokenDB.populate('user');
|
||||||
|
|
||||||
//Check our password is correct
|
//Check our password is correct
|
||||||
if(await tokenDB.checkToken(token)){
|
if(await tokenDB.checkToken(token)){
|
||||||
//Populate the user field
|
|
||||||
await tokenDB.populate('user');
|
|
||||||
|
|
||||||
//Return the user doc
|
//Return the user doc
|
||||||
return tokenDB.user;
|
return tokenDB.user;
|
||||||
}else{
|
}else{
|
||||||
|
loggerUtils.dumpSecurityLog(`Failed attempt at ${tokenDB.user.user}'s Remember-Me token {${tokenDB.id}}... Nuking token!`);
|
||||||
//Nuke the token for security
|
//Nuke the token for security
|
||||||
await tokenDB.deleteOne();
|
await tokenDB.deleteOne();
|
||||||
//if not scream and shout
|
//if not scream and shout
|
||||||
|
|
|
||||||
|
|
@ -186,6 +186,11 @@ userSchema.pre('save', async function (next){
|
||||||
|
|
||||||
//If rank was changed
|
//If rank was changed
|
||||||
if(this.isModified("rank")){
|
if(this.isModified("rank")){
|
||||||
|
//If this rank change is above 2 (Mod or above)
|
||||||
|
if(permissionModel.rankToNum(this.rank) > 2){
|
||||||
|
loggerUtils.dumpSecurityLog(`${this.user}'s rank was set to ${this.rank}.`);
|
||||||
|
}
|
||||||
|
|
||||||
//force a full log-out
|
//force a full log-out
|
||||||
await this.killAllSessions("Your site-wide rank has changed. Sign-in required.");
|
await this.killAllSessions("Your site-wide rank has changed. Sign-in required.");
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -16,6 +16,7 @@ along with this program. If not, see <https://www.gnu.org/licenses/>.*/
|
||||||
|
|
||||||
//Node
|
//Node
|
||||||
const fs = require('node:fs/promises');
|
const fs = require('node:fs/promises');
|
||||||
|
const crypto = require('node:crypto');
|
||||||
|
|
||||||
//Config
|
//Config
|
||||||
const config = require('../../config.json');
|
const config = require('../../config.json');
|
||||||
|
|
@ -172,42 +173,68 @@ module.exports.errorMiddleware = function(err, req, res, next){
|
||||||
* Dumps unexpected server crashes to dedicated log files
|
* Dumps unexpected server crashes to dedicated log files
|
||||||
* @param {Error} err - error to dump to file
|
* @param {Error} err - error to dump to file
|
||||||
* @param {Date} date - Date of error, defaults to now
|
* @param {Date} date - Date of error, defaults to now
|
||||||
|
* @param {String} subDir - subdirectory inside the log folder we want to dump to
|
||||||
|
* @param {Boolean} muzzle - Tells the function to STFU
|
||||||
*/
|
*/
|
||||||
module.exports.dumpError = async function(err, date = new Date(), subDir = ''){
|
module.exports.dumpError = async function(err, date = new Date(), subDir = 'crash/', muzzle = false){
|
||||||
|
//Generate content from error
|
||||||
|
const content = `Error Date: ${date.toLocaleString()} (UTC-${date.getTimezoneOffset()/60})\nError Type: ${err.name}\nError Msg:${err.message}\nStack Trace:\n\n${err.stack}`;
|
||||||
|
|
||||||
|
//Dump text to file
|
||||||
|
module.exports.dumpLog(content, date.getTime(), subDir, muzzle);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
module.exports.dumpSecurityLog = async function(content, date = new Date()){
|
||||||
|
module.exports.dumpLog(content, `Incident-{${crypto.randomUUID()}}-${date.getTime()}`, 'security/', true);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Dumps log file to log folder
|
||||||
|
* @param {String} content - Text to dump to file
|
||||||
|
* @param {String} name - file name to save to
|
||||||
|
* @param {String} subDir - subdirectory inside the log folder we want to dump to
|
||||||
|
* @param {Boolean} muzzle - Tells the function to STFU
|
||||||
|
*/
|
||||||
|
module.exports.dumpLog = async function(content, name, subDir = '/', muzzle = false){
|
||||||
try{
|
try{
|
||||||
//Crash directory
|
//Crash directory
|
||||||
const dir = `./log/crash/${subDir}`
|
const dir = `./log/${subDir}`
|
||||||
|
|
||||||
//Double check crash folder exists
|
//Double check crash folder exists
|
||||||
try{
|
try{
|
||||||
await fs.stat(dir);
|
await fs.stat(dir);
|
||||||
//If we caught an error (most likely it's missing)
|
//If we caught an error (most likely it's missing)
|
||||||
}catch(err){
|
}catch(err){
|
||||||
//Shout about it
|
if(!muzzle){
|
||||||
module.exports.consoleWarn("Log folder missing, mking dir!")
|
//Shout about it
|
||||||
|
module.exports.consoleWarn("Log folder missing, mking dir!")
|
||||||
|
}
|
||||||
|
|
||||||
//Make it if doesn't
|
//Make it if doesn't
|
||||||
await fs.mkdir(dir, {recursive: true});
|
await fs.mkdir(dir, {recursive: true});
|
||||||
}
|
}
|
||||||
|
|
||||||
//Assemble log file path
|
//Assemble log file path
|
||||||
const path = `${dir}${date.getTime()}.log`;
|
const path = `${dir}${name}.log`;
|
||||||
//Generate error file content
|
|
||||||
const content = `Error Date: ${date.toLocaleString()} (UTC-${date.getTimezoneOffset()/60})\nError Type: ${err.name}\nError Msg:${err.message}\nStack Trace:\n\n${err.stack}`;
|
|
||||||
|
|
||||||
//Write content to file
|
//Write content to file
|
||||||
fs.writeFile(path, content);
|
fs.writeFile(path, content);
|
||||||
|
|
||||||
//Whine about the error
|
if(!muzzle){
|
||||||
module.exports.consoleWarn(`Warning: Unexpected Server Crash gracefully dumped to '${path}'... SOMETHING MAY BE VERY BROKEN!!!!`);
|
//Whine about the error
|
||||||
|
module.exports.consoleWarn(`Warning: Unexpected Server Crash gracefully dumped to '${path}'... SOMETHING MAY BE VERY BROKEN!!!!`);
|
||||||
|
}
|
||||||
//If somethine went really really wrong
|
//If somethine went really really wrong
|
||||||
}catch(doubleErr){
|
}catch(doubleErr){
|
||||||
//Use humor to cope with the pain
|
if(!muzzle){
|
||||||
module.exports.consoleWarn("Yo Dawg, I herd you like errors, so I put an error in your error dump, so you can dump while you dump:");
|
//Use humor to cope with the pain
|
||||||
//Dump the original error to console
|
module.exports.consoleWarn("Yo Dawg, I herd you like errors, so I put an error in your error dump, so you can dump while you dump:");
|
||||||
module.exports.consoleWarn(err);
|
//Dump the original error to console
|
||||||
//Dump the error we had saving that error to file to console
|
module.exports.consoleWarn(err);
|
||||||
module.exports.consoleWarn(doubleErr);
|
//Dump the error we had saving that error to file to console
|
||||||
|
module.exports.consoleWarn(doubleErr);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -78,7 +78,7 @@ module.exports.mailem = async function(to, subject, body, htmlBody = false){
|
||||||
//return the mail info
|
//return the mail info
|
||||||
return sentMail;
|
return sentMail;
|
||||||
}catch(err){
|
}catch(err){
|
||||||
loggerUtils.dumpError(err, new Date(), 'mail/');
|
loggerUtils.dumpError(err, new Date(), 'crash/mail/');
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue