-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: Add NPS data handling and web request handling functions
- Loading branch information
Showing
6 changed files
with
296 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,119 @@ | ||
|
||
/** @type {Map<number, import("obsidian-main").IShardEntry>} */ | ||
const shards = new Map(); | ||
|
||
class ShardEntry { | ||
/** | ||
* @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: serverGroupName, | ||
mcotsServerIP: ip, | ||
diagServerIP: ip, | ||
})); | ||
} | ||
|
||
/** | ||
* @returns {string} | ||
*/ | ||
getShardList() { | ||
return Array.from(shards.values()) | ||
.map((entry) => entry.formatForWeb()) | ||
.join("\n\n"); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
/** @type {Array<{username: string, password: string, customerId: number}>} */ | ||
const users = [{ username: "admin", password: "admin", customerId: 1 }]; | ||
/** @type {Map<string, number>} */ | ||
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); | ||
} | ||
|
||
async deleteAllTokens() { | ||
return new Promise((resolve) => { | ||
tokens.clear(); | ||
console.log("All tokens deleted"); | ||
resolve(void 0); | ||
}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,92 @@ | ||
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); | ||
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 | ||
*/ | ||
export 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!"); | ||
} |