add redis ip limiting
This commit is contained in:
parent
5fe11eaf0b
commit
8c5d7ce3f8
39
app.js
39
app.js
@ -1,6 +1,7 @@
|
||||
var express = require('express')
|
||||
, app = express.createServer()
|
||||
, nodemailer = require('nodemailer')
|
||||
, redis = require('redis')
|
||||
|
||||
// Express config
|
||||
app.set('views', __dirname + '/views');
|
||||
@ -27,16 +28,38 @@ app.get('/', function(req, res) {
|
||||
});
|
||||
|
||||
app.post('/text', function(req, res) {
|
||||
if (true) {
|
||||
sendText(req.body.number, req.body.msg, function(err) {
|
||||
if (err)
|
||||
res.send({success:false});
|
||||
else
|
||||
res.send({success:true});
|
||||
});
|
||||
}
|
||||
var keystr = req.connection.remoteAddress + '_' + dateStr();
|
||||
|
||||
var rclient = redis.createClient();
|
||||
rclient.incr(keystr, function(err, num) {
|
||||
rclient.quit();
|
||||
|
||||
if (err) {
|
||||
res.send({success:false,msg:'Could not validate IP quota.'});
|
||||
return;
|
||||
}
|
||||
if (num < 51) {
|
||||
sendText(req.body.number, req.body.msg, function(err) {
|
||||
if (err)
|
||||
res.send({success:false,msg:'Communication with SMS gateway failed.'});
|
||||
else
|
||||
res.send({success:true});
|
||||
});
|
||||
}
|
||||
else {
|
||||
res.send({success:false,msg:'Exceeded quota.'});
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
function dateStr() {
|
||||
var today = new Date();
|
||||
var dd = today.getDate();
|
||||
var mm = today.getMonth()+1;
|
||||
var yyyy = today.getFullYear();
|
||||
return mm + '/' + dd + '/' + yyyy;
|
||||
}
|
||||
|
||||
function sendText(phone, msg, cb) {
|
||||
var transport = nodemailer.createTransport("SES", {
|
||||
AWSAccessKeyID: config.aws.access,
|
||||
|
@ -4,6 +4,7 @@
|
||||
"express": "2.5.2"
|
||||
, "jade": "0.14.2"
|
||||
, "nodemailer": "latest"
|
||||
, "redis": "latest"
|
||||
}
|
||||
, "devDependencies": {
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user