configurable flood timers close #255

merge-requests/208/head
Thomas Lynch 4 years ago
parent 58d84696f4
commit a2fa19742d
  1. 6
      configs/main.js.example
  2. 66
      helpers/checks/spamcheck.js

@ -66,6 +66,12 @@ module.exports = {
cacheTime: 3600 //in seconds, idk whats a good value cacheTime: 3600 //in seconds, idk whats a good value
}, },
floodTimers: { //basic delays to stop flooding, in ms. 0 to disable
sameContentSameIp: 120000, //same message or any file from same ip
sameContentAnyIp: 30000, //same message or file from any ip
anyContentSameIp: 5000, //any post from the same ip
},
//block bypasses //block bypasses
blockBypass: { blockBypass: {
enabled: false, enabled: false,

@ -3,6 +3,7 @@
const Mongo = require(__dirname+'/../../db/db.js') const Mongo = require(__dirname+'/../../db/db.js')
, { Posts } = require(__dirname+'/../../db/') , { Posts } = require(__dirname+'/../../db/')
, timeUtils = require(__dirname+'/../timeutils.js') , timeUtils = require(__dirname+'/../timeutils.js')
, { sameContentSameIp, sameContentAnyIp, anyContentSameIp } = require(__dirname+'/../../configs/main.js').floodTimers;
module.exports = async (req, res) => { module.exports = async (req, res) => {
@ -10,10 +11,13 @@ module.exports = async (req, res) => {
return false; return false;
} }
if (sameContentSameIp === 0
&& sameContentAnyIp === 0
&& anyContentSameIp === 0) {
return false;
}
const now = Date.now(); const now = Date.now();
const last120id = Mongo.ObjectId.createFromTime(Math.floor((now - (timeUtils.MINUTE*2))/1000));
const last30id = Mongo.ObjectId.createFromTime(Math.floor((now - (timeUtils.MINUTE*0.5))/1000));
const last5id = Mongo.ObjectId.createFromTime(Math.floor((now - 5000)/1000));
const ors = []; const ors = [];
const contentOr = []; const contentOr = [];
if (res.locals.numFiles > 0) { if (res.locals.numFiles > 0) {
@ -32,28 +36,40 @@ module.exports = async (req, res) => {
'nomarkup': req.body.message 'nomarkup': req.body.message
}) })
} }
//matching content from any IP in the past 30 seconds
ors.push({ if (sameContentAnyIp) {
'_id': { //matching content from any IP
'$gt': last30id const sameContentAnyIpMongoId = Mongo.ObjectId.createFromTime(Math.floor((now - sameContentAnyIp)/1000));
}, ors.push({
'$or': contentOr '_id': {
}); '$gt': sameContentAnyIpMongoId
//matching content from same IP in last 2 minutes },
ors.push({ '$or': contentOr
'_id': { });
'$gt': last120id }
},
'ip.single': res.locals.ip.single, if (sameContentSameIp > 0) {
'$or': contentOr //matching content from same IP
}); const sameContentSameIpMongoId = Mongo.ObjectId.createFromTime(Math.floor((now - sameContentSameIp)/1000));
//any posts from same IP in past 5 seconds TODO: make this just use a redis key of IP and expire after 5 seconds ors.push({
ors.push({ '_id': {
'_id': { '$gt': sameContentSameIpMongoId
'$gt': last5id },
}, 'ip.single': res.locals.ip.single,
'ip.single': res.locals.ip.single '$or': contentOr
}) });
}
if (anyContentSameIp > 0) {
//any posts from same IP
const anyContentSameIpMongoId = Mongo.ObjectId.createFromTime(Math.floor((now - anyContentSameIp)/1000));
ors.push({
'_id': {
'$gt': anyContentSameIpMongoId
},
'ip.single': res.locals.ip.single
})
}
let flood = await Posts.db.find({ let flood = await Posts.db.find({
'$or': ors '$or': ors

Loading…
Cancel
Save