2022-09-18 03:27:08 +09:00
|
|
|
import { Inject, Injectable } from '@nestjs/common';
|
|
|
|
import Limiter from 'ratelimiter';
|
|
|
|
import Redis from 'ioredis';
|
|
|
|
import { DI } from '@/di-symbols.js';
|
2022-09-18 23:07:41 +09:00
|
|
|
import type Logger from '@/logger.js';
|
|
|
|
import { LoggerService } from '@/core/LoggerService.js';
|
2022-12-04 15:03:09 +09:00
|
|
|
import { bindThis } from '@/decorators.js';
|
2023-01-01 11:58:16 +09:00
|
|
|
import type { IEndpointMeta } from './endpoints.js';
|
2022-09-18 03:27:08 +09:00
|
|
|
|
|
|
|
@Injectable()
|
|
|
|
export class RateLimiterService {
|
2022-09-19 03:11:50 +09:00
|
|
|
private logger: Logger;
|
2023-01-01 11:58:16 +09:00
|
|
|
private disabled = false;
|
2022-09-18 23:07:41 +09:00
|
|
|
|
2022-09-18 03:27:08 +09:00
|
|
|
constructor(
|
|
|
|
@Inject(DI.redis)
|
|
|
|
private redisClient: Redis.Redis,
|
2022-09-18 23:07:41 +09:00
|
|
|
|
|
|
|
private loggerService: LoggerService,
|
2022-09-18 03:27:08 +09:00
|
|
|
) {
|
2022-09-19 03:11:50 +09:00
|
|
|
this.logger = this.loggerService.getLogger('limiter');
|
2023-01-01 11:58:16 +09:00
|
|
|
|
|
|
|
if (process.env.NODE_ENV !== 'production') {
|
|
|
|
this.disabled = true;
|
|
|
|
}
|
2022-09-18 03:27:08 +09:00
|
|
|
}
|
|
|
|
|
2022-12-04 15:03:09 +09:00
|
|
|
@bindThis
|
2022-09-18 03:27:08 +09:00
|
|
|
public limit(limitation: IEndpointMeta['limit'] & { key: NonNullable<string> }, actor: string) {
|
|
|
|
return new Promise<void>((ok, reject) => {
|
2023-01-01 11:58:16 +09:00
|
|
|
if (this.disabled) ok();
|
|
|
|
|
2022-09-18 03:27:08 +09:00
|
|
|
// Short-term limit
|
|
|
|
const min = (): void => {
|
|
|
|
const minIntervalLimiter = new Limiter({
|
|
|
|
id: `${actor}:${limitation.key}:min`,
|
|
|
|
duration: limitation.minInterval,
|
|
|
|
max: 1,
|
|
|
|
db: this.redisClient,
|
|
|
|
});
|
|
|
|
|
|
|
|
minIntervalLimiter.get((err, info) => {
|
|
|
|
if (err) {
|
|
|
|
return reject('ERR');
|
|
|
|
}
|
|
|
|
|
2022-09-19 03:11:50 +09:00
|
|
|
this.logger.debug(`${actor} ${limitation.key} min remaining: ${info.remaining}`);
|
2022-09-18 03:27:08 +09:00
|
|
|
|
|
|
|
if (info.remaining === 0) {
|
|
|
|
reject('BRIEF_REQUEST_INTERVAL');
|
|
|
|
} else {
|
|
|
|
if (hasLongTermLimit) {
|
|
|
|
max();
|
|
|
|
} else {
|
|
|
|
ok();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
// Long term limit
|
|
|
|
const max = (): void => {
|
|
|
|
const limiter = new Limiter({
|
|
|
|
id: `${actor}:${limitation.key}`,
|
|
|
|
duration: limitation.duration,
|
|
|
|
max: limitation.max,
|
|
|
|
db: this.redisClient,
|
|
|
|
});
|
|
|
|
|
|
|
|
limiter.get((err, info) => {
|
|
|
|
if (err) {
|
|
|
|
return reject('ERR');
|
|
|
|
}
|
|
|
|
|
2022-09-19 03:11:50 +09:00
|
|
|
this.logger.debug(`${actor} ${limitation.key} max remaining: ${info.remaining}`);
|
2022-09-18 03:27:08 +09:00
|
|
|
|
|
|
|
if (info.remaining === 0) {
|
|
|
|
reject('RATE_LIMIT_EXCEEDED');
|
|
|
|
} else {
|
|
|
|
ok();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
const hasShortTermLimit = typeof limitation.minInterval === 'number';
|
|
|
|
|
|
|
|
const hasLongTermLimit =
|
|
|
|
typeof limitation.duration === 'number' &&
|
|
|
|
typeof limitation.max === 'number';
|
|
|
|
|
|
|
|
if (hasShortTermLimit) {
|
|
|
|
min();
|
|
|
|
} else if (hasLongTermLimit) {
|
|
|
|
max();
|
|
|
|
} else {
|
|
|
|
ok();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|