2021-10-23 01:35:13 -07:00
|
|
|
const { RateLimiter } = require("limiter");
|
2021-11-15 08:52:28 -08:00
|
|
|
const { log_info } = require("../src/util");
|
2021-10-23 01:35:13 -07:00
|
|
|
|
|
|
|
class KumaRateLimiter {
|
|
|
|
constructor(config) {
|
|
|
|
this.errorMessage = config.errorMessage;
|
|
|
|
this.rateLimiter = new RateLimiter(config);
|
|
|
|
}
|
|
|
|
|
|
|
|
async pass(callback, num = 1) {
|
|
|
|
const remainingRequests = await this.removeTokens(num);
|
2021-11-15 08:52:28 -08:00
|
|
|
log_info("rate-limit", "remaining requests: " + remainingRequests);
|
2021-10-23 01:35:13 -07:00
|
|
|
if (remainingRequests < 0) {
|
|
|
|
if (callback) {
|
|
|
|
callback({
|
|
|
|
ok: false,
|
|
|
|
msg: this.errorMessage,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
async removeTokens(num = 1) {
|
|
|
|
return await this.rateLimiter.removeTokens(num);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const loginRateLimiter = new KumaRateLimiter({
|
|
|
|
tokensPerInterval: 20,
|
|
|
|
interval: "minute",
|
|
|
|
fireImmediately: true,
|
|
|
|
errorMessage: "Too frequently, try again later."
|
|
|
|
});
|
|
|
|
|
2022-03-29 02:38:48 -07:00
|
|
|
const twoFaRateLimiter = new KumaRateLimiter({
|
|
|
|
tokensPerInterval: 30,
|
|
|
|
interval: "minute",
|
|
|
|
fireImmediately: true,
|
|
|
|
errorMessage: "Too frequently, try again later."
|
|
|
|
});
|
|
|
|
|
2021-10-23 01:35:13 -07:00
|
|
|
module.exports = {
|
2022-03-29 02:38:48 -07:00
|
|
|
loginRateLimiter,
|
|
|
|
twoFaRateLimiter,
|
2021-10-23 01:35:13 -07:00
|
|
|
};
|