From 1e607d2b8a37a1f4e27038d5d5e3473a9d6d3f49 Mon Sep 17 00:00:00 2001 From: Shawn Date: Sat, 20 Oct 2018 03:23:46 -0500 Subject: [PATCH] Build persisting IP block lists --- server/routes/api/index.js | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-) diff --git a/server/routes/api/index.js b/server/routes/api/index.js index f42cd0b9..b4c31df9 100644 --- a/server/routes/api/index.js +++ b/server/routes/api/index.js @@ -1,3 +1,5 @@ +const fs = require('fs'); + // middleware const multipartMiddleware = require('../../middleware/multipartMiddleware'); const torCheckMiddleware = require('../../middleware/torCheckMiddleware'); @@ -22,17 +24,28 @@ const getBlockedList = require('../../controllers/api/blocked'); const getOEmbedData = require('../../controllers/api/oEmbed'); const logger = require('winston'); - +const ipBanFile = '../../../config/ipBan.txt'; const forbiddenMessage = '

Forbidden

If you are seeing this by mistake, please contact us using https://chat.lbry.io/'; 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]; if(blockedAddresses.indexOf(ip) !== -1) { - logger.warn(`Banned IP publish attempt: ${ip}`); res.status(403).send(forbiddenMessage); res.end(); @@ -53,6 +66,8 @@ const autoblockPublishMiddleware = (req, res, next) => { blockedAddresses.push(ip); res.status(403).send(forbiddenMessage); res.end(); + + fs.appendFile(ipBanFile, ip + '\n', () => {}); } else { next(); }