From 18be6c6fd430eb25853b7aff9a54f65c74cb8f27 Mon Sep 17 00:00:00 2001 From: Maxwell Jeffress Date: Mon, 28 Oct 2024 18:07:37 +1100 Subject: [PATCH] Lay the groundwork for some new features --- server/src/main/kotlin/Main.kt | 129 ++++++++++++++++----------------- 1 file changed, 64 insertions(+), 65 deletions(-) diff --git a/server/src/main/kotlin/Main.kt b/server/src/main/kotlin/Main.kt index 0c117bf..f7d4b9b 100644 --- a/server/src/main/kotlin/Main.kt +++ b/server/src/main/kotlin/Main.kt @@ -7,41 +7,62 @@ import java.util.UUID import java.io.File import java.io.BufferedReader -/* -object WsSessionManager { - val sessions = ConcurrentHashMap() - fun addSession(sessionID: String, ctx: WsContext) { - sessions[sessionID] = ctx - } - fun removeSession(sessionID: String) { - sessions.remove(sessionID) - } - fun broadcast(message: String) { - sessions.values.forEach { ctx -> - ctx.send(message) - } - } + +import java.math.BigInteger +import java.security.MessageDigest + +fun md5(input:String): String { + val md = MessageDigest.getInstance("MD5") + return BigInteger(1, md.digest(input.toByteArray())).toString(16).padStart(32, '0') } -*/ + object WsSessionManager { - private val sessions = ConcurrentHashMap() + private val sessions = ConcurrentHashMap() + private val sessionIds = ConcurrentHashMap() fun addSession(ctx: WsContext) { - // Generate our own UUID for the session since we can't access Javalin's private sessionId - val sessionId = UUID.randomUUID().toString() - sessions[sessionId] = ctx + try { + val sessionId = UUID.randomUUID().toString() + sessions[ctx] = sessionId + sessionIds[sessionId] = ctx + } catch (e: Exception) { + println("Error adding session: ${e.message}") + } } fun removeSession(ctx: WsContext) { - // Find and remove the session by context - sessions.entries.removeIf { it.value === ctx } + try { + val sessionId = sessions[ctx] + if (sessionId != null) { + sessions.remove(ctx) + sessionIds.remove(sessionId) + } + } catch (e: Exception) { + println("Error removing session: ${e.message}") + } } fun broadcast(message: String) { - sessions.values.forEach { ctx -> - ctx.send(message) + val deadSessions = mutableListOf() + + sessions.keys.forEach { ctx -> + try { + if (ctx.session.isOpen) { + ctx.send(message) + } else { + deadSessions.add(ctx) + } + } catch (e: Exception) { + println("Error broadcasting to session: ${e.message}") + deadSessions.add(ctx) + } } + + // Clean up any dead sessions + deadSessions.forEach { removeSession(it) } } + + fun getSessionCount(): Int = sessions.size } fun extractMessageContent(inputData: String): String { @@ -79,6 +100,7 @@ fun handleSentMessage(inputData: String): String { var token = "" var message = "" var dataType = "" + var command = "" var isParsingData = 0 for (char in inputData) { val character = char @@ -95,6 +117,8 @@ fun handleSentMessage(inputData: String): String { token += character } else if (dataType == "message") { message += character + } else if (dataType == "command") { + command += character } } } else { @@ -120,6 +144,7 @@ fun handleSentMessage(inputData: String): String { var usernameInDatabase = "" var tokenInDatabase = "" + var saltInDatabase = "" var currentStage = 0 for (char in userLine) { if (char == ':') { @@ -129,10 +154,17 @@ fun handleSentMessage(inputData: String): String { usernameInDatabase += char } else if (currentStage == 1) { tokenInDatabase += char + } else if (currentStage == 2) { + saltInDatabase += char } } tokenInDatabase = tokenInDatabase.replace(":", "") - if (token != tokenInDatabase) { + saltInDatabase = saltInDatabase.replace(":", "") + val tokenWithSalt = (md5(token + saltInDatabase)) + /*println(saltInDatabase) + println(tokenWithSalt) + if (tokenWithSalt != tokenInDatabase) {*/ + if (token != tokenInDatabase) { return("Invalid token! Please try putting in your password right") } // Make the message to respond to the client @@ -359,6 +391,7 @@ fun authKey(inputData: String): String { } fun main(args: Array) { + var users = listOf("") val app = Javalin.create() .get("/") { ctx -> ctx.result("dingus") } .get("/api/send/{content}") { ctx -> @@ -372,10 +405,10 @@ fun main(args: Array) { .get("/api/createaccount/{content}") { ctx -> ctx.result(createAccount(ctx.pathParam("content")))} .get("/api/syncmessages/{content}") { ctx -> ctx.result(syncMessages(ctx.pathParam("content")))} .get("/api/authkey/{content}") { ctx -> ctx.result(authKey(ctx.pathParam("content")))} + .ws("/api/websocket") { ws -> ws.onConnect { ctx -> WsSessionManager.addSession(ctx) - ctx.send("Websocket success") } ws.onClose { ctx -> WsSessionManager.removeSession(ctx) @@ -384,52 +417,18 @@ fun main(args: Array) { println(ctx.message()) val successState = handleSentMessage(ctx.message()) if (successState != "Success") { - ctx.send(successState) + try { + ctx.send(successState) + } catch (e: Exception) { + println("Error sending error message: ${e.message}") + } } else { - // Broadcast the message to all clients if successful val messageContent = extractMessageContent(ctx.message()) WsSessionManager.broadcast(messageContent) - ctx.send("Message sent successfully") - } - } - } - .start(7070) -} - -/* -fun main(args: Array) { - val app = Javalin.create() - .get("/") { ctx -> ctx.result("dingus") } - .get("/api/send/{content}") { ctx -> - val result = handleSentMessage(ctx.pathParam("content")) - if (result == "Success") { - val messageContent = extractMessageContent(ctx.pathParam("content") - WsSessionManager.broadcast(messageContent) - ctx.result(result) - } - } - .get("/api/createaccount/{content}") { ctx -> ctx.result(createAccount(ctx.pathParam("content")))} - .get("/api/syncmessages/{content}") { ctx -> ctx.result(syncMessages(ctx.pathParam("content")))} - .get("/api/authkey/{content}") { ctx -> ctx.result(authKey(ctx.pathParam("content")))} - .ws("/api/websocket") { ws -> - ws.onConnect { ctx -> - WsSessionManager.addSession(ctx.sessionId, ctx) - ctx.send("Websocket success") - } - ws.onClose { ctx -> - WsSessionManager.removeSession(ctx.sessionId) - } - ws.onMessage { ctx -> - println(ctx.message()) - val successState = handleSentMessage(ctx.message()) - if (successState != "Success") { - ctx.send(successState) - } else { - ctx.send("Message sent successfully") } } } .start(7070) -}*/ +}