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,211 +11,298 @@ 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) { println("API request recieved: $inputData")
val response = "Testing, testing, 1, 2, 3... Somehow this is going to make it to production one day" // Parse data sent to the server by client
t.sendResponseHeaders(200, response.length.toLong()) var username = ""
val os = t.responseBody var token = ""
os.write(response.toByteArray()) var message = ""
println("Test request recieved!") var dataType = ""
os.close() var isParsingData = 0
} for (char in inputData) {
} val character = char
if (character == ':') {
class ApiHandler : HttpHandler { isParsingData = 1
override fun handle(t: HttpExchange) { } else if (isParsingData == 1) {
println("Request recieved...") if (character == '}') {
// Get the data isParsingData = 0
val path = t.requestURI.path dataType = ""
val inputData = path.substringAfter("/api/") } else if (character != '{') {
println("API request recieved: $inputData") if (dataType == "username") {
username += character
// Parse data sent to the server by client } else if (dataType == "token") {
var username = "" token += character
var token = "" } else if (dataType == "message") {
var message = "" message += character
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
}
} }
} 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 == "") {
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
}
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) {
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
}
// Make the message to respond to the client
val chatHistoryView = File("chatHistory")
var fullMessage = ""
if (message != "") {
fullMessage = "${chatHistoryView.readText()}$username: $message"
// Add the client's message to the chat history
val chatHistory = File("chatHistory")
chatHistory.appendText("$username: $message ${System.lineSeparator()}")
message = ""
} else { } else {
fullMessage = "${chatHistoryView.readText()}" dataType += character
} }
val response = if (inputData.isNotEmpty()) { }
fullMessage val userDatabaseParser = BufferedReader(File("userDatabase").reader())
} else { var lineNumber = 1
"No data provided" 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()
// Send the message to the client if (userLine == "") {
t.sendResponseHeaders(200, response.length.toLong()) return("That account does not exist on this server.")
val os = t.responseBody }
os.write(response.toByteArray())
println("API request handled: $inputData")
os.close()
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! Please try putting in your password right")
}
// Make the message to respond to the client
val chatHistoryView = File("chatHistory")
var fullMessage = ""
if (message != "") {
fullMessage = "${chatHistoryView.readText()}$username: $message"
// Add the client's message to the chat history
val chatHistory = File("chatHistory")
chatHistory.appendText("$username: $message ${System.lineSeparator()}")
message = ""
return("Success")
} else {
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")
// Parse data sent to the server by client fun syncMessages(inputData: String): String {
var username = "" println("API request recieved: $inputData")
var token = "" // Parse data sent to the server by client
var message = "" var username = ""
var dataType = "" var token = ""
var isParsingData = 0 var dataType = ""
for (char in inputData) { var isParsingData = 0
val character = char for (char in inputData) {
if (character == ':') { val character = char
isParsingData = 1 if (character == ':') {
} else if (isParsingData == 1) { isParsingData = 1
if (character == '}') { } else if (isParsingData == 1) {
isParsingData = 0 if (character == '}') {
dataType = "" isParsingData = 0
} else if (character != '{') { dataType = ""
if (dataType == "username") { } else if (character != '{') {
username += character if (dataType == "username") {
} else if (dataType == "token") { username += character
token += character } else if (dataType == "token") {
} else if (dataType == "message") { token += character
message += character
}
} }
} else {
dataType += character
} }
} else {
dataType += character
} }
val userDatabaseParser = BufferedReader(File("userDatabase").reader())
var lineNumber = 1
var userExists = 0
// Search the user database to find required information about the user
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
}
lineNumber++
}
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
}
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
}
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()
} }
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 = ""
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
}
}
} else {
dataType += character
}
}
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)) {
response = "Username already exists"
}
lineNumber++
}
if (response != "") {
return(response)
}
userDatabaseParser.close()
if (username == "") {
return("No username")
}
if (token == "") {
return("No token")
}
val userDatabaseFile = File("userDatabase")
userDatabaseFile.appendText("${System.lineSeparator()}$username:$token")
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")
}