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/.gradle
server/userDatabase
server/messageHistory
server/chatHistory
client-cli/build
client-cli/.gradle
client-cli/.chookpen.profile

View File

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

View File

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