2022-02-27 14:21:25 +09:00
|
|
|
import Limiter from 'ratelimiter';
|
2022-03-25 16:27:41 +09:00
|
|
|
import { CacheableLocalUser, User } from '@/models/entities/user.js';
|
2022-02-27 11:07:39 +09:00
|
|
|
import Logger from '@/services/logger.js';
|
2022-06-14 18:01:23 +09:00
|
|
|
import { redisClient } from '../../db/redis.js';
|
|
|
|
import { IEndpointMeta } from './endpoints.js';
|
2016-12-29 07:49:51 +09:00
|
|
|
|
2019-02-03 16:45:13 +09:00
|
|
|
const logger = new Logger('limiter');
|
2017-01-23 18:25:52 +09:00
|
|
|
|
2022-05-28 12:06:47 +09:00
|
|
|
export const limiter = (limitation: IEndpointMeta['limit'] & { key: NonNullable<string> }, actor: string) => new Promise<void>((ok, reject) => {
|
2022-06-26 19:16:32 +09:00
|
|
|
if (process.env.NODE_ENV === 'test') ok();
|
|
|
|
|
2022-05-28 12:06:47 +09:00
|
|
|
const hasShortTermLimit = typeof limitation.minInterval === 'number';
|
2016-12-29 07:49:51 +09:00
|
|
|
|
2017-03-01 22:33:43 +09:00
|
|
|
const hasLongTermLimit =
|
2022-05-28 12:06:47 +09:00
|
|
|
typeof limitation.duration === 'number' &&
|
|
|
|
typeof limitation.max === 'number';
|
2016-12-29 07:49:51 +09:00
|
|
|
|
2017-03-01 22:33:43 +09:00
|
|
|
if (hasShortTermLimit) {
|
2016-12-29 07:49:51 +09:00
|
|
|
min();
|
2017-03-01 22:33:43 +09:00
|
|
|
} else if (hasLongTermLimit) {
|
2016-12-29 07:49:51 +09:00
|
|
|
max();
|
|
|
|
} else {
|
|
|
|
ok();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Short-term limit
|
2022-01-21 17:15:14 +09:00
|
|
|
function min(): void {
|
2016-12-29 07:49:51 +09:00
|
|
|
const minIntervalLimiter = new Limiter({
|
2022-05-28 12:06:47 +09:00
|
|
|
id: `${actor}:${limitation.key}:min`,
|
2017-03-01 22:33:43 +09:00
|
|
|
duration: limitation.minInterval,
|
2016-12-29 07:49:51 +09:00
|
|
|
max: 1,
|
2021-12-09 23:58:30 +09:00
|
|
|
db: redisClient,
|
2016-12-29 07:49:51 +09:00
|
|
|
});
|
|
|
|
|
2017-01-23 18:25:52 +09:00
|
|
|
minIntervalLimiter.get((err, info) => {
|
|
|
|
if (err) {
|
|
|
|
return reject('ERR');
|
|
|
|
}
|
|
|
|
|
2022-05-28 12:06:47 +09:00
|
|
|
logger.debug(`${actor} ${limitation.key} min remaining: ${info.remaining}`);
|
2017-01-23 18:25:52 +09:00
|
|
|
|
|
|
|
if (info.remaining === 0) {
|
2016-12-29 07:49:51 +09:00
|
|
|
reject('BRIEF_REQUEST_INTERVAL');
|
|
|
|
} else {
|
2017-03-01 22:33:43 +09:00
|
|
|
if (hasLongTermLimit) {
|
2016-12-29 07:49:51 +09:00
|
|
|
max();
|
|
|
|
} else {
|
|
|
|
ok();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
// Long term limit
|
2022-01-21 17:15:14 +09:00
|
|
|
function max(): void {
|
2016-12-29 07:49:51 +09:00
|
|
|
const limiter = new Limiter({
|
2022-05-28 12:06:47 +09:00
|
|
|
id: `${actor}:${limitation.key}`,
|
2017-03-01 22:33:43 +09:00
|
|
|
duration: limitation.duration,
|
|
|
|
max: limitation.max,
|
2021-12-09 23:58:30 +09:00
|
|
|
db: redisClient,
|
2016-12-29 07:49:51 +09:00
|
|
|
});
|
|
|
|
|
2017-01-23 18:25:52 +09:00
|
|
|
limiter.get((err, info) => {
|
|
|
|
if (err) {
|
|
|
|
return reject('ERR');
|
|
|
|
}
|
|
|
|
|
2022-05-28 12:06:47 +09:00
|
|
|
logger.debug(`${actor} ${limitation.key} max remaining: ${info.remaining}`);
|
2017-01-23 18:25:52 +09:00
|
|
|
|
|
|
|
if (info.remaining === 0) {
|
2016-12-29 07:49:51 +09:00
|
|
|
reject('RATE_LIMIT_EXCEEDED');
|
|
|
|
} else {
|
|
|
|
ok();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|