thinkjs/lib/Lib/Behavior/DenyIpBehavior.js
2015-04-27 14:31:29 +08:00

43 lines
1.1 KiB
JavaScript
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

/**
* 阻止ip来源访问
* @return {[type]} [description]
*/
module.exports = Behavior(function(){
'use strict';
return {
options: {
deny_ip: [] //阻止的ip列表
},
run: function(){
var deny_ip = this.options.deny_ip;
if (isArray(deny_ip) && deny_ip.length === 0) {
return true;
}
var clientIps = this.http.ip().split('.');
var promise = getPromise(deny_ip);
if (isFunction(deny_ip)) {
promise = getPromise(deny_ip());
}
var self = this;
return promise.then(function(deny_ip){
if (!deny_ip || deny_ip.length === 0) {
return true;
}
var flag = deny_ip.some(function(item){
return item.split('.').every(function(num, i){
if (num === '*' || num === clientIps[i]) {
return true;
}
});
});
//如果在阻止的ip在列表里则返回一个pendding promise让后面的代码不执行
if (flag) {
self.http.res.statusCode = 403;
self.http.end();
return getDefer().promise;
}
return true;
})
}
};
});