2016-12-28 22:49:51 +00:00
|
|
|
import * as Limiter from 'ratelimiter';
|
2021-08-19 09:33:41 +00:00
|
|
|
import { redisClient } from '../../db/redis.js';
|
|
|
|
import { IEndpoint } from './endpoints.js';
|
|
|
|
import { getAcct } from '@/misc/acct.js';
|
|
|
|
import { User } from '@/models/entities/user.js';
|
|
|
|
import Logger from '@/services/logger.js';
|
2016-12-28 22:49:51 +00:00
|
|
|
|
2019-02-03 07:45:13 +00:00
|
|
|
const logger = new Logger('limiter');
|
2017-01-23 09:25:52 +00:00
|
|
|
|
2021-03-24 02:05:37 +00:00
|
|
|
export default (endpoint: IEndpoint, user: User) => new Promise<void>((ok, reject) => {
|
2019-04-12 16:43:22 +00:00
|
|
|
const limitation = endpoint.meta.limit!;
|
2017-03-01 13:33:43 +00:00
|
|
|
|
|
|
|
const key = limitation.hasOwnProperty('key')
|
|
|
|
? limitation.key
|
2016-12-28 22:49:51 +00:00
|
|
|
: endpoint.name;
|
|
|
|
|
2017-03-01 13:33:43 +00:00
|
|
|
const hasShortTermLimit =
|
|
|
|
limitation.hasOwnProperty('minInterval');
|
2016-12-28 22:49:51 +00:00
|
|
|
|
2017-03-01 13:33:43 +00:00
|
|
|
const hasLongTermLimit =
|
|
|
|
limitation.hasOwnProperty('duration') &&
|
|
|
|
limitation.hasOwnProperty('max');
|
2016-12-28 22:49:51 +00:00
|
|
|
|
2017-03-01 13:33:43 +00:00
|
|
|
if (hasShortTermLimit) {
|
2016-12-28 22:49:51 +00:00
|
|
|
min();
|
2017-03-01 13:33:43 +00:00
|
|
|
} else if (hasLongTermLimit) {
|
2016-12-28 22:49:51 +00:00
|
|
|
max();
|
|
|
|
} else {
|
|
|
|
ok();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Short-term limit
|
2017-01-23 09:25:52 +00:00
|
|
|
function min() {
|
2016-12-28 22:49:51 +00:00
|
|
|
const minIntervalLimiter = new Limiter({
|
2019-04-07 12:50:36 +00:00
|
|
|
id: `${user.id}:${key}:min`,
|
2017-03-01 13:33:43 +00:00
|
|
|
duration: limitation.minInterval,
|
2016-12-28 22:49:51 +00:00
|
|
|
max: 1,
|
2021-03-23 05:54:09 +00:00
|
|
|
db: redisClient
|
2016-12-28 22:49:51 +00:00
|
|
|
});
|
|
|
|
|
2017-01-23 09:25:52 +00:00
|
|
|
minIntervalLimiter.get((err, info) => {
|
|
|
|
if (err) {
|
|
|
|
return reject('ERR');
|
|
|
|
}
|
|
|
|
|
2019-02-03 07:45:13 +00:00
|
|
|
logger.debug(`@${getAcct(user)} ${endpoint.name} min remaining: ${info.remaining}`);
|
2017-01-23 09:25:52 +00:00
|
|
|
|
|
|
|
if (info.remaining === 0) {
|
2016-12-28 22:49:51 +00:00
|
|
|
reject('BRIEF_REQUEST_INTERVAL');
|
|
|
|
} else {
|
2017-03-01 13:33:43 +00:00
|
|
|
if (hasLongTermLimit) {
|
2016-12-28 22:49:51 +00:00
|
|
|
max();
|
|
|
|
} else {
|
|
|
|
ok();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
// Long term limit
|
2017-01-23 09:25:52 +00:00
|
|
|
function max() {
|
2016-12-28 22:49:51 +00:00
|
|
|
const limiter = new Limiter({
|
2019-04-07 12:50:36 +00:00
|
|
|
id: `${user.id}:${key}`,
|
2017-03-01 13:33:43 +00:00
|
|
|
duration: limitation.duration,
|
|
|
|
max: limitation.max,
|
2021-03-23 05:54:09 +00:00
|
|
|
db: redisClient
|
2016-12-28 22:49:51 +00:00
|
|
|
});
|
|
|
|
|
2017-01-23 09:25:52 +00:00
|
|
|
limiter.get((err, info) => {
|
|
|
|
if (err) {
|
|
|
|
return reject('ERR');
|
|
|
|
}
|
|
|
|
|
2019-02-03 07:45:13 +00:00
|
|
|
logger.debug(`@${getAcct(user)} ${endpoint.name} max remaining: ${info.remaining}`);
|
2017-01-23 09:25:52 +00:00
|
|
|
|
|
|
|
if (info.remaining === 0) {
|
2016-12-28 22:49:51 +00:00
|
|
|
reject('RATE_LIMIT_EXCEEDED');
|
|
|
|
} else {
|
|
|
|
ok();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|