Files
collabvm-1.2.ts/src/User.ts

117 lines
4.2 KiB
TypeScript
Raw Normal View History

import * as Utilities from './Utilities.js';
import * as guacutils from './guacutils.js';
2023-01-31 22:00:30 -05:00
import {WebSocket} from 'ws';
import {IPData} from './IPData.js';
import IConfig from './IConfig.js';
import RateLimiter from './RateLimiter.js';
import { execaCommand } from 'execa';
2023-01-31 22:00:30 -05:00
export class User {
socket : WebSocket;
nopSendInterval : NodeJS.Timer;
msgRecieveInterval : NodeJS.Timer;
nopRecieveTimeout? : NodeJS.Timer;
username? : string;
connectedToNode : boolean;
rank : Rank;
msgsSent : number;
Config : IConfig;
IP : IPData;
2023-01-31 22:00:30 -05:00
// Rate limiters
ChatRateLimit : RateLimiter;
LoginRateLimit : RateLimiter;
RenameRateLimit : RateLimiter;
TurnRateLimit : RateLimiter;
constructor(ws : WebSocket, ip : IPData, config : IConfig, username? : string, node? : string) {
2023-01-31 22:00:30 -05:00
this.IP = ip;
this.connectedToNode = false;
this.Config = config;
this.socket = ws;
this.msgsSent = 0;
this.socket.on('close', () => {
clearInterval(this.nopSendInterval);
});
this.socket.on('message', (e) => {
clearTimeout(this.nopRecieveTimeout);
clearInterval(this.msgRecieveInterval);
this.msgRecieveInterval = setInterval(() => this.onNoMsg(), 10000);
})
this.nopSendInterval = setInterval(() => this.sendNop(), 5000);
this.msgRecieveInterval = setInterval(() => this.onNoMsg(), 10000);
this.sendNop();
if (username) this.username = username;
this.rank = 0;
this.ChatRateLimit = new RateLimiter(this.Config.collabvm.automute.messages, this.Config.collabvm.automute.seconds);
this.ChatRateLimit.on('limit', () => this.mute(false));
this.RenameRateLimit = new RateLimiter(4, 3);
this.RenameRateLimit.on('limit', () => this.closeConnection());
this.LoginRateLimit = new RateLimiter(4, 3);
this.LoginRateLimit.on('limit', () => this.closeConnection());
this.TurnRateLimit = new RateLimiter(5, 3);
this.TurnRateLimit.on('limit', () => this.closeConnection());
}
assignGuestName(existingUsers : string[]) : string {
var username;
do {
username = "guest" + Utilities.Randint(10000, 99999);
} while (existingUsers.indexOf(username) !== -1);
this.username = username;
return username;
}
sendNop() {
this.socket.send("3.nop;");
}
sendMsg(msg : string | Buffer) {
if (this.socket.readyState !== this.socket.OPEN) return;
clearInterval(this.nopSendInterval);
this.nopSendInterval = setInterval(() => this.sendNop(), 5000);
this.socket.send(msg);
}
private onNoMsg() {
this.sendNop();
this.nopRecieveTimeout = setTimeout(() => {
this.closeConnection();
}, 3000);
}
closeConnection() {
this.socket.send(guacutils.encode("disconnect"));
this.socket.close();
}
onMsgSent() {
if (!this.Config.collabvm.automute.enabled) return;
if (this.rank !== 0) return;
this.ChatRateLimit.request();
}
mute(permanent : boolean) {
this.IP.muted = true;
2023-01-31 22:00:30 -05:00
this.sendMsg(guacutils.encode("chat", "", `You have been muted${permanent ? "" : ` for ${this.Config.collabvm.tempMuteTime} seconds`}.`));
if (!permanent) {
clearTimeout(this.IP.tempMuteExpireTimeout);
this.IP.tempMuteExpireTimeout = setTimeout(() => this.unmute(), this.Config.collabvm.tempMuteTime * 1000);
2023-01-31 22:00:30 -05:00
}
}
unmute() {
clearTimeout(this.IP.tempMuteExpireTimeout);
this.IP.muted = false;
2023-01-31 22:00:30 -05:00
this.sendMsg(guacutils.encode("chat", "", "You are no longer muted."));
}
async ban() {
// Prevent the user from taking turns or chatting, in case the ban command takes a while
this.IP.muted = true;
//@ts-ignore
var cmd = this.Config.collabvm.bancmd.replace(/\$IP/g, this.IP).replace(/\$NAME/g, this.username);
await execaCommand(cmd);
this.kick();
}
async kick() {
2023-02-07 12:39:47 -05:00
this.sendMsg("10.disconnect;");
this.socket.close();
}
2023-01-31 22:00:30 -05:00
}
export enum Rank {
Unregistered = 0,
Admin = 2,
Moderator = 3,
}