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