chookchat/server/src/main/kotlin/Main.kt

489 lines
16 KiB
Kotlin
Raw Normal View History

2024-11-23 18:12:07 +11:00
package xyz.maxwellj.chookchat
2024-10-20 13:37:03 +11:00
import io.javalin.Javalin
import io.javalin.websocket.WsContext
2024-11-25 14:50:03 +11:00
import io.javalin.http.UploadedFile
import java.util.concurrent.ConcurrentHashMap
import java.util.UUID
2024-10-20 13:37:03 +11:00
2024-10-28 20:37:20 +11:00
import kotlin.concurrent.fixedRateTimer
2024-11-23 18:12:07 +11:00
import org.json.JSONObject
import org.json.JSONArray
import org.json.JSONException
2024-10-20 13:37:03 +11:00
import java.io.File
import java.io.BufferedReader
import java.math.BigInteger
import java.security.MessageDigest
2024-11-05 20:35:10 +11:00
import java.nio.file.Paths
import java.nio.file.Files
fun md5(input:String): String {
val md = MessageDigest.getInstance("MD5")
return BigInteger(1, md.digest(input.toByteArray())).toString(16).padStart(32, '0')
}
2024-12-04 08:39:58 +11:00
object config {
var address = ""
var port = ""
var security = ""
var serviceName = ""
fun getConfig() {
2024-12-04 13:11:36 +11:00
val configFile = File("chookchat.config")
2024-12-04 08:39:58 +11:00
try {
val config = configFile.readLines()
var type = ""
var isEditing = 0
for (line in config) {
for (char in line) {
if (char == ':') {
isEditing = 1
} else if (char == ';') {
isEditing = 0
type = ""
} else {
if (isEditing == 0) {
type += char
} else if (isEditing == 1)
if (type == "address") {
address += char
} else if (type == "port") {
port += char
} else if (type == "security") {
security += char
} else if (type == "serviceName") {
serviceName += char
}
}
}
}
} catch (e: Exception) {
println("Something went wrong :/ Here's the error: $e")
}
2024-11-23 18:12:07 +11:00
}
}
2024-12-04 08:39:58 +11:00
object WsSessionManager {
2024-11-23 18:12:07 +11:00
val peopleOnline = mutableListOf("")
val sessionsList = mutableListOf("")
val sessions = ConcurrentHashMap<WsContext, String>()
val sessionIds = ConcurrentHashMap<String, WsContext>()
val userSessions = ConcurrentHashMap<String, String>()
2024-10-20 13:37:03 +11:00
2024-10-28 20:37:20 +11:00
init {
fixedRateTimer("websocket-ping", period = 5000) {
sendPing()
}
}
private fun sendPing() {
val deadSessions = mutableListOf<WsContext>()
sessions.keys.forEach { ctx ->
try {
if (ctx.session.isOpen) {
ctx.send("ping")
} else {
deadSessions.add(ctx)
}
} catch (e: Exception) {
println("Error sending ping: ${e.message}")
deadSessions.add(ctx)
}
}
// Clean up any dead sessions
deadSessions.forEach { removeSession(it) }
}
fun broadcastOnlineUsers() {
2024-11-23 18:12:07 +11:00
val processedData = JSONObject().apply {
put("type", "users")
put("username", "system")
put("content", peopleOnline.joinToString(", "))
}
broadcast(processedData.toString())
2024-10-28 20:37:20 +11:00
}
fun handleUserLogin(username: String) {
2024-11-23 18:12:07 +11:00
if (!peopleOnline.contains(username)) {
peopleOnline.add(username)
broadcastOnlineUsers()
}
2024-10-28 20:37:20 +11:00
}
fun addSession(ctx: WsContext) {
try {
val sessionId = UUID.randomUUID().toString()
2024-11-23 18:12:07 +11:00
sessionsList.add(sessionId) // Changed from += to add()
sessions[ctx] = sessionId
sessionIds[sessionId] = ctx
} catch (e: Exception) {
println("Error adding session: ${e.message}")
}
}
fun removeSession(ctx: WsContext) {
try {
val sessionId = sessions[ctx]
if (sessionId != null) {
2024-11-23 18:12:07 +11:00
// Find and remove the username associated with this session
userSessions.entries.find { it.value == sessionId }?.let { entry ->
peopleOnline.remove(entry.key)
userSessions.remove(entry.key)
}
sessionsList.remove(sessionId)
sessions.remove(ctx)
sessionIds.remove(sessionId)
2024-10-28 20:37:20 +11:00
broadcastOnlineUsers()
}
} catch (e: Exception) {
println("Error removing session: ${e.message}")
}
}
2024-11-23 18:12:07 +11:00
fun associateUserWithSession(username: String, ctx: WsContext) {
val sessionId = sessions[ctx]
if (sessionId != null) {
userSessions[username] = sessionId
}
}
fun broadcast(message: String) {
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
}
2024-11-23 18:12:07 +11:00
fun extractMessageContent(inputData: String, ctx: WsContext): String {
val jsonInputData = JSONObject(inputData)
if (jsonInputData.getString("type") == "connect") {
val username = jsonInputData.getString("username")
WsSessionManager.associateUserWithSession(username, ctx)
WsSessionManager.handleUserLogin(username)
val processedData = JSONObject().apply {
put("type", "connect")
put("username", "system")
put("content", "${jsonInputData.getString("username")} just joined the room!")
}
2024-11-23 18:12:07 +11:00
return(processedData.toString())
}
val processedData = JSONObject().apply {
put("type", jsonInputData.getString("type"))
put("username", jsonInputData.getString("username"))
put("content", jsonInputData.getString("content"))
}
return(processedData.toString())
2024-10-20 13:37:03 +11:00
}
fun handleSentMessage(inputData: String): String {
println("API request recieved: $inputData")
2024-11-23 18:12:07 +11:00
var jsonInputData: JSONObject
try {jsonInputData = JSONObject(inputData)} catch (error: JSONException){return(error.toString())}
val username = jsonInputData.getString("username")
val token = jsonInputData.getString("token")
val content = jsonInputData.getString("content")
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 == "") {
2024-11-23 18:12:07 +11:00
val processedData = JSONObject().apply {
put("type", "error")
put("username", "system")
put("content", "unknown-account")
}
return(processedData.toString())
}
var usernameInDatabase = ""
var tokenInDatabase = ""
var saltInDatabase = ""
2024-11-23 18:12:07 +11:00
var banStatus = ""
var currentStage = 0
for (char in userLine) {
if (char == ':') {
currentStage ++
}
if (currentStage == 0) {
usernameInDatabase += char
} else if (currentStage == 1) {
tokenInDatabase += char
} else if (currentStage == 2) {
saltInDatabase += char
2024-11-23 18:12:07 +11:00
} else if (currentStage == 3) {
banStatus += char
}
}
tokenInDatabase = tokenInDatabase.replace(":", "")
saltInDatabase = saltInDatabase.replace(":", "")
2024-11-23 18:12:07 +11:00
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))
/*println(saltInDatabase)
println(tokenWithSalt)
if (tokenWithSalt != tokenInDatabase) {*/
if (token != tokenInDatabase) {
2024-11-23 18:12:07 +11:00
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
val chatHistoryView = File("chatHistory")
var fullMessage = ""
2024-11-23 18:12:07 +11:00
if (content != "") {
fullMessage = "${chatHistoryView.readText()}$username: $content"
// Add the client's message to the chat history
val chatHistory = File("chatHistory")
2024-11-23 18:12:07 +11:00
chatHistory.appendText("$username: $content ${System.lineSeparator()}")
return("Success")
} else {
return("No data provided")
2024-10-20 13:37:03 +11:00
}
2024-11-23 18:12:07 +11:00
return("Chookchat")
}
fun createAccount(inputData: String): String {
println("Account creation request recieved: $inputData")
// Parse data sent to the server by client
var username = ""
var token = ""
var message = ""
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 == "message") {
message += character
}
}
2024-10-20 13:37:03 +11:00
} else {
dataType += character
2024-10-20 13:37:03 +11:00
}
}
val userDatabaseParser = BufferedReader(File("userDatabase").reader())
var lineNumber = 1
var userExists = 0
// Search the user database to find required information about the user
var response = ""
userDatabaseParser.forEachLine { line ->
if (line.contains(username)) {
2024-11-23 18:12:07 +11:00
val processedData = JSONObject().apply {
put("type", "error")
put("username", "system")
put("content", "username-taken")
}
response = processedData.toString()
2024-10-20 13:37:03 +11:00
}
lineNumber++
}
if (response != "") {
return(response)
}
userDatabaseParser.close()
if (username == "") {
2024-11-23 18:12:07 +11:00
val processedData = JSONObject().apply {
put("type", "error")
put("username", "system")
put("content", "no-username")
}
return(processedData.toString())
}
2024-10-20 13:37:03 +11:00
if (token == "") {
2024-11-23 18:12:07 +11:00
val processedData = JSONObject().apply {
put("type", "error")
put("username", "system")
put("content", "no-token")
}
return(processedData.toString())
2024-10-20 13:37:03 +11:00
}
val userDatabaseFile = File("userDatabase")
userDatabaseFile.appendText("${System.lineSeparator()}$username:$token")
2024-11-23 18:12:07 +11:00
val processedData = JSONObject().apply {
put("type", "success")
put("username", "system")
put("content", "success")
}
2024-11-23 18:12:07 +11:00
return(processedData.toString())
}
2024-10-20 13:37:03 +11:00
2024-11-23 18:12:07 +11:00
fun handleServerCommand(command: String): String {
val commandArgs = mutableListOf("")
commandArgs.drop(1)
var currentStage = 0
2024-11-23 18:12:07 +11:00
for (char in command) {
if (char == ' ') {
currentStage ++
2024-11-23 18:12:07 +11:00
commandArgs += ""
} else {
commandArgs[currentStage] += char
2024-10-20 13:37:03 +11:00
}
}
2024-11-23 18:12:07 +11:00
return("I'm not sure how to ${commandArgs.toString()}")
2024-10-20 13:37:03 +11:00
}
2024-12-04 08:39:58 +11:00
fun buildHTML(): String {
try {
config.getConfig()
val htmlFile = File("resources/index.html")
val html = htmlFile.readLines()
var editedhtml = ""
for (line in html) {
if (line == """ <input type="text" id="serverUrl" value="bobcompass.online" placeholder="Server URL"><br>""") {
editedhtml += """ <input type="text" id="serverUrl" value="${config.address}" placeholder="Server URL"><br>"""
} else if (line == """ <input type="text" id="serverPort" value="443" placeholder="Server Port"><br>""") {
editedhtml += """ <input type="text" id="serverPort" value="${config.port}" placeholder="Server Port"><br>"""
} else if (line == """ <input type="checkbox" id="securityStatus" checked>""" && config.security == "false") {
editedhtml += """ <input type="checkbox" id="securityStatus">"""
} else if (line == """ <h3>Chookchat</h3>""") {
editedhtml += """ <h3>${config.serviceName}</h3>"""
} else {
editedhtml += line
}
}
return(editedhtml)
} catch (e: Exception) {
println(e)
return("There was an error! If you're the server's admin, here are the details: $e")
}
return("dingus")
}
fun main(args: Array<String>) {
2024-10-28 20:37:20 +11:00
WsSessionManager.peopleOnline.removeAt(0)
WsSessionManager.sessionsList.removeAt(0)
2024-11-05 20:35:10 +11:00
val app = Javalin.create { config ->
config.staticFiles.add("/public")
2024-12-04 08:39:58 +11:00
}.get("/") { ctx ->
ctx.html(buildHTML())
//ctx.redirect("/index.html")
2024-11-05 20:35:10 +11:00
}
.get("/api/createaccount/{content}") { ctx -> ctx.result(createAccount(ctx.pathParam("content")))}
2024-11-25 14:50:03 +11:00
.post("/api/upload") { ctx ->
val uploadedFiles = ctx.uploadedFiles()
if (uploadedFiles.isEmpty()) {
ctx.status(400).result("No files uploaded")
return@post
}
val uploadedFile = uploadedFiles[0]
val originalFilename = uploadedFile.filename()
val uuid = UUID.randomUUID().toString()
val fileExtension = originalFilename.substringAfterLast(".", "")
val baseFilename = originalFilename.substringBeforeLast(".")
val newFilename = "${baseFilename}_${uuid}${if (fileExtension.isNotEmpty()) ".$fileExtension" else ""}"
val filePath = Paths.get("uploads", newFilename)
Files.copy(uploadedFile.content(), filePath)
val processedData = JSONObject().apply {
put("type", "fileStatus")
put("username", "system")
put("content", "success")
}
ctx.result(processedData.toString())
val processedData2 = JSONObject().apply {
put("type", "file")
put("username", "system")
put("content", "https://maxwellj.xyz/chookchat/uploads/$newFilename")
}
WsSessionManager.broadcast(processedData2.toString())
}
.ws("/api/websocket") { ws ->
ws.onConnect { ctx ->
WsSessionManager.addSession(ctx)
}
ws.onClose { ctx ->
WsSessionManager.removeSession(ctx)
}
2024-10-28 20:37:20 +11:00
ws.onMessage { ctx ->
when (ctx.message()) {
"pong" -> {}
else -> {
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 {
2024-11-23 18:12:07 +11:00
val messageContent = extractMessageContent(ctx.message(), ctx)
2024-10-28 20:37:20 +11:00
WsSessionManager.broadcast(messageContent)
}
}
2024-10-28 20:37:20 +11:00
}
}
}
.start(7070)
try {
if (args[0] == "-i") {
println("Type a command for the server")
while (1 == 1) {
println(handleServerCommand(readln()))
}
} else {
println("Interactive mode disabled, add -i to enable")
}
} catch (error: Exception) {
println("Interactive mode disabled, add -i to enable")
2024-11-23 18:12:07 +11:00
}
}