From a86f60657667350d28cfb27227942a90fa747b3a Mon Sep 17 00:00:00 2001 From: Molly Draven Date: Sun, 2 Jun 2024 08:39:18 -0400 Subject: [PATCH] chore: Remove unused files and code --- .gitignore | 2 + packages/main/src/MainLoop.js | 96 -------------- packages/main/src/NPSMessage.js | 47 ------- packages/main/src/NPSMessageHeader.js | 68 ---------- packages/main/src/NPSMessagePayload.js | 63 ---------- packages/main/src/NPSUserLoginPayload.js | 70 ----------- packages/main/src/ShardService.js | 133 -------------------- packages/main/src/TCPServer.js | 55 --------- packages/main/src/UserLoginService.js | 61 --------- packages/main/src/WebServer.js | 67 ---------- packages/main/src/handleUserLogin.js | 10 -- packages/main/src/index.js | 151 ----------------------- packages/main/src/nps.js | 28 ----- packages/main/src/payloadMap.js | 40 ------ packages/main/src/types.js | 2 - packages/main/src/web.js | 79 ------------ 16 files changed, 2 insertions(+), 970 deletions(-) delete mode 100644 packages/main/src/MainLoop.js delete mode 100644 packages/main/src/NPSMessage.js delete mode 100644 packages/main/src/NPSMessageHeader.js delete mode 100644 packages/main/src/NPSMessagePayload.js delete mode 100644 packages/main/src/NPSUserLoginPayload.js delete mode 100644 packages/main/src/ShardService.js delete mode 100644 packages/main/src/TCPServer.js delete mode 100644 packages/main/src/UserLoginService.js delete mode 100644 packages/main/src/WebServer.js delete mode 100644 packages/main/src/handleUserLogin.js delete mode 100644 packages/main/src/index.js delete mode 100644 packages/main/src/nps.js delete mode 100644 packages/main/src/payloadMap.js delete mode 100644 packages/main/src/types.js delete mode 100644 packages/main/src/web.js diff --git a/.gitignore b/.gitignore index c6bba59..ba83972 100644 --- a/.gitignore +++ b/.gitignore @@ -128,3 +128,5 @@ dist .yarn/build-state.yml .yarn/install-state.gz .pnp.* + +packages/*/*.js diff --git a/packages/main/src/MainLoop.js b/packages/main/src/MainLoop.js deleted file mode 100644 index 096110b..0000000 --- a/packages/main/src/MainLoop.js +++ /dev/null @@ -1,96 +0,0 @@ -// obsidian-spoon is a game server -// Copyright (C) 2024 Molly Crendraven -// This program is free software: you can redistribute it and/or modify -// it under the terms of the GNU General Public License as published by -// the Free Software Foundation, either version 3 of the License, or -// (at your option) any later version. -// This program is distributed in the hope that it will be useful, -// but WITHOUT ANY WARRANTY; without even the implied warranty of -// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -// GNU General Public License for more details. -// You should have received a copy of the GNU General Public License -// along with this program. If not, see . -import { emitKeypressEvents } from "node:readline"; -import { _atExit } from "obsidian-main"; -export class MainLoop { - /** @type {NodeJS.Timeout | undefined} */ - _timer = undefined; - /** @type {Array} */ - _startTasks = []; - /** @type {Array} */ - _stopTasks = []; - /** @type {Array} */ - _loopTasks = []; - /** - * - * @param {import("obsidian-main").KeypressEvent} key - */ - handleKeypressEvent(key) { - const keyString = key.sequence; - if (keyString === "x") { - this.stop(); - } - } - /** - * - * @param {"start" | "loop" | "stop"} type - * @param {import("obsidian-main").Task} task - */ - addTask(type, task) { - if (type === "start") { - this._startTasks.push(task); - } else if (type === "stop") { - this._stopTasks.push(task); - } else if (type === "loop") { - this._loopTasks.push(task); - } - } - /** - * @param {Array} tasks - */ - async _callTasks(tasks) { - tasks.forEach(async (task) => { - await task(); - }); - } - /** - * Starts the main loop. - * - */ - async start() { - this._timer = setTimeout(this.loop.bind(this), 1000); - if (process.stdin.isTTY !== true) { - return; - } - emitKeypressEvents(process.stdin); - process.stdin.setRawMode(true); - process.stdin.resume(); - console.log("Press X to exit"); - process.stdin.on("keypress", (str, key) => { - if (key !== undefined) { - this.handleKeypressEvent(key); - } - }); - await this._callTasks(this._startTasks); - } - /** - * Stops the main loop. - */ - async stop() { - if (this._timer !== undefined) { - clearInterval(this._timer); - process.stdin.setRawMode(false); - console.log("Exiting..."); - await this._callTasks(this._stopTasks); - _atExit(); - } - } - /** - * Body of the main loop. - */ - async loop() { - await this._callTasks(this._loopTasks); - this._timer = setTimeout(this.loop.bind(this), 1000); - } -} -//# sourceMappingURL=MainLoop.js.map diff --git a/packages/main/src/NPSMessage.js b/packages/main/src/NPSMessage.js deleted file mode 100644 index 65499b9..0000000 --- a/packages/main/src/NPSMessage.js +++ /dev/null @@ -1,47 +0,0 @@ -import { NPSMessageHeader } from "./NPSMessageHeader.js"; -import { NPSMessagePayload } from "./NPSMessagePayload.js"; -/** - * Class representing an NPS message. - * - * @property {NPSMessageHeader} _header - * @property {NPSMessagePayload} data - */ -export class NPSMessage { - _header; - data; - constructor() { - this._header = new NPSMessageHeader(); - this.data = new NPSMessagePayload(); - } - /** - * - * @param {Buffer} data - * @returns {NPSMessage} - */ - static parse(data) { - const self = new NPSMessage(); - if (data.length < 8) { - throw new Error(`Invalid message length: ${data.length}`); - } - self._header = NPSMessageHeader.parse(data); - const expectedLength = self._header.messageLength - self._header.dataOffset; - self.data = NPSMessagePayload.parse( - data.subarray(self._header.dataOffset), - expectedLength, - ); - return self; - } - /** - * @returns Buffer - */ - toBuffer() { - return Buffer.concat([this._header.toBuffer(), this.data.toBuffer()]); - } - /** - * @returns string - */ - toString() { - return `${this._header.toString()}, Data: ${this.data.toString()}`; - } -} -//# sourceMappingURL=NPSMessage.js.map diff --git a/packages/main/src/NPSMessageHeader.js b/packages/main/src/NPSMessageHeader.js deleted file mode 100644 index 4c40651..0000000 --- a/packages/main/src/NPSMessageHeader.js +++ /dev/null @@ -1,68 +0,0 @@ -/** - * Class representing an NPS message header. - */ -export class NPSMessageHeader { - _dataStart; - messageId; - messageLength; - version; - constructor() { - this._dataStart = -1; - this.messageId = -1; - this.messageLength = -1; - this.version = -1; - } - /** - * - * @param {Buffer} data - * @returns NPSMessageHeader - */ - static parse(data) { - const self = new NPSMessageHeader(); - if (data.length < 6) { - throw new Error("Invalid header length"); - } - self.messageId = data.readUInt16BE(0); - self.messageLength = data.readUInt16BE(2); - self.version = data.readUInt16BE(4); - if (self.version === 257) { - self._dataStart = 12; - } else { - self._dataStart = 6; - } - return self; - } - get dataOffset() { - return this._dataStart; - } - /** - * @private - * @returns Buffer - */ - _writeExtraData() { - const buffer = Buffer.alloc(6); - buffer.writeUInt16BE(0, 0); - buffer.writeUInt32BE(this.messageLength, 2); - return buffer; - } - /** - * @returns Buffer - */ - toBuffer() { - const buffer = Buffer.alloc(6); - buffer.writeUInt16BE(this.messageId, 0); - buffer.writeUInt16BE(this.messageLength, 2); - buffer.writeUInt16BE(this.version, 4); - if (this.version === 257) { - return Buffer.concat([buffer, this._writeExtraData()]); - } - return buffer; - } - /** - * @returns string - */ - toString() { - return `ID: ${this.messageId}, Length: ${this.messageLength}, Version: ${this.version}`; - } -} -//# sourceMappingURL=NPSMessageHeader.js.map diff --git a/packages/main/src/NPSMessagePayload.js b/packages/main/src/NPSMessagePayload.js deleted file mode 100644 index 5b49c50..0000000 --- a/packages/main/src/NPSMessagePayload.js +++ /dev/null @@ -1,63 +0,0 @@ -/** - * To be used as a base class for NPS message payloads. - * - * @implements {INPSPayload} - * @class - * @property {Buffer} data - * - * @example - * class MyPayload extends NPSMessagePayload { - * constructor() { - * super(); - * this.myProperty = 0; - * } - * - * static parse(data) { - * this.myProperty = data.readUInt32LE(0); - * } - * - * toBuffer() { - * const buffer = Buffer.alloc(4); - * buffer.writeUInt32LE(this.myProperty, 0); - * return buffer; - * } - * - * toString() { - * return `MyPayload: ${this.myProperty}`; - * } - * } - */ -export class NPSMessagePayload { - data; - constructor() { - this.data = Buffer.alloc(0); - } - /** - * - * @param {Buffer} data - * @returns NPSMessagePayload - */ - static parse(data, len = data.length) { - if (data.length !== len) { - throw new Error( - `Invalid payload length: ${data.length}, expected: ${len}`, - ); - } - const self = new NPSMessagePayload(); - self.data = data; - return self; - } - /** - * @returns Buffer - */ - toBuffer() { - return this.data; - } - /** - * @returns string - */ - toString() { - return this.data.toString("hex"); - } -} -//# sourceMappingURL=NPSMessagePayload.js.map diff --git a/packages/main/src/NPSUserLoginPayload.js b/packages/main/src/NPSUserLoginPayload.js deleted file mode 100644 index a7019b4..0000000 --- a/packages/main/src/NPSUserLoginPayload.js +++ /dev/null @@ -1,70 +0,0 @@ -import { NPSMessagePayload } from "./NPSMessagePayload.js"; -/** - * @typedef INPSPayload - * @type {import("./NPSMessagePayload.js").INPSPayload} - */ -/** - * @implements {INPSPayload} - * @extends {NPSMessagePayload} - * Payload for the NPSUserLogin message. - */ -export class NPSUserLoginPayload extends NPSMessagePayload { - ticket; - sessionKey; - gameId; - constructor() { - super(); - this.data = Buffer.alloc(0); - this.ticket = ""; - this.sessionKey = ""; - this.gameId = ""; - } - /** - * - * @param {number} len - * @param {Buffer} data - * @returns {NPSUserLoginPayload} - */ - static parse(data, len = data.length) { - if (data.length !== len) { - throw new Error( - `Invalid payload length: ${data.length}, expected: ${len}`, - ); - } - const self = new NPSUserLoginPayload(); - try { - let offset = 0; - let nextLen = data.readUInt16BE(0); - self.ticket = data.toString("utf8", 2, nextLen + 2); - offset = nextLen + 2; - offset += 2; // Skip one empty word - nextLen = data.readUInt16BE(offset); - self.sessionKey = data.toString("hex", offset + 2, offset + 2 + nextLen); - offset += nextLen + 2; - nextLen = data.readUInt16BE(offset); - self.gameId = data - .subarray(offset + 2, offset + 2 + nextLen) - .toString("utf8"); - } catch (error) { - if (!(error instanceof Error)) { - throw new Error(`Error parsing payload: ${error}`); - } - console.error(`Error parsing payload: ${error.message}`); - throw new Error(`Error parsing payload: ${error.message}`); - } - return self; - } - /** - * @returns {Buffer} - */ - toBuffer() { - throw new Error("Method not implemented."); - } - /** - * @returns {string} - */ - toString() { - return `Ticket: ${this.ticket}, SessionKey: ${this.sessionKey}, GameId: ${this.gameId}`; - } -} -//# sourceMappingURL=NPSUserLoginPayload.js.map diff --git a/packages/main/src/ShardService.js b/packages/main/src/ShardService.js deleted file mode 100644 index 3284f5a..0000000 --- a/packages/main/src/ShardService.js +++ /dev/null @@ -1,133 +0,0 @@ -/** @type {Map} */ -const shards = new Map(); -class ShardEntry { - id; - name; - description; - loginServerIP; - loginServerPort; - lobbyServerIP; - lobbyServerPort; - mcotsServerIP; - statusId; - statusReason; - serverGroupName; - population; - maxPersonasPerUser; - diagServerIP; - diagServerPort; - /** - * @param {object} values - * @param {number} values.id - * @param {string} values.name - * @param {string} values.description - * @param {string} values.loginServerIP - * @param {number} [values.loginServerPort] - * @param {string} values.lobbyServerIP - * @param {number} [values.lobbyServerPort] - * @param {string} values.mcotsServerIP - * @param {number} [values.statusId] - * @param {string} [values.statusReason] - * @param {string} values.serverGroupName - * @param {number} [values.population] - * @param {number} [values.maxPersonasPerUser] - * @param {string} values.diagServerIP - * @param {number} [values.diagServerPort] - */ - constructor({ - id, - name, - description, - loginServerIP, - loginServerPort = 8226, - lobbyServerIP, - lobbyServerPort = 7003, - mcotsServerIP, - statusId = 0, - statusReason = "", - serverGroupName: serverGroupsName, - population = 0, - maxPersonasPerUser = 1, - diagServerIP, - diagServerPort = 80, - }) { - this.id = id; - this.name = name; - this.description = description; - this.loginServerIP = loginServerIP; - this.loginServerPort = loginServerPort; - this.lobbyServerIP = lobbyServerIP; - this.lobbyServerPort = lobbyServerPort; - this.mcotsServerIP = mcotsServerIP; - this.statusId = statusId; - this.statusReason = statusReason; - this.serverGroupName = serverGroupsName; - this.population = population; - this.maxPersonasPerUser = maxPersonasPerUser; - this.diagServerIP = diagServerIP; - this.diagServerPort = diagServerPort; - } - /** - * - * @param {number} population - */ - setPopulation(population) { - this.population = population; - } - /** - * @returns {string} - */ - formatForWeb() { - return ( - `[${this.name}]\n` + - `\tDescription=${this.id}\n` + - `\tShardId=${this.id}\n` + - `\tLoginServerIP=${this.loginServerIP}\n` + - `\tLoginServerPort=${this.loginServerPort}\n` + - `\tLobbyServerIP=${this.lobbyServerIP}\n` + - `\tLobbyServerPort=${this.lobbyServerPort}\n` + - `\tMCOTSServerIP=${this.mcotsServerIP}\n` + - `\tStatusId=${this.statusId}\n` + - `\tStatus_Reason=${this.statusReason}\n` + - `\tServerGroup_Name=${this.serverGroupName}\n` + - `\tPopulation=${this.population}\n` + - `\tMaxPersonasPerUser=${this.maxPersonasPerUser}\n` + - `\tDiagnosticServerHost=${this.diagServerIP}\n` + - `\tDiagnosticServerPort=${this.diagServerPort}\n` - ); - } -} -export class ShardService { - /** - * - * @param {number} id - * @param {string} name - * @param {string} description - * @param {string} ip - * @param {string} serverGroupName - */ - addShard(id, name, description, ip, serverGroupName) { - shards.set( - id, - new ShardEntry({ - id, - name, - description, - loginServerIP: ip, - lobbyServerIP: ip, - serverGroupName, - mcotsServerIP: ip, - diagServerIP: ip, - }), - ); - } - /** - * @returns {string} - */ - getShardList() { - return Array.from(shards.values()) - .map((entry) => entry.formatForWeb()) - .join("\n\n"); - } -} -//# sourceMappingURL=ShardService.js.map diff --git a/packages/main/src/TCPServer.js b/packages/main/src/TCPServer.js deleted file mode 100644 index 1d8c2d5..0000000 --- a/packages/main/src/TCPServer.js +++ /dev/null @@ -1,55 +0,0 @@ -// obsidian-spoon is a game server -// Copyright (C) 2024 Molly Crendraven -// This program is free software: you can redistribute it and/or modify -// it under the terms of the GNU General Public License as published by -// the Free Software Foundation, either version 3 of the License, or -// (at your option) any later version. -// This program is distributed in the hope that it will be useful, -// but WITHOUT ANY WARRANTY; without even the implied warranty of -// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -// GNU General Public License for more details. -// You should have received a copy of the GNU General Public License -// along with this program. If not, see . -import net from "node:net"; -export class TCPServer { - port; - server; - /** - * - * @param {number} port - * @param {function(net.Server): void} onListening - * @param {function(net.Socket): void} onConnection - * @param {errorHandler} onServerError - */ - constructor(port, onListening, onConnection, onServerError) { - this.port = port; - this.server = net.createServer(onConnection); - this.server.on("error", onServerError); - this.server.on("listening", () => { - onListening(this.server); - }); - } - /** - * Start the server listening on the configured port. - */ - listen() { - this.server.listen(this.port); - } - /** - * - * @param {errorHandler} onError - * @returns {Promise} - */ - async close(onError) { - return new Promise((resolve, reject) => { - this.server.close((err) => { - if (err) { - onError(err); - reject(err); - } - resolve(); - }); - }); - } -} -//# sourceMappingURL=TCPServer.js.map diff --git a/packages/main/src/UserLoginService.js b/packages/main/src/UserLoginService.js deleted file mode 100644 index 4961ae5..0000000 --- a/packages/main/src/UserLoginService.js +++ /dev/null @@ -1,61 +0,0 @@ -/** @type {Array<{username: string, password: string, customerId: number}>} */ -const users = [{ username: "admin", password: "admin", customerId: 1 }]; -/** @type {Map} */ -const tokens = new Map(); -export class UserLoginService { - /** - * Returns the customer ID if the user is valid, otherwise -1. - * - * @param {string} username - * @param {string} password - * @returns {number} - */ - checkUser(username, password) { - const user = users.find( - (user) => user.username === username && user.password === password, - ); - return user ? user.customerId : -1; - } - /** - * Creates a token for the given customer ID. - * - * @param {number} customerId - * @returns {string} - */ - createToken(customerId) { - const token = crypto.randomUUID(); - tokens.set(token, customerId); - return token; - } - /** - * Checks if the token is valid and returns the customer ID. - * If the token is invalid, returns -1. - * - * @param {string} token - * @returns {number} - */ - checkToken(token) { - const customerId = tokens.get(token); - return customerId ?? -1; - } - /** - * Deletes the token. - * - * @param {string} token - */ - deleteToken(token) { - tokens.delete(token); - } - /** - * Deletes all tokens. - * @returns {Promise} - */ - async deleteAllTokens() { - return new Promise((resolve) => { - tokens.clear(); - console.log("All tokens deleted"); - resolve(); - }); - } -} -//# sourceMappingURL=UserLoginService.js.map diff --git a/packages/main/src/WebServer.js b/packages/main/src/WebServer.js deleted file mode 100644 index a6dd875..0000000 --- a/packages/main/src/WebServer.js +++ /dev/null @@ -1,67 +0,0 @@ -// obsidian-spoon is a game server -// Copyright (C) 2024 Molly Crendraven -// This program is free software: you can redistribute it and/or modify -// it under the terms of the GNU General Public License as published by -// the Free Software Foundation, either version 3 of the License, or -// (at your option) any later version. -// This program is distributed in the hope that it will be useful, -// but WITHOUT ANY WARRANTY; without even the implied warranty of -// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -// GNU General Public License for more details. -// You should have received a copy of the GNU General Public License -// along with this program. If not, see . -import * as Sentry from "@sentry/node"; -import http from "node:http"; -import express from "express"; -/** @typedef connectionHandler - * @type {function(NodeJS.Socket): void} - */ -/** @typedef errorHandler - * @type {function(Error): void} - */ -export class WebServer { - port; - server; - /** - * - * @param {number} port - * @param {function(http.Server): void} onListening - * @param {function(http.IncomingMessage, http.ServerResponse): void} onConnection - * @param {errorHandler} onServerError - */ - constructor(port, onListening, onConnection, onServerError) { - this.port = port; - const app = express(); - Sentry.setupExpressErrorHandler(app); - app.use(onConnection); - /** @type {http.Server} */ - this.server = http.createServer(app); - this.server.on("error", onServerError); - this.server.on("listening", () => { - onListening(this.server); - }); - } - /** - * Start the server listening on the configured port. - */ - listen() { - this.server.listen(this.port); - } - /** - * - * @param {errorHandler} onError - * @returns {Promise} - */ - async close(onError) { - return new Promise((resolve, reject) => { - this.server.close((err) => { - if (err) { - onError(err); - reject(err); - } - resolve(); - }); - }); - } -} -//# sourceMappingURL=WebServer.js.map diff --git a/packages/main/src/handleUserLogin.js b/packages/main/src/handleUserLogin.js deleted file mode 100644 index 46d0e96..0000000 --- a/packages/main/src/handleUserLogin.js +++ /dev/null @@ -1,10 +0,0 @@ -/** - * - * @param {import("./NPSUserLoginPayload.js").NPSUserLoginPayload} payload - * @param {TClientCallback} clientCallback - */ -export function handleUserLogin(payload, clientCallback) { - const userLoginPayload = payload; - console.log(`User login: ${userLoginPayload.toString()}`); -} -//# sourceMappingURL=handleUserLogin.js.map diff --git a/packages/main/src/index.js b/packages/main/src/index.js deleted file mode 100644 index 574466e..0000000 --- a/packages/main/src/index.js +++ /dev/null @@ -1,151 +0,0 @@ -// probable-spoon is a game server -// Copyright (C) 2024 Molly Crendraven -// This program is free software: you can redistribute it and/or modify -// it under the terms of the GNU General Public License as published by -// the Free Software Foundation, either version 3 of the License, or -// (at your option) any later version. -// This program is distributed in the hope that it will be useful, -// but WITHOUT ANY WARRANTY; without even the implied warranty of -// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -// GNU General Public License for more details. -// You should have received a copy of the GNU General Public License -// along with this program. If not, see . -import { MainLoop } from "./MainLoop.js"; -import { ShardService } from "./ShardService.js"; -import { TCPServer } from "./TCPServer.js"; -import { UserLoginService } from "./UserLoginService.js"; -import { WebServer } from "./WebServer.js"; -import { onNPSData } from "./nps.js"; -import { onWebRequest } from "./web.js"; -import * as crypto from "node:crypto"; -import * as Sentry from "@sentry/node"; -/** - * @param {import("node:net").Socket} socket - * @param {TOnDataHandler} onData - */ -function onSocketConnection(socket, onData) { - console.log("Connection established"); - const connectionId = crypto.randomUUID(); - Sentry.setTag("connection_id", connectionId); - /** - * Callback for sending data to the client. - * @param {Buffer} data - */ - const sendToClient = (data) => { - socket.write(data); - }; - socket.on("data", (data) => { - onData(socket.localPort ?? -1, data, sendToClient); - }); -} -/** - * - * @param {Error} err - */ -function onServerError(err) { - console.error(`Server error: ${err.message}`); -} -/** - * - * @param {Error | undefined} err - */ -function onClose(err) { - if (err) { - console.error(`Server close error: ${err.message}`); - } - console.log("Server closed"); -} -/** - * - * @param {import("net").Server} s - * @returns string - */ -function getPort(s) { - const address = s.address(); - if (address === null || typeof address === "string") { - return String(address); - } - return String(address.port); -} -/** - * @param {import("node:http").Server} s - */ -function onWebListening(s) { - const port = getPort(s); - console.log(`Web server listening on port ${port}`); - s.on("close", () => { - console.log(`Server on port ${port} closed`); - }); -} -/** - * @param {import("net").Server} s - */ -function onSocketListening(s) { - const port = getPort(s); - console.log(`Server listening on port ${port}`); - s.on("close", () => { - console.log(`Server on port ${port} closed`); - }); - s.on("error", (err) => { - console.error(`Server on port ${port} errored: ${err.message}`); - }); -} -/** - * - * @param {number} exitCode - */ -async function _atExit(exitCode = 0) { - console.log("Goodbye, world!"); - process.exit(exitCode); -} -// === MAIN === -function main() { - process.on("exit", (/** @type {number} **/ code) => { - console.log(`Server exited with code ${code}`); - }); - console.log("Starting obsidian..."); - const authServer = new WebServer( - 3000, - onWebListening, - onWebRequest, - onServerError, - ); - const loginServer = new TCPServer( - 8226, - onSocketListening, - (socket) => onSocketConnection(socket, onNPSData), - onServerError, - ); - const personaServer = new TCPServer( - 8228, - onSocketListening, - (socket) => onSocketConnection(socket, onNPSData), - onServerError, - ); - const shardService = new ShardService(); - shardService.addShard( - 1, - "Rusty Motors", - "A test shard", - "10.10.5.20", - "Group - 1", - ); - const userLoginService = new UserLoginService(); - const mainLoop = new MainLoop(); - mainLoop.addTask("start", authServer.listen.bind(authServer)); - mainLoop.addTask("start", loginServer.listen.bind(loginServer)); - mainLoop.addTask("start", personaServer.listen.bind(personaServer)); - mainLoop.addTask("stop", authServer.close.bind(authServer, onClose)); - mainLoop.addTask("stop", loginServer.close.bind(loginServer, onServerError)); - mainLoop.addTask( - "stop", - personaServer.close.bind(personaServer, onServerError), - ); - mainLoop.addTask( - "stop", - userLoginService.deleteAllTokens.bind(userLoginService), - ); - mainLoop.start(); -} -export { main, _atExit, onServerError }; -//# sourceMappingURL=index.js.map diff --git a/packages/main/src/nps.js b/packages/main/src/nps.js deleted file mode 100644 index 2b5e455..0000000 --- a/packages/main/src/nps.js +++ /dev/null @@ -1,28 +0,0 @@ -import { NPSMessage } from "./NPSMessage.js"; -import { getPayloadHandler, getPayloadParser } from "./payloadMap.js"; -/** - * @param {number} port - * @param {Buffer} data - * @param {(data: Buffer) => void} sendToClient - */ -function onNPSData(port, data, sendToClient) { - const message = NPSMessage.parse(data); - console.log(`Received message on port ${port}: ${message.toString()}`); - const messageType = getPayloadParser(message._header.messageId); - if (!messageType) { - console.error(`Unknown message type: ${message._header.messageId}`); - return; - } - const payload = messageType( - message.data.data, - message._header.messageLength - message._header.dataOffset, - ); - const handler = getPayloadHandler(message._header.messageId); - if (!handler) { - console.error(`Unknown message type: ${message._header.messageId}`); - return; - } - handler(payload, sendToClient); -} -export { onNPSData }; -//# sourceMappingURL=nps.js.map diff --git a/packages/main/src/payloadMap.js b/packages/main/src/payloadMap.js deleted file mode 100644 index d5861e4..0000000 --- a/packages/main/src/payloadMap.js +++ /dev/null @@ -1,40 +0,0 @@ -import { NPSUserLoginPayload } from "./NPSUserLoginPayload.js"; -import { handleUserLogin } from "./handleUserLogin.js"; -/** - * @typedef INPSPayload - * @type {import("./NPSMessagePayload.js").INPSPayload} - */ -/** @type {Map INPSPayload>} */ -const payloadParserMap = new Map(); -payloadParserMap.set(1281, NPSUserLoginPayload.parse); -/** @type {Map void) => void) | undefined>} */ -const payloadHandlerMap = new Map(); -payloadHandlerMap.set(1281, handleUserLogin); -/** - * - * @param {number} messageId - * @returns {((data: Buffer, len: number) => INPSPayload) | undefined} - */ -function getPayloadParser(messageId) { - const payloadParser = payloadParserMap.get(messageId); - if (!payloadParser) { - console.error(`Unknown message type: ${messageId}, no parser found`); - return; - } - return payloadParser; -} -/** - * - * @param {number} messageId - * @returns {((payload: INPSPayload, clientCallback: (data: Buffer) => void) => void) | undefined} - */ -function getPayloadHandler(messageId) { - const payloadHandler = payloadHandlerMap.get(messageId); - if (!payloadHandler) { - console.error(`Unknown message type: ${messageId}, no handler found`); - return; - } - return payloadHandler; -} -export { getPayloadParser, getPayloadHandler }; -//# sourceMappingURL=payloadMap.js.map diff --git a/packages/main/src/types.js b/packages/main/src/types.js deleted file mode 100644 index cd268e9..0000000 --- a/packages/main/src/types.js +++ /dev/null @@ -1,2 +0,0 @@ -export {}; -//# sourceMappingURL=types.js.map diff --git a/packages/main/src/web.js b/packages/main/src/web.js deleted file mode 100644 index a26a1cc..0000000 --- a/packages/main/src/web.js +++ /dev/null @@ -1,79 +0,0 @@ -import { ShardService } from "./ShardService.js"; -import { UserLoginService } from "./UserLoginService.js"; -/** - * - * @param {import("node:http").ServerResponse} res - * @param {string} ticket - */ -function sendTicket(res, ticket) { - res.statusCode = 200; - res.setHeader("Content-Type", "text/plain"); - res.end(`Valid=TRUE\nTicket=${ticket}`); -} -/** - * - * @param {import("node:http").ServerResponse} res - * @param {number} statusCode - * @param {string} message - */ -function sendError(res, statusCode, message) { - res.statusCode = statusCode; - res.setHeader("Content-Type", "text/plain"); - res.end( - `reasoncode=INV-200\nreasontext=${message}\nreasonurl=https://rusty-motors.com`, - ); -} -/** - * @param {import("node:http").IncomingMessage} req - * @param {import("node:http").ServerResponse} res - */ -function homePage(req, res) { - res.end("Hello, world!"); -} -/** - * @param {import("node:http").IncomingMessage} req - * @param {import("node:http").ServerResponse} res - * @param {string} username - * @param {string} password - */ -function authLogin(req, res, username, password) { - const userLoginService = new UserLoginService(); - const customerId = userLoginService.checkUser(username, password); - if (customerId === -1) { - return sendError(res, 401, "Invalid username or password"); - } - const token = userLoginService.createToken(customerId); - return sendTicket(res, token); -} -/** - * @param {import("node:http").IncomingMessage} req - * @param {import("node:http").ServerResponse} res - */ -function getShardList(req, res) { - const shardService = new ShardService(); - res.statusCode = 200; - res.setHeader("Content-Type", "text/plain"); - res.end(shardService.getShardList()); -} -/** - * @param {import("node:http").IncomingMessage} req - * @param {import("node:http").ServerResponse} res - */ -function onWebRequest(req, res) { - console.log(`Request URL: ${req.url}`); - const url = new URL(`http://${process.env.HOST ?? "localhost"}${req.url}`); - if (url.pathname === "/") { - return homePage(req, res); - } - if (url.pathname === "/AuthLogin") { - const username = url.searchParams.get("username") ?? ""; - const password = url.searchParams.get("password") ?? ""; - return authLogin(req, res, username, password); - } - if (url.pathname === "/ShardList/") { - return getShardList(req, res); - } - res.end("Hello, world!"); -} -export { onWebRequest }; -//# sourceMappingURL=web.js.map