Base server on Javalin for easy future development

This commit is contained in:
Maxwell Jeffress 2024-10-23 14:25:25 +11:00
parent 294b08297f
commit 24e50c26c4
3 changed files with 282 additions and 191 deletions

2
.gitignore vendored
View File

@ -3,7 +3,7 @@
server/build server/build
server/.gradle server/.gradle
server/userDatabase server/userDatabase
server/messageHistory server/chatHistory
client-cli/build client-cli/build
client-cli/.gradle client-cli/.gradle
client-cli/.chookpen.profile client-cli/.chookpen.profile

View File

@ -24,6 +24,8 @@ tasks.withType<Jar> {
dependencies { dependencies {
testImplementation(kotlin("test")) testImplementation(kotlin("test"))
implementation("io.javalin:javalin:6.3.0")
implementation("org.slf4j:slf4j-simple:2.0.16")
} }
tasks.test { tasks.test {

View File

@ -1,5 +1,7 @@
package xyz.maxwellj.chookpen package xyz.maxwellj.chookpen
import io.javalin.Javalin
import com.sun.net.httpserver.HttpExchange import com.sun.net.httpserver.HttpExchange
import com.sun.net.httpserver.HttpHandler import com.sun.net.httpserver.HttpHandler
import com.sun.net.httpserver.HttpServer import com.sun.net.httpserver.HttpServer
@ -9,35 +11,17 @@ import java.io.File
import java.io.BufferedReader import java.io.BufferedReader
fun main(args: Array<String>) { fun main(args: Array<String>) {
// Crete a server and start listening for arguments val app = Javalin.create()
val server = HttpServer.create(InetSocketAddress(8000), 0) .get("/") { ctx -> ctx.result("dingus") }
server.createContext("/test", ServerTester()) .get("/api/send/{content}") { ctx -> ctx.result(handleSentMessage(ctx.pathParam("content")))}
server.createContext("/api", ApiHandler()) .get("/api/createaccount/{content}") { ctx -> ctx.result(createAccount(ctx.pathParam("content")))}
server.createContext("/api/createAccount", CreateAccount()) .get("/api/syncmessages/{content}") { ctx -> ctx.result(syncMessages(ctx.pathParam("content")))}
server.executor = null .get("/api/authkey/{content}") { ctx -> ctx.result(authKey(ctx.pathParam("content")))}
server.start() .start(7070)
println("Server has started on port 8000")
} }
class ServerTester : HttpHandler { fun handleSentMessage(inputData: String): String {
override fun handle(t: HttpExchange) {
val response = "Testing, testing, 1, 2, 3... Somehow this is going to make it to production one day"
t.sendResponseHeaders(200, response.length.toLong())
val os = t.responseBody
os.write(response.toByteArray())
println("Test request recieved!")
os.close()
}
}
class ApiHandler : HttpHandler {
override fun handle(t: HttpExchange) {
println("Request recieved...")
// Get the data
val path = t.requestURI.path
val inputData = path.substringAfter("/api/")
println("API request recieved: $inputData") println("API request recieved: $inputData")
// Parse data sent to the server by client // Parse data sent to the server by client
var username = "" var username = ""
var token = "" var token = ""
@ -79,12 +63,7 @@ class ApiHandler : HttpHandler {
userDatabaseParser.close() userDatabaseParser.close()
if (userLine == "") { if (userLine == "") {
val response = "That account does not exist on this server." return("That account does not exist on this server.")
t.sendResponseHeaders(403, response.length.toLong())
val os = t.responseBody
os.write(response.toByteArray())
os.close()
return
} }
var usernameInDatabase = "" var usernameInDatabase = ""
@ -102,12 +81,7 @@ class ApiHandler : HttpHandler {
} }
tokenInDatabase = tokenInDatabase.replace(":", "") tokenInDatabase = tokenInDatabase.replace(":", "")
if (token != tokenInDatabase) { if (token != tokenInDatabase) {
val response = "Invalid token! Please try putting in your password right" return("Invalid token! Please try putting in your password right")
t.sendResponseHeaders(403, response.length.toLong())
val os = t.responseBody
os.write(response.toByteArray())
os.close()
return
} }
// Make the message to respond to the client // Make the message to respond to the client
val chatHistoryView = File("chatHistory") val chatHistoryView = File("chatHistory")
@ -118,30 +92,79 @@ class ApiHandler : HttpHandler {
val chatHistory = File("chatHistory") val chatHistory = File("chatHistory")
chatHistory.appendText("$username: $message ${System.lineSeparator()}") chatHistory.appendText("$username: $message ${System.lineSeparator()}")
message = "" message = ""
return("Success")
} else { } else {
fullMessage = "${chatHistoryView.readText()}" return("No data provided")
} }
val response = if (inputData.isNotEmpty()) {
fullMessage
} else {
"No data provided"
} }
// Send the message to the client fun syncMessages(inputData: String): String {
t.sendResponseHeaders(200, response.length.toLong()) println("API request recieved: $inputData")
val os = t.responseBody // Parse data sent to the server by client
os.write(response.toByteArray()) var username = ""
println("API request handled: $inputData") var token = ""
os.close() 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
} }
} }
class CreateAccount : HttpHandler { tokenInDatabase = tokenInDatabase.replace(":", "")
override fun handle(t: HttpExchange) { if (token != tokenInDatabase) {
val path = t.requestURI.path return("Invalid token")
val inputData = path.substringAfter("/api/createAccount/") }
// Send back message history
val chatHistoryView = File("chatHistory")
return(chatHistoryView.readText())
}
fun createAccount(inputData: String): String {
println("Account creation request recieved: $inputData") println("Account creation request recieved: $inputData")
// Parse data sent to the server by client // Parse data sent to the server by client
var username = "" var username = ""
var token = "" var token = ""
@ -174,46 +197,112 @@ class CreateAccount : HttpHandler {
var userExists = 0 var userExists = 0
// Search the user database to find required information about the user // Search the user database to find required information about the user
var response = ""
userDatabaseParser.forEachLine { line -> userDatabaseParser.forEachLine { line ->
if (line.contains(username)) { if (line.contains(username)) {
val response = "That username already exists on the server! Please choose a different username" response = "Username already exists"
t.sendResponseHeaders(422, response.length.toLong())
val os = t.responseBody
os.write(response.toByteArray())
os.close()
userExists = 1
} }
lineNumber++ lineNumber++
} }
if (response != "") {
return(response)
}
userDatabaseParser.close() userDatabaseParser.close()
if (userExists == 1) {return}
println("$username:$token")
if (username == "") { if (username == "") {
val response = "Please input a username" return("No username")
t.sendResponseHeaders(422, response.length.toLong())
val os = t.responseBody
os.write(response.toByteArray())
os.close()
return
} }
if (token == "") { if (token == "") {
val response = "Please input a password" return("No token")
t.sendResponseHeaders(422, response.length.toLong())
val os = t.responseBody
os.write(response.toByteArray())
os.close()
return
} }
val userDatabaseFile = File("userDatabase") val userDatabaseFile = File("userDatabase")
userDatabaseFile.appendText("${System.lineSeparator()}$username:$token") userDatabaseFile.appendText("${System.lineSeparator()}$username:$token")
val response = "Creating account was successful!" return("Success")
t.sendResponseHeaders(200, response.length.toLong()) }
val os = t.responseBody fun authKey(inputData: String): String {
os.write(response.toByteArray()) println("API request recieved: $inputData")
println("")
os.close() // Parse data sent to the server by client
var username = ""
var token = ""
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
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
return("Success")
}