npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

express-ipfilter

v1.3.2

Published

A light-weight IP address based filtering system

Downloads

110,724

Readme

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

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

Installation

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

npm install express-ipfilter

Usage with Express

Denying certain IP addresses, while allowing all other IPs:

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

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

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

Allowing certain IP addresses, while denying all other IPs:

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

// Allow 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

Using wildcard ip ranges and nginx forwarding:

  let allowlist_ips = ['10.1.*.*', '123.??.34.8*'] // matches '10.1.76.32' and '123.77.34.89'

  let clientIp = function(req, res) {
    return req.headers['x-forwarded-for'] ? (req.headers['x-forwarded-for']).split(',')[0] : ""
  }
  
  app.use(
    ipFilter({
      detectIp: clientIp,
      forbidden: 'You are not authorized to access this page.',
      filter: allowlist_ips,
    })
  )

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(ips, { 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