Build persisting IP block lists (#643)

* Fix IP ban and add logging

* Build persisting IP block lists
This commit is contained in:
Shawn K 2018-10-20 03:26:18 -05:00 committed by GitHub
parent 5c2a33250e
commit fdf0d21f1d
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -1,3 +1,5 @@
const fs = require('fs');
// middleware
const multipartMiddleware = require('../../middleware/multipartMiddleware');
const torCheckMiddleware = require('../../middleware/torCheckMiddleware');
@ -22,12 +24,24 @@ const getBlockedList = require('../../controllers/api/blocked');
const getOEmbedData = require('../../controllers/api/oEmbed');
const logger = require('winston');
const ipBanFile = '../../../config/ipBan.txt';
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 = [];
if(fs.existsSync(ipBanFile)) {
const lineReader = require('readline').createInterface({
input: require('fs').createReadStream(ipBanFile),
});
lineReader.on('line', (line) => {
if(line && line !== '') {
blockedAddresses.push(line);
}
});
}
const autoblockPublishMiddleware = (req, res, next) => {
let ip = (req.headers['x-forwarded-for'] || req.connection.remoteAddress).split(/,\s?/)[0];
@ -53,6 +67,8 @@ const autoblockPublishMiddleware = (req, res, next) => {
blockedAddresses.push(ip);
res.status(403).send(forbiddenMessage);
res.end();
fs.appendFile(ipBanFile, ip + '\n', () => {});
} else {
next();
}