2024-10-20 13:37:03 +11:00
|
|
|
package xyz.maxwellj.chookpen
|
|
|
|
|
2024-10-23 14:25:25 +11:00
|
|
|
import io.javalin.Javalin
|
|
|
|
|
2024-10-20 13:37:03 +11:00
|
|
|
import com.sun.net.httpserver.HttpExchange
|
|
|
|
import com.sun.net.httpserver.HttpHandler
|
|
|
|
import com.sun.net.httpserver.HttpServer
|
|
|
|
import java.net.InetSocketAddress
|
|
|
|
|
|
|
|
import java.io.File
|
|
|
|
import java.io.BufferedReader
|
|
|
|
|
|
|
|
fun main(args: Array<String>) {
|
2024-10-23 14:25:25 +11:00
|
|
|
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)
|
2024-10-20 13:37:03 +11:00
|
|
|
}
|
|
|
|
|
2024-10-23 14:25:25 +11:00
|
|
|
fun handleSentMessage(inputData: String): String {
|
|
|
|
println("API 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 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("That account does not exist on this server.")
|
|
|
|
}
|
|
|
|
|
|
|
|
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")
|
2024-10-20 13:37:03 +11:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-10-23 14:25:25 +11:00
|
|
|
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
|
2024-10-20 13:37:03 +11:00
|
|
|
}
|
|
|
|
}
|
2024-10-23 14:25:25 +11:00
|
|
|
} else {
|
|
|
|
dataType += character
|
2024-10-20 13:37:03 +11:00
|
|
|
}
|
2024-10-23 14:25:25 +11:00
|
|
|
}
|
|
|
|
val userDatabaseParser = BufferedReader(File("userDatabase").reader())
|
|
|
|
var lineNumber = 1
|
|
|
|
var userLine = ""
|
2024-10-20 13:37:03 +11:00
|
|
|
|
2024-10-23 14:25:25 +11:00
|
|
|
// Search the user database to find required information about the user
|
|
|
|
userDatabaseParser.forEachLine { line ->
|
|
|
|
if (line.contains(username)) {
|
|
|
|
userLine = line
|
2024-10-20 13:37:03 +11:00
|
|
|
}
|
2024-10-23 14:25:25 +11:00
|
|
|
lineNumber++
|
|
|
|
}
|
|
|
|
userDatabaseParser.close()
|
2024-10-20 13:37:03 +11:00
|
|
|
|
2024-10-23 14:25:25 +11:00
|
|
|
if (userLine == "") {
|
|
|
|
return("Account not found")
|
|
|
|
}
|
2024-10-20 13:37:03 +11:00
|
|
|
|
2024-10-23 14:25:25 +11:00
|
|
|
var usernameInDatabase = ""
|
|
|
|
var tokenInDatabase = ""
|
|
|
|
var currentStage = 0
|
|
|
|
for (char in userLine) {
|
|
|
|
if (char == ':') {
|
|
|
|
currentStage ++
|
2024-10-20 13:37:03 +11:00
|
|
|
}
|
2024-10-23 14:25:25 +11:00
|
|
|
if (currentStage == 0) {
|
|
|
|
usernameInDatabase += char
|
|
|
|
} else if (currentStage == 1) {
|
|
|
|
tokenInDatabase += char
|
2024-10-20 13:37:03 +11:00
|
|
|
}
|
2024-10-23 14:25:25 +11:00
|
|
|
}
|
|
|
|
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
|
|
|
|
}
|
|
|
|
}
|
2024-10-20 13:37:03 +11:00
|
|
|
} else {
|
2024-10-23 14:25:25 +11:00
|
|
|
dataType += character
|
2024-10-20 13:37:03 +11:00
|
|
|
}
|
2024-10-23 14:25:25 +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)) {
|
|
|
|
response = "Username already exists"
|
2024-10-20 13:37:03 +11:00
|
|
|
}
|
2024-10-23 14:25:25 +11:00
|
|
|
lineNumber++
|
|
|
|
}
|
|
|
|
if (response != "") {
|
|
|
|
return(response)
|
|
|
|
}
|
|
|
|
userDatabaseParser.close()
|
|
|
|
if (username == "") {
|
|
|
|
return("No username")
|
|
|
|
}
|
2024-10-20 13:37:03 +11:00
|
|
|
|
2024-10-23 14:25:25 +11:00
|
|
|
if (token == "") {
|
|
|
|
return("No token")
|
2024-10-20 13:37:03 +11:00
|
|
|
}
|
2024-10-23 14:25:25 +11:00
|
|
|
|
|
|
|
val userDatabaseFile = File("userDatabase")
|
|
|
|
userDatabaseFile.appendText("${System.lineSeparator()}$username:$token")
|
|
|
|
return("Success")
|
2024-10-20 13:37:03 +11:00
|
|
|
}
|
2024-10-23 14:25:25 +11:00
|
|
|
fun authKey(inputData: String): String {
|
|
|
|
println("API request recieved: $inputData")
|
2024-10-20 13:37:03 +11:00
|
|
|
|
2024-10-23 14:25:25 +11:00
|
|
|
// 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
|
2024-10-20 13:37:03 +11:00
|
|
|
}
|
|
|
|
}
|
2024-10-23 14:25:25 +11:00
|
|
|
} else {
|
|
|
|
dataType += character
|
2024-10-20 13:37:03 +11:00
|
|
|
}
|
2024-10-23 14:25:25 +11:00
|
|
|
}
|
|
|
|
val userDatabaseParser = BufferedReader(File("userDatabase").reader())
|
|
|
|
var lineNumber = 1
|
|
|
|
var userLine = ""
|
2024-10-20 13:37:03 +11:00
|
|
|
|
2024-10-23 14:25:25 +11:00
|
|
|
// Search the user database to find required information about the user
|
|
|
|
userDatabaseParser.forEachLine { line ->
|
|
|
|
if (line.contains(username)) {
|
|
|
|
userLine = line
|
2024-10-20 13:37:03 +11:00
|
|
|
}
|
2024-10-23 14:25:25 +11:00
|
|
|
lineNumber++
|
|
|
|
}
|
|
|
|
userDatabaseParser.close()
|
2024-10-20 13:37:03 +11:00
|
|
|
|
2024-10-23 14:25:25 +11:00
|
|
|
if (userLine == "") {
|
|
|
|
return("Account not found")
|
|
|
|
}
|
|
|
|
|
|
|
|
var usernameInDatabase = ""
|
|
|
|
var tokenInDatabase = ""
|
|
|
|
var currentStage = 0
|
|
|
|
for (char in userLine) {
|
|
|
|
if (char == ':') {
|
|
|
|
currentStage ++
|
2024-10-20 13:37:03 +11:00
|
|
|
}
|
2024-10-23 14:25:25 +11:00
|
|
|
if (currentStage == 0) {
|
|
|
|
usernameInDatabase += char
|
|
|
|
} else if (currentStage == 1) {
|
|
|
|
tokenInDatabase += char
|
2024-10-20 13:37:03 +11:00
|
|
|
}
|
|
|
|
}
|
2024-10-23 14:25:25 +11:00
|
|
|
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")
|
2024-10-20 13:37:03 +11:00
|
|
|
}
|
2024-10-23 14:25:25 +11:00
|
|
|
|