/ip-blacklist

Basic middleware for blacklisting ip addresses

Primary LanguageJavaScriptMIT LicenseMIT

ip-blacklist

Build Status David Coveralls npm npm GitHub issues

Basic middleware for blacklisting ip addresses

Looking for an ip-whitelist? Check out the pendant to this package at npm or GitHub

Usage

Install and save the package to your project npm i --save ip-blacklist

const ipBlacklist = require('ip-blacklist'), path = require('path');

// Use the predefined array callback
// NOTE: Changes in the array you pass to ipBlacklist.array will not be considered!
app.use(ipBlacklist(ipBlacklist.array(['127.0.0.1', '::1'])));

// Use the predefined file callback
// NOTE: One line in the file represents an IP address
app.use(ipBlacklist(ipBlacklist.file(path.join(__dirname, 'blacklist.txt'))));

// Create your own callback
app.use(ipBlacklist(ip => {
    return ip === '192.168.178.1' || ip === '192.168.178.2';
}));

// Chain multiple callbacks
app.use(ipBlacklist(ipBlacklist.chain(
    ipBlacklist.file(path.join(__dirname, 'blacklist-a.txt')),
    ipBlacklist.file(path.join(__dirname, 'blacklist-b.txt'))
)));

More advanced usage

const ipBlacklist = require('ip-blacklist');

let blacklist = [];

app.use(ipBlacklist(ip => {
    return blacklist.indexOf(ip) !== -1;
}));
app.post('/api/blacklist/:ip', (req, res) => {
    blacklist.push(req.params.ip);
    res.end('Added IP to blacklist');
});
app.get('/api/blacklist', (req, res) => {
    res.json(blacklist);
});