JSPM

  • ESM via JSPM
  • ES Module Entrypoint
  • Export Map
  • Keywords
  • License
  • Repository URL
  • TypeScript Types
  • README
  • Created
  • Published
  • Downloads 1898
  • Score
    100M100P100Q117520F
  • License MIT

An express.js middleware for access control.

Package Exports

  • express-ip-access-control

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (express-ip-access-control) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

Express IP Access Control

An express.js middleware for access control.

Installation

npm install ipaddr.js

Feature

  • Support Express.
  • Control who can access resources base on IP address.
  • Support IPv4, IPv6, CIDR format & IPv4 mapped IPv6 addresses (thanks to ipaddr.js).
  • Deny mode (Blacklist) & Allow mode (Whitelist), similar Apache Access Control.
  • Choose from connection address or real address. You may find it useful if you are behind proxy and needed to reject direct access.
  • Custom action on denied. (Redirect or show error message)
  • Custom log function.

Usage

If you are not familiar with Express and Express's middleware, you are recommended to see these first.

var accessControl = require('express-ip-access-control');

Create middleware by calling accessControl(options) or directly load it into your app by app.use(accessControl(options)).

Options

var options = {
    mode: 'deny',
    denys: [],
    allows: [],
    forceConnectionAddress: false,
    log: function(clientIp, access) {
        console.log(clientIp + (access ? ' accessed.' : ' denied.'));
    },

    statusCode: 401,
    redirectTo: '',
    message: 'Unauthorized'
};

mode (default: 'deny')

'deny' mode (Blacklist)

Allow by default, only deny IP in the blacklist (denys) and not excluded by the whitelist (allows).

'allow' mode (Whilelist)

Deny by default, only allow IP in the whitelist (allows) and not excluded by the blacklist (denys).

denys (default: [])

The blacklist. Works differently in different mode.

allows (default: [])

The whitelist. Works differently in different mode.

forceConnectionAddress (default: false)

If set to true, the connection address (req.connection.remoteAddress) will be used even express.set('trust proxy', []) is set. So that you can reject direct access if you are behind proxy and needed to do so.

log (default: Simple log function)

Pass a log function or false to disable log. The function should have signature like this Function(String clientIp, Boolean access).

statusCode (default: 401)

The HTTP status code sent when denied. Set to 301 or 302 means redirect to redirectTo.

redirectTo (default: '')

The URL to redirect when denied and statusCode is set to redirect.

message (default: 'Unauthorized')

The message sent when denied and statusCode is not set to redirect.

Repository

You may find the source code at GitHub. Please feel free to report bugs and contribute your changes.

License

MIT