Skip to content

Commit

Permalink
blacklisting ips
Browse files Browse the repository at this point in the history
  • Loading branch information
nsjames committed Nov 4, 2019
1 parent 172e1fd commit f5c6ea8
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 1 deletion.
7 changes: 7 additions & 0 deletions src/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import routes from './routes';
import cors from 'cors';
import compression from 'compression';
import AnalyticsService from "./services/AnalyticsService";
import Blacklist from "./util/blacklist";

const app = express();
app.disable('x-powered-by');
Expand Down Expand Up @@ -41,4 +42,10 @@ app.use((err, req, res, next) => { // eslint-disable-line no-unused-vars
});
});

const senderIp = req => req.headers['x-forwarded-for'] || req.connection.remoteAddress;
app.use((req, res, next) => {
if(Blacklist.get(senderIp(req)) < 20) next();
else res.send(null);
});

export default app;
6 changes: 5 additions & 1 deletion src/routes.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import config from "./util/config";
import * as ecc from "eosjs-ecc";
import BitcoinService from "./services/BitcoinService";
import WalletPackHelpers from "./services/WalletPackHelpers";
import Blacklist from "./util/blacklist";

const bucket = couchbase('scatter');

Expand Down Expand Up @@ -280,6 +281,9 @@ routes.post('/btc/pushtx', async (req, res) => {



routes.all('*', (req, res) => res.sendStatus(403));
routes.all('*', (req, res) => {
Blacklist.add(senderIp(req));
res.sendStatus(403);
});

export default routes;
13 changes: 13 additions & 0 deletions src/util/blacklist.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
const BLACKLIST = {};
export default class Blacklist {

static add(ip){
if(!BLACKLIST.hasOwnProperty(ip)) BLACKLIST[ip] = 0;
BLACKLIST[ip]++;
}

static get(ip){
return BLACKLIST[ip] || 0;
}

}

0 comments on commit f5c6ea8

Please sign in to comment.