spee.ch/server/middleware/autoblockPublishMiddleware.js

72 lines
1.8 KiB
JavaScript
Raw Normal View History

2018-10-29 21:43:15 +01:00
const fs = require('fs');
const logger = require('winston');
const { publishing: { publishingChannelWhitelist } } = require('@config/siteConfig');
2018-11-12 21:12:31 +01:00
const ipBanFile = './site/config/ipBan.txt';
2018-10-29 21:43:15 +01:00
const forbiddenMessage = '<h1>Forbidden</h1>If you are seeing this by mistake, please contact us using <a href="https://chat.lbry.io/">https://chat.lbry.io/</a>';
let ipCounts = {};
let blockedAddresses = [];
2018-11-11 01:11:12 +01:00
if (fs.existsSync(ipBanFile)) {
2018-10-29 21:43:15 +01:00
const lineReader = require('readline').createInterface({
input: require('fs').createReadStream(ipBanFile),
});
lineReader.on('line', (line) => {
2018-11-11 01:11:12 +01:00
if (line && line !== '') {
2018-10-29 21:43:15 +01:00
blockedAddresses.push(line);
}
});
}
const autoblockPublishMiddleware = (req, res, next) => {
let ip = (req.headers['x-forwarded-for'] || req.connection.remoteAddress).split(/,\s?/)[0];
2018-11-11 01:11:12 +01:00
if (blockedAddresses.indexOf(ip) !== -1) {
2018-10-29 21:43:15 +01:00
res.status(403).send(forbiddenMessage);
res.end();
return;
}
let count = ipCounts[ip] = (ipCounts[ip] || 0) + 1;
setTimeout(() => {
2018-11-11 01:11:12 +01:00
if (ipCounts[ip]) {
2018-10-29 21:43:15 +01:00
ipCounts[ip]--;
2018-11-11 01:11:12 +01:00
if (ipCounts[ip] === 0) {
2018-10-29 21:43:15 +01:00
delete ipCounts[ip];
}
}
2018-11-11 01:11:12 +01:00
}, 600000 /* 10 minute retainer */);
2018-10-29 21:43:15 +01:00
2018-11-11 01:11:12 +01:00
if (count === 10) {
2018-10-29 21:43:15 +01:00
logger.error(`Banning IP: ${ip}`);
blockedAddresses.push(ip);
res.status(403).send(forbiddenMessage);
res.end();
fs.appendFile(ipBanFile, ip + '\n', () => {});
} else {
next();
}
2018-11-11 01:11:12 +01:00
};
2018-10-29 21:43:15 +01:00
const autoblockPublishBodyMiddleware = (req, res, next) => {
2018-11-11 01:11:12 +01:00
if (req.body && publishingChannelWhitelist) {
2018-10-29 21:43:15 +01:00
let ip = (req.headers['x-forwarded-for'] || req.connection.remoteAddress).split(/,\s?/)[0];
const { channelName } = req.body;
2018-11-12 21:21:07 +01:00
if (channelName && publishingChannelWhitelist.indexOf(channelName.toLowerCase()) !== -1) {
2018-10-29 21:43:15 +01:00
delete ipCounts[ip];
}
}
next();
2018-11-11 01:11:12 +01:00
};
2018-10-29 21:43:15 +01:00
module.exports = {
autoblockPublishMiddleware,
autoblockPublishBodyMiddleware,
};