Nomadic People Migration

    ip-whitelist

    1.2.2 • Public • Published

    ip-whitelist

    Build Status David Coveralls npm npm GitHub issues

    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);
    });

    Install

    npm i ip-whitelist

    DownloadsWeekly Downloads

    53

    Version

    1.2.2

    License

    MIT

    Unpacked Size

    6.91 kB

    Total Files

    5

    Last publish

    Collaborators

    • mbrandau