文章详情

短信预约-IT技能 免费直播动态提醒

请输入下面的图形验证码

提交验证

短信预约提醒成功

nodejs实现黑名单中间件设计

2022-06-04 17:24

关注

黑名单Schema:



'use strict';

var mongoose = require('mongoose');
var Schema = mongoose.Schema;

//1.短暂屏蔽 2.永久屏蔽
var degree = {TEMP:1, FOREVER:2};


var BlackList = new Schema({
ip:{
type: String,
index:true
},
createAt:{
type: Date,
default: Date.now
},
expireTime:{
type: Date
},
forbiddenDegree:{
type: Number,
default:degree.TEMP
},
reason:{
type: String,
default: '请求次数频繁'
}
});

mongoose.model('BlackList', BlackList);

IP与提交记录Schema:


'use strict';

var mongoose = require('mongoose');
var Schema = mongoose.Schema;
var ObjectId = Schema.ObjectId;


var IpAnswerLog = new Schema({
answerId: {
type: ObjectId
},
createAt: {
type: Date,
default:Date.now
},
ip:{
type: String,
index:true
}
});

mongoose.model('IpAnswerLog', IpAnswerLog);

相关Proxy代码:



'use strict';

var IpAnswerLog = require('../models').IpAnswerLog;


var newAndSave = function(ipAnswerLog, callback){
if(ipAnswerLog instanceof IpAnswerLog){
ipAnswerLog.save(callback);
}else{
var m = new IpAnswerLog(ipAnswerLog);
m.save(callback);
}
}


var countOneMinuteAnswer = function(ip, callback){
var endTime = Date.now();
var beginTime = endTime - 1000*60*1;
countIpAnswerByTime(beginTime, endTime, ip, callback);
}


var countOneHourAnswer = function(ip, callback){
var endTime = Date.now();
var beginTime = endTime - 1000*60*60*1;
countIpAnswerByTime(beginTime, endTime, ip, callback);
}


var countOneDayAnswer = function(ip, callback){
var endTime = Date.now();
var beginTime = endTime - 1000*60*60*24;
countIpAnswerByTime(beginTime, endTime, ip, callback);
}


var countIpAnswerByTime = function(beginTime, endTime, ip, callback){
if(!endTime){
endTime = Date.now();
}
IpAnswerLog.count({ip:ip, '$and':{$lt:beginTime, $gt:endTime}}, callback);
}

exports.countIpAnswerByTime =countIpAnswerByTime;
exports.countOneDayAnswer = countOneDayAnswer;
exports.countOneHourAnswer = countOneHourAnswer;
exports.countOneMinuteAnswer = countOneMinuteAnswer;
exports.newAndSave = newAndSave;

黑名单Proxy:



'use strict';
var BlackList = require('../models').BlackList;


var newAndSave = function(backList, callback){
if(backList instanceof BlackList){
backList.save(callback);
}else{
var m = new BlackList(backList);
m.save(callback);
}
}


var newAndSaveOneHourTempForbidden = function(ip, callback){
var expireTime = Date.now() + 1000*60*60;
newAndSaveTempForbidden(ip,expireTime, callback);
}


var newAndSaveOneDayTempForbidden = function(ip, callback){
var expireTime = Date.now() + 1000*60*60*24;
newAndSaveTempForbidden(ip, expireTime, callback);
}


var newAndSaveTempForbidden = function(ip, expireTime,callback){
var blackList = new BlackList({ip:ip, expireTime:expireTime, forbiddenDegree:1});
newAndSave(blackList, callback);
}


var newAndSaveForeverForbidden = function(ip, callback){
var blackList = new BlackList({ip:ip, forbiddenDegree:2});
newAndSave(blackList, callback);
}


var isInBlackList = function(ip, callback){
getBlackListByIp(ip, function(err, blackList){
if(err){
callback(err);
}else if(blackList){
var currentDate = Date.now();
if(blackList.forbiddenDegree ===1 && blackList.expireTime> currentDate){
removeBlackListByIp(ip, function(err){
if(err){
callback(err);
}else{
callback(null, false);
}
})
}else{
callback(null, true);
}
}else{
callback(null, false);
}
})
}


var getBlackListByIp = function(ip, callback){
BlackList.findOne({ip:ip}, callback);
}


var removeBlackListByIp = function(ip, callback){
getBlackListByIp(ip, function(err, blackList){
if(err){
callback(err);
}else if(blackList){
blackList.remove(callback);
}else{
callback(null,null);
}
})
}

exports.newAndSave = newAndSave;
exports.isInBlackList = isInBlackList;
exports.getBlackListByIp = getBlackListByIp;
exports.removeBlackListByIp = removeBlackListByIp;
exports.newAndSaveOneHourTempForbidden = newAndSaveOneHourTempForbidden;
exports.newAndSaveOneDayTempForbidden = newAndSaveOneDayTempForbidden;
exports.newAndSaveForeverForbidden = newAndSaveForeverForbidden;
exports.newAndSaveTempForbidden = newAndSaveTempForbidden;

中间件详情:



'use strict';

var BlackListProxy = require('../../proxy').BlackListPorxy;
var IpAnswerLogProxy = require('../../proxy').IpAnswerLogProxy;
var EventProxy = require('eventproxy');


var isNeedMoveToBlackList = function(req, res, next){
var ip = req.ip;
//判断是否在黑名单中
requireNotInBlackList(req, res, function(){
var ep = new EventProxy();
ep.fail(next);

ep.all('minuteCount', 'hourCount', 'dayCount', function(minuteCount, hourCount, dayCount){
if(minuteCount > 10){
BlackListProxy.newAndSaveOneHourTempForbidden(ip, function(err, blackList){
if(err){
return next(err);
}else{
return res.send('提交过于频繁,1小时后重试!');
}
});
}else if(hourCount > 100){
BlackListProxy.newAndSaveOneDayTempForbidden(ip, function(err, blackList){
if(err){
return next(err);
}else{
return res.send('提交过于频繁,1天后重试!');
}
})
}else if(dayCount > 1000){
BlackListProxy.newAndSaveOneDayTempForbidden(ip, function(err, blackList){
if(err){
return next(err);
}else{
return res.send('提交过于频繁,1天后重试!');
}
})
}else{
return next();
}
})

IpAnswerLogProxy.countOneMinuteAnswer(ip,ep.done('minuteCount'));
IpAnswerLogProxy.countOneHourAnswer(ip, ep.done('hourCount'));
IpAnswerLogProxy.countOneDayAnswer(ip, ep.done('dayCount'));
});
}


var requireNotInBlackList = function(req, res, next){
var ip = req.ip;
BlackListProxy.isInBlackList(ip, function(err, result){
if(err){
next(err);
}else if(result){
return res.send('您的Ip禁止提交,如有疑问请联系lihui.wang@tulingdao.com');
}else{
next();
}
})
}

exports.isNeedMoveToBlackList = isNeedMoveToBlackList;
exports.requireNotInBlackList = requireNotInBlackList;

在路由中使用:


//网页提交接口
router.post('/create', middleware.isNeedMoveToBlackList, paperAnswers.create);

阅读原文内容投诉

免责声明:

① 本站未注明“稿件来源”的信息均来自网络整理。其文字、图片和音视频稿件的所属权归原作者所有。本站收集整理出于非商业性的教育和科研之目的,并不意味着本站赞同其观点或证实其内容的真实性。仅作为临时的测试数据,供内部测试之用。本站并未授权任何人以任何方式主动获取本站任何信息。

② 本站未注明“稿件来源”的临时测试数据将在测试完成后最终做删除处理。有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341

软考中级精品资料免费领

  • 历年真题答案解析
  • 备考技巧名师总结
  • 高频考点精准押题
  • 2024年上半年信息系统项目管理师第二批次真题及答案解析(完整版)

    难度     813人已做
    查看
  • 【考后总结】2024年5月26日信息系统项目管理师第2批次考情分析

    难度     354人已做
    查看
  • 【考后总结】2024年5月25日信息系统项目管理师第1批次考情分析

    难度     318人已做
    查看
  • 2024年上半年软考高项第一、二批次真题考点汇总(完整版)

    难度     435人已做
    查看
  • 2024年上半年系统架构设计师考试综合知识真题

    难度     224人已做
    查看

相关文章

发现更多好内容

猜你喜欢

AI推送时光机
位置:首页-资讯-前端开发
咦!没有更多了?去看看其它编程学习网 内容吧
首页课程
资料下载
问答资讯