thinkjs/lib/Extend/Behavior/DenyIpBehavior.class.js
2014-01-15 23:08:33 +08:00

32 lines
753 B
JavaScript
Raw 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(){
return {
options: {
deny_ip: [] //阻止的ip列表
},
run: function(){
if (this.options.deny_ip.length === 0) {
return true;
};
var clientIps = this.http.ip().split(".");
var flag = this.options.deny_ip.some(function(item){
return item.split(".").every(function(num, i){
if (num === "*" || num === clientIps[i]) {
return true;
};
})
});
//如果在阻止的ip在列表里则返回一个不会resolve的promise
//从而让后面的代码不执行
if (flag) {
this.http.res.statusCode = 403;
this.http.res.end();
return getDefer().promise;
};
return true;
}
}
});