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:
a: 1500
m: 50
ch: 1000
chains:
userset:
interval: 1800000,
num: 1000
crown:
normal:
a: 600
@ -15,7 +15,6 @@ crown:
userset:
interval: 1800000,
num: 1000
admin:
normal:
a: 120

View File

@ -20,7 +20,11 @@
"zod": "^3.22.2"
},
"devDependencies": {
"@types/bun": "latest",
"@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",
"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.on("command", (msg, socket) => {
console.log();
});
}
/**
@ -316,7 +320,7 @@ export class Channel extends EventEmitter {
* @param socket Socket that is leaving
*/
public leave(socket: Socket) {
// this.logger.debug("Leave called");
this.logger.debug("Leave called");
const part = socket.getParticipant() as Participant;
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) {
const p = this.ppl.find(p => p.id == socket.getParticipantID());
@ -336,7 +340,6 @@ export class Channel extends EventEmitter {
if (p) {
this.ppl.splice(this.ppl.indexOf(p), 1);
}
}
// Broadcast bye
this.sendArray([
@ -348,6 +351,7 @@ export class Channel extends EventEmitter {
this.emit("update", this);
}
}
/**
* Determine whether this channel has too many users

View File

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

View File

@ -11,20 +11,66 @@ const logger = new Logger("CLI");
rl.setPrompt("mpps> ");
rl.prompt();
rl.on("line", msg => {
// TODO readline commands
class Command {
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;
for (const command of this.commands) {
if (
command.aliases.map(alias => alias.toLowerCase()).includes(cmd)
) {
foundCommand = command;
}
}
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();
logger.info(
`Memory: ${(mem.heapUsed / 1000 / 1000).toFixed(2)} MB used / ${(
return `Memory: ${(mem.heapUsed / 1000 / 1000).toFixed(2)} MB used / ${(
mem.heapTotal /
1000 /
1000
).toFixed(2)} MB total`
);
}
).toFixed(2)} MB total`;
})
);
rl.on("line", async line => {
const out = await Command.handleCommand(line);
logger.info(out);
rl.prompt();
});

View File

@ -55,7 +55,10 @@ export class Socket extends EventEmitter {
public currentChannelID: string | undefined;
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();
this.ip = ws.remoteAddress; // Participant ID
@ -81,7 +84,11 @@ export class Socket extends EventEmitter {
this.id = createID();
} else {
// Use original session ID
this.id = foundSocket.id;
// this.id = foundSocket.id;
// Break us off
this.id = "broken";
this.destroy();
}
(async () => {

View File

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

View File

@ -5,7 +5,8 @@ import { RateLimitConstructorList, config } from "../config";
export const userLimits: RateLimitConstructorList = {
normal: {
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: {
userset: () =>

View File

@ -57,6 +57,7 @@ export const app = Bun.serve({
close: (ws, code, message) => {
// logger.debug("Close called");
const socket = (ws as unknown as any).socket as Socket;
if (socket) {
socket.destroy();
for (const sockID of socketsBySocketID.keys()) {
@ -68,6 +69,7 @@ export const app = Bun.serve({
}
}
}
}
});
logger.info("Listening on port", env.PORT);

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