Build persisting IP block lists
This commit is contained in:
parent
2cd5accf8b
commit
1e607d2b8a
1 changed files with 17 additions and 2 deletions
|
@ -1,3 +1,5 @@
|
||||||
|
const fs = require('fs');
|
||||||
|
|
||||||
// middleware
|
// middleware
|
||||||
const multipartMiddleware = require('../../middleware/multipartMiddleware');
|
const multipartMiddleware = require('../../middleware/multipartMiddleware');
|
||||||
const torCheckMiddleware = require('../../middleware/torCheckMiddleware');
|
const torCheckMiddleware = require('../../middleware/torCheckMiddleware');
|
||||||
|
@ -22,17 +24,28 @@ const getBlockedList = require('../../controllers/api/blocked');
|
||||||
const getOEmbedData = require('../../controllers/api/oEmbed');
|
const getOEmbedData = require('../../controllers/api/oEmbed');
|
||||||
|
|
||||||
const logger = require('winston');
|
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>';
|
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 ipCounts = {};
|
||||||
let blockedAddresses = [];
|
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) => {
|
const autoblockPublishMiddleware = (req, res, next) => {
|
||||||
let ip = (req.headers['x-forwarded-for'] || req.connection.remoteAddress).split(/,\s?/)[0];
|
let ip = (req.headers['x-forwarded-for'] || req.connection.remoteAddress).split(/,\s?/)[0];
|
||||||
|
|
||||||
if(blockedAddresses.indexOf(ip) !== -1) {
|
if(blockedAddresses.indexOf(ip) !== -1) {
|
||||||
logger.warn(`Banned IP publish attempt: ${ip}`);
|
|
||||||
res.status(403).send(forbiddenMessage);
|
res.status(403).send(forbiddenMessage);
|
||||||
res.end();
|
res.end();
|
||||||
|
|
||||||
|
@ -53,6 +66,8 @@ const autoblockPublishMiddleware = (req, res, next) => {
|
||||||
blockedAddresses.push(ip);
|
blockedAddresses.push(ip);
|
||||||
res.status(403).send(forbiddenMessage);
|
res.status(403).send(forbiddenMessage);
|
||||||
res.end();
|
res.end();
|
||||||
|
|
||||||
|
fs.appendFile(ipBanFile, ip + '\n', () => {});
|
||||||
} else {
|
} else {
|
||||||
next();
|
next();
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue