add redis ip limiting

This commit is contained in:
typpo 2012-04-06 17:22:23 -04:00
parent 5fe11eaf0b
commit 8c5d7ce3f8
2 changed files with 32 additions and 8 deletions

27
app.js
View File

@ -1,6 +1,7 @@
var express = require('express') var express = require('express')
, app = express.createServer() , app = express.createServer()
, nodemailer = require('nodemailer') , nodemailer = require('nodemailer')
, redis = require('redis')
// Express config // Express config
app.set('views', __dirname + '/views'); app.set('views', __dirname + '/views');
@ -27,16 +28,38 @@ app.get('/', function(req, res) {
}); });
app.post('/text', function(req, res) { app.post('/text', function(req, res) {
if (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) { sendText(req.body.number, req.body.msg, function(err) {
if (err) if (err)
res.send({success:false}); res.send({success:false,msg:'Communication with SMS gateway failed.'});
else else
res.send({success:true}); 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) { function sendText(phone, msg, cb) {
var transport = nodemailer.createTransport("SES", { var transport = nodemailer.createTransport("SES", {
AWSAccessKeyID: config.aws.access, AWSAccessKeyID: config.aws.access,

View File

@ -4,6 +4,7 @@
"express": "2.5.2" "express": "2.5.2"
, "jade": "0.14.2" , "jade": "0.14.2"
, "nodemailer": "latest" , "nodemailer": "latest"
, "redis": "latest"
} }
, "devDependencies": { , "devDependencies": {
} }