Socket
Socket
Sign inDemoInstall

koa-ip

Package Overview
Dependencies
4
Maintainers
1
Versions
8
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    koa-ip

Ip filter middleware for koa, support `whitelist` and `blacklist`.


Version published
Weekly downloads
124K
decreased by-8.66%
Maintainers
1
Install size
73.2 kB
Created
Weekly downloads
 

Changelog

Source

2.1.3/2023-03-02

  • update deps

Readme

Source

koa-ip

KoaJs Slack

koa-ip is a ip filter middleware for koa, support whitelist and blacklist.

Install

$ npm i koa-ip --save

Usage

ip(String|RegExp)
ip(Array{String|RegExp})
ip({
  whitelist: Array{String|RegExp},
  blacklist: Array{String|RegExp},
  handler: async (ctx, next) => {} // handle blacklist ip
})

Examples

const Koa = require('koa')
const ip = require('koa-ip')

const app = new Koa()

app.use(ip('192.168.0.*')) // whitelist
// app.use(ip(['192.168.0.*', '8.8.8.[0-3]'])) // whitelist
// app.use(ip({
//   whitelist: ['192.168.0.*', '8.8.8.[0-3]'],
//   blacklist: ['144.144.*']
// }))

app.listen(3000)
blacklist handler
const app = new Koa()
app.use((ctx, next) => {
  ctx.request.ip = '127.0.0.1'
  return next()
})
app.use(ip({
  blacklist: ['127.0.0.*'],
  handler: async (ctx, next) => {
    ctx.status = 403
  }
}))

app.use((ctx, next) => {
  ctx.status = 200
})

app.listen(3000)

NB: If missing blacklist handler, default ctx.status = 403.

More examples see test.

Test

$ npm test (coverage 100%)

License

MIT

Keywords

FAQs

Last updated on 02 Mar 2023

Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc