Skip to content

Commit

Permalink
Add Authentication and config handler to web api | other small tweaks
Browse files Browse the repository at this point in the history
  • Loading branch information
spartacus04 committed Feb 8, 2024
1 parent a286b13 commit 9943705
Show file tree
Hide file tree
Showing 17 changed files with 366 additions and 23 deletions.
8 changes: 3 additions & 5 deletions src/main/java/me/spartacus04/jext/Jext.kt
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
package me.spartacus04.jext

import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.cancel
import me.spartacus04.jext.State.CONFIG
import me.spartacus04.jext.State.DISCS
import me.spartacus04.jext.State.INTEGRATIONS
Expand Down Expand Up @@ -41,7 +39,9 @@ internal class Jext : JavaPlugin() {
}

private fun load() {
DISCS.registerDiscSource(FileSource())
DISCS.registerDiscSource(FileSource()) {
JukeboxGuiContainer.loadFromFile()
}
ListenerRegistrant.registerListeners()
INTEGRATIONS.reloadDefaultIntegrations()

Expand All @@ -57,7 +57,5 @@ internal class Jext : JavaPlugin() {
}
}
}

JukeboxGuiContainer.loadFromFile()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ enum class FieldGuiStyle {
@SerializedName("page-vertical")
PAGE_VERTICAL;

fun toGuiStyle(): String = this.name.replace("_", "-").lowercase()

companion object {
fun fromString(name: String): FieldGuiStyle {
return FieldGuiStyle.entries.find { it.name == name || it.name == name.replace("-", "_").uppercase() } ?: throw IllegalArgumentException("Invalid serialized name")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ enum class FieldJukeboxBehaviour {
@SerializedName("gui")
GUI;

fun toJukeboxBehaviour(): String = this.name.lowercase()

companion object {
fun fromString(name: String): FieldJukeboxBehaviour {
return entries.find { it.name == name || it.name == name.replace("-", "_").uppercase() } ?: throw IllegalArgumentException("Invalid serialized name")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,14 @@ enum class FieldLanguageMode {
pl_pl, pt_br, pt_pt, ro_ro, ru_ru, sr_sp,
sv_se, tr_tr, uk_ua, vi_vn, zh_cn, zh_tw;

fun toLocale(): String {
return if(this == AUTO || this == SILENT || this == CUSTOM) {
this.name
} else {
this.name.replace("_", "-")
}.lowercase()
}

companion object {
fun fromString(name: String): FieldLanguageMode {
// return enum value, name can either be the actual name or the serialized name
Expand Down
9 changes: 5 additions & 4 deletions src/main/java/me/spartacus04/jext/discs/DiscManager.kt
Original file line number Diff line number Diff line change
Expand Up @@ -11,23 +11,24 @@ class DiscManager : Iterable<Disc> {
private val discSources = arrayListOf<DiscSource>()
private var discs: ArrayList<Disc> = arrayListOf()

fun reloadDiscs() {
fun reloadDiscs(onReload: (() -> Unit)? = null) {
discs.clear()

SCHEDULER.runTaskAsynchronously {
CoroutineScope(Dispatchers.Default).launch {
val discs = CoroutineScope(Dispatchers.Default).launch {
discSources.forEach {
discs.addAll(it.getDiscs())
}
}

discs.invokeOnCompletion { onReload?.invoke() }
}
}

fun registerDiscSource(vararg discSources: DiscSource) {
fun registerDiscSource(vararg discSources: DiscSource, onReload: (() -> Unit)? = null) {
this.discSources.addAll(discSources)

reloadDiscs()
reloadDiscs(onReload)
}

override fun iterator(): Iterator<Disc> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,6 @@ class LanguageManager {
const val BEDROCK_NOT_SUPPORTED = "[§cJEXT§f] §cJukebox GUI is not supported on bedrock edition!"
const val WEBAPI_RESOURCEPACK_NOT_FOUND = "[§cJEXT§f] §cresource-pack.zip not found, please provide it in the plugin directory"
const val WEBSERVER_STARTED = "[§aJEXT§f] §aWebserver started on port %port%"
const val WEBSERVER_STOPPED = "aJEXT§f] §aWebserver stopped"
const val WEBSERVER_STOPPED = "eJEXT§f] §eWebserver stopped"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,9 @@ internal class RecordPacketEvent : JextPacketListener(packetType = PacketType.Pl
fun actionBarDisplay(player: Player, container: Disc) {
player.spigot().sendMessage(
ChatMessageType.ACTION_BAR,
*TextComponent.fromLegacyText(
LANG.getKey(player, "now-playing", mapOf(
"name" to container.displayName
))
)
TextComponent(LANG.getKey(player, "now-playing", mapOf(
"name" to container.displayName
)))
)
}
}
16 changes: 16 additions & 0 deletions src/main/java/me/spartacus04/jext/utils/FileBind.kt
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,22 @@ open class FileBind(@Transient private val filePath: String, @Transient private
}
}

fun fromText(text: String) : Boolean {
try {
val obj = gson.fromJson(text, typeToken)

obj.javaClass.declaredFields.forEach { field ->
field.isAccessible = true

field.set(this, field.get(obj))
}

return true
} catch (_: Exception) {
return false
}
}

fun save() {
val text = gson.toJson(this)

Expand Down
30 changes: 30 additions & 0 deletions src/main/java/me/spartacus04/jext/webapi/JextWebServer.kt
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,11 @@ import me.spartacus04.jext.State.SCHEDULER
import me.spartacus04.jext.language.LanguageManager.Companion.WEBAPI_RESOURCEPACK_NOT_FOUND
import me.spartacus04.jext.language.LanguageManager.Companion.WEBSERVER_STARTED
import me.spartacus04.jext.language.LanguageManager.Companion.WEBSERVER_STOPPED
import me.spartacus04.jext.webapi.auth.ConnectHandler
import me.spartacus04.jext.webapi.auth.DisconnectHandler
import me.spartacus04.jext.webapi.auth.HealthHandler
import me.spartacus04.jext.webapi.config.ConfigApplyHandler
import me.spartacus04.jext.webapi.config.ConfigReadHandler
import org.bukkit.Bukkit
import java.net.InetSocketAddress

Expand Down Expand Up @@ -49,6 +54,15 @@ class JextWebServer {
Bukkit.getConsoleSender().sendMessage(WEBAPI_RESOURCEPACK_NOT_FOUND)
}
}

if(apiEnabled) {
server!!.createContext("/connect", ConnectHandler())
server!!.createContext("/disconnect", DisconnectHandler())
server!!.createContext("/health", HealthHandler())

server!!.createContext("/config/read", ConfigReadHandler())
server!!.createContext("/config/apply", ConfigApplyHandler())
}
}
}
}
Expand Down Expand Up @@ -79,6 +93,22 @@ class JextWebServer {
} else {
server!!.removeContext("/resource-pack.zip")
}

if(apiEnabled) {
server!!.createContext("/connect", ConnectHandler())
server!!.createContext("/disconnect", DisconnectHandler())
server!!.createContext("/health", HealthHandler())

server!!.createContext("/config/read", ConfigReadHandler())
server!!.createContext("/config/apply", ConfigApplyHandler())
} else {
server!!.removeContext("/connect")
server!!.removeContext("/disconnect")
server!!.removeContext("/health")

server!!.removeContext("/config/read")
server!!.removeContext("/config/apply")
}
}
} else {
stop()
Expand Down
12 changes: 5 additions & 7 deletions src/main/java/me/spartacus04/jext/webapi/ResourcePackHandler.kt
Original file line number Diff line number Diff line change
@@ -1,24 +1,22 @@
package me.spartacus04.jext.webapi

import com.sun.net.httpserver.HttpExchange
import com.sun.net.httpserver.HttpHandler
import me.spartacus04.jext.State.PLUGIN
import me.spartacus04.jext.webapi.utils.JextHttpHandler

class ResourcePackHandler : HttpHandler {
class ResourcePackHandler : JextHttpHandler(false) {
private val file = PLUGIN.dataFolder.resolve("resource-pack.zip")
override fun handle(exchange: HttpExchange) {
override fun onGet(exchange: HttpExchange) {
if(!file.exists()) {
exchange.sendResponseHeaders(404, 0)
exchange.close()
return
return notFound(exchange)
}

exchange.sendResponseHeaders(200, file.length())

file.inputStream().use { input ->
exchange.responseBody.use { output ->
input.copyTo(output)
}
}
exchange.close()
}
}
36 changes: 36 additions & 0 deletions src/main/java/me/spartacus04/jext/webapi/auth/ConnectHandler.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package me.spartacus04.jext.webapi.auth

import com.sun.net.httpserver.HttpExchange
import me.spartacus04.jext.State.CONFIG
import me.spartacus04.jext.webapi.utils.JextHttpHandler
import java.util.HashMap

class ConnectHandler : JextHttpHandler(false) {
override fun onPost(exchange: HttpExchange) {
val addr = exchange.remoteAddress.address.address.map { it.toInt() }.joinToString(".")
val body = exchange.requestBody.bufferedReader().readText()

if(body == CONFIG.WEB_INTERFACE_PASSWORD) {
val token = (0..64).map { (('a'..'z') + ('A'..'Z') + ('0'..'9')).random() }.joinToString("")

connectedHashMap[addr] = token

exchange.sendResponseHeaders(200, token.length.toLong())

exchange.responseBody.use { os ->
os.write(token.toByteArray())
}
} else {
val response = "401 Unauthorized"
exchange.sendResponseHeaders(401, response.length.toLong())

exchange.responseBody.use { os ->
os.write(response.toByteArray())
}
}
}

companion object {
internal val connectedHashMap = HashMap<String, String>()
}
}
14 changes: 14 additions & 0 deletions src/main/java/me/spartacus04/jext/webapi/auth/DisconnectHandler.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package me.spartacus04.jext.webapi.auth

import com.sun.net.httpserver.HttpExchange
import me.spartacus04.jext.webapi.utils.JextHttpHandler

class DisconnectHandler : JextHttpHandler(true) {
override fun onPost(exchange: HttpExchange) {
val addr = exchange.remoteAddress.address.address.map { it.toInt() }.joinToString(".")

ConnectHandler.connectedHashMap.remove(addr)

exchange.sendResponseHeaders(200, 0)
}
}
22 changes: 22 additions & 0 deletions src/main/java/me/spartacus04/jext/webapi/auth/HealthHandler.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package me.spartacus04.jext.webapi.auth

import com.sun.net.httpserver.HttpExchange
import me.spartacus04.jext.webapi.utils.JextHttpHandler

class HealthHandler : JextHttpHandler(false) {
override fun onGet(exchange: HttpExchange) {
val token = exchange.requestHeaders["Authorization"]?.first()?.replace("Bearer ", "")

if(token == null) {
exchange.sendResponseHeaders(200, 0)
} else {
val addr = exchange.remoteAddress.address.address.map { it.toInt() }.joinToString(".")

if(ConnectHandler.connectedHashMap[addr] == token) {
exchange.sendResponseHeaders(200, 0)
} else {
exchange.sendResponseHeaders(401, 0)
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package me.spartacus04.jext.webapi.config

import com.sun.net.httpserver.HttpExchange
import me.spartacus04.jext.State.CONFIG
import me.spartacus04.jext.State.WEBSERVER
import me.spartacus04.jext.utils.JextMetrics
import me.spartacus04.jext.webapi.utils.JextHttpHandler

class ConfigApplyHandler : JextHttpHandler(true) {
override fun onPost(exchange: HttpExchange) {
val body = exchange.requestBody.bufferedReader().use { it.readText() }

if(CONFIG.fromText(body)) {
CONFIG.save()

exchange.sendResponseHeaders(200, 0)
JextMetrics.reloadMetrics()
WEBSERVER.reload()
} else {
exchange.sendResponseHeaders(400, 0)
}
}
}
Loading

0 comments on commit 9943705

Please sign in to comment.