Validates valid IPs (IPv4 and IPv6) using micromatch - glob patterns, RegExp, string or array of globs. If match returns the IP, otherwise null.
You might also be interested in is-match-ip, to-object-path, to-file-path or koa-ip-filter.
Install
npm i ip-filter --save
Usage
For more use-cases see the tests
const ipFilter = require('ip-filter')
Filter ip
against glob patterns
, using micromatch under the hood, so options
are passed to it.
Params
ip
{String}: Accepts only valid IPs by defaultpatterns
{String|Array|RegExp|Function|Object}: Basically everything that is-match can accept.options
{Object}: Pass strict: false
if want to validate non-ip values, also passed to is-match.returns
{String|null}: If not match returns null
, otherwise the passed ip
.
Example
var ipFilter = require('ip-filter')
console.log(ipFilter('123.77.34.89', '123.??.34.8*'))
console.log(ipFilter('123.222.34.88', '123.??.34.8*'))
console.log(ipFilter('123.222.33.1', ['123.*.34.*', '*.222.33.*']))
console.log(ipFilter('123.222.34.88', ['123.*.34.*', '!123.222.**']))
console.log(ipFilter('123.222.34.88', ['123.*.34.*', '!123.222.*']))
var res = ipFilter('x-koaip', ['*-koaip', '!foo-koa*'], { strict: false })
console.log(res)
var res = ipFilter('x-koa.foo', ['*-koa.*', '!foo-koa.*'], { strict: false })
console.log(res)
Related
Contributing
Pull requests and stars are always welcome. For bugs and feature requests, please create an issue.
But before doing anything, please read the CONTRIBUTING.md guidelines.