Skip to content

Commit 02d1dbd

Browse files
committed
use whitelist command directly, read in whitelist.json for listing, readme with usage / info
1 parent deedcad commit 02d1dbd

File tree

5 files changed

+51
-28
lines changed

5 files changed

+51
-28
lines changed

README.md

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,31 @@
22

33
Enable Minecraft whitelist management from an exposed HTTP endpoint
44

5-
_wip_
5+
---
6+
7+
`GET / -> [] of {uuid: "...", name: "..."}` - Returns the `whitelist.json` of the server.
8+
`POST / ({name: "..."}) -> 201` - Adds a user to the whitelist via username.
9+
`DELETE / ({name: "..."}) -> 201` - Removes a user from the whitelist via username.
10+
11+
---
12+
13+
- Interfacing with the whitelist via Bukkit's API doesn't seem to work properly, so this plugin just calls the `whitelist` command directly, as you would via the server console.
14+
- Even if the server fails to whitelist a player, the response will still be a `201`, so it is assumed you hand the server a valid Minecraft username.
15+
- Default port is 7500, configurable via `<server dir>/plugins/<plugin dir>/config.yml`.
16+
- Everything is `application/json`.
17+
- Authentication is done via Bearer Tokens, any request must contain a valid Bearer Token to be served anything but a 403.
18+
19+
> `<server dir>/plugins/<plugin dir>/config.yml`:
20+
>
21+
> ```
22+
> bearer_token: <token>
23+
> ```
24+
>
25+
> Example HTTP request w/valid header:
26+
>
27+
> ```
28+
> GET / HTTP/x.x
29+
> Content-Type: application/json
30+
> Authorization: WHA <token>
31+
> Host: <host>:<port>
32+
> ```

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
<groupId>com.jackharrhy.whitelist</groupId>
88
<artifactId>whitelist</artifactId>
9-
<version>1.0-SNAPSHOT</version>
9+
<version>1.0</version>
1010
<packaging>jar</packaging>
1111

1212
<name>Whitelist HTTP API</name>

src/main/kotlin/com/jackharrhy/whitelist/WebServer.kt

Lines changed: 20 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,19 @@
11
package com.jackharrhy.whitelist
22

3+
import com.fasterxml.jackson.databind.ObjectMapper
34
import io.javalin.Javalin
4-
import io.javalin.http.BadRequestResponse
55
import io.javalin.http.UnauthorizedResponse
66
import org.bukkit.Bukkit
7-
import java.util.*
8-
import kotlin.collections.HashSet
7+
import java.io.File
8+
import java.lang.Integer.parseInt
99

10-
class WebServer(plugin: Whitelist, bearerToken: String) {
11-
private data class User(val uuid: UUID)
10+
11+
class WebServer(plugin: Whitelist) {
12+
private data class User(val name: String)
1213

1314
init {
14-
val port = 7500
15+
val port = parseInt(plugin.config.getString("port"))
16+
val bearerToken = plugin.config.getString("bearer_token")!!
1517

1618
val classLoader = Thread.currentThread().contextClassLoader
1719
Thread.currentThread().contextClassLoader = Whitelist::class.java.classLoader
@@ -30,34 +32,27 @@ class WebServer(plugin: Whitelist, bearerToken: String) {
3032
}
3133
}
3234

35+
val whitelistPath = plugin.dataFolder.absolutePath + "/../../whitelist.json"
3336
app.get("/") { ctx ->
34-
val players = HashSet<UUID>()
35-
for (player in Bukkit.getWhitelistedPlayers()) {
36-
players.add(player.uniqueId)
37-
}
38-
ctx.json(players)
37+
val whitelist = File(whitelistPath).readText()
38+
val mapper = ObjectMapper()
39+
ctx.json(mapper.readTree(whitelist))
3940
}
4041

4142
app.post("/") { ctx ->
42-
val uuid = ctx.body<User>().uuid
43-
val player = Bukkit.getOfflinePlayer(uuid)
44-
if (player.isWhitelisted) {
45-
throw BadRequestResponse("Already whitelisted!")
46-
} else {
47-
player.isWhitelisted = true
48-
ctx.status(201).result("")
43+
val name = ctx.body<User>().name
44+
Bukkit.getScheduler().callSyncMethod(plugin) {
45+
Bukkit.dispatchCommand(Bukkit.getConsoleSender(), "whitelist add $name")
4946
}
47+
ctx.status(201).result("")
5048
}
5149

5250
app.delete("/") { ctx ->
53-
val uuid = ctx.body<User>().uuid
54-
val player = Bukkit.getOfflinePlayer(uuid)
55-
if (player.isWhitelisted) {
56-
player.isWhitelisted = false
57-
ctx.status(201).result("")
58-
} else {
59-
throw BadRequestResponse("Not whitelisted!")
51+
val name = ctx.body<User>().name
52+
Bukkit.getScheduler().callSyncMethod(plugin) {
53+
Bukkit.dispatchCommand(Bukkit.getConsoleSender(), "whitelist remove $name")
6054
}
55+
ctx.status(201).result("")
6156
}
6257

6358
plugin.logger.info("WebServer now running on port $port")

src/main/kotlin/com/jackharrhy/whitelist/Whitelist.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ class Whitelist : JavaPlugin() {
1111
if (bearerToken.isNullOrEmpty() || bearerToken.equals("unset")) {
1212
logger.warning("bearer_token is unset, plugin useless!")
1313
} else {
14-
WebServer(this, bearerToken)
14+
WebServer(this)
1515
}
1616
}
1717

src/main/resources/config.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
bearer_token: unset
2+
port: 7500

0 commit comments

Comments
 (0)