Skip to content

Commit 0925b29

Browse files
authored
Merge pull request #1 from carbon-host/staging
Stepen's PR of Velocity, Bungee, Waterfall, and auth.
2 parents 6210a89 + ddc7370 commit 0925b29

12 files changed

Lines changed: 271 additions & 26 deletions

File tree

bungee/build.gradle.kts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
11
dependencies {
2+
compileOnly(projects.carbonCommon)
3+
compileOnly(libs.spark)
24
compileOnly(libs.bungee)
5+
implementation(libs.snakeYaml)
36
}
Lines changed: 44 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,49 @@
11
package host.carbon.plugin.bungee
22

3+
import host.carbon.common.KtorManager
34
import net.md_5.bungee.api.plugin.Plugin
5+
import net.md_5.bungee.config.Configuration
6+
import net.md_5.bungee.config.ConfigurationProvider
7+
import net.md_5.bungee.config.YamlConfiguration
8+
import org.yaml.snakeyaml.Yaml
9+
import java.io.File
10+
import java.util.*
411

5-
class CarbonBungeePlugin: Plugin() {
6-
override fun onEnable() {}
7-
override fun onDisable() {}
12+
class CarbonBungeePlugin : Plugin() {
13+
private lateinit var ktorManager: KtorManager
14+
15+
override fun onEnable() {
16+
if (!dataFolder.exists()) {
17+
dataFolder.mkdir()
18+
}
19+
20+
var port = 25505
21+
var carbonKey = UUID.randomUUID().toString()
22+
23+
val configFile = File(dataFolder, "config.yml")
24+
if (configFile.exists()) {
25+
val config = ConfigurationProvider.getProvider(YamlConfiguration::class.java).load(configFile)
26+
port = config.getInt("port")
27+
carbonKey = config.getString("carbon-key")
28+
} else {
29+
configFile.createNewFile()
30+
val config = ConfigurationProvider.getProvider(YamlConfiguration::class.java).load(configFile)
31+
config.set("port", port)
32+
config.set("carbon-key", carbonKey)
33+
ConfigurationProvider.getProvider(YamlConfiguration::class.java).save(config, configFile)
34+
}
35+
36+
ktorManager = KtorManager(CarbonPluginAPI(), port, carbonKey)
37+
ktorManager.startServer()
38+
39+
logger.info("Carbon API started on port $port")
40+
41+
}
42+
43+
override fun onDisable() {
44+
if (::ktorManager.isInitialized) {
45+
ktorManager.stopServer()
46+
logger.info("Carbon API stopped")
47+
}
48+
}
849
}
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
package host.carbon.plugin.bungee
2+
3+
import host.carbon.common.CarbonAPI
4+
import host.carbon.common.types.players.PlayerInfo
5+
import net.md_5.bungee.api.ProxyServer
6+
import java.lang.management.ManagementFactory
7+
8+
class CarbonPluginAPI : CarbonAPI() {
9+
override fun getMaxPlayers(): Int {
10+
return ProxyServer.getInstance().config.playerLimit
11+
}
12+
13+
override fun getOnlinePlayerCount(): Int {
14+
return ProxyServer.getInstance().onlineCount
15+
}
16+
17+
override fun getOnlinePlayers(): List<PlayerInfo> {
18+
return ProxyServer.getInstance().players
19+
.map { player ->
20+
PlayerInfo(
21+
player.name,
22+
player.uniqueId.toString(),
23+
null,
24+
null,
25+
null,
26+
null,
27+
null,
28+
player.ping,
29+
null
30+
)
31+
}
32+
}
33+
34+
override fun getCommands(query: String?): List<String> {
35+
return ProxyServer.getInstance().pluginManager.commands.map { it.key }.filter { query == null || it.startsWith(query, true) }
36+
}
37+
38+
override fun getTPS(): Double? {
39+
return null
40+
}
41+
42+
override fun getMSPT(): Double? {
43+
return null
44+
}
45+
46+
override fun getMemoryUsage(): Long {
47+
return ManagementFactory.getMemoryMXBean().heapMemoryUsage.used
48+
}
49+
50+
override fun getTotalMemory(): Long {
51+
return ManagementFactory.getMemoryMXBean().heapMemoryUsage.max
52+
}
53+
54+
override fun getCPUUsage(): Double {
55+
return (ManagementFactory.getPlatformMXBean(com.sun.management.OperatingSystemMXBean::class.java).processCpuLoad * 100)
56+
}
57+
58+
override fun getCPUCores(): Double {
59+
return Runtime.getRuntime().availableProcessors().toDouble()
60+
}
61+
}

common/src/main/kotlin/host/carbon/common/KtorManager.kt

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,21 @@ import io.ktor.server.plugins.contentnegotiation.*
1313
import io.ktor.server.plugins.cors.routing.*
1414
import io.ktor.server.response.*
1515
import io.ktor.server.routing.*
16+
import io.ktor.http.*
1617

18+
class KtorManager(private val carbonAPI: CarbonAPI, port: Int, private val carbonKey: String) {
1719

18-
class KtorManager(private val carbonAPI: CarbonAPI, private val port: Int) {
20+
private val keyAuth = createRouteScopedPlugin("keyAuth") {
21+
onCall { call ->
22+
val authHeader = call.request.headers["Authorization"]
23+
val key = authHeader?.removePrefix("Bearer ")?.trim()
24+
25+
if (key != carbonKey) {
26+
call.respond(HttpStatusCode.Unauthorized, "Invalid or missing API key")
27+
return@onCall
28+
}
29+
}
30+
}
1931

2032
private val server = embeddedServer(
2133
Netty,
@@ -34,6 +46,8 @@ class KtorManager(private val carbonAPI: CarbonAPI, private val port: Int) {
3446
}
3547

3648
route("/v1") {
49+
install(keyAuth)
50+
3751
get {
3852
val info = ServerInfo(
3953
carbonAPI.getTPS(),
@@ -67,7 +81,6 @@ class KtorManager(private val carbonAPI: CarbonAPI, private val port: Int) {
6781
)
6882
}
6983

70-
// TODO: Implement pagination
7184
get("/commands") {
7285
val limit = call.request.queryParameters["limit"]?.toInt() ?: 250
7386
val offset = call.request.queryParameters["offset"]?.toInt() ?: 0
@@ -93,5 +106,4 @@ class KtorManager(private val carbonAPI: CarbonAPI, private val port: Int) {
93106
fun stopServer() {
94107
server.stop()
95108
}
96-
97109
}

common/src/main/kotlin/host/carbon/common/types/players/PlayerInfo.kt

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,15 @@ data class PlayerInfo(
44
val name: String,
55
val uuid: String,
66

7-
val firstPlayed: Long,
8-
val lastSeen: Long,
9-
val lastLogin: Long,
7+
val firstPlayed: Long?,
8+
val lastSeen: Long?,
9+
val lastLogin: Long?,
1010

11-
val isOp: Boolean,
12-
val idleDuration: Long,
11+
val isOp: Boolean?,
12+
val idleDuration: Long?,
1313
val ping: Int,
1414

15-
val location: PlayerLocation,
16-
15+
val location: PlayerLocation?
1716
)
1817

1918
data class PlayerLocation(

spigot/src/main/kotlin/host/carbon/plugin/spigot/CarbonPlugin.kt

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,34 @@
11
package host.carbon.plugin.spigot
22

33
import host.carbon.common.KtorManager
4-
import org.bukkit.Bukkit
5-
import org.bukkit.command.Command
6-
import org.bukkit.command.CommandSender
7-
import org.bukkit.command.SimpleCommandMap
8-
import org.bukkit.command.TabExecutor
94
import org.bukkit.plugin.java.JavaPlugin
10-
5+
import java.io.File
6+
import java.util.*
117

128
class CarbonPlugin : JavaPlugin() {
139
private lateinit var ktorManager: KtorManager
1410

1511
override fun onEnable() {
16-
saveConfig()
12+
if (!dataFolder.exists()) {
13+
dataFolder.mkdir()
14+
}
15+
16+
var port = 25505
17+
var carbonKey = UUID.randomUUID().toString()
1718

18-
val port = config.getInt("port")
19+
val configFile = File(dataFolder, "config.yml")
20+
if (configFile.exists()) {
21+
port = config.getInt("port")
22+
carbonKey = config.getString("carbon-key")!!
23+
} else {
24+
configFile.createNewFile()
25+
26+
config.set("port", 25505)
27+
config.set("carbon-key", carbonKey)
28+
saveConfig()
29+
}
1930

20-
ktorManager = KtorManager(CarbonPluginAPI(), port)
31+
ktorManager = KtorManager(CarbonPluginAPI(), port, carbonKey)
2132
ktorManager.startServer()
2233

2334
logger.info("Carbon API started on port $port")

spigot/src/main/kotlin/host/carbon/plugin/spigot/CarbonPluginAPI.kt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,5 +68,4 @@ class CarbonPluginAPI : CarbonAPI() {
6868
override fun getCPUCores(): Double {
6969
return Runtime.getRuntime().availableProcessors().toDouble()
7070
}
71-
7271
}

spigot/src/main/resources/config.yml

Lines changed: 0 additions & 1 deletion
This file was deleted.

velocity/build.gradle.kts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ dependencies {
88
exclude("com.velocitypowered", "velocity-brigadier")
99
}
1010
annotationProcessor(libs.velocity)
11+
compileOnly(projects.carbonCommon)
12+
implementation(libs.snakeYaml)
1113
}
1214

1315
//sourceSets {
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
package host.carbon.plugin.velocity
2+
3+
import com.velocitypowered.api.proxy.ProxyServer
4+
import host.carbon.common.CarbonAPI
5+
import host.carbon.common.types.players.PlayerInfo
6+
import java.lang.management.ManagementFactory
7+
8+
class CarbonPluginAPI(private val server: ProxyServer) : CarbonAPI() {
9+
override fun getMaxPlayers(): Int {
10+
return server.configuration.showMaxPlayers
11+
}
12+
13+
override fun getOnlinePlayerCount(): Int {
14+
return server.playerCount
15+
}
16+
17+
override fun getOnlinePlayers(): List<PlayerInfo> {
18+
return server.allPlayers
19+
.map { player ->
20+
PlayerInfo(
21+
player.username,
22+
player.uniqueId.toString(),
23+
null,
24+
null,
25+
null,
26+
null,
27+
null,
28+
player.ping.toInt(),
29+
null
30+
)
31+
}
32+
}
33+
34+
override fun getCommands(query: String?): List<String> {
35+
return server.commandManager.aliases.filter { query == null || it.startsWith(query, true) }
36+
}
37+
38+
override fun getTPS(): Double? {
39+
return null
40+
}
41+
42+
override fun getMSPT(): Double? {
43+
return null
44+
}
45+
46+
override fun getMemoryUsage(): Long {
47+
return ManagementFactory.getMemoryMXBean().heapMemoryUsage.used
48+
}
49+
50+
override fun getTotalMemory(): Long {
51+
return ManagementFactory.getMemoryMXBean().heapMemoryUsage.max
52+
}
53+
54+
override fun getCPUUsage(): Double {
55+
return (ManagementFactory.getPlatformMXBean(com.sun.management.OperatingSystemMXBean::class.java).processCpuLoad * 100)
56+
}
57+
58+
override fun getCPUCores(): Double {
59+
return Runtime.getRuntime().availableProcessors().toDouble()
60+
}
61+
}

0 commit comments

Comments
 (0)