Press n or j to go to the next uncovered block, b, p or k for the previous block.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 | 3x 2x 2x 2x 2x 2x 2x 6x 6x 5x 5x 5x 5x 5x 2x 1x 1x 1x 3x | "use strict";
/**
* Implementation of failure aware rate restriction.
*
* Will intercept monitor requests. After a certain number of failures, it will block for a duration.
*
* Requires ```block-failed``` package.
*
* @param {object} [settings] settings for rate restriction, or null to disable
* @param {number} settings.blockAttemptMs duration to monitor failure for
* @param {number} settings.blockAttemptCount number of failures allowed in this duration
* @param {number} settings.blockDurationMs duration to block for
* @return {ExpressMiddlewareFunction}
*/
function failure(settings = null)
{
if (settings)
{
let blocker = require('block-failed');
// decypher config and defaults
const blockAttemptMs = settings.blockAttemptMs || 60 * 1000;
const blockAttemptCount = settings.blockAttemptCount || 5;
const blockDurationMs = settings.blockDurationMs || 5 * 60 * 1000;
// construct block object
const block = blocker(blockDurationMs, blockAttemptMs, blockAttemptCount - 1);
return function (req, res, next)
{
let attempted = false;
block(req.clientIp, (on_failure) =>
{
attempted = true;
res.on('finish', function ()
{
Eif (res.statusCode !== 200)
{
on_failure();
}
});
next();
}, (blockedMs) =>
{
if (!attempted)
{
res.error(`Operation is disabled due to too many failed attempts. Please try again in ${Math.round(blockedMs / 1000)} seconds`);
}
});
};
}
return function (req, res, next)
{
next();
};
}
module.exports = failure;
|