added log levels

This commit is contained in:
Mystikfluu 2022-05-27 12:56:45 +02:00
parent 03e022b3d8
commit e41aefd9cc
2 changed files with 27 additions and 23 deletions

View File

@ -14,9 +14,12 @@ function ensureExists(path, mask, cb) {
}); });
} }
const config = JSON.parse(fs.readFileSync("server_config.json"))
const time = Date.now() const time = Date.now()
const original_log = console.log const original_log = console.log
function log_info(...info) { function log_info(level, ...info) {
if(config["logs"] && config["logs"]["level"] && config["logs"]["level"] >= level) {
ensureExists(__dirname + '/logs/', function(err) { ensureExists(__dirname + '/logs/', function(err) {
if(err) { if(err) {
process.stderr.write(tolog) //just write it to stderr process.stderr.write(tolog) //just write it to stderr
@ -32,6 +35,7 @@ function log_info(...info) {
}) })
} }
}); });
}
} }
console.log = log_info console.log = log_info
@ -56,8 +60,6 @@ const app = express();
const csrfProtection = csurf({ cookie: true }) const csrfProtection = csurf({ cookie: true })
const config = JSON.parse(fs.readFileSync("server_config.json"))
const HASHES_DB = config.cookies.server_hashes const HASHES_DB = config.cookies.server_hashes
const HASHES_COOKIE = config.cookies.client_hashes const HASHES_COOKIE = config.cookies.client_hashes
const HASHES_DIFF = HASHES_DB - HASHES_COOKIE const HASHES_DIFF = HASHES_DB - HASHES_COOKIE
@ -129,7 +131,7 @@ function clientErrorHandler(err, req, res, next) {
if (req.xhr) { if (req.xhr) {
res.status(200).send({ error: 'Something failed!' }); res.status(200).send({ error: 'Something failed!' });
} else { } else {
console.log(err); console.log(1,err);
} }
} else { } else {
next() next()
@ -185,14 +187,14 @@ function increaseAPICall(req,res,next) {
REVERSE_SESSIONS[ip]=undefined REVERSE_SESSIONS[ip]=undefined
},50000) },50000)
res.cookie('session',session, { maxAge: 100000, httpOnly: true, secure: DID_I_FINALLY_ADD_HTTPS }); res.cookie('session',session, { maxAge: 100000, httpOnly: true, secure: DID_I_FINALLY_ADD_HTTPS });
console.log("sending session to " + ip); console.log(3,"sending session to " + ip);
} }
} }
if(API_CALLS[ip] >= config.rate_limits.api.max_with_session) { if(API_CALLS[ip] >= config.rate_limits.api.max_with_session) {
res.status(429) res.status(429)
res.send("You are sending too many api calls!") res.send("You are sending too many api calls!")
console.log("rate limiting " + ip); console.log(3,"rate limiting " + ip);
return false return false
} }
API_CALLS[ip]++; API_CALLS[ip]++;
@ -206,7 +208,7 @@ function increaseUSERCall(req,res,next) {
if(USER_CALLS[ip] >= config.rate_limits.user.max) { if(USER_CALLS[ip] >= config.rate_limits.user.max) {
res.status(429) res.status(429)
res.send("You are sending too many requests!") res.send("You are sending too many requests!")
console.log("rate limiting " + ip); console.log(2,"rate limiting " + ip);
return false return false
} }
USER_CALLS[ip]++; USER_CALLS[ip]++;
@ -271,7 +273,6 @@ router.use("/api/*",async function(req,res,next) {
res.locals.bio = result[0].User_Bio || "" res.locals.bio = result[0].User_Bio || ""
next() next()
} else { } else {
console.log(result[0],values[0],values[1]);
res.status(400) res.status(400)
res.json({"error":"you cannot access the api without being logged in"}) res.json({"error":"you cannot access the api without being logged in"})
} }
@ -313,7 +314,7 @@ router.post("/api/post", async function(req,res) {
ws.send("new_post " + res.locals.username) ws.send("new_post " + res.locals.username)
}); });
res.json({"success":"successfully posted message"}) res.json({"success":"successfully posted message"})
console.log(`posted new message by ${res.locals.username} : ${req.body.message}`); console.log(5,`posted new message by ${res.locals.username} : ${req.body.message}`);
}); });
}) })

View File

@ -19,5 +19,8 @@
"reset_time": 30000, "reset_time": 30000,
"max": 60 "max": 60
} }
},
"logs": {
"level": 5
} }
} }