Skip to content

Commit

Permalink
refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
tunnckoCore committed May 24, 2015
1 parent 06fa089 commit 18f59ad
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 33 deletions.
30 changes: 9 additions & 21 deletions index.js
Expand Up @@ -7,17 +7,16 @@

'use strict'

var micromatch = require('is-match')
var statuses = require('statuses')
var ipFilter = require('ip-filter')

/**
* > Filtering incoming request with glob patterns
* array, regexp, string or matcher function
*
* @param {Object} `options`
* @option {Function} [options] `id` custom identifier, defaults to `this.ip`
* @option {Array|String|RegExp|Function} [options] `blacklist` blacklist filter
* @option {Array|String|RegExp|Function} [options] `whitelist` whitelist filter
* @option {Boolean} [options] `strict` to throw when not valid ip? default `true`
* @option {Array|String|RegExp|Function} [options] `filter` black/white list filter
* @option {String|Function} [options] `forbidden` message to display when 403 forbidden
* @return {GeneratorFunction}
* @api public
Expand All @@ -32,25 +31,14 @@ module.exports = function koaIpFilter (options) {
return yield * next
}

var blacklist = options.blacklist || options.blackList
var whitelist = options.whitelist || options.whiteList
var forbidden = options.forbidden || '403 ' + statuses[403]
var filter = options.filter || '*'
var strict = typeof options.strict === 'boolean' ? options.strict : true
var forbidden = options.forbidden || '403 Forbidden'

if (typeof forbidden === 'function') {
forbidden = forbidden.call(this, this)
}

var whiteMatch = whitelist ? micromatch(whitelist) : null
if (whiteMatch && !whiteMatch(id)) {
this.status = 403
this.body = forbidden
return
}

var blackMatch = blacklist ? micromatch(blacklist) : null
if (blackMatch && blackMatch(id)) {
var identifier = ipFilter(id, filter, !strict)
if (identifier === null) {
this.status = 403
this.body = forbidden
this.body = typeof forbidden === 'function' ? forbidden.call(this, this) : forbidden
return
}

Expand Down
3 changes: 1 addition & 2 deletions package.json
Expand Up @@ -10,8 +10,7 @@
"test": "standard && node test.js"
},
"dependencies": {
"is-match": "^0.2.0",
"statuses": "^1.2.1"
"ip-filter": "~1.0.0"
},
"devDependencies": {
"assertit": "^0.1.0",
Expand Down
20 changes: 10 additions & 10 deletions test.js
Expand Up @@ -57,7 +57,7 @@ test('koa-ip-filter:', function () {
test('should support custom message for 403 Forbidden', function (done) {
var app = middleware({
forbidden: '403, Get out of here!',
blacklist: ['1.2.3.4']
filter: ['!1.2.3.4']
})

request(app.callback())
Expand All @@ -71,19 +71,19 @@ test('koa-ip-filter:', function () {
forbidden: function (ctx) {
return 'Get out of here!'
},
blacklist: ['123.*.*.77']
filter: ['123.48.*.77', '!123.*.192.??']
})

request(app.callback())
.get('/')
.set('x-koaip', '123.48.92.77')
.set('x-koaip', '123.48.192.77')
.expect(403, 'Get out of here!')
.end(done)
})
test('should support blacklist option', function () {
test('expect `403 Forbidden` when array blacklist and match', function (done) {
var app = middleware({
blacklist: ['1.2.3.4']
filter: ['1.2.*.4', '!1.2.3.4']
})

request(app.callback())
Expand All @@ -92,10 +92,10 @@ test('koa-ip-filter:', function () {
.expect(403, '403 Forbidden')
.end(done)
})
test('expect `403 Forbidden` when blacklist function return truthy', function (done) {
test('expect `403 Forbidden` when blacklist function return falsey', function (done) {
var app = middleware({
blacklist: function (ip) {
return ip.indexOf('1.2.3') !== -1
filter: function (ip) {
return ip.indexOf('1.2.3') === -1
},
id: function () {
return this.request.header['x-koaip']
Expand All @@ -112,18 +112,18 @@ test('koa-ip-filter:', function () {
test('should support whitelist option', function () {
test('expect `200 OK`: ip match to whitelist', function (done) {
var app = middleware({
whitelist: ['1.2.3.4', '1.2.3.7']
filter: ['127.??.6*.12', '!1.2.*.4']
})

request(app.callback())
.get('/')
.set('x-koaip', '1.2.3.7')
.set('x-koaip', '127.43.65.12')
.expect(200, 'Hello World')
.end(done)
})
test('expect `403 Forbidden`: ip not match to whitelist (glob patterns)', function (done) {
var app = middleware({
whitelist: ['1.2.3.*']
filter: ['1.2.3.*']
})

request(app.callback())
Expand Down

0 comments on commit 18f59ad

Please sign in to comment.