Add ch ratelimit, readline commands, various debugging

This commit is contained in:
Hri7566 2024-01-23 01:19:51 -05:00
parent 52e21f77d6
commit 8da720f4c3
13 changed files with 1363 additions and 36 deletions

29
.eslintrc.js Normal file
View File

@ -0,0 +1,29 @@
module.exports = {
"env": {
"browser": true,
"es2021": true
},
"overrides": [
{
"env": {
"node": true
},
"files": [
".eslintrc.{js,cjs}"
],
"parserOptions": {
"sourceType": "script"
}
}
],
"parser": "@typescript-eslint/parser",
"parserOptions": {
"ecmaVersion": "latest",
"sourceType": "module"
},
"plugins": [
"@typescript-eslint"
],
"rules": {
}
}

BIN
bun.lockb

Binary file not shown.

View File

@ -2,11 +2,11 @@ user:
normal: normal:
a: 1500 a: 1500
m: 50 m: 50
ch: 1000
chains: chains:
userset: userset:
interval: 1800000, interval: 1800000,
num: 1000 num: 1000
crown: crown:
normal: normal:
a: 600 a: 600
@ -15,7 +15,6 @@ crown:
userset: userset:
interval: 1800000, interval: 1800000,
num: 1000 num: 1000
admin: admin:
normal: normal:
a: 120 a: 120

View File

@ -20,7 +20,11 @@
"zod": "^3.22.2" "zod": "^3.22.2"
}, },
"devDependencies": { "devDependencies": {
"@types/bun": "latest",
"@types/node": "^20.5.9", "@types/node": "^20.5.9",
"@typescript-eslint/eslint-plugin": "^6.19.1",
"@typescript-eslint/parser": "^6.19.1",
"eslint": "^8.56.0",
"prisma": "5.7.0", "prisma": "5.7.0",
"typescript": "^5.2.2" "typescript": "^5.2.2"
} }

1232
pnpm-lock.yaml Normal file

File diff suppressed because it is too large Load Diff

View File

@ -158,6 +158,10 @@ export class Channel extends EventEmitter {
this.logger.error(err); this.logger.error(err);
} }
}); });
this.on("command", (msg, socket) => {
console.log();
});
} }
/** /**
@ -316,7 +320,7 @@ export class Channel extends EventEmitter {
* @param socket Socket that is leaving * @param socket Socket that is leaving
*/ */
public leave(socket: Socket) { public leave(socket: Socket) {
// this.logger.debug("Leave called"); this.logger.debug("Leave called");
const part = socket.getParticipant() as Participant; const part = socket.getParticipant() as Participant;
let dupeCount = 0; let dupeCount = 0;
@ -328,7 +332,7 @@ export class Channel extends EventEmitter {
} }
} }
// this.logger.debug("Dupes:", dupeCount); this.logger.debug("Dupes:", dupeCount);
if (dupeCount == 1) { if (dupeCount == 1) {
const p = this.ppl.find(p => p.id == socket.getParticipantID()); const p = this.ppl.find(p => p.id == socket.getParticipantID());
@ -336,17 +340,17 @@ export class Channel extends EventEmitter {
if (p) { if (p) {
this.ppl.splice(this.ppl.indexOf(p), 1); this.ppl.splice(this.ppl.indexOf(p), 1);
} }
// Broadcast bye
this.sendArray([
{
m: "bye",
p: part.id
}
]);
this.emit("update", this);
} }
// Broadcast bye
this.sendArray([
{
m: "bye",
p: part.id
}
]);
this.emit("update", this);
} }
/** /**

View File

@ -4,10 +4,11 @@
* by Hri7566 * by Hri7566
*/ */
// import { app } from "./ws/server";
import "./ws/server"; import "./ws/server";
import { Logger } from "./util/Logger"; import { Logger } from "./util/Logger";
const logger = new Logger("Main"); const logger = new Logger("Main");
import "./util/readline"; import "./util/readline";
logger.info("Ready");

View File

@ -11,20 +11,66 @@ const logger = new Logger("CLI");
rl.setPrompt("mpps> "); rl.setPrompt("mpps> ");
rl.prompt(); rl.prompt();
rl.on("line", msg => { class Command {
// TODO readline commands public static commands: Command[] = [];
public static async handleCommand(line: string) {
const args = line.split(" ");
const cmd = args[0].toLowerCase();
if (msg == "mem" || msg == "memory") { let foundCommand: Command | undefined;
const mem = process.memoryUsage();
logger.info( for (const command of this.commands) {
`Memory: ${(mem.heapUsed / 1000 / 1000).toFixed(2)} MB used / ${( if (
mem.heapTotal / command.aliases.map(alias => alias.toLowerCase()).includes(cmd)
1000 / ) {
1000 foundCommand = command;
).toFixed(2)} MB total` }
); }
if (!foundCommand) return `No such command "${cmd}"`;
try {
const out = await foundCommand.callback({
a: line,
cmd,
args
});
if (out) return out;
} catch (err) {
return logger.error(err);
}
} }
public static addCommand(command: Command) {
this.commands.push(command);
}
constructor(
public aliases: string[],
public usage: string,
public callback: (msg: {
a: string;
cmd: string;
args: string[];
}) => Promise<void | string> | void | string
) {}
}
Command.addCommand(
new Command(["memory", "mem"], "mem", msg => {
const mem = process.memoryUsage();
return `Memory: ${(mem.heapUsed / 1000 / 1000).toFixed(2)} MB used / ${(
mem.heapTotal /
1000 /
1000
).toFixed(2)} MB total`;
})
);
rl.on("line", async line => {
const out = await Command.handleCommand(line);
logger.info(out);
rl.prompt(); rl.prompt();
}); });

View File

@ -55,7 +55,10 @@ export class Socket extends EventEmitter {
public currentChannelID: string | undefined; public currentChannelID: string | undefined;
private cursorPos: Vector2<CursorValue> = { x: 200, y: 100 }; private cursorPos: Vector2<CursorValue> = { x: 200, y: 100 };
constructor(private ws: ServerWebSocket<unknown>, public socketID: string) { constructor(
private ws: ServerWebSocket<unknown>,
public socketID: string
) {
super(); super();
this.ip = ws.remoteAddress; // Participant ID this.ip = ws.remoteAddress; // Participant ID
@ -81,7 +84,11 @@ export class Socket extends EventEmitter {
this.id = createID(); this.id = createID();
} else { } else {
// Use original session ID // Use original session ID
this.id = foundSocket.id; // this.id = foundSocket.id;
// Break us off
this.id = "broken";
this.destroy();
} }
(async () => { (async () => {

View File

@ -9,6 +9,7 @@ export interface RateLimitConfigList<
normal: { normal: {
m: RL; m: RL;
a: RL; a: RL;
ch: RL;
}; };
chains: { chains: {
@ -33,7 +34,8 @@ export const config = loadConfig<RateLimitsConfig>("config/ratelimits.yml", {
user: { user: {
normal: { normal: {
a: 6000 / 4, a: 6000 / 4,
m: 1000 / 20 m: 1000 / 20,
ch: 1000 / 1
}, },
chains: { chains: {
userset: { userset: {

View File

@ -5,7 +5,8 @@ import { RateLimitConstructorList, config } from "../config";
export const userLimits: RateLimitConstructorList = { export const userLimits: RateLimitConstructorList = {
normal: { normal: {
a: () => new RateLimit(config.user.normal.a), a: () => new RateLimit(config.user.normal.a),
m: () => new RateLimit(config.user.normal.m) m: () => new RateLimit(config.user.normal.m),
ch: () => new RateLimit(config.user.normal.ch)
}, },
chains: { chains: {
userset: () => userset: () =>

View File

@ -57,13 +57,15 @@ export const app = Bun.serve({
close: (ws, code, message) => { close: (ws, code, message) => {
// logger.debug("Close called"); // logger.debug("Close called");
const socket = (ws as unknown as any).socket as Socket; const socket = (ws as unknown as any).socket as Socket;
socket.destroy(); if (socket) {
socket.destroy();
for (const sockID of socketsBySocketID.keys()) { for (const sockID of socketsBySocketID.keys()) {
const sock = socketsBySocketID.get(sockID); const sock = socketsBySocketID.get(sockID);
if (sock == socket) { if (sock == socket) {
socketsBySocketID.delete(sockID); socketsBySocketID.delete(sockID);
}
} }
} }
} }

0
test/index.test.ts Normal file
View File