GithubHelp home page GithubHelp logo

gnorbsl / express-ipfilter Goto Github PK

View Code? Open in Web Editor NEW

This project forked from jetersen/express-ipfilter

0.0 0.0 0.0 549 KB

A light-weight IP address based connection filtering system

License: MIT License

JavaScript 98.91% CSS 0.29% HTML 0.79%

express-ipfilter's Introduction

express-ipfilter: A light-weight IP address based filtering system

This package provides easy IP based access control. This can be achieved either by blacklisting certain IPs and whitelisting all others, or whitelisting certain IPs and blacklisting all others.

Installation

Recommended installation is with npm. To add node-ipfilter to your project, do:

npm install express-ipfilter

Usage with Express

Blacklisting certain IP addresses, while allowing all other IPs:

// Init dependencies
const express = require('express')
const ipfilter = require('express-ipfilter').IpFilter

// Blacklist the following IPs
const ips = ['127.0.0.1']

// Create the server
app.use(ipfilter(ips))
app.listen(3000)

Whitelisting certain IP addresses, while denying all other IPs:

// Init dependencies
// Init dependencies
const express = require('express')
const ipfilter = require('express-ipfilter').IpFilter

// Whitelist the following IPs
const ips = ['127.0.0.1']

// Create the server
app.use(ipfilter(ips, { mode: 'allow' }))

module.exports = app

Using CIDR subnet masks for ranges:

const ips = ['127.0.0.1/24']

// Create the server
app.use(ipfilter(ips, { mode: 'allow' }))

module.exports = app

Using IP ranges:

const ips = [['127.0.0.1', '127.0.0.10']]

// Create the server
app.use(ipfilter(ips, { mode: 'allow' }))

module.exports = app

Using a function to get Ips:

const ips = function() {
  return ['127.0.0.1']
}

// Create the server
app.use(ipfilter(ips, { mode: 'allow' }))

module.exports = app

Error Handling

When an IP is denied, an IpDeniedError will be thrown by the middleware. If you do not handle the error, it will cause your app to crash due to an unhandled exception. Here is an example of how to handle the error, which can also be found in the example app:

if (app.get('env') === 'development') {
  app.use((err, req, res, _next) => {
    console.log('Error handler', err)
    if (err instanceof IpDeniedError) {
      res.status(401)
    } else {
      res.status(err.status || 500)
    }

    res.render('error', {
      message: 'You shall not pass',
      error: err
    })
  })
}

You will need to require the IpDeniedError type in order to handle it.

Options

Property Description Type Default
mode whether to deny or allow to the IPs provided string deny
log console log actions boolean true
logLevel level of logging (all,deny,allow) string all
excluding routes that should be excluded from ip filtering array []
detectIp define a custom function that takes an Express request object and returns an IP address to test against function built-in detection
trustProxy This setting is implemented using the proxy-addr package. Check the documentation for the trust parameter. boolean, array, string, number, function false

A note on detectIp

If you need to parse an IP address in a way that is not supported by default, you can write your own parser and pass that to ipfilter.

const customDetection = req => {
  var ipAddress

  ipAddress = req.connection.remoteAddress.replace(/\//g, '.')

  return ipAddress
}

ipfilter(ids, { detectIp: customDetection })

Contributing

See the CONTRIBUTING.MD document for more information on contributing.

Running Tests

Run tests by using npm test

Changelog

Moved to GitHub Releases

Credits

BaM Interactive - code.bamideas.com

express-ipfilter's People

Contributors

ryanbillingsley avatar longstone avatar jetersen avatar armiiller avatar renovate-bot avatar pdefreitas avatar gswalden avatar nsbingham avatar jfix avatar lafama avatar thii avatar eggman64 avatar renovate[bot] avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.