From 9fcc3864c05df59cd585b950377cca26b5c9bd76 Mon Sep 17 00:00:00 2001 From: Hri7566 Date: Thu, 19 Sep 2024 11:51:10 -0400 Subject: [PATCH] Bugfixes --- src/channel/Channel.ts | 19 +++++++++++++++++++ src/event/behaviors.ts | 10 +++++----- src/ws/Gateway.ts | 2 +- src/ws/events/user/handlers/userset.ts | 17 ++++++++++++++--- src/ws/ratelimit/NoteQuota.ts | 5 ++--- src/ws/ratelimit/limits/admin.ts | 15 ++++++++++++--- src/ws/ratelimit/limits/crown.ts | 15 ++++++++++++--- src/ws/ratelimit/limits/user.ts | 15 ++++++++++++--- src/ws/server.ts | 2 +- 9 files changed, 78 insertions(+), 22 deletions(-) diff --git a/src/channel/Channel.ts b/src/channel/Channel.ts index 9feb534..c8d883f 100644 --- a/src/channel/Channel.ts +++ b/src/channel/Channel.ts @@ -149,6 +149,8 @@ export class Channel extends EventEmitter { // Copy default settings mixin(this.settings, config.defaultSettings); + if (owner_id) this.settings.owner_id = owner_id; + if (!this.isLobby()) { if (set) { // Copied from changeSettings below @@ -717,6 +719,9 @@ export class Channel extends EventEmitter { this.emit("update", this); //this.logger.debug("Settings:", this.settings); + if (this.settings.owner_id === part._id) { + this.giveCrown(part, true, true); + } } /** @@ -889,6 +894,20 @@ export class Channel extends EventEmitter { const part = socket.getParticipant(); if (!part) return; pianoPartID = part.id; + + const flags = socket.getUserFlags(); + + if (flags) { + // Is crownsolo on? + if ( + this.settings.crownsolo && + this.crown && + !(flags.admin || flags.mod) + ) { + // Do they have the crown? + if (part.id !== this.crown.participantId) return; + } + } } const clientMsg: OutgoingSocketEvents["n"] = { diff --git a/src/event/behaviors.ts b/src/event/behaviors.ts index a265a74..34119fe 100644 --- a/src/event/behaviors.ts +++ b/src/event/behaviors.ts @@ -1,6 +1,6 @@ import { ChannelList } from "~/channel/ChannelList"; import { bus } from "./bus"; -import type { ClientEvents, ServerEvents } from "~/util/types"; +import type { OutgoingSocketEvents, IncomingSocketEvents } from "~/util/types"; import { socketsByUUID, type Socket } from "~/ws/Socket"; export function loadBehaviors() { @@ -12,7 +12,7 @@ export function loadBehaviors() { bus.on("ls", () => {}); - bus.on("custom", (msg: ServerEvents["custom"], sender: Socket) => { + bus.on("custom", (msg: IncomingSocketEvents["custom"], sender: Socket) => { if (typeof msg !== "object") return; if (typeof msg.data === "undefined") return; if (typeof msg.target !== "object") return; @@ -52,7 +52,7 @@ export function loadBehaviors() { m: "custom", data: msg.data, p: sender.getUserID() - } as ClientEvents["custom"] + } as OutgoingSocketEvents["custom"] ]); } } else if (msg.target.mode === "ids") { @@ -65,7 +65,7 @@ export function loadBehaviors() { m: "custom", data: msg.data, p: sender.getUserID() - } as ClientEvents["custom"] + } as OutgoingSocketEvents["custom"] ]); } } else if (msg.target.mode === "subscribed") { @@ -74,7 +74,7 @@ export function loadBehaviors() { m: "custom", data: msg.data, p: sender.getUserID() - } as ClientEvents["custom"] + } as OutgoingSocketEvents["custom"] ]); } } diff --git a/src/ws/Gateway.ts b/src/ws/Gateway.ts index cb43908..00a86f5 100644 --- a/src/ws/Gateway.ts +++ b/src/ws/Gateway.ts @@ -30,7 +30,7 @@ export class Gateway { public isTokenValid = false; // implemented // Their user agent, if sent - public userAgent = ""; // TODO + public userAgent = ""; // partially implemented // Whether they have moved their cursor public hasCursorMoved = false; // implemented diff --git a/src/ws/events/user/handlers/userset.ts b/src/ws/events/user/handlers/userset.ts index 5555499..64e92d8 100644 --- a/src/ws/events/user/handlers/userset.ts +++ b/src/ws/events/user/handlers/userset.ts @@ -6,13 +6,24 @@ export const userset: ServerEventListener<"userset"> = { callback: async (msg, socket) => { // Change username/color if (!socket.rateLimits?.chains.userset.attempt()) return; - if (typeof msg.set.name !== "string" && typeof msg.set.color !== "string") return; + if ( + typeof msg.set.name !== "string" && + typeof msg.set.color !== "string" + ) + return; - if (typeof msg.set.name == "string") { + const user = socket.getUser(); + if (!user) return; + + if (typeof msg.set.name == "string" && msg.set.name !== user.name) { socket.gateway.hasChangedName = true; } - if (typeof msg.set.color == "string" && config.enableColorChanging) { + if ( + typeof msg.set.color == "string" && + msg.set.color !== user.color && + config.enableColorChanging + ) { socket.gateway.hasChangedColor = true; } diff --git a/src/ws/ratelimit/NoteQuota.ts b/src/ws/ratelimit/NoteQuota.ts index 78c7b6d..f2b3624 100644 --- a/src/ws/ratelimit/NoteQuota.ts +++ b/src/ws/ratelimit/NoteQuota.ts @@ -1,6 +1,5 @@ -// This is some convoluted dark magic I copied from some old mpp server I wrote -// No fucking clue where it came from or how it works internally, but I typedefized it -// It's just a bunch of rate limits in a chain... like a RateLimitChain...... hmmmm....... +// Replicated note quota class from client +// with types! export class NoteQuota { public allowance = 8000; public max = 24000; diff --git a/src/ws/ratelimit/limits/admin.ts b/src/ws/ratelimit/limits/admin.ts index 4a136ab..5735658 100644 --- a/src/ws/ratelimit/limits/admin.ts +++ b/src/ws/ratelimit/limits/admin.ts @@ -14,10 +14,14 @@ export const adminLimits: RateLimitConstructorList = { "-ls": () => new RateLimit(config.admin.normal["-ls"]), chown: () => new RateLimit(config.admin.normal.chown), + "+custom": () => new RateLimit(config.admin.normal["+custom"]), + "-custom": () => new RateLimit(config.admin.normal["-custom"]), + hi: () => new RateLimit(config.admin.normal.hi), bye: () => new RateLimit(config.admin.normal.bye), devices: () => new RateLimit(config.admin.normal.devices), - "admin message": () => new RateLimit(config.admin.normal["admin message"]) + "admin message": () => + new RateLimit(config.admin.normal["admin message"]) }, chains: { userset: () => @@ -28,12 +32,17 @@ export const adminLimits: RateLimitConstructorList = { chset: () => new RateLimitChain( config.admin.chains.chset.num, - config.admin.chains.userset.interval + config.admin.chains.chset.interval ), n: () => new RateLimitChain( config.admin.chains.n.num, - config.admin.chains.userset.interval + config.admin.chains.n.interval + ), + custom: () => + new RateLimitChain( + config.admin.chains.custom.num, + config.admin.chains.custom.interval ) } }; diff --git a/src/ws/ratelimit/limits/crown.ts b/src/ws/ratelimit/limits/crown.ts index c4a77ab..866f296 100644 --- a/src/ws/ratelimit/limits/crown.ts +++ b/src/ws/ratelimit/limits/crown.ts @@ -14,10 +14,14 @@ export const crownLimits: RateLimitConstructorList = { "-ls": () => new RateLimit(config.crown.normal["-ls"]), chown: () => new RateLimit(config.crown.normal.chown), + "+custom": () => new RateLimit(config.crown.normal["+custom"]), + "-custom": () => new RateLimit(config.crown.normal["-custom"]), + hi: () => new RateLimit(config.crown.normal.hi), bye: () => new RateLimit(config.crown.normal.bye), devices: () => new RateLimit(config.crown.normal.devices), - "admin message": () => new RateLimit(config.crown.normal["admin message"]) + "admin message": () => + new RateLimit(config.crown.normal["admin message"]) }, chains: { userset: () => @@ -28,12 +32,17 @@ export const crownLimits: RateLimitConstructorList = { chset: () => new RateLimitChain( config.crown.chains.chset.num, - config.crown.chains.userset.interval + config.crown.chains.chset.interval ), n: () => new RateLimitChain( config.crown.chains.n.num, - config.crown.chains.userset.interval + config.crown.chains.n.interval + ), + custom: () => + new RateLimitChain( + config.crown.chains.custom.num, + config.crown.chains.custom.interval ) } }; diff --git a/src/ws/ratelimit/limits/user.ts b/src/ws/ratelimit/limits/user.ts index 0b18cfd..c382fab 100644 --- a/src/ws/ratelimit/limits/user.ts +++ b/src/ws/ratelimit/limits/user.ts @@ -14,10 +14,14 @@ export const userLimits: RateLimitConstructorList = { "-ls": () => new RateLimit(config.user.normal["-ls"]), chown: () => new RateLimit(config.user.normal.chown), + "+custom": () => new RateLimit(config.user.normal["+custom"]), + "-custom": () => new RateLimit(config.user.normal["-custom"]), + hi: () => new RateLimit(config.user.normal.hi), bye: () => new RateLimit(config.user.normal.bye), devices: () => new RateLimit(config.user.normal.devices), - "admin message": () => new RateLimit(config.user.normal["admin message"]) + "admin message": () => + new RateLimit(config.user.normal["admin message"]) }, chains: { userset: () => @@ -28,12 +32,17 @@ export const userLimits: RateLimitConstructorList = { chset: () => new RateLimitChain( config.user.chains.chset.num, - config.user.chains.userset.interval + config.user.chains.chset.interval ), n: () => new RateLimitChain( config.user.chains.n.num, - config.user.chains.userset.interval + config.user.chains.n.interval + ), + custom: () => + new RateLimitChain( + config.user.chains.custom.num, + config.user.chains.custom.num ) } }; diff --git a/src/ws/server.ts b/src/ws/server.ts index 40953dc..abdba40 100644 --- a/src/ws/server.ts +++ b/src/ws/server.ts @@ -15,7 +15,7 @@ const logger = new Logger("WebSocket Server"); // ip -> timestamp // for checking if they visited the site and are also connected to the websocket -const httpIpCache = new Map(); +export const httpIpCache = new Map(); interface IFrontendConfig { topButtons: "original" | "none";