2016-12-29 07:49:51 +09:00
|
|
|
import * as Limiter from 'ratelimiter';
|
2017-01-23 18:25:52 +09:00
|
|
|
import * as debug from 'debug';
|
2018-03-29 01:20:40 +09:00
|
|
|
import limiterDB from '../../db/redis';
|
2017-03-01 22:33:43 +09:00
|
|
|
import { Endpoint } from './endpoints';
|
2016-12-29 07:49:51 +09:00
|
|
|
import { IAuthContext } from './authenticate';
|
2018-04-02 04:15:27 +09:00
|
|
|
import getAcct from '../../misc/user/get-acct';
|
2016-12-29 07:49:51 +09:00
|
|
|
|
2017-01-23 18:25:52 +09:00
|
|
|
const log = debug('misskey:limitter');
|
|
|
|
|
2017-03-01 22:33:43 +09:00
|
|
|
export default (endpoint: Endpoint, ctx: IAuthContext) => new Promise((ok, reject) => {
|
|
|
|
const limitation = endpoint.limit;
|
|
|
|
|
|
|
|
const key = limitation.hasOwnProperty('key')
|
|
|
|
? limitation.key
|
2016-12-29 07:49:51 +09:00
|
|
|
: endpoint.name;
|
|
|
|
|
2017-03-01 22:33:43 +09:00
|
|
|
const hasShortTermLimit =
|
|
|
|
limitation.hasOwnProperty('minInterval');
|
2016-12-29 07:49:51 +09:00
|
|
|
|
2017-03-01 22:33:43 +09:00
|
|
|
const hasLongTermLimit =
|
|
|
|
limitation.hasOwnProperty('duration') &&
|
|
|
|
limitation.hasOwnProperty('max');
|
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
|
2017-01-23 18:25:52 +09:00
|
|
|
function min() {
|
2016-12-29 07:49:51 +09:00
|
|
|
const minIntervalLimiter = new Limiter({
|
2017-03-01 22:33:43 +09:00
|
|
|
id: `${ctx.user._id}:${key}:min`,
|
|
|
|
duration: limitation.minInterval,
|
2016-12-29 07:49:51 +09:00
|
|
|
max: 1,
|
|
|
|
db: limiterDB
|
|
|
|
});
|
|
|
|
|
2017-01-23 18:25:52 +09:00
|
|
|
minIntervalLimiter.get((err, info) => {
|
|
|
|
if (err) {
|
|
|
|
return reject('ERR');
|
|
|
|
}
|
|
|
|
|
2018-03-27 16:51:12 +09:00
|
|
|
log(`@${getAcct(ctx.user)} ${endpoint.name} 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
|
2017-01-23 18:25:52 +09:00
|
|
|
function max() {
|
2016-12-29 07:49:51 +09:00
|
|
|
const limiter = new Limiter({
|
2017-03-01 22:33:43 +09:00
|
|
|
id: `${ctx.user._id}:${key}`,
|
|
|
|
duration: limitation.duration,
|
|
|
|
max: limitation.max,
|
2016-12-29 07:49:51 +09:00
|
|
|
db: limiterDB
|
|
|
|
});
|
|
|
|
|
2017-01-23 18:25:52 +09:00
|
|
|
limiter.get((err, info) => {
|
|
|
|
if (err) {
|
|
|
|
return reject('ERR');
|
|
|
|
}
|
|
|
|
|
2018-03-27 16:51:12 +09:00
|
|
|
log(`@${getAcct(ctx.user)} ${endpoint.name} 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();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|