From ea011422f281f5207808af20af38e704019ce076 Mon Sep 17 00:00:00 2001 From: sophie Date: Fri, 19 Jul 2024 05:14:39 +0300 Subject: [PATCH] fix fuck tonne bug --- src/Client.ts | 16 ---------------- src/Room.ts | 25 ++++++++++++++++++++----- src/Server.ts | 25 +++++++++++-------------- 3 files changed, 31 insertions(+), 35 deletions(-) diff --git a/src/Client.ts b/src/Client.ts index 399d52b..d4bd747 100644 --- a/src/Client.ts +++ b/src/Client.ts @@ -17,22 +17,6 @@ export class Client { .join(""); this.uniqWsID = crypto.randomUUID(); } - sendWelcome() { - const profile = Server.profiles.get(this.id) - if(!profile) return; - this.send(new proto.ServerMessage({ - event: proto.ServerMessage_EventType.WELCOME, - welcome: { - id: this.id, - name: profile.name, - color: profile.color, - room: this.room!.name, - owner: this.room!.owner, - chat: this.room!.chats, - role: profile.role, - }, - })); - } send(message: proto.ServerMessage) { this.ws.send(message.toBinary()); } diff --git a/src/Room.ts b/src/Room.ts index c909c5d..4c783ea 100644 --- a/src/Room.ts +++ b/src/Room.ts @@ -26,10 +26,15 @@ export class Room { } this.systemMessage("This is a development server. Instability may occur. Written by @fucksophie") } + + private addToChatArray(chat: proto.ServerMessage_Chat) { + this.chats.push(chat); + if (this.chats.length > 150) this.chats.shift(); + } + systemMessage(message: string) { const system = Server.systemMessage(message); - this.chats.push(system.chat!); - + this.addToChatArray(system.chat!); this.particpiants.forEach((z) => { z.clients.forEach(g => { g.send(system); @@ -54,7 +59,7 @@ export class Room { z.send(chatPacket); }); - this.chats.push(chatPacket.chat!); + this.addToChatArray(chatPacket.chat!); } ban(ws: ServerWebSocket, minutes: number) { @@ -86,8 +91,18 @@ export class Room { particpiant.addClient(ws.data); } - ws.data.sendWelcome(); - + ws.data.send(new proto.ServerMessage({ + event: proto.ServerMessage_EventType.WELCOME, + welcome: { + id: ws.data.id, + name: particpiant.profile.name, + color: particpiant.profile.color, + room: ws.data.room!.name, + owner: ws.data.room!.owner, + chat: ws.data.room!.chats, + role: particpiant.profile.role, + }, + })); if (particpiant.clients.length <= 1) { //#region Annouce to everyone that the user has joined const joinMessage = new proto.ServerMessage({ diff --git a/src/Server.ts b/src/Server.ts index 4f119d1..1ccaca0 100644 --- a/src/Server.ts +++ b/src/Server.ts @@ -42,10 +42,12 @@ export class Server { // #region WS upgrading fetch(req, server) { let data; - if(process.env.TRUST_PROXY) { - data = req.headers.get("X-Forwarded-For")?.split(",")[0]?.trim() - if(!data) { - console.log('Trust proxy is enabled, but XFF is empty. Spoofing / server issue?') + if (process.env.TRUST_PROXY) { + data = req.headers.get("X-Forwarded-For")?.split(",")[0]?.trim(); + if (!data) { + console.log( + "Trust proxy is enabled, but XFF is empty. Spoofing / server issue?" + ); } } if (server.upgrade(req, { data })) { @@ -125,9 +127,7 @@ export class Server { profile.commit(); profile.emit("update"); Server.wses.forEach((z) => { - if (z.data.id == profile.id) { - z?.data?.sendWelcome(); - } + if (z.data.id == profile.id) z.close(6969); }); return; } @@ -162,9 +162,7 @@ export class Server { profile.commit(); profile.emit("update"); Server.wses.forEach((z) => { - if (z.data.id == profile.id) { - z?.data?.sendWelcome(); - } + if (z.data.id == profile.id) z.close(6969); }); ws.data.send( Server.systemMessage( @@ -182,9 +180,7 @@ export class Server { profile.commit(); profile.emit("update"); Server.wses.forEach((z) => { - if (z.data.id == profile.id) { - z?.data?.sendWelcome(); - } + if (z.data.id == profile.id) z.close(6969); }); ws.data.send( @@ -202,6 +198,7 @@ export class Server { if (!ws.data.room) return; const profile = Server.profiles.get(ws.data.id); if (!profile) return; + if(!ws.data.room.owner) return; if (profile.role !== proto.Role.DEVELOPER) { if (ws.data.room.owner != ws.data.id) return; } @@ -305,7 +302,7 @@ export class Server { }, open(ws: ServerWebSocket) { let ip: string = ws.remoteAddress; - if(process.env.TRUST_PROXY) ip = ws.data as unknown as string; + if (process.env.TRUST_PROXY) ip = ws.data as unknown as string; const client = new Client(ws, ip); if (getSiteBan(client.id)) {