nightscout
Version:
Nightscout acts as a web-based CGM (Continuous Glucose Monitor) to allow multiple caregivers to remotely view a patients glucose data in realtime.
59 lines (47 loc) • 1.39 kB
JavaScript
;
const _ = require('lodash');
function init (env) {
const ipDelayList = {};
const DELAY_ON_FAIL = _.get(env, 'settings.authFailDelay') || 5000;
const FAIL_AGE = 60000;
ipDelayList.addFailedRequest = function addFailedRequest (ip) {
const ipString = String(ip);
let entry = ipDelayList[ipString];
const now = Date.now();
if (!entry) {
ipDelayList[ipString] = now + DELAY_ON_FAIL;
return;
}
if (now >= entry) { entry = now; }
ipDelayList[ipString] = entry + DELAY_ON_FAIL;
};
ipDelayList.shouldDelayRequest = function shouldDelayRequest (ip) {
const ipString = String(ip);
const entry = ipDelayList[ipString];
let now = Date.now();
if (entry) {
if (now < entry) {
return entry - now;
}
}
return false;
};
ipDelayList.requestSucceeded = function requestSucceeded (ip) {
const ipString = String(ip);
if (ipDelayList[ipString]) {
delete ipDelayList[ipString];
}
};
// Clear items older than a minute
setTimeout(function clearList () {
for (var key in ipDelayList) {
if (ipDelayList.hasOwnProperty(key)) {
if (Date.now() > ipDelayList[key] + FAIL_AGE) {
delete ipDelayList[key];
}
}
}
}, 30000);
return ipDelayList;
}
module.exports = init;