Clean up code, support JSON

This commit is contained in:
Maxwell Jeffress 2024-11-23 18:12:07 +11:00
parent b1b1c0accc
commit d839652916
3 changed files with 147 additions and 250 deletions

View File

@ -5,11 +5,11 @@ plugins {
} }
application { application {
mainClass.set("xyz.maxwellj.chookpen.MainKt") mainClass.set("xyz.maxwellj.chookchat.MainKt")
layout.buildDirectory.dir("distributions/") layout.buildDirectory.dir("distributions/")
} }
group = "xyz.maxwellj.chookpen" group = "xyz.maxwellj.chookchat"
version = "0.0.1" version = "0.0.1"
repositories { repositories {
@ -18,7 +18,7 @@ repositories {
tasks.withType<Jar> { tasks.withType<Jar> {
manifest { manifest {
attributes["Main-Class"] = "xyz.maxwellj.chookpen.MainKt" attributes["Main-Class"] = "xyz.maxwellj.chookchat.MainKt"
} }
} }
@ -27,6 +27,7 @@ dependencies {
implementation("org.jetbrains.kotlinx:kotlinx-coroutines-core:1.7.3") implementation("org.jetbrains.kotlinx:kotlinx-coroutines-core:1.7.3")
implementation("io.javalin:javalin:6.3.0") implementation("io.javalin:javalin:6.3.0")
implementation("org.slf4j:slf4j-simple:2.0.16") implementation("org.slf4j:slf4j-simple:2.0.16")
implementation("org.json:json:20230618")
} }
tasks.test { tasks.test {

View File

@ -1,4 +1,4 @@
package xyz.maxwellj.chookpen package xyz.maxwellj.chookchat
import io.javalin.Javalin import io.javalin.Javalin
import io.javalin.websocket.WsContext import io.javalin.websocket.WsContext
@ -7,6 +7,10 @@ import java.util.UUID
import kotlin.concurrent.fixedRateTimer import kotlin.concurrent.fixedRateTimer
import org.json.JSONObject
import org.json.JSONArray
import org.json.JSONException
import java.io.File import java.io.File
import java.io.BufferedReader import java.io.BufferedReader
@ -20,13 +24,26 @@ fun md5(input:String): String {
val md = MessageDigest.getInstance("MD5") val md = MessageDigest.getInstance("MD5")
return BigInteger(1, md.digest(input.toByteArray())).toString(16).padStart(32, '0') return BigInteger(1, md.digest(input.toByteArray())).toString(16).padStart(32, '0')
} }
/*
fun removeLines(fileName: String, lineNumber: String) {
require(!fileName.isEmpty() && startLine >= 1 && numLines >= 1)
val f = File(fileName)
var lines = f.readLines()
if (startLine > size) {
println("The starting line is beyond the length of the file")
return
}
lines = lines.take(startLine - 1) + lines.drop(startLine + n - 1)
val text = lines.joinToString(System.lineSeparator())
f.writeText(text)
}
*/
object WsSessionManager { object WsSessionManager {
var peopleOnline = mutableListOf("") val peopleOnline = mutableListOf("")
var sessionsList = mutableListOf("") val sessionsList = mutableListOf("")
val sessions = ConcurrentHashMap<WsContext, String>()
private val sessions = ConcurrentHashMap<WsContext, String>() val sessionIds = ConcurrentHashMap<String, WsContext>()
private val sessionIds = ConcurrentHashMap<String, WsContext>() val userSessions = ConcurrentHashMap<String, String>()
init { init {
fixedRateTimer("websocket-ping", period = 5000) { fixedRateTimer("websocket-ping", period = 5000) {
@ -54,18 +71,25 @@ object WsSessionManager {
} }
fun broadcastOnlineUsers() { fun broadcastOnlineUsers() {
broadcast("!users:{${peopleOnline.joinToString(",")}}") val processedData = JSONObject().apply {
put("type", "users")
put("username", "system")
put("content", peopleOnline.joinToString(", "))
}
broadcast(processedData.toString())
} }
fun handleUserLogin(username: String) { fun handleUserLogin(username: String) {
peopleOnline += username if (!peopleOnline.contains(username)) {
peopleOnline.add(username)
broadcastOnlineUsers() broadcastOnlineUsers()
} }
}
fun addSession(ctx: WsContext) { fun addSession(ctx: WsContext) {
try { try {
val sessionId = UUID.randomUUID().toString() val sessionId = UUID.randomUUID().toString()
sessionsList += sessionId sessionsList.add(sessionId) // Changed from += to add()
sessions[ctx] = sessionId sessions[ctx] = sessionId
sessionIds[sessionId] = ctx sessionIds[sessionId] = ctx
} catch (e: Exception) { } catch (e: Exception) {
@ -77,8 +101,13 @@ object WsSessionManager {
try { try {
val sessionId = sessions[ctx] val sessionId = sessions[ctx]
if (sessionId != null) { if (sessionId != null) {
peopleOnline.removeAt(sessionsList.indexOf(sessionId)) // Find and remove the username associated with this session
sessionsList.removeAt(sessionsList.indexOf(sessionId)) userSessions.entries.find { it.value == sessionId }?.let { entry ->
peopleOnline.remove(entry.key)
userSessions.remove(entry.key)
}
sessionsList.remove(sessionId)
sessions.remove(ctx) sessions.remove(ctx)
sessionIds.remove(sessionId) sessionIds.remove(sessionId)
broadcastOnlineUsers() broadcastOnlineUsers()
@ -88,6 +117,13 @@ object WsSessionManager {
} }
} }
fun associateUserWithSession(username: String, ctx: WsContext) {
val sessionId = sessions[ctx]
if (sessionId != null) {
userSessions[username] = sessionId
}
}
fun broadcast(message: String) { fun broadcast(message: String) {
val deadSessions = mutableListOf<WsContext>() val deadSessions = mutableListOf<WsContext>()
@ -111,69 +147,36 @@ object WsSessionManager {
fun getSessionCount(): Int = sessions.size fun getSessionCount(): Int = sessions.size
} }
fun extractMessageContent(inputData: String): String { fun extractMessageContent(inputData: String, ctx: WsContext): String {
var username = "" val jsonInputData = JSONObject(inputData)
var message = "" if (jsonInputData.getString("type") == "connect") {
var dataType = "" val username = jsonInputData.getString("username")
var isParsingData = 0 WsSessionManager.associateUserWithSession(username, ctx)
WsSessionManager.handleUserLogin(username)
for (char in inputData) { val processedData = JSONObject().apply {
if (char == ':') { put("type", "connect")
isParsingData = 1 put("username", "system")
} else if (isParsingData == 1) { put("content", "${jsonInputData.getString("username")} just joined the room!")
if (char == '}') {
isParsingData = 0
dataType = ""
} else if (char != '{') {
if (dataType == "username") {
username += char
} else if (dataType == "message") {
message += char
} }
return(processedData.toString())
} }
} else { val processedData = JSONObject().apply {
dataType += char put("type", jsonInputData.getString("type"))
put("username", jsonInputData.getString("username"))
put("content", jsonInputData.getString("content"))
} }
} return(processedData.toString())
return("$username: $message")
} }
fun handleSentMessage(inputData: String): String { fun handleSentMessage(inputData: String): String {
println("API request recieved: $inputData") println("API request recieved: $inputData")
// Parse data sent to the server by client var jsonInputData: JSONObject
var username = "" try {jsonInputData = JSONObject(inputData)} catch (error: JSONException){return(error.toString())}
var token = ""
var message = "" val username = jsonInputData.getString("username")
var dataType = "" val token = jsonInputData.getString("token")
var command = "" val content = jsonInputData.getString("content")
var commandArg = ""
var isParsingData = 0
for (char in inputData) {
val character = char
if (character == ':') {
isParsingData = 1
} else if (isParsingData == 1) {
if (character == '}') {
isParsingData = 0
dataType = ""
} else if (character != '{') {
if (dataType == "username") {
username += character
} else if (dataType == "token") {
token += character
} else if (dataType == "message") {
message += character
} else if (dataType == "command") {
command += character
} else if (dataType == "commandArg") {
commandArg += character
}
}
} else {
dataType += character
}
}
val userDatabaseParser = BufferedReader(File("userDatabase").reader()) val userDatabaseParser = BufferedReader(File("userDatabase").reader())
var lineNumber = 1 var lineNumber = 1
var userLine = "" var userLine = ""
@ -188,12 +191,18 @@ fun handleSentMessage(inputData: String): String {
userDatabaseParser.close() userDatabaseParser.close()
if (userLine == "") { if (userLine == "") {
return("That account does not exist on this server.") val processedData = JSONObject().apply {
put("type", "error")
put("username", "system")
put("content", "unknown-account")
}
return(processedData.toString())
} }
var usernameInDatabase = "" var usernameInDatabase = ""
var tokenInDatabase = "" var tokenInDatabase = ""
var saltInDatabase = "" var saltInDatabase = ""
var banStatus = ""
var currentStage = 0 var currentStage = 0
for (char in userLine) { for (char in userLine) {
if (char == ':') { if (char == ':') {
@ -205,103 +214,46 @@ fun handleSentMessage(inputData: String): String {
tokenInDatabase += char tokenInDatabase += char
} else if (currentStage == 2) { } else if (currentStage == 2) {
saltInDatabase += char saltInDatabase += char
} else if (currentStage == 3) {
banStatus += char
} }
} }
tokenInDatabase = tokenInDatabase.replace(":", "") tokenInDatabase = tokenInDatabase.replace(":", "")
saltInDatabase = saltInDatabase.replace(":", "") saltInDatabase = saltInDatabase.replace(":", "")
banStatus = banStatus.replace(":", "")
if (banStatus == "1") {
val processedData = JSONObject().apply {
put("type", "error")
put("username", "system")
put("content", "banned")
}
return(processedData.toString())
}
val tokenWithSalt = (md5(token + saltInDatabase)) val tokenWithSalt = (md5(token + saltInDatabase))
/*println(saltInDatabase) /*println(saltInDatabase)
println(tokenWithSalt) println(tokenWithSalt)
if (tokenWithSalt != tokenInDatabase) {*/ if (tokenWithSalt != tokenInDatabase) {*/
if (token != tokenInDatabase) { if (token != tokenInDatabase) {
return("Invalid token! Please try putting in your password right") val processedData = JSONObject().apply {
put("type", "error")
put("username", "system")
put("content", "invalid-token")
}
return(processedData.toString())
} }
// Make the message to respond to the client // Make the message to respond to the client
val chatHistoryView = File("chatHistory") val chatHistoryView = File("chatHistory")
var fullMessage = "" var fullMessage = ""
if (message != "") { if (content != "") {
fullMessage = "${chatHistoryView.readText()}$username: $message" fullMessage = "${chatHistoryView.readText()}$username: $content"
// Add the client's message to the chat history // Add the client's message to the chat history
val chatHistory = File("chatHistory") val chatHistory = File("chatHistory")
chatHistory.appendText("$username: $message ${System.lineSeparator()}") chatHistory.appendText("$username: $content ${System.lineSeparator()}")
message = ""
return("Success") return("Success")
} else if (command != "") {
if (command == "sync") {
return(chatHistoryView.readText())
} else if (command == "login") {
WsSessionManager.handleUserLogin(commandArg)
return("Login successful")
}
} else { } else {
return("No data provided") return("No data provided")
} }
return("System: Welcome to Chookpen, $username!") return("Chookchat")
}
fun syncMessages(inputData: String): String {
println("API request recieved: $inputData")
// Parse data sent to the server by client
var username = ""
var token = ""
var dataType = ""
var isParsingData = 0
for (char in inputData) {
val character = char
if (character == ':') {
isParsingData = 1
} else if (isParsingData == 1) {
if (character == '}') {
isParsingData = 0
dataType = ""
} else if (character != '{') {
if (dataType == "username") {
username += character
} else if (dataType == "token") {
token += character
}
}
} else {
dataType += character
}
}
val userDatabaseParser = BufferedReader(File("userDatabase").reader())
var lineNumber = 1
var userLine = ""
// Search the user database to find required information about the user
userDatabaseParser.forEachLine { line ->
if (line.contains(username)) {
userLine = line
}
lineNumber++
}
userDatabaseParser.close()
if (userLine == "") {
return("Account not found")
}
var usernameInDatabase = ""
var tokenInDatabase = ""
var currentStage = 0
for (char in userLine) {
if (char == ':') {
currentStage ++
}
if (currentStage == 0) {
usernameInDatabase += char
} else if (currentStage == 1) {
tokenInDatabase += char
}
}
tokenInDatabase = tokenInDatabase.replace(":", "")
if (token != tokenInDatabase) {
return("Invalid token")
}
// Send back message history
val chatHistoryView = File("chatHistory")
return(chatHistoryView.readText())
} }
fun createAccount(inputData: String): String { fun createAccount(inputData: String): String {
@ -341,7 +293,12 @@ fun createAccount(inputData: String): String {
var response = "" var response = ""
userDatabaseParser.forEachLine { line -> userDatabaseParser.forEachLine { line ->
if (line.contains(username)) { if (line.contains(username)) {
response = "Username already exists" val processedData = JSONObject().apply {
put("type", "error")
put("username", "system")
put("content", "username-taken")
}
response = processedData.toString()
} }
lineNumber++ lineNumber++
} }
@ -350,101 +307,47 @@ fun createAccount(inputData: String): String {
} }
userDatabaseParser.close() userDatabaseParser.close()
if (username == "") { if (username == "") {
return("No username") val processedData = JSONObject().apply {
put("type", "error")
put("username", "system")
put("content", "no-username")
}
return(processedData.toString())
} }
if (token == "") { if (token == "") {
return("No token") val processedData = JSONObject().apply {
put("type", "error")
put("username", "system")
put("content", "no-token")
}
return(processedData.toString())
} }
val userDatabaseFile = File("userDatabase") val userDatabaseFile = File("userDatabase")
userDatabaseFile.appendText("${System.lineSeparator()}$username:$token") userDatabaseFile.appendText("${System.lineSeparator()}$username:$token")
return("Success") val processedData = JSONObject().apply {
put("type", "success")
put("username", "system")
put("content", "success")
}
return(processedData.toString())
} }
fun authKey(inputData: String): String {
println("API request recieved: $inputData")
// Parse data sent to the server by client fun handleServerCommand(command: String): String {
var username = "" val commandArgs = mutableListOf("")
var token = "" commandArgs.drop(1)
var authKey = ""
var dataType = ""
var isParsingData = 0
for (char in inputData) {
val character = char
if (character == ':') {
isParsingData = 1
} else if (isParsingData == 1) {
if (character == '}') {
isParsingData = 0
dataType = ""
} else if (character != '{') {
if (dataType == "username") {
username += character
} else if (dataType == "token") {
token += character
} else if (dataType == "authkey") {
authKey += character
}
}
} else {
dataType += character
}
}
val userDatabaseParser = BufferedReader(File("userDatabase").reader())
var lineNumber = 1
var userLine = ""
// Search the user database to find required information about the user
userDatabaseParser.forEachLine { line ->
if (line.contains(username)) {
userLine = line
}
lineNumber++
}
userDatabaseParser.close()
if (userLine == "") {
return("Account not found")
}
var usernameInDatabase = ""
var tokenInDatabase = ""
var currentStage = 0 var currentStage = 0
for (char in userLine) {
if (char == ':') {
currentStage ++
}
if (currentStage == 0) {
usernameInDatabase += char
} else if (currentStage == 1) {
tokenInDatabase += char
}
}
tokenInDatabase = tokenInDatabase.replace(":", "")
if (token != tokenInDatabase) {
return("Invalid token")
}
if (authKey == "") {
return("No auth key provided")
}
// Make the message to respond to the client
val chatHistoryView = File("chatHistory")
var fullMessage = ""
if (authKey != "") {
fullMessage = "encryptionKey:$username:$authKey"
authKey = ""
} else {
fullMessage = "${chatHistoryView.readText()}"
}
val response = if (inputData.isNotEmpty()) {
fullMessage
} else {
"No data provided"
}
// Send the message to the client for (char in command) {
return("Success") if (char == ' ') {
currentStage ++
commandArgs += ""
} else {
commandArgs[currentStage] += char
}
}
return("I'm not sure how to ${commandArgs.toString()}")
} }
fun main(args: Array<String>) { fun main(args: Array<String>) {
@ -455,18 +358,7 @@ fun main(args: Array<String>) {
}.get("/") { ctx -> }.get("/") { ctx ->
ctx.redirect("/index.html") ctx.redirect("/index.html")
} }
.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/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("/api/websocket") { ws ->
ws.onConnect { ctx -> ws.onConnect { ctx ->
WsSessionManager.addSession(ctx) WsSessionManager.addSession(ctx)
@ -487,7 +379,7 @@ fun main(args: Array<String>) {
println("Error sending error message: ${e.message}") println("Error sending error message: ${e.message}")
} }
} else { } else {
val messageContent = extractMessageContent(ctx.message()) val messageContent = extractMessageContent(ctx.message(), ctx)
WsSessionManager.broadcast(messageContent) WsSessionManager.broadcast(messageContent)
} }
} }
@ -495,6 +387,9 @@ fun main(args: Array<String>) {
} }
} }
.start(7070) .start(7070)
println("Type a command for the server")
while (1 == 1) {
println(handleServerCommand(readln()))
}
} }

View File

@ -0,0 +1 @@
../../../../../client-web/gradient.css