Nodejs implementation of blacklist middleware design _node.js

Source: Internet
Author: User

Blacklist schema:

Copy Code code as follows:

/**
* Created by Ycxj-wanglihui on 2014/5/28.
*/
' Use strict ';

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

1. Temporary shielding 2. Permanent Shielding
var degree = {temp:1, forever:2};

/**
* Blacklist
* @type {Schema}
*
* @param IP {String} blacklist ip
* @param createat {Date} creation time
* @param expiretime {Date} If it is a temporary mask, the shielding expiration time
* @param forbiddendegree {Number} Shield level 1. Temporary shielding 2. Permanent Shielding
* @param reason {String} shielding Reason
*/
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: ' Frequent requests '
}
});

Mongoose.model (' blacklist ', blacklist);

IP and commit record schema:

Copy Code code 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 responses and IP of participating questionnaires
* @type {Schema}
*
* @param answerid {ObjectId} reply ID
* @param createat {Date} creation time
* @param IP {String} who participates in the reply
*/
var ipanswerlog = new Schema ({
Answerid: {
Type:objectid
},
Createat: {
Type:date,
Default:Date.now
},
ip:{
Type:string,
Index:true
}
});

Mongoose.model (' Ipanswerlog ', ipanswerlog);

Related proxy code:

Copy Code code as follows:

/**
* Created by Ycxj-wanglihui on 2014/5/28.
*/
' Use strict ';

var ipanswerlog = require ('.. /models '). Ipanswerlog;

/**
* New and saved
* @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);
}
}

/**
* Back to plural in 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 in 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 a certain period of time back to the plural
* @param begintime {number} start time stamp
* @param endtime {Number} end time if NULL, use current time timestamp
* @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:

Copy Code code as follows:

/**
* Created by Ycxj-wanglihui on 2014/5/28.
*/
' Use strict ';
var blacklist = require ('.. /models '). blacklist;

/**
* New and saved
* @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);
}
}

/**
* Disable 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 Day
* @param IP {String}
* @param callback
*/
var Newandsaveonedaytempforbidden = function (IP, callback) {
var expiretime = Date.now () + 1000*60*60*24;
Newandsavetempforbidden (IP, Expiretime, callback);
}

/**
* New 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 a permanent blacklist
* @param IP
* @param callback
*/
var Newandsaveforeverforbidden = function (IP, callback) {
var blacklist = new blacklist ({Ip:ip, forbiddendegree:2});
Newandsave (blacklist, callback);
}

/**
* To determine whether in the blacklist
* @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);
}
})
}

/**
* Get blacklist entries via IP
* @param IP
* @param callback
*/
var Getblacklistbyip = function (IP, callback) {
Blacklist.findone ({Ip:ip}, callback);
}

/**
* Delete blacklist based on IP
* @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:

Copy Code code 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 if you need to move IP to the blacklist
* @param req
* @param Res
* @param Next
*/
var isneedmovetoblacklist = function (req, res, next) {
var ip = req.ip;
To determine if it's in the blacklist.
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, retry after 1 hours! ');
}
});
}else if (Hourcount > 100) {
Blacklistproxy.newandsaveonedaytempforbidden (IP, function (Err, blacklist) {
if (err) {
Return next (ERR);
}else{
Return Res.send (' Submit too frequently, retry after 1 days! ');
}
})
}else if (Daycount > 1000) {
Blacklistproxy.newandsaveonedaytempforbidden (IP, function (Err, blacklist) {
if (err) {
Return next (ERR);
}else{
Return Res.send (' Submit too frequently, retry after 1 days! ');
}
})
}else{
return next ();
}
})

Ipanswerlogproxy.countoneminuteanswer (Ip,ep.done (' Minutecount '));
Ipanswerlogproxy.countonehouranswer (IP, ep.done (' Hourcount '));
Ipanswerlogproxy.countonedayanswer (IP, ep.done (' Daycount '));
});
}

/**
* Middleware requires IP not in 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 is forbidden to submit, please contact lihui.wang@tulingdao.com ' If you have any questions);
}else{
Next ();
}
})
}

Exports.isneedmovetoblacklist = isneedmovetoblacklist;
Exports.requirenotinblacklist = requirenotinblacklist;

To use in routing:

Copy Code code as follows:

Web page Submission Interface
Router.post ('/create ', middleware.isneedmovetoblacklist, paperanswers.create);

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.