move chat ratelimiter to core/ChatProvider

This commit is contained in:
HF 2020-11-29 04:05:49 +01:00
parent a7f356b7bc
commit 1bec84ae08
2 changed files with 18 additions and 19 deletions

View File

@ -3,6 +3,7 @@ import logger from './logger';
import redis from '../data/redis';
import User from '../data/models/User';
import webSockets from '../socket/websockets';
import RateLimiter from '../utils/RateLimiter';
import { Channel, RegUser, UserChannel } from '../data/models';
import ChatMessageBuffer from './ChatMessageBuffer';
import { cheapDetector } from './isProxy';
@ -238,21 +239,27 @@ export class ChatProvider {
return this.adminCommands(message, channelId);
}
if (!user.rateLimiter) {
user.rateLimiter = new RateLimiter(20, 15, true);
}
const waitLeft = user.rateLimiter.tick();
if (waitLeft) {
// eslint-disable-next-line max-len
return `You are sending messages too fast, you have to wait ${Math.floor(waitLeft / 1000)}s :(`;
}
if (!this.userHasChannelAccess(user, channelId)) {
return 'You don\'t have access to this channel';
}
const country = user.regUser.flag || 'xx';
let displayCountry = (name.endsWith('berg') || name.endsWith('stein'))
const displayCountry = (name.endsWith('berg') || name.endsWith('stein'))
? 'il'
: country;
if (!user.regUser.verified) {
return 'Your mail has to be verified in order to chat';
}
if (name === 'Aquila') {
displayCountry = 'ug';
}
if (message.length > 2
&& message === message.toUpperCase()

View File

@ -5,7 +5,6 @@ import WebSocket from 'ws';
import logger from '../core/logger';
import Counter from '../utils/Counter';
import RateLimiter from '../utils/RateLimiter';
import { getIPFromRequest } from '../utils/ip';
import CoolDownPacket from './packets/CoolDownPacket';
@ -85,7 +84,6 @@ class SocketServer extends WebSocketEvents {
const user = await authenticateClient(req);
ws.user = user;
ws.name = user.getName();
ws.rateLimiter = new RateLimiter(20, 15, true);
cheapDetector(user.ip);
ws.send(OnlineCounter.dehydrate({
@ -316,18 +314,6 @@ class SocketServer extends WebSocketEvents {
*/
if (ws.name && message) {
const { user } = ws;
const waitLeft = ws.rateLimiter.tick();
if (waitLeft) {
ws.send(JSON.stringify([
'info',
// eslint-disable-next-line max-len
`You are sending messages too fast, you have to wait ${Math.floor(waitLeft / 1000)}s :(`,
'il',
channelId,
]));
return;
}
/*
* if DM channel, make sure that other user has DM open
* (needed because we allow user to leave one-sided
@ -356,7 +342,13 @@ class SocketServer extends WebSocketEvents {
channelId,
);
if (errorMsg) {
ws.send(JSON.stringify(['info', errorMsg, 'il', channelId]));
ws.send(JSON.stringify([
'info',
errorMsg,
'il',
channelId,
chatProvider.infoUserId,
]));
}
} else {
logger.info('Got empty message or message from unidentified ws');