Compare commits
2 Commits
31ce3f29f3
...
97a26f7615
Author | SHA1 | Date | |
---|---|---|---|
|
97a26f7615 | ||
|
18be6c6fd4 |
|
@ -7,41 +7,62 @@ import java.util.UUID
|
|||
|
||||
import java.io.File
|
||||
import java.io.BufferedReader
|
||||
/*
|
||||
object WsSessionManager {
|
||||
val sessions = ConcurrentHashMap<String, WsContext>()
|
||||
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<String, WsContext>()
|
||||
private val sessions = ConcurrentHashMap<WsContext, String>()
|
||||
private val sessionIds = ConcurrentHashMap<String, WsContext>()
|
||||
|
||||
fun addSession(ctx: WsContext) {
|
||||
// Generate our own UUID for the session since we can't access Javalin's private sessionId
|
||||
try {
|
||||
val sessionId = UUID.randomUUID().toString()
|
||||
sessions[sessionId] = ctx
|
||||
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 ->
|
||||
val deadSessions = mutableListOf<WsContext>()
|
||||
|
||||
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,9 +154,16 @@ fun handleSentMessage(inputData: String): String {
|
|||
usernameInDatabase += char
|
||||
} else if (currentStage == 1) {
|
||||
tokenInDatabase += char
|
||||
} else if (currentStage == 2) {
|
||||
saltInDatabase += char
|
||||
}
|
||||
}
|
||||
tokenInDatabase = tokenInDatabase.replace(":", "")
|
||||
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")
|
||||
}
|
||||
|
@ -359,6 +391,7 @@ fun authKey(inputData: String): String {
|
|||
}
|
||||
|
||||
fun main(args: Array<String>) {
|
||||
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<String>) {
|
|||
.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<String>) {
|
|||
println(ctx.message())
|
||||
val successState = handleSentMessage(ctx.message())
|
||||
if (successState != "Success") {
|
||||
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<String>) {
|
||||
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)
|
||||
}*/
|
||||
|
||||
|
||||
|
|
Loading…
Reference in New Issue
Block a user