make the ip prune thingy a schedule

merge-requests/218/head
Thomas Lynch 4 years ago
parent 32ec1152b4
commit 621a83a589
  1. 8
      configs/main.js.example
  2. 49
      gulpfile.js
  3. 6
      helpers/tasks.js
  4. 7
      schedules/index.js
  5. 53
      schedules/ips.js

@ -123,9 +123,11 @@ module.exports = {
//max wait time in ms for obtaining locks for saving files
lockWait: 3000,
//optionally prune oldest modlog entries (prunes when newer modlog entries are generated i.e. dead boards wont have older logs pruned)
pruneModlogs: true,
pruneAfterDays: 30,
//optionally prune modlog entries older than x days, false to disable (prunes when newer modlog entries are generated i.e. dead boards wont have older logs pruned)
pruneModlogs: 30,
//option to prune ips on posts older than x days, false to disable
pruneIps: false,
//enable the webring (also copy configs/webring.json.example -> configs/webring.json and edit)
enableWebring: false,

@ -16,7 +16,7 @@ const gulp = require('gulp')
, pug = require('pug')
, gulppug = require('gulp-pug')
, { migrateVersion } = require(__dirname+'/package.json')
, { createHash, randomBytes } = require('crypto')
, { randomBytes } = require('crypto')
, paths = {
styles: {
src: 'gulp/res/css/',
@ -59,54 +59,11 @@ async function password() {
}
async function ips() {
/*
prune IPs from old posts (actually, rehash them with a temporary random salt to maintain
post history and prevent *-by-ip action unintentionally deleting many posts)
NOTE: ips may still remain in the following collections:
- bans, because bans need the IP to function
- modlog actioner ips, modlogs are already auto-pruned
- ratelimits, these only last 1 minute
- stats, these last max of 24 hours
*/
const Mongo = require(__dirname+'/db/db.js')
await Mongo.connect();
const Redis = require(__dirname+'/redis.js')
const { Posts } = require(__dirname+'/db/');
const beforeDate = new Date();
beforeDate.setDate(beforeDate.getDate() - 7); //7 days in the past, static number for now until i implement yargs or similar
const beforeDateMongoId = Mongo.ObjectId.createFromTime(Math.floor(beforeDate.getTime()/1000));
const tempIpHashSecret = randomBytes(20).toString('base64');
const bulkWrites = [];
await Posts.db.find({
_id: {
$lte: beforeDateMongoId,
},
'ip.pruned': {
$ne: true
}
}).forEach(post => {
const randomIP = createHash('sha256').update(tempIpHashSecret + post.ip.single).digest('base64');
bulkWrites.push({
updateOne: {
filter: {
_id: post._id,
},
update: {
$set: {
'ip.pruned': true,
'ip.raw': randomIP,
'ip.single': randomIP,
'ip.qrange': randomIP,
'ip.hrange': randomIP,
}
}
}
});
});
console.log(`Randomising ip on ${bulkWrites.length} posts`);
if (bulkWrites.length.length > 0) {
await Posts.db.bulkWrite(bulkWrites);
}
const ipSchedule = require(__dirname+'/schedules/ips.js');
await ipSchedule();
Redis.redisClient.quit();
return Mongo.client.close();
}

@ -4,7 +4,7 @@ const Mongo = require(__dirname+'/../db/db.js')
, timeUtils = require(__dirname+'/timeutils.js')
, uploadDirectory = require(__dirname+'/files/uploadDirectory.js')
, { remove } = require('fs-extra')
, { debugLogs, pruneModlogs, pruneAfterDays, enableWebring, maxRecentNews } = require(__dirname+'/../configs/main.js')
, { debugLogs, pruneModlogs, enableWebring, maxRecentNews } = require(__dirname+'/../configs/main.js')
, { CustomPages, Stats, Posts, Files, Boards, News, Modlogs } = require(__dirname+'/../db/')
, cache = require(__dirname+'/../redis.js')
, render = require(__dirname+'/render.js')
@ -188,9 +188,9 @@ module.exports = {
const label = `/${options.board._id}/logs.html`;
const start = process.hrtime();
let dates = await Modlogs.getDates(options.board);
if (pruneModlogs === true) {
if (pruneModlogs) {
const pruneLogs = [];
const pruneAfter = new Date(Date.now()-timeUtils.DAY*pruneAfterDays);
const pruneAfter = new Date(Date.now()-timeUtils.DAY*pruneModlogs);
dates = dates.filter(date => {
const { year, month, day } = date.date;
if (new Date(year, month-1, day) > pruneAfter) { //-1 for 0-index months

@ -6,7 +6,7 @@ process
const timeUtils = require(__dirname+'/../helpers/timeutils.js')
, Mongo = require(__dirname+'/../db/db.js')
, { pruneImmediately, debugLogs, enableWebring } = require(__dirname+'/../configs/main.js')
, { pruneIps, pruneImmediately, debugLogs, enableWebring } = require(__dirname+'/../configs/main.js')
, doInterval = require(__dirname+'/../helpers/dointerval.js');
(async () => {
@ -37,6 +37,11 @@ const timeUtils = require(__dirname+'/../helpers/timeutils.js')
doInterval(pruneFiles, timeUtils.DAY, true);
}
if (pruneIps) {
const ipSchedule = require(__dirname+'/ips.js');
doInterval(ipSchedule, timeUtils.DAY, true);
}
//update the webring
if (enableWebring) {
const updateWebring = require(__dirname+'/webring.js');

@ -0,0 +1,53 @@
'use strict';
/*
prune IPs from old posts (actually, rehash them with a temporary random salt to maintain
post history and prevent *-by-ip action unintentionally deleting many posts)
NOTE: ips may still remain in the following collections:
- bans, because bans need the IP to function
- modlog actioner ips, modlogs are already auto-pruned
- ratelimits, these only last 1 minute
- stats, these last max of 24 hours
*/
const Mongo = require(__dirname+'/../db/db.js')
, { Posts } = require(__dirname+'/../db/')
, { createHash, randomBytes } = require('crypto')
, { pruneIps } = require(__dirname+'/../configs/main.js');
module.exports = async (days) => {
const beforeDate = new Date();
beforeDate.setDate(beforeDate.getDate() - days);
const beforeDateMongoId = Mongo.ObjectId.createFromTime(Math.floor(beforeDate.getTime()/1000));
const tempIpHashSecret = randomBytes(20).toString('base64');
const bulkWrites = [];
await Posts.db.find({
_id: {
$lte: beforeDateMongoId,
},
'ip.pruned': {
$ne: true
}
}).forEach(post => {
const randomIP = createHash('sha256').update(tempIpHashSecret + post.ip.single).digest('base64');
bulkWrites.push({
updateOne: {
filter: {
_id: post._id,
},
update: {
$set: {
'ip.pruned': true,
'ip.raw': randomIP,
'ip.single': randomIP,
'ip.qrange': randomIP,
'ip.hrange': randomIP,
}
}
}
});
});
console.log(`Randomising ip on ${bulkWrites.length} posts`);
if (bulkWrites.length.length > 0) {
await Posts.db.bulkWrite(bulkWrites);
}
}
Loading…
Cancel
Save