From c03766b565ff9a67017542e915765fae43b4e10a Mon Sep 17 00:00:00 2001 From: fenwick67 Date: Mon, 5 Aug 2019 17:31:13 -0400 Subject: [PATCH] add blocklist --- index.js | 68 ++++++++++++++++++++++++++++++++++++++++-------- lib/template.ejs | 18 +++++++------ 2 files changed, 67 insertions(+), 19 deletions(-) diff --git a/index.js b/index.js index fe22c01..310a1f0 100644 --- a/index.js +++ b/index.js @@ -25,6 +25,14 @@ function doCache(res,durationSecs){ }) } +// web.site:another.one.here => [ /web\.site/i , /another\.one/i ] +var blocklist = []; +if (process.env["BLOCKLIST"]){ + blocklist = process.env["BLOCKLIST"].split(':').map((s)=>{ + var dotsFixed = s.replace(/\./gi,'\\.'); + return new RegExp(dotsFixed, 'i'); + }); +} // this just redirects to the app.options('/api/feed',cors()); @@ -64,7 +72,6 @@ app.get('/apiv2/feed',cors(),logger,function(req,res){ res.send(errorPage(400,'You need to specify a user URL')); return; } - var feedUrl = req.query.feedurl; var opts = {}; @@ -103,16 +110,55 @@ app.get('/apiv2/feed',cors(),logger,function(req,res){ opts.feedUrl = feedUrl; opts.mastofeedUrl = req.url; - convertv2(opts).then((data)=>{ - res.status(200); - doCache(res,60*60); - res.send(data); - }).catch((er)=>{ - res.status(500); - res.send(errorPage(500,null,{theme:opts.theme,size:opts.size})); - // TODO log the error - console.error(er,er.stack); - }) + var blocked = false; + + function fakeFail(){ + var t = 1000 + 1000 * Math.random() * Math.random(); + blocked = true; + setTimeout(function(){ + res.status(500); + res.send(errorPage(500,null,{theme:opts.theme,size:opts.size})); + },t); + } + + // shall I block the user? + var base = new URL(userUrl).hostname; + for (var i = 0; i < blocklist.length; i++){ + var re = blocklist[i]; + if (re.test(base)){ + fakeFail(); + console.log("blocked domain: "+base+" (matches "+re.source+")"); + return; // need to exit this function so feed isn't actually fetched + } + } + + // block by referer + var ref = req.get("referer") + if (ref){ + for (var i = 0; i < blocklist.length; i++){ + var re = blocklist[i]; + if (re.test(ref)){ + fakeFail(); + console.log("blocked domain via referer: "+base+" (matches "+re.source+")"); + return; // need to exit this function so feed isn't actually fetched + } + } + } + + if (!blocked){ + + convertv2(opts).then((data)=>{ + res.status(200); + doCache(res,60*60); + res.send(data); + }).catch((er)=>{ + res.status(500); + res.send(errorPage(500,null,{theme:opts.theme,size:opts.size})); + // TODO log the error + console.error(er,er.stack); + }) + + } }) app.listen(process.env.PORT || 8000,function(){ diff --git a/lib/template.ejs b/lib/template.ejs index 8627c63..c81ee48 100644 --- a/lib/template.ejs +++ b/lib/template.ejs @@ -88,15 +88,17 @@
<%= item.stringDate %>
<% }); %> - <% if (nextPageLink) %> - - <% %> + <% if (nextPageLink) {%> + + <% } %> - + <% if (nextPageLink) {%> + + <% } %> <% if ( isIndex ){ %>