Compare commits

..

2 Commits

Author SHA1 Message Date
Hri7566 232922ce25 Make all handlers async 2024-07-31 20:08:01 -04:00
Hri7566 f314cfd17e Update README.md 2024-07-31 19:59:51 -04:00
19 changed files with 22 additions and 263 deletions

View File

@ -45,10 +45,10 @@ Brandon's server originally used MongoDB for storing user data, but there are to
## TODO
- Fully implement and test tags
- Redo all of the validations with Zod
- This probably means making Zod schemas for every single message type
- Also user and channel data
- Fully implement and test tags
- Test every frontend
- Test fishing bot

1
public

@ -1 +0,0 @@
Subproject commit ccc02f653a3eebeb2d64f894b978cc78a5c62c42

4
src/util/types.d.ts vendored
View File

@ -393,9 +393,11 @@ declare interface ClientEvents {
};
}
declare type ServerEventCallback<EventID extends keyof ServerEvents> = (msg: ServerEvents[EventID], socket: Socket) => Promise<void>;
declare type ServerEventListener<EventID extends keyof ServerEvents> = {
id: EventID;
callback: (msg: ServerEvents[EventID], socket: Socket) => void;
callback: ServerEventCallback<EventID>;
};
declare type Vector2<T = number> = {

View File

@ -2,7 +2,7 @@ import { ServerEventListener } from "../../../../util/types";
export const plus_ls: ServerEventListener<"+ls"> = {
id: "+ls",
callback: (msg, socket) => {
callback: async (msg, socket) => {
// Give us the latest news on literally everything
// that's happening in the server. In fact, I want
// to know when someone clicks a button instantly,

View File

@ -2,7 +2,7 @@ import { ServerEventListener } from "../../../../util/types";
export const minus_ls: ServerEventListener<"-ls"> = {
id: "-ls",
callback: (msg, socket) => {
callback: async (msg, socket) => {
// Stop giving us the latest server forecast
if (socket.rateLimits) {
if (!socket.rateLimits.normal["-ls"].attempt()) return;

View File

@ -2,7 +2,7 @@ import { ServerEventListener } from "../../../../util/types";
export const a: ServerEventListener<"a"> = {
id: "a",
callback: (msg, socket) => {
callback: async (msg, socket) => {
// Chat message
const flags = socket.getUserFlags();
if (!flags) return;

View File

@ -1,10 +1,11 @@
import { Logger } from "../../../../util/Logger";
import env from "../../../../util/env";
import { ServerEventListener } from "../../../../util/types";
export const admin_message: ServerEventListener<"admin message"> = {
id: "admin message",
callback: (msg, socket) => {
// Administrator control message
callback: async (msg, socket) => {
// Administrator control message (Brandonism)
if (socket.rateLimits)
if (!socket.rateLimits.normal["admin message"].attempt()) return;

View File

@ -2,7 +2,7 @@ import { ServerEventListener } from "../../../../util/types";
export const bye: ServerEventListener<"bye"> = {
id: "bye",
callback: (msg, socket) => {
callback: async (msg, socket) => {
// Leave server
if (socket.rateLimits)
if (!socket.rateLimits.normal.bye.attempt()) return;

View File

@ -2,7 +2,7 @@ import { ServerEventListener } from "../../../../util/types";
export const ch: ServerEventListener<"ch"> = {
id: "ch",
callback: (msg, socket) => {
callback: async (msg, socket) => {
// Switch channel
if (!socket.rateLimits?.normal.ch.attempt()) return;
if (typeof msg._id !== "string") return;

View File

@ -5,7 +5,7 @@ const logger = new Logger("chown");
export const chown: ServerEventListener<"chown"> = {
id: "chown",
callback: (msg, socket) => {
callback: async (msg, socket) => {
// Change channel ownership
if (socket.rateLimits)
if (!socket.rateLimits.normal["chown"].attempt()) return;

View File

@ -2,7 +2,7 @@ import { ServerEventListener } from "../../../../util/types";
export const chset: ServerEventListener<"chset"> = {
id: "chset",
callback: (msg, socket) => {
callback: async (msg, socket) => {
// Change channel settings
if (socket.rateLimits)
if (!socket.rateLimits.chains.chset.attempt()) return;

View File

@ -2,7 +2,7 @@ import { ServerEventListener } from "../../../../util/types";
export const devices: ServerEventListener<"devices"> = {
id: "devices",
callback: (msg, socket) => {
callback: async (msg, socket) => {
// List of MIDI Devices (or software ports, because nobody can tell the difference)
if (socket.rateLimits)
if (!socket.rateLimits.normal.devices.attempt()) return;

View File

@ -2,7 +2,7 @@ import { ServerEventListener } from "../../../../util/types";
export const kickban: ServerEventListener<"kickban"> = {
id: "kickban",
callback: (msg, socket) => {
callback: async (msg, socket) => {
// Kickbanning someone from channel
if (typeof msg._id !== "string") return;
if (typeof msg.ms !== "number") return;

View File

@ -2,7 +2,7 @@ import { ServerEventListener } from "../../../../util/types";
export const m: ServerEventListener<"m"> = {
id: "m",
callback: (msg, socket) => {
callback: async (msg, socket) => {
// Cursor movement
if (socket.rateLimits) {
if (!socket.rateLimits.normal.m.attempt()) return;

View File

@ -3,7 +3,7 @@ import { config } from "../../../usersConfig";
export const n: ServerEventListener<"n"> = {
id: "n",
callback: (msg, socket) => {
callback: async (msg, socket) => {
// Piano note
if (socket.rateLimits)
if (!socket.rateLimits.chains.n.attempt()) return;

View File

@ -2,7 +2,7 @@ import { ServerEventListener } from "../../../../util/types";
export const t: ServerEventListener<"t"> = {
id: "t",
callback: (msg, socket) => {
callback: async (msg, socket) => {
// Ping
if (socket.rateLimits)

View File

@ -2,12 +2,12 @@ import { ServerEventListener } from "../../../../util/types";
export const unban: ServerEventListener<"unban"> = {
id: "unban",
callback: (msg, socket) => {
callback: async (msg, socket) => {
// Kickbanning someone from channel
if (typeof msg._id !== "string") return;
if (socket.rateLimits)
if (!socket.rateLimits.normal.unban.attempt()) return;
if (!socket.rateLimits.normal.kickban.attempt()) return;
socket.unban(msg._id);
}

View File

@ -2,7 +2,7 @@ import { ServerEventListener } from "../../../../util/types";
export const userset: ServerEventListener<"userset"> = {
id: "userset",
callback: (msg, socket) => {
callback: async (msg, socket) => {
// Change username/color
if (!socket.rateLimits?.chains.userset.attempt()) return;
// You can disable color in the config because

243
top
View File

@ -1,243 +0,0 @@
diff --git a/src/channel/Channel.ts b/src/channel/Channel.ts
index e33ddc6..b6072be 100755
--- a/src/channel/Channel.ts
+++ b/src/channel/Channel.ts
@@ -17,7 +17,7 @@ import Crown from "./Crown";
import { ChannelList } from "./ChannelList";
import { config } from "./config";
import { config as usersConfig } from "../ws/usersConfig";
import { saveChatHistory, [-getChatHistory-]{+getChatHistory, deleteChatHistory+} } from "../data/history";
import { mixin, darken } from "../util/helpers";
import { User } from "@prisma/client";
import { heapStats } from "bun:jsc";
@@ -42,7 +42,18 @@ export class Channel extends EventEmitter {
public chatHistory = new Array<ClientEvents["a"]>();
private async loadChatHistory() {
{+try {+}
this.chatHistory = await getChatHistory(this.getID());
{+this.sendArray([{+}
{+ m: "c",+}
{+ c: this.chatHistory+}
{+ }]);+}
{+ } catch (err) { }+}
{+ }+}
{+ private async deleteChatHistory() {+}
{+ await deleteChatHistory(this.getID());+}
}
public logger: Logger;
@@ -97,10 +108,8 @@ export class Channel extends EventEmitter {
// ...and, possibly, an owner, too
if (creator) {
[- // if (this.crown.canBeSetBy(creator)) {-]
const part = creator.getParticipant();
if (part) [-this.giveCrown(part);-]
[- // }-]{+this.giveCrown(part, true, false);+}
}
} else {
this.settings = config.lobbySettings;
@@ -129,14 +138,13 @@ export class Channel extends EventEmitter {
this.loadChatHistory();
this.logger.info("Loaded chat history");
this.on("update", [-()-]{+(self, uuid)+} => {[-//this.logger.debug("-------- UPDATE START --------");-]
// Send updated info
for (const socket of socketsBySocketID.values()) {
for (const p of this.ppl) {
[-//if (socket.getParticipantID() == p.id) {-]{+const socketUUID = socket.getUUID();+}
if [-(p.uuids.includes(socket.getUUID()))-]{+(p.uuids.includes(socketUUID) && socketUUID !== uuid)+} {[-//this.logger.debug("sending to", socket.getUUID())-]
socket.sendChannelUpdate(
this.getInfo(),
this.getParticipantList()
@@ -276,6 +284,7 @@ export class Channel extends EventEmitter {
);
}
{+this.logger.debug("Update from user data update handler");+}
this.emit("update", this);
} catch (err) {
this.logger.error(err);
@@ -328,6 +337,7 @@ export class Channel extends EventEmitter {
// probably causes jank, but people can just reload their page or whatever
// not sure what to do about the URL situation
this._id = _id;
{+this.logger.debug("Update from setID");+}
this.emit("update", this);
}
@@ -407,6 +417,7 @@ export class Channel extends EventEmitter {
}
*/
{+this.logger.debug("Update from changeSettings");+}
this.emit("update", this);
}
@@ -486,7 +497,7 @@ export class Channel extends EventEmitter {
p.uuids.push(socket.getUUID())
}
[-socket.sendChannelUpdate(this.getInfo(),-]{+//socket.sendChannelUpdate(this.getInfo(),+} this.getParticipantList());
} else {
// Add them to the channel
hasChangedChannel = true;
@@ -534,15 +545,13 @@ export class Channel extends EventEmitter {
if (p) {
// Give the crown back
this.giveCrown(p, [-true);-]{+true, false);+}
}
}
}
}
}
[- // Send our state data back-]
[- // TODO Does this go above?-]
socket.sendArray([
// {
// m: "ch",
@@ -578,7 +587,9 @@ export class Channel extends EventEmitter {
part.id
);
// Broadcast a channel update so everyone subscribed to the channel list can see [-us-]{+the new user count+}
{+ //this.emit("update", this, socket.getUUID());+}
{+ this.logger.debug("Update from join");+}
this.emit("update", this);
//this.logger.debug("Settings:", this.settings);
@@ -625,6 +636,7 @@ export class Channel extends EventEmitter {
}
]);
{+this.logger.debug("Update from leave");+}
this.emit("update", this);
} else {
for (const p of this.ppl) {
@@ -789,6 +801,7 @@ export class Channel extends EventEmitter {
}
ChannelList.remove(this);
{+this.deleteChatHistory();+}
this.logger.info("Destroyed");
}
@@ -819,7 +832,7 @@ export class Channel extends EventEmitter {
* @param part Participant to give crown to
* @param force Whether or not to force-create a crown (useful for lobbies)
*/
public giveCrown(part: Participant, force = [-false)-]{+false, update = true)+} {
if (force) {
if (!this.crown) this.crown = new Crown();
}
@@ -828,7 +841,11 @@ export class Channel extends EventEmitter {
this.crown.userId = part._id;
this.crown.participantId = part.id;
this.crown.time = Date.now();
{+if (update) {+}
{+ this.logger.debug("Update from giveCrown");+}
this.emit("update", this);
{+}+}
}
}
@@ -865,6 +882,7 @@ export class Channel extends EventEmitter {
delete this.crown.participantId;
{+this.logger.debug("Update from dropCrown");+}
this.emit("update", this);
}
}
@@ -951,6 +969,7 @@ export class Channel extends EventEmitter {
}
if (shouldUpdate) {
{+this.logger.debug("Update from kickban");+}
this.emit("update", this);
if (typeof banner !== "undefined") {
@@ -1149,7 +1168,7 @@ export class Channel extends EventEmitter {
public printMemoryInChat() {
const mem = heapStats();
[-this.sendChatAdmin(`Size:-]{+this.sendChatAdmin(`Used:+} ${(mem.heapSize / 1000 / 1000).toFixed(2)}M / [-Capacity:-]{+Allocated:+} ${(mem.heapCapacity / 1000 / 1000).toFixed(2)}M`);
}
}
diff --git a/src/data/history.ts b/src/data/history.ts
index b769e27..0b68962 100755
--- a/src/data/history.ts
+++ b/src/data/history.ts
@@ -27,3 +27,7 @@ export async function getChatHistory(_id: string) {
return [];
}
}
{+export async function deleteChatHistory(_id: string) {+}
{+ await prisma.chatHistory.delete({ where: { id: _id } });+}
{+}+}
diff --git a/src/util/token.ts b/src/util/token.ts
index 54d22af..4e7bf6f 100644
--- a/src/util/token.ts
+++ b/src/util/token.ts
@@ -50,7 +50,7 @@ export async function createToken(userID: string, gateway: Gateway) {
let token = "";
if (config.tokenAuth == "uuid") {
token = {+userID + "." ++} crypto.randomUUID();
} else if (config.tokenAuth == "jwt") {
token = generateJWT(userID, gateway);
}
diff --git a/src/ws/events/user/handlers/hi.ts b/src/ws/events/user/handlers/hi.ts
index 91067b1..964c2cb 100755
--- a/src/ws/events/user/handlers/hi.ts
+++ b/src/ws/events/user/handlers/hi.ts
@@ -32,28 +32,30 @@ export const hi: ServerEventListener<"hi"> = {
let token: string | undefined;
let generatedToken = false;
if {+(config.tokenAuth !== "none") {+}
{+ if+} (typeof msg.token !== "string") {
// Get a saved token
token = await getToken(socket.getUserID());[-if (typeof token !== "string") {-]
[- // Generate a new one-]
[- token = await createToken(socket.getUserID(), socket.gateway);-]
if (typeof token !== "string") {
{+// Generate a new one+}
{+ token = await createToken(socket.getUserID(), socket.gateway);+}
{+ if (typeof token !== "string") {+}
logger.warn(`Unable to generate token for user ${socket.getUserID()}`);
} else {
generatedToken = true;
{+}+}
{+ }+}
{+ } else {+}
{+ // Validate the token+}
{+ const valid = await validateToken(socket.getUserID(), msg.token);+}
{+ if (!valid) {+}
{+ socket.ban(60000, "Invalid token");+}
{+ return;+}
}
[- }-]
[- } else {-]
[- // Validate the token-]
[- const valid = await validateToken(socket.getUserID(), msg.token);-]
[- if (!valid) {-]
[- socket.ban(60000, "Invalid token");-]
[- return;-]
[- }-]
token = msg.token;
{+}+}
}
let part = socket.getParticipant();