Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/nswbmw/koa-ip
Ip filter middleware for koa, support whitelist and blacklist.
https://github.com/nswbmw/koa-ip
blacklist ip koa whitelist
Last synced: about 2 months ago
JSON representation
Ip filter middleware for koa, support whitelist and blacklist.
- Host: GitHub
- URL: https://github.com/nswbmw/koa-ip
- Owner: nswbmw
- Created: 2014-08-07T10:35:46.000Z (over 10 years ago)
- Default Branch: master
- Last Pushed: 2023-03-02T07:42:09.000Z (almost 2 years ago)
- Last Synced: 2024-08-04T00:05:01.166Z (5 months ago)
- Topics: blacklist, ip, koa, whitelist
- Language: JavaScript
- Size: 26.4 KB
- Stars: 37
- Watchers: 10
- Forks: 9
- Open Issues: 0
-
Metadata Files:
- Readme: readme.md
- Changelog: History.md
Awesome Lists containing this project
- awesome-koa - koa-ip - 用于Koa的Ip过滤中间件。支持白名单和黑名单。 ![](https://img.shields.io/github/stars/MangroveTech/koa-ip.svg?style=social&label=Star) ![](https://img.shields.io/npm/dm/koa-ip.svg?style=flat-square) (仓库 / 中间件)
README
## koa-ip
> koa-ip is a ip filter middleware for koa, support `whitelist` and `blacklist`.
### Install
```sh
$ npm i koa-ip --save
```### Usage
```js
ip(String|RegExp)
ip(Array{String|RegExp})
ip({
whitelist: Array{String|RegExp},
blacklist: Array{String|RegExp},
handler: async (ctx, next) => {} // handle blacklist ip
})
```### Examples
```js
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
```js
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](./__tests__/).
### Test
```sh
$ npm test (coverage 100%)
```### License
MIT