cap the amount of pings users are allowed to send per second

This commit is contained in:
Holly Stubbs 2024-04-20 17:20:13 +01:00
parent 98776062ca
commit 7014be3e25
Signed by: tgpholly
GPG key ID: B8583C4B7D18119E
2 changed files with 25 additions and 7 deletions

View file

@ -54,7 +54,8 @@ server.on("connection", (socket) => {
return; return;
} }
const username = reader.readShortString(); const username = reader.readShortString();
let page = reader.readString().toLowerCase().replace(".htm", "").replace(".html", ""); const rawURL = reader.readString();
let page = rawURL.toLowerCase().replace(".htm", "").replace(".html", "");
if (page === "index") { if (page === "index") {
page = ""; page = "";
} }
@ -70,7 +71,7 @@ server.on("connection", (socket) => {
for (const otherUser of usersOnPage) { for (const otherUser of usersOnPage) {
usersToSend.writeUInt(otherUser.id).writeShortString(otherUser.username).writeFloat(otherUser.cursorX).writeInt(otherUser.cursorY); usersToSend.writeUInt(otherUser.id).writeShortString(otherUser.username).writeFloat(otherUser.cursorX).writeInt(otherUser.cursorY);
} }
user = users.set(myUUID, new User(socket, username, page)); user = users.set(myUUID, new User(socket, username, page, rawURL));
sendToAllButSelf(user, createWriter(Endian.LE, 6 + username.length).writeByte(MessageType.ClientJoined).writeUInt(user.id).writeShortString(username).toBuffer()); sendToAllButSelf(user, createWriter(Endian.LE, 6 + username.length).writeByte(MessageType.ClientJoined).writeUInt(user.id).writeShortString(username).toBuffer());
user.send(usersToSend.toBuffer()); user.send(usersToSend.toBuffer());
break; break;
@ -86,10 +87,21 @@ server.on("connection", (socket) => {
} }
case MessageType.Ping: case MessageType.Ping:
{ {
const cursorX = reader.readFloat(); if (user === undefined) {
const cursorY = reader.readInt(); return;
const packet = createWriter(Endian.LE, 9).writeByte(MessageType.Ping).writeFloat(cursorX).writeInt(cursorY).toBuffer(); }
sendToAll(user, packet);
if ((Date.now() - user.lastPingReset) >= 1000) {
user.allowedPings = 10;
}
if (user.allowedPings > 0) {
user.allowedPings--;
const cursorX = reader.readFloat();
const cursorY = reader.readInt();
const packet = createWriter(Endian.LE, 9).writeByte(MessageType.Ping).writeFloat(cursorX).writeInt(cursorY).toBuffer();
sendToAll(user, packet);
}
break; break;
} }
} }

View file

@ -7,14 +7,20 @@ export default class User {
public readonly id:number; public readonly id:number;
public readonly username:string; public readonly username:string;
public readonly currentURL:string; public readonly currentURL:string;
public readonly rawURL:string = "";
public cursorX:number = 0; public cursorX:number = 0;
public cursorY:number = 0; public cursorY:number = 0;
public allowedPings:number;
public lastPingReset:number;
constructor(socket:WebSocket, username:string, currentURL:string) { constructor(socket:WebSocket, username:string, currentURL:string, rawURL:string) {
this.socket = socket; this.socket = socket;
this.id = User.USER_IDS++; this.id = User.USER_IDS++;
this.username = username; this.username = username;
this.currentURL = currentURL; this.currentURL = currentURL;
this.rawURL = rawURL;
this.allowedPings = 10;
this.lastPingReset = Date.now();
} }
send(data:Buffer) { send(data:Buffer) {