Add a 'key' query parameter that will skip verification limits.

This commit is contained in:
Ian 2014-04-20 18:59:27 -07:00
parent 90ffab09bd
commit 22c4b4183c
2 changed files with 58 additions and 23 deletions

78
app.js
View File

@ -8,6 +8,14 @@ var express = require('express')
, Stream = require('stream') , Stream = require('stream')
, providers = require('./providers.js') , providers = require('./providers.js')
var access_keys;
try {
access_keys = require('./keys.json');
} catch (e) {
access_keys = {};
}
console.log('Loaded access keys:', access_keys);
var mpq = new mixpanel.Client('6e6e6b71ed5ada4504c52d915388d73d'); var mpq = new mixpanel.Client('6e6e6b71ed5ada4504c52d915388d73d');
var redis = require('redis-url').connect(); var redis = require('redis-url').connect();
@ -39,18 +47,18 @@ app.post('/text', function(req, res) {
res.send({success:false,message:'Invalid phone number.'}); res.send({success:false,message:'Invalid phone number.'});
return; return;
} }
textRequestHandler(req, res, number, 'us'); textRequestHandler(req, res, number, 'us', req.query.key);
}); });
app.post('/canada', function(req, res) { app.post('/canada', function(req, res) {
textRequestHandler(req, res, stripPhone(req.body.number), 'canada'); textRequestHandler(req, res, stripPhone(req.body.number), 'canada', req.query.key);
}); });
app.post('/intl', function(req, res) { app.post('/intl', function(req, res) {
textRequestHandler(req, res, stripPhone(req.body.number), 'intl'); textRequestHandler(req, res, stripPhone(req.body.number), 'intl', req.query.key);
}); });
function textRequestHandler(req, res, number, region) { function textRequestHandler(req, res, number, region, key) {
if (!req.body.number || !req.body.message) { if (!req.body.number || !req.body.message) {
mpq.track('incomplete request'); mpq.track('incomplete request');
res.send({success:false,message:'Number and message parameters are required.'}); res.send({success:false,message:'Number and message parameters are required.'});
@ -61,16 +69,50 @@ function textRequestHandler(req, res, number, region) {
var message = req.body.message; var message = req.body.message;
if (message.indexOf('http') === 0) { if (message.indexOf('http') === 0) {
// Handle problem with vtext where message would not get sent properly if it
// begins with h ttp
message = ' ' + message; message = ' ' + message;
} }
var tracking_details = {
number: req.body.number,
message: req.body.message,
ip: ip
};
var doSendText = function() {
// Time to actually send the message
sendText(req.body.number, message, region, function(err) {
if (err) {
mpq.track('sendText failed', tracking_details);
res.send({success:false, message:'Communication with SMS gateway failed.'});
}
else {
mpq.track('sendText success', tracking_details);
res.send({success:true});
}
});
};
// Do they have a valid access key?
if (key && key in access_keys) {
console.log('Got valid key', key, '... not applying limits.');
// Skip verification
mpq.track('sendText skipping verification', _.extend(tracking_details, {
key: key,
}));
doSendText();
return;
}
// If they don't have a special key, apply rate limiting and verification
var ipkey = 'textbelt:ip:' + ip + '_' + dateStr(); var ipkey = 'textbelt:ip:' + ip + '_' + dateStr();
var phonekey = 'textbelt:phone:' + number; var phonekey = 'textbelt:phone:' + number;
redis.incr(phonekey, function(err, num) { redis.incr(phonekey, function(err, num) {
if (err) { if (err) {
mpq.track('redis fail'); mpq.track('redis fail');
res.send({success:false,message:'Could not validate phone# quota.'}); res.send({success:false, message:'Could not validate phone# quota.'});
return; return;
} }
@ -84,7 +126,7 @@ function textRequestHandler(req, res, number, region) {
}, 1000*60*3); }, 1000*60*3);
if (num > 3) { if (num > 3) {
mpq.track('exceeded phone quota'); mpq.track('exceeded phone quota');
res.send({success:false,message:'Exceeded quota for this phone number. ' + number}); res.send({success:false, message:'Exceeded quota for this phone number. ' + number});
return; return;
} }
@ -92,12 +134,12 @@ function textRequestHandler(req, res, number, region) {
redis.incr(ipkey, function(err, num) { redis.incr(ipkey, function(err, num) {
if (err) { if (err) {
mpq.track('redis fail'); mpq.track('redis fail');
res.send({success:false,message:'Could not validate IP quota.'}); res.send({success:false, message:'Could not validate IP quota.'});
return; return;
} }
if (num > 75) { if (num > 75) {
mpq.track('exceeded ip quota'); mpq.track('exceeded ip quota');
res.send({success:false,message:'Exceeded quota for this IP address. ' + ip}); res.send({success:false, message:'Exceeded quota for this IP address. ' + ip});
return; return;
} }
setTimeout(function() { setTimeout(function() {
@ -109,21 +151,11 @@ function textRequestHandler(req, res, number, region) {
}); });
}, 1000*60*60*24); }, 1000*60*60*24);
sendText(req.body.number, message, region, function(err) { // Cleared to send now
if (err) { doSendText();
mpq.track('sendText failed', {number: req.body.number, message: req.body.message, ip: ip}); }); // end redis ipkey incr
res.send({success:false,message:'Communication with SMS gateway failed.'}); }); // end redis phonekey incr
} } // end textRequestHandler
else {
mpq.track('sendText success', {number: req.body.number, message: req.body.message, ip: ip, region: region});
res.send({success:true});
}
});
});
});
}
function dateStr() { function dateStr() {
var today = new Date(); var today = new Date();

3
keys_example.json Normal file
View File

@ -0,0 +1,3 @@
{
"specialkey": -1
}