From 90cf9f4325043c26a50378030c0827a06b6235d1 Mon Sep 17 00:00:00 2001 From: Maxwell Jeffress Date: Wed, 4 Dec 2024 08:39:58 +1100 Subject: [PATCH] Add Chookpen config file --- .gitignore | 1 + client-cli/README.md | 26 ------- client-cli/build.gradle.kts | 35 --------- client-cli/settings.gradle.kts | 5 -- client-cli/src/main/kotlin/Main.kt | 114 ----------------------------- server/chookpen.config | 1 + server/resources/InterVariable.ttf | 1 + server/resources/gradient.css | 1 + server/resources/index.css | 1 + server/resources/index.html | 1 + server/resources/index.js | 1 + server/src/main/kotlin/Main.kt | 84 +++++++++++++++++---- 12 files changed, 77 insertions(+), 194 deletions(-) delete mode 100644 client-cli/README.md delete mode 100644 client-cli/build.gradle.kts delete mode 100644 client-cli/settings.gradle.kts delete mode 100644 client-cli/src/main/kotlin/Main.kt create mode 100644 server/chookpen.config create mode 120000 server/resources/InterVariable.ttf create mode 120000 server/resources/gradient.css create mode 120000 server/resources/index.css create mode 120000 server/resources/index.html create mode 120000 server/resources/index.js diff --git a/.gitignore b/.gitignore index 6b3d03b..97cd106 100644 --- a/.gitignore +++ b/.gitignore @@ -2,6 +2,7 @@ .swp server/build server/.gradle +server/uploads server/userDatabase server/chatHistory client-cli/build diff --git a/client-cli/README.md b/client-cli/README.md deleted file mode 100644 index bda6cf9..0000000 --- a/client-cli/README.md +++ /dev/null @@ -1,26 +0,0 @@ -# Chookpen Client (CLI) - -This is the reference client for Chookpen. It is quite a basic client. This is a good starting point if you need an example, or are building a GUI Kotlin client. - -## Features - -* Profile file -* Create account (buggy) -* Log into existing account -* Sync messages -* Send message -* Auto hash password -* Configure whether HTTPS is used - -## Documentation - -To run this client, do the following: - -1. Install Java 17+ and Gradle. -2. `git clone https://git.maxwellj.xyz/max/chookpen` -3. In the client-cli directory of the git repo, run `gradle installDist`. -5. Inside build/install/bin, create a .chookpen.profile file that looks like this: ```name:password:server:port:0``` -6. Run the script in build/install/bin, or the bat file if you're a crazy weird guy who uses Windows (Make sure to use the command line otherwise it'll close instantly) -7. If you'd like to send a message, add that as your argument after specifying the script. - -Note: The password in .chookpen.profile is hashed by the client. If you're just testing the server, set your password to `dingus`. The hash recognised by the server for that password is `750c63441033127dccaa91c16b21614e`. Create an account with the method specified in the root README using that password. \ No newline at end of file diff --git a/client-cli/build.gradle.kts b/client-cli/build.gradle.kts deleted file mode 100644 index 2f84be0..0000000 --- a/client-cli/build.gradle.kts +++ /dev/null @@ -1,35 +0,0 @@ -plugins { - kotlin("jvm") version "2.0.0" - application - distribution -} - -application { - mainClass.set("xyz.maxwellj.chookpen.client.MainKt") - layout.buildDirectory.dir("distributions/") -} - -group = "xyz.maxwellj.chookpen.client" -version = "0.0.2" - -repositories { - mavenCentral() -} - -tasks.withType { - manifest { - attributes["Main-Class"] = "xyz.maxwellj.chookpen.client.MainKt" - } -} - -dependencies { - testImplementation(kotlin("test")) - implementation("com.squareup.okhttp3:okhttp:4.12.0") -} - -tasks.test { - useJUnitPlatform() -} -kotlin { - jvmToolchain(17) -} diff --git a/client-cli/settings.gradle.kts b/client-cli/settings.gradle.kts deleted file mode 100644 index 1568f91..0000000 --- a/client-cli/settings.gradle.kts +++ /dev/null @@ -1,5 +0,0 @@ -plugins { - id("org.gradle.toolchains.foojay-resolver-convention") version "0.8.0" -} -rootProject.name = "chookpen.client" - diff --git a/client-cli/src/main/kotlin/Main.kt b/client-cli/src/main/kotlin/Main.kt deleted file mode 100644 index eeb923a..0000000 --- a/client-cli/src/main/kotlin/Main.kt +++ /dev/null @@ -1,114 +0,0 @@ -package xyz.maxwellj.chookpen.client - -import okhttp3.* -import java.util.Scanner -import java.util.concurrent.TimeUnit - -import kotlin.system.exitProcess - -import java.io.File - -import kotlin.system.exitProcess - -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') -} - -fun main() { - // Variables - var name = "" - var server = "" - var port = "" - var hasHTTPS = "" - var password = "" - var configFile = File("${System.getProperty("user.home")}/chookpen.profile") - - var configStage = 0 - for (char in configFile.readText()) { - if (char == ':') {configStage ++} - if (configStage == 0) { - name += char - } else if (configStage == 1) { - password += char - } else if (configStage == 2) { - server += char - } else if (configStage == 3) { - port += char - } else if (configStage == 4) { - hasHTTPS += char - } - } - - server = server.replace(":", "") - port = port.replace(":", "") - hasHTTPS = hasHTTPS.replace(":", "") - password = password.replace(":", "") - - if (password == "x") { - println("Enter your password:") - password = readln() - } - - val client = OkHttpClient.Builder() - .pingInterval(30, TimeUnit.SECONDS) - .build() - - val request = Request.Builder() - .url("ws://localhost:7070/api/websocket") - .build() - - var webSocket: WebSocket? = null - - val listener = object : WebSocketListener() { - override fun onOpen(webSocket: WebSocket, response: Response) { - println(password) - println(md5(password)) - println("Connection opened") - webSocket.send("username:{$name}token:{${md5(password)}}message:{Joined the room}") - } - - override fun onMessage(webSocket: WebSocket, text: String) { - println("$text") - } - - override fun onClosing(webSocket: WebSocket, code: Int, reason: String) { - println("Connection closing: $reason") - webSocket.close(1000, null) - } - - override fun onFailure(webSocket: WebSocket, t: Throwable, response: Response?) { - println("Connection failed: ${t.message}") - } - } - - // Set up shutdown hook for Ctrl+C handling - Runtime.getRuntime().addShutdownHook(Thread { - println("\nShutting down gracefully...") - webSocket?.close(1000, "Client shutting down") - client.dispatcher.executorService.shutdown() - }) - - // Initialize WebSocket connection - webSocket = client.newWebSocket(request, listener) - - // Set up input handling - val scanner = Scanner(System.`in`) - println("Type your messages (press Enter to send, Ctrl+C to quit):") - - while (true) { - try { - val input = scanner.nextLine() - if (input.isNotEmpty()) { - webSocket?.send("username:{$name}token:{${md5(password)}}message:{$input}") - } - } catch (e: Exception) { - // Handle any input-related exceptions - println("Error reading input: ${e.message}") - break - } - } -} diff --git a/server/chookpen.config b/server/chookpen.config new file mode 100644 index 0000000..054201f --- /dev/null +++ b/server/chookpen.config @@ -0,0 +1 @@ +address:localhost;port:7070;security:false;serviceName:fnkjdsnfiewnifdwsocidw; diff --git a/server/resources/InterVariable.ttf b/server/resources/InterVariable.ttf new file mode 120000 index 0000000..28afca5 --- /dev/null +++ b/server/resources/InterVariable.ttf @@ -0,0 +1 @@ +../../client-web/InterVariable.ttf \ No newline at end of file diff --git a/server/resources/gradient.css b/server/resources/gradient.css new file mode 120000 index 0000000..94056fa --- /dev/null +++ b/server/resources/gradient.css @@ -0,0 +1 @@ +../../client-web/gradient.css \ No newline at end of file diff --git a/server/resources/index.css b/server/resources/index.css new file mode 120000 index 0000000..42fce40 --- /dev/null +++ b/server/resources/index.css @@ -0,0 +1 @@ +../../client-web/index.css \ No newline at end of file diff --git a/server/resources/index.html b/server/resources/index.html new file mode 120000 index 0000000..99b8027 --- /dev/null +++ b/server/resources/index.html @@ -0,0 +1 @@ +../../client-web/index.html \ No newline at end of file diff --git a/server/resources/index.js b/server/resources/index.js new file mode 120000 index 0000000..d99de5f --- /dev/null +++ b/server/resources/index.js @@ -0,0 +1 @@ +../../client-web/index.js \ No newline at end of file diff --git a/server/src/main/kotlin/Main.kt b/server/src/main/kotlin/Main.kt index 8fc1066..f8ac423 100644 --- a/server/src/main/kotlin/Main.kt +++ b/server/src/main/kotlin/Main.kt @@ -26,20 +26,48 @@ fun md5(input:String): String { val md = MessageDigest.getInstance("MD5") 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 + +object config { + var address = "" + var port = "" + var security = "" + var serviceName = "" + + fun getConfig() { + val configFile = File("chookpen.config") + 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") + } } - lines = lines.take(startLine - 1) + lines.drop(startLine + n - 1) - val text = lines.joinToString(System.lineSeparator()) - f.writeText(text) } -*/ + object WsSessionManager { val peopleOnline = mutableListOf("") val sessionsList = mutableListOf("") @@ -352,13 +380,41 @@ fun handleServerCommand(command: String): String { return("I'm not sure how to ${commandArgs.toString()}") } +fun buildHTML(): String { + try { + config.getConfig() + val htmlFile = File("resources/index.html") + val html = htmlFile.readLines() + var editedhtml = "" + for (line in html) { + if (line == """
""") { + editedhtml += """
""" + } else if (line == """
""") { + editedhtml += """
""" + } else if (line == """ """ && config.security == "false") { + editedhtml += """ """ + } else if (line == """

Chookchat

""") { + editedhtml += """

${config.serviceName}

""" + } 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) { WsSessionManager.peopleOnline.removeAt(0) WsSessionManager.sessionsList.removeAt(0) val app = Javalin.create { config -> config.staticFiles.add("/public") - }.get("/") { ctx -> - ctx.redirect("/index.html") + }.get("/") { ctx -> + ctx.html(buildHTML()) + //ctx.redirect("/index.html") } .get("/api/createaccount/{content}") { ctx -> ctx.result(createAccount(ctx.pathParam("content")))} .post("/api/upload") { ctx ->