2022-02-27 06:21:25 +01:00
|
|
|
import Limiter from 'ratelimiter';
|
2022-02-27 03:07:39 +01:00
|
|
|
import { redisClient } from '../../db/redis.js';
|
|
|
|
import { IEndpoint } from './endpoints.js';
|
|
|
|
import * as Acct from '@/misc/acct.js';
|
|
|
|
import { User } from '@/models/entities/user.js';
|
|
|
|
import Logger from '@/services/logger.js';
|
2016-12-28 23:49:51 +01:00
|
|
|
|
2019-02-03 08:45:13 +01:00
|
|
|
const logger = new Logger('limiter');
|
2017-01-23 10:25:52 +01:00
|
|
|
|
2022-01-21 09:15:14 +01:00
|
|
|
export const limiter = (endpoint: IEndpoint & { meta: { limit: NonNullable<IEndpoint['meta']['limit']> } }, user: User) => new Promise<void>((ok, reject) => {
|
|
|
|
const limitation = endpoint.meta.limit;
|
2017-03-01 14:33:43 +01:00
|
|
|
|
2021-11-12 02:52:10 +01:00
|
|
|
const key = Object.prototype.hasOwnProperty.call(limitation, 'key')
|
2017-03-01 14:33:43 +01:00
|
|
|
? limitation.key
|
2016-12-28 23:49:51 +01:00
|
|
|
: endpoint.name;
|
|
|
|
|
2017-03-01 14:33:43 +01:00
|
|
|
const hasShortTermLimit =
|
2021-11-12 02:52:10 +01:00
|
|
|
Object.prototype.hasOwnProperty.call(limitation, 'minInterval');
|
2016-12-28 23:49:51 +01:00
|
|
|
|
2017-03-01 14:33:43 +01:00
|
|
|
const hasLongTermLimit =
|
2021-11-12 02:52:10 +01:00
|
|
|
Object.prototype.hasOwnProperty.call(limitation, 'duration') &&
|
|
|
|
Object.prototype.hasOwnProperty.call(limitation, 'max');
|
2016-12-28 23:49:51 +01:00
|
|
|
|
2017-03-01 14:33:43 +01:00
|
|
|
if (hasShortTermLimit) {
|
2016-12-28 23:49:51 +01:00
|
|
|
min();
|
2017-03-01 14:33:43 +01:00
|
|
|
} else if (hasLongTermLimit) {
|
2016-12-28 23:49:51 +01:00
|
|
|
max();
|
|
|
|
} else {
|
|
|
|
ok();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Short-term limit
|
2022-01-21 09:15:14 +01:00
|
|
|
function min(): void {
|
2016-12-28 23:49:51 +01:00
|
|
|
const minIntervalLimiter = new Limiter({
|
2019-04-07 14:50:36 +02:00
|
|
|
id: `${user.id}:${key}:min`,
|
2017-03-01 14:33:43 +01:00
|
|
|
duration: limitation.minInterval,
|
2016-12-28 23:49:51 +01:00
|
|
|
max: 1,
|
2021-12-09 15:58:30 +01:00
|
|
|
db: redisClient,
|
2016-12-28 23:49:51 +01:00
|
|
|
});
|
|
|
|
|
2017-01-23 10:25:52 +01:00
|
|
|
minIntervalLimiter.get((err, info) => {
|
|
|
|
if (err) {
|
|
|
|
return reject('ERR');
|
|
|
|
}
|
|
|
|
|
2021-11-11 18:02:25 +01:00
|
|
|
logger.debug(`@${Acct.toString(user)} ${endpoint.name} min remaining: ${info.remaining}`);
|
2017-01-23 10:25:52 +01:00
|
|
|
|
|
|
|
if (info.remaining === 0) {
|
2016-12-28 23:49:51 +01:00
|
|
|
reject('BRIEF_REQUEST_INTERVAL');
|
|
|
|
} else {
|
2017-03-01 14:33:43 +01:00
|
|
|
if (hasLongTermLimit) {
|
2016-12-28 23:49:51 +01:00
|
|
|
max();
|
|
|
|
} else {
|
|
|
|
ok();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
// Long term limit
|
2022-01-21 09:15:14 +01:00
|
|
|
function max(): void {
|
2016-12-28 23:49:51 +01:00
|
|
|
const limiter = new Limiter({
|
2019-04-07 14:50:36 +02:00
|
|
|
id: `${user.id}:${key}`,
|
2017-03-01 14:33:43 +01:00
|
|
|
duration: limitation.duration,
|
|
|
|
max: limitation.max,
|
2021-12-09 15:58:30 +01:00
|
|
|
db: redisClient,
|
2016-12-28 23:49:51 +01:00
|
|
|
});
|
|
|
|
|
2017-01-23 10:25:52 +01:00
|
|
|
limiter.get((err, info) => {
|
|
|
|
if (err) {
|
|
|
|
return reject('ERR');
|
|
|
|
}
|
|
|
|
|
2021-11-11 18:02:25 +01:00
|
|
|
logger.debug(`@${Acct.toString(user)} ${endpoint.name} max remaining: ${info.remaining}`);
|
2017-01-23 10:25:52 +01:00
|
|
|
|
|
|
|
if (info.remaining === 0) {
|
2016-12-28 23:49:51 +01:00
|
|
|
reject('RATE_LIMIT_EXCEEDED');
|
|
|
|
} else {
|
|
|
|
ok();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|