move chat ratelimiter to core/ChatProvider
This commit is contained in:
parent
a7f356b7bc
commit
1bec84ae08
|
@ -3,6 +3,7 @@ import logger from './logger';
|
||||||
import redis from '../data/redis';
|
import redis from '../data/redis';
|
||||||
import User from '../data/models/User';
|
import User from '../data/models/User';
|
||||||
import webSockets from '../socket/websockets';
|
import webSockets from '../socket/websockets';
|
||||||
|
import RateLimiter from '../utils/RateLimiter';
|
||||||
import { Channel, RegUser, UserChannel } from '../data/models';
|
import { Channel, RegUser, UserChannel } from '../data/models';
|
||||||
import ChatMessageBuffer from './ChatMessageBuffer';
|
import ChatMessageBuffer from './ChatMessageBuffer';
|
||||||
import { cheapDetector } from './isProxy';
|
import { cheapDetector } from './isProxy';
|
||||||
|
@ -238,21 +239,27 @@ export class ChatProvider {
|
||||||
return this.adminCommands(message, channelId);
|
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)) {
|
if (!this.userHasChannelAccess(user, channelId)) {
|
||||||
return 'You don\'t have access to this channel';
|
return 'You don\'t have access to this channel';
|
||||||
}
|
}
|
||||||
|
|
||||||
const country = user.regUser.flag || 'xx';
|
const country = user.regUser.flag || 'xx';
|
||||||
let displayCountry = (name.endsWith('berg') || name.endsWith('stein'))
|
const displayCountry = (name.endsWith('berg') || name.endsWith('stein'))
|
||||||
? 'il'
|
? 'il'
|
||||||
: country;
|
: country;
|
||||||
|
|
||||||
if (!user.regUser.verified) {
|
if (!user.regUser.verified) {
|
||||||
return 'Your mail has to be verified in order to chat';
|
return 'Your mail has to be verified in order to chat';
|
||||||
}
|
}
|
||||||
if (name === 'Aquila') {
|
|
||||||
displayCountry = 'ug';
|
|
||||||
}
|
|
||||||
|
|
||||||
if (message.length > 2
|
if (message.length > 2
|
||||||
&& message === message.toUpperCase()
|
&& message === message.toUpperCase()
|
||||||
|
|
|
@ -5,7 +5,6 @@ import WebSocket from 'ws';
|
||||||
|
|
||||||
import logger from '../core/logger';
|
import logger from '../core/logger';
|
||||||
import Counter from '../utils/Counter';
|
import Counter from '../utils/Counter';
|
||||||
import RateLimiter from '../utils/RateLimiter';
|
|
||||||
import { getIPFromRequest } from '../utils/ip';
|
import { getIPFromRequest } from '../utils/ip';
|
||||||
|
|
||||||
import CoolDownPacket from './packets/CoolDownPacket';
|
import CoolDownPacket from './packets/CoolDownPacket';
|
||||||
|
@ -85,7 +84,6 @@ class SocketServer extends WebSocketEvents {
|
||||||
const user = await authenticateClient(req);
|
const user = await authenticateClient(req);
|
||||||
ws.user = user;
|
ws.user = user;
|
||||||
ws.name = user.getName();
|
ws.name = user.getName();
|
||||||
ws.rateLimiter = new RateLimiter(20, 15, true);
|
|
||||||
cheapDetector(user.ip);
|
cheapDetector(user.ip);
|
||||||
|
|
||||||
ws.send(OnlineCounter.dehydrate({
|
ws.send(OnlineCounter.dehydrate({
|
||||||
|
@ -316,18 +314,6 @@ class SocketServer extends WebSocketEvents {
|
||||||
*/
|
*/
|
||||||
if (ws.name && message) {
|
if (ws.name && message) {
|
||||||
const { user } = ws;
|
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
|
* if DM channel, make sure that other user has DM open
|
||||||
* (needed because we allow user to leave one-sided
|
* (needed because we allow user to leave one-sided
|
||||||
|
@ -356,7 +342,13 @@ class SocketServer extends WebSocketEvents {
|
||||||
channelId,
|
channelId,
|
||||||
);
|
);
|
||||||
if (errorMsg) {
|
if (errorMsg) {
|
||||||
ws.send(JSON.stringify(['info', errorMsg, 'il', channelId]));
|
ws.send(JSON.stringify([
|
||||||
|
'info',
|
||||||
|
errorMsg,
|
||||||
|
'il',
|
||||||
|
channelId,
|
||||||
|
chatProvider.infoUserId,
|
||||||
|
]));
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
logger.info('Got empty message or message from unidentified ws');
|
logger.info('Got empty message or message from unidentified ws');
|
||||||
|
|
Loading…
Reference in New Issue
Block a user