diff --git a/backend/api/controllers/result.js b/backend/api/controllers/result.js index ceceaaaf7..adc9c86df 100644 --- a/backend/api/controllers/result.js +++ b/backend/api/controllers/result.js @@ -47,6 +47,7 @@ class ResultController { const { uid } = req.decodedToken; const { result } = req.body; result.testDuration = parseFloat(result.testDuration); + result.uid = uid; if (validateObjectValues(result) > 0) return res.status(400).json({ message: "Bad input" }); if ( @@ -159,6 +160,19 @@ class ResultController { (result.wpm > 200 && result.consistency < 70) ) { //possible bot + Logger.log( + "anticheat_triggered", + { + durationSD: result.keyDurationStats.sd, + durationAvg: result.keyDurationStats.average, + spacingSD: result.keySpacingStats.sd, + spacingAvg: result.keySpacingStats.average, + wpm: result.wpm, + acc: result.acc, + consistency: result.consistency, + }, + uid + ); return res.status(400).json({ message: "Possible bot detected" }); } if ( @@ -170,6 +184,19 @@ class ResultController { result.keyDurationStats.average <= 20) ) { //close to the bot detection threshold + Logger.log( + "anticheat_close", + { + durationSD: result.keyDurationStats.sd, + durationAvg: result.keyDurationStats.average, + spacingSD: result.keySpacingStats.sd, + spacingAvg: result.keySpacingStats.average, + wpm: result.wpm, + acc: result.acc, + consistency: result.consistency, + }, + uid + ); } } else { return res.status(400).json({ message: "Missing key data" }); diff --git a/backend/handlers/validation.js b/backend/handlers/validation.js index 23fcb2e87..0412bdd8f 100644 --- a/backend/handlers/validation.js +++ b/backend/handlers/validation.js @@ -1,5 +1,5 @@ const MonkeyError = require("./error"); - +const Logger = require("../../handlers/logger"); const { roundTo2 } = require("./misc"); function isUsernameValid(name) { @@ -13,8 +13,10 @@ function isUsernameValid(name) { function validateResult(result) { if (result.wpm > result.rawWpm) { - console.error( - `Could not validate result for ${result.uid}. ${result.wpm} > ${result.rawWpm}` + Logger.log( + "result_validation_error", + `${result.wpm} wpm > ${result.rawWpm} raw`, + result.uid ); return false; } @@ -23,8 +25,10 @@ function validateResult(result) { wpm < result.wpm - result.wpm * 0.01 || wpm > result.wpm + result.wpm * 0.01 ) { - console.error( - `Could not validate result for ${result.uid}. wpm ${wpm} != ${result.wpm}` + Logger.log( + "result_validation_error", + `wpm ${wpm} != ${result.wpm}`, + result.uid ); return false; } @@ -37,8 +41,10 @@ function validateResult(result) { keyPressTimeSum < result.testDuration - 1 || keyPressTimeSum > result.testDuration + 1 ) { - console.error( - `Could not validate key spacing sum for ${result.uid}. ${keyPressTimeSum} !~ ${result.testDuration}` + Logger.log( + "result_validation_error", + `key spacing sum ${keyPressTimeSum} !~ ${result.testDuration}`, + result.uid ); return false; } @@ -47,8 +53,10 @@ function validateResult(result) { result.testDuration < result.mode2 - 1 || result.testDuration > result.mode2 + 1 ) { - console.error( - `Could not validate test duration for ${result.uid}. ${result.testDuration} !~ ${result.mode2}` + Logger.log( + "result_validation_error", + `test duration ${result.testDuration} !~ ${result.mode2}`, + result.uid ); return false; }