Add request quota system

This commit is contained in:
Cadence Ember
2020-07-23 00:58:21 +12:00
parent 2095be2742
commit 112d9cc90e
17 changed files with 253 additions and 38 deletions

33
src/lib/quota/index.js Normal file
View File

@@ -0,0 +1,33 @@
const constants = require("../constants")
const LimitByFrame = require("./LimitByFrame")
const {getIdentifier} = require("./get_identifier")
require("../testimports")(LimitByFrame, getIdentifier)
const limiter = new LimitByFrame()
function getIPFromReq(req) {
if (constants.quota.ip_mode === "header") {
return req.headers[constants.quota.ip_header]
} else { // constants.quota.ip_mode === "address"
return req.connection.remoteAddress
}
}
function remaining(req) {
if (!constants.quota.enabled) return Infinity // sure.
const ip = getIPFromReq(req)
const identifier = getIdentifier(ip)
return limiter.remaining(identifier)
}
function add(req, count) {
if (!constants.quota.enabled) return Infinity // why not.
const ip = getIPFromReq(req)
const identifier = getIdentifier(ip)
return limiter.add(identifier, count)
}
module.exports.remaining = remaining
module.exports.add = add