MSAgent-Chat/server/src/client.ts

265 lines
7.4 KiB
TypeScript
Raw Normal View History

2024-07-14 15:26:05 -04:00
import EventEmitter from 'events';
import { WebSocket } from 'ws';
import {
MSAgentAdminBanMessage,
MSAgentAdminGetIPMessage,
MSAgentAdminGetIPResponse,
MSAgentAdminKickMessage,
MSAgentAdminLoginMessage,
MSAgentAdminLoginResponse,
MSAgentAdminMessage,
MSAgentAdminOperation,
MSAgentErrorMessage,
2024-07-14 19:35:00 -04:00
MSAgentSendImageMessage,
2024-07-14 15:26:05 -04:00
MSAgentJoinMessage,
MSAgentProtocolMessage,
MSAgentProtocolMessageType,
MSAgentTalkMessage,
MSAgentPlayAnimationMessage
2024-07-14 15:26:05 -04:00
} from '@msagent-chat/protocol';
import { MSAgentChatRoom } from './room.js';
2024-07-02 23:42:03 -04:00
import * as htmlentities from 'html-entities';
2024-07-14 15:26:05 -04:00
import RateLimiter from './ratelimiter.js';
import { createHash } from 'crypto';
2024-07-02 23:42:03 -04:00
// Event types
export interface Client {
2024-07-14 15:26:05 -04:00
on(event: 'join', listener: () => void): this;
on(event: 'close', listener: () => void): this;
on(event: 'talk', listener: (msg: string) => void): this;
on(event: 'animation', listener: (anim: string) => void): this;
2024-07-14 19:35:00 -04:00
on(event: 'image', listener: (id: string) => void): this;
2024-07-02 23:42:03 -04:00
2024-07-14 15:26:05 -04:00
on(event: string, listener: Function): this;
2024-07-02 23:42:03 -04:00
}
2024-07-01 23:34:28 -04:00
export class Client extends EventEmitter {
2024-07-14 15:26:05 -04:00
ip: string;
username: string | null;
agent: string | null;
admin: boolean;
2024-07-02 23:42:03 -04:00
2024-07-14 15:26:05 -04:00
room: MSAgentChatRoom;
socket: WebSocket;
2024-07-11 23:27:21 -04:00
2024-07-14 15:26:05 -04:00
nopTimer: NodeJS.Timeout | undefined;
nopLevel: number;
2024-07-14 15:26:05 -04:00
chatRateLimit: RateLimiter;
animRateLimit: RateLimiter;
2024-07-11 23:27:21 -04:00
2024-07-14 15:26:05 -04:00
constructor(socket: WebSocket, room: MSAgentChatRoom, ip: string) {
super();
this.socket = socket;
this.ip = ip;
this.room = room;
this.username = null;
this.agent = null;
this.admin = false;
this.resetNop();
this.nopLevel = 0;
2024-07-11 23:27:21 -04:00
2024-07-14 15:26:05 -04:00
this.chatRateLimit = new RateLimiter(this.room.config.ratelimits.chat);
this.animRateLimit = new RateLimiter(this.room.config.ratelimits.anim);
2024-07-01 23:34:28 -04:00
2024-07-14 15:26:05 -04:00
this.socket.on('message', (msg, isBinary) => {
if (isBinary) {
this.socket.close();
return;
}
this.parseMessage(msg.toString('utf-8'));
});
this.socket.on('error', () => {});
this.socket.on('close', () => {
this.emit('close');
});
}
2024-07-02 00:30:10 -04:00
2024-07-14 15:26:05 -04:00
send(msg: MSAgentProtocolMessage) {
return new Promise<void>((res, rej) => {
if (this.socket.readyState !== WebSocket.OPEN) {
res();
return;
}
this.socket.send(JSON.stringify(msg), (err) => {
if (err) {
rej(err);
return;
}
res();
});
});
}
2024-07-14 15:26:05 -04:00
private resetNop() {
clearInterval(this.nopTimer);
this.nopLevel = 0;
this.nopTimer = setInterval(() => {
if (this.nopLevel++ >= 3) {
this.socket.close();
} else {
this.send({
op: MSAgentProtocolMessageType.KeepAlive
});
}
}, 10000);
}
private async parseMessage(data: string) {
let msg: MSAgentProtocolMessage;
try {
msg = JSON.parse(data);
} catch {
this.socket.close();
return;
}
this.resetNop();
switch (msg.op) {
case MSAgentProtocolMessageType.Join: {
let joinMsg = msg as MSAgentJoinMessage;
2024-07-17 13:16:37 -04:00
if (this.username !== null || !joinMsg.data || !joinMsg.data.username || !joinMsg.data.username) {
2024-07-14 15:26:05 -04:00
this.socket.close();
return;
}
let username = joinMsg.data.username.trim();
if (!validateUsername(username)) {
let msg: MSAgentErrorMessage = {
op: MSAgentProtocolMessageType.Error,
data: {
error: 'Usernames can contain only numbers, letters, spaces, dashes, underscores, and dots, and it must be between 3 and 20 characters.'
}
};
await this.send(msg);
this.socket.close();
return;
}
if (this.room.config.bannedWords.some((w) => username.indexOf(w) !== -1)) {
this.socket.close();
return;
}
if (this.room.clients.some((u) => u.username === username)) {
let i = 1;
let uo = username;
do {
username = uo + i++;
} while (this.room.clients.some((u) => u.username === username));
}
if (!this.room.agents.some((a) => a.filename === joinMsg.data.agent)) {
this.socket.close();
return;
}
this.username = username;
this.agent = joinMsg.data.agent;
this.emit('join');
break;
}
case MSAgentProtocolMessageType.Talk: {
let talkMsg = msg as MSAgentTalkMessage;
if (!talkMsg.data || !talkMsg.data.msg || !this.chatRateLimit.request()) {
return;
}
if (talkMsg.data.msg.length > this.room.config.charlimit) return;
2024-07-14 23:15:48 -04:00
if (this.room.config.bannedWords.some((w) => talkMsg.data.msg.toLowerCase().indexOf(w.toLowerCase()) !== -1)) {
2024-07-14 15:26:05 -04:00
return;
}
this.emit('talk', talkMsg.data.msg);
break;
}
case MSAgentProtocolMessageType.PlayAnimation: {
let animMsg = msg as MSAgentPlayAnimationMessage;
if (!animMsg.data || !animMsg.data.anim || !this.animRateLimit.request()) return;
this.emit('animation', animMsg.data.anim);
break;
}
2024-07-14 19:35:00 -04:00
case MSAgentProtocolMessageType.SendImage: {
let imgMsg = msg as MSAgentSendImageMessage;
if (!imgMsg.data || !imgMsg.data.id || !this.chatRateLimit.request()) return;
this.emit('image', imgMsg.data.id);
break;
}
2024-07-14 15:26:05 -04:00
case MSAgentProtocolMessageType.Admin: {
let adminMsg = msg as MSAgentAdminMessage;
if (!adminMsg.data) return;
switch (adminMsg.data.action) {
case MSAgentAdminOperation.Login: {
let loginMsg = adminMsg as MSAgentAdminLoginMessage;
if (this.admin || !loginMsg.data.password) return;
let sha256 = createHash('sha256');
sha256.update(loginMsg.data.password);
let hash = sha256.digest('hex');
sha256.destroy();
let success = false;
if (hash === this.room.config.adminPasswordHash) {
this.admin = true;
success = true;
this.emit('admin');
}
let res: MSAgentAdminLoginResponse = {
op: MSAgentProtocolMessageType.Admin,
data: {
action: MSAgentAdminOperation.Login,
success
}
};
this.send(res);
break;
}
case MSAgentAdminOperation.GetIP: {
let getIPMsg = adminMsg as MSAgentAdminGetIPMessage;
if (!this.admin || !getIPMsg.data || !getIPMsg.data.username) return;
let _user = this.room.clients.find((c) => c.username === getIPMsg.data.username);
if (!_user) return;
let res: MSAgentAdminGetIPResponse = {
op: MSAgentProtocolMessageType.Admin,
data: {
action: MSAgentAdminOperation.GetIP,
username: _user.username!,
ip: _user.ip
}
};
this.send(res);
break;
}
case MSAgentAdminOperation.Kick: {
let kickMsg = adminMsg as MSAgentAdminKickMessage;
if (!this.admin || !kickMsg.data || !kickMsg.data.username) return;
let _user = this.room.clients.find((c) => c.username === kickMsg.data.username);
if (!_user) return;
let res: MSAgentErrorMessage = {
op: MSAgentProtocolMessageType.Error,
data: {
error: 'You have been kicked.'
}
};
await _user.send(res);
_user.socket.close();
break;
}
case MSAgentAdminOperation.Ban: {
let banMsg = adminMsg as MSAgentAdminBanMessage;
if (!this.admin || !banMsg.data || !banMsg.data.username) return;
let _user = this.room.clients.find((c) => c.username === banMsg.data.username);
if (!_user) return;
let res: MSAgentErrorMessage = {
op: MSAgentProtocolMessageType.Error,
data: {
error: 'You have been banned.'
}
};
await this.room.db.banUser(_user.ip, _user.username!);
await _user.send(res);
_user.socket.close();
break;
}
}
break;
}
}
}
2024-07-12 21:20:37 -04:00
}
function validateUsername(username: string) {
2024-07-14 15:26:05 -04:00
return username.length >= 3 && username.length <= 20 && /^[a-zA-Z0-9\ \-\_\.]+$/.test(username);
}