1.2.2 • Published 5 years ago

ip-whitelist v1.2.2

Weekly downloads
140
License
MIT
Repository
github
Last release
5 years ago

ip-whitelist

Build Status David Coveralls npm npm GitHub issues npm.io

Basic middleware for whitelisting ip addresses

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

Usage

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

const ipWhitelist = require('ip-whitelist'), path = require('path');

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

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

// Create your own callback
app.use(ipWhitelist(ip => {
    return ip === '127.0.0.1' || ip === '::1';
}));

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

More advanced usage

The default behaviour when handling a blocked IP is to end the request with status 403 and 'IP not whitelisted'. To change that, pass a function as a second parameter to ipWhitelist(). This function takes to arguments: req and res.

const ipWhitelist = require('ip-whitelist');

let whitelist = [];

app.use(ipWhitelist(ip => {
    return whitelist.indexOf(ip) !== -1;
}, function (req, res) { // Custom handling of blocked IPs
  res.statusCode = 500;
  res.end('You shall not pass!');
}));
app.post('/api/whitelist/:ip', (req, res) => {
    whitelist.push(req.params.ip);
    res.end('Added IP to whitelist');
});
app.get('/api/whitelist', (req, res) => {
    res.json(whitelist);
});
1.2.2

5 years ago

1.2.1

5 years ago

1.2.0

6 years ago

1.1.1

7 years ago

1.1.0

7 years ago

1.0.0

7 years ago