I recently wrote a project that involved the function of preventing irrigation. So I designed the blacklist middleware and shared it with you. I also hope you can make good suggestions. The Blacklist Schema:
The Code is as follows:
/**
* Created by YCXJ-wanglihui on 2014/5/28.
*/
'Use strict ';
Var mongoose = require ('mongoose ');
Var Schema = mongoose. Schema;
// 1. Transient shielding 2. Permanent shielding
Var degree = {TEMP: 1, FOREVER: 2 };
/**
* Blacklist
* @ Type {Schema}
*
* @ Param ip {String} blacklist Ip Address
* @ Param createAt {Date} Creation Time
* @ Param expireTime {Date} if it is blocked for a short time, the expiration time is blocked.
* @ Param forbiddenDegree {Number} blocking Level 1. temporary blocking 2. Permanent Blocking
* @ Param reason {String} reason for blocking
*/
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: 'request frequency'
}
});
Mongoose. model ('blacklist', BlackList );
IP address and submission record Schema:
The Code is as follows:
/**
* Created by YCXJ-wanglihui on 2014/5/28.
*/
'Use strict ';
Var mongoose = require ('mongoose ');
Var Schema = mongoose. Schema;
Var ObjectId = Schema. ObjectId;
/**
* Record the reply and Ip address of the questionnaire
* @ Type {Schema}
*
* @ Param answerId {ObjectId} reply Id
* @ Param createAt {Date} Creation Time
* @ Param ip {String} Ip address of the person involved in the reply
*/
Var IpAnswerLog = new Schema ({
AnswerId :{
Type: ObjectId
},
CreateAt :{
Type: Date,
Default: Date. now
},
Ip :{
Type: String,
Index: true
}
});
Mongoose. model ('ipancerlog', ipancerlog );
Related Proxy code:
The Code is as follows:
/**
* Created by YCXJ-wanglihui on 2014/5/28.
*/
'Use strict ';
Var IpAnswerLog = require ('../models'). IpAnswerLog;
/**
* Create and save
* @ Param ipAnswerLog {Schema or dict}
* @ Param callback
*/
Var newAndSave = function (ipAnswerLog, callback ){
If (ipAnswerLog instanceof IpAnswerLog ){
IpAnswerLog. save (callback );
} Else {
Var m = new IpAnswerLog (ipAnswerLog );
M. save (callback );
}
}
/**
* Number of replies within one minute
* @ Param ip
* @ Param callback
*/
Var countOneMinuteAnswer = function (ip, callback ){
Var endTime = Date. now ();
Var beginTime = endTime-1000*60*1;
CountIpAnswerByTime (beginTime, endTime, ip, callback );
}
/**
* Reply to the number within one hour
* @ Param ip
* @ Param callback
*/
Var countOneHourAnswer = function (ip, callback ){
Var endTime = Date. now ();
Var beginTime = endTime-1000*60*60*1;
CountIpAnswerByTime (beginTime, endTime, ip, callback );
}
/**
* Reply within one day
* @ Param ip
* @ Param callback
*/
Var countOneDayAnswer = function (ip, callback ){
Var endTime = Date. now ();
Var beginTime = endTime-1000*60*60*24;
CountIpAnswerByTime (beginTime, endTime, ip, callback );
}
/**
* Calculate the number of replies within a certain period of time.
* @ Param beginTime {Number} start Timestamp
* @ Param endTime {Number} if the end time is null, the current timestamp is used.
* @ Param ip {String} ip Address
* @ Param 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;
Blacklist Proxy:
The Code is as follows:
/**
* Created by YCXJ-wanglihui on 2014/5/28.
*/
'Use strict ';
Var BlackList = require ('../models'). BlackList;
/**
* Create and save
* @ Param backList {BlackList} or {dict} BlackList data
* @ Param callback
*/
Var newAndSave = function (backList, callback ){
If (backList instanceof BlackList ){
BackList. save (callback );
} Else {
Var m = new BlackList (backList );
M. save (callback );
}
}
/**
* Disabling Ip Access for one hour
* @ Param ip {String}
* @ Param callback
*/
Var newAndSaveOneHourTempForbidden = function (ip, callback ){
Var expireTime = Date. now () + 1000*60*60;
NewAndSaveTempForbidden (ip, expireTime, callback );
}
/**
* Disable one day
* @ Param ip {String}
* @ Param callback
*/
Var newAndSaveOneDayTempForbidden = function (ip, callback ){
Var expireTime = Date. now () + 1000*60*60*24;
NewAndSaveTempForbidden (ip, expireTime, callback );
}
/**
* Create a temporary blacklist
* @ Param ip {String}
* @ Param expireTime {Number} expiration time
* @ Param callback
*/
Var newAndSaveTempForbidden = function (ip, expireTime, callback ){
Var blackList = new BlackList ({ip: ip, expireTime: expireTime, forbiddenDegree: 1 });
NewAndSave (blackList, callback );
}
/**
* Create and save the permanent blacklist
* @ Param ip
* @ Param callback
*/
Var newAndSaveForeverForbidden = function (ip, callback ){
Var blackList = new BlackList ({ip: ip, forbiddenDegree: 2 });
NewAndSave (blackList, callback );
}
/**
* Checks whether the blacklist is used.
* @ Param ip {String} ip Address
* @ Param 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 );
}
})
}
/**
* Obtain blacklist entries through Ip addresses
* @ Param ip
* @ Param callback
*/
Var getBlackListByIp = function (ip, callback ){
BlackList. findOne ({ip: ip}, callback );
}
/**
* Delete the blacklist by Ip Address
* @ Param ip
* @ Param 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;
Middleware details:
The Code is as follows:
/**
* Created by YCXJ-wanglihui on 2014/5/28.
*/
'Use strict ';
Var BlackListProxy = require ('.../../proxy'). BlackListPorxy;
Var IpAnswerLogProxy = require ('.../../proxy'). IpAnswerLogProxy;
Var EventProxy = require ('eventproxy ');
/**
* Determine whether to move the Ip address to the blacklist.
* @ Param req
* @ Param res
* @ Param next
*/
Var isNeedMoveToBlackList = function (req, res, next ){
Var ip = req. ip;
// Checks whether the blacklist is used.
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 ('submit too frequently. Try again in 1 hour! ');
}
});
} Else if (hourCount> 100 ){
BlackListProxy. newAndSaveOneDayTempForbidden (ip, function (err, blackList ){
If (err ){
Return next (err );
} Else {
Return res. send ('submit too frequently, retry in 1 day! ');
}
})
} Else if (dayCount & gt; 1000 ){
BlackListProxy. newAndSaveOneDayTempForbidden (ip, function (err, blackList ){
If (err ){
Return next (err );
} Else {
Return res. send ('submit too frequently, retry in 1 day! ');
}
})
} Else {
Return next ();
}
})
IpAnswerLogProxy. countOneMinuteAnswer (ip, ep. done ('minutecount '));
IpAnswerLogProxy. countOneHourAnswer (ip, ep. done ('urcount '));
IpAnswerLogProxy. countOneDayAnswer (ip, ep. done ('daycount '));
});
}
/**
* The middleware requires that the Ip address is not in the blacklist.
* @ Param req
* @ Param res
* @ Param next
*/
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 ('your Ip cannot be submitted, please contact lihui.wang@tulingdao.com if you have any doubts ');
} Else {
Next ();
}
})
}
Exports. isNeedMoveToBlackList = isNeedMoveToBlackList;
Exports. requireNotInBlackList = requireNotInBlackList;
Use in routing:
The Code is as follows:
// Webpage submission Interface
Router. post ('/create', middleware. isNeedMoveToBlackList, paperAnswers. create );