feat(Access): added access list
Some checks failed
Build the Docker Image / docker (push) Has been cancelled

This commit is contained in:
Paul Fey 2025-06-07 14:54:38 +02:00
parent 9460828187
commit 02a88f53da
2 changed files with 35 additions and 1 deletions

View file

@ -3,12 +3,14 @@ package de.pauljako.cosmeticserver;
import de.craftsblock.craftsnet.addon.Addon;
import java.io.File;
import java.io.IOException;
public class CosmeticServer extends Addon {
private static CosmeticServer instance;
public File cosmeticFile;
public File accessFile;
private Thread thread;
public static CosmeticServer instance() {
@ -25,6 +27,24 @@ public class CosmeticServer extends Addon {
cosmeticFile = new File(getDataFolder(), "cosmetics.json");
if (!cosmeticFile.exists()) {
try {
cosmeticFile.createNewFile();
} catch (IOException e) {
throw new RuntimeException(e);
}
}
accessFile = new File(getDataFolder(), "access.json");
if (!accessFile.exists()) {
try {
accessFile.createNewFile();
} catch (IOException e) {
throw new RuntimeException(e);
}
}
routeRegistry().share("/v1/cosmetic/assets", assets);
CosmeticSocket socket = new CosmeticSocket();

View file

@ -15,6 +15,7 @@ import de.craftsblock.craftsnet.api.websocket.annotations.Socket;
import de.craftsblock.craftsnet.events.sockets.ClientDisconnectEvent;
import java.io.IOException;
import java.util.Collection;
import java.util.UUID;
import java.util.concurrent.ConcurrentHashMap;
@ -79,7 +80,9 @@ public class CosmeticSocket implements SocketHandler, ListenerAdapter {
UUID uuid = UUID.fromString(data.getString("uuid"));
ConcurrentHashMap<String, Object> cosmetics = new ConcurrentHashMap<>();
data.get("cosmetics").getAsJsonObject().entrySet().forEach(entry -> cosmetics.put(entry.getKey(), entry.getValue()));
data.get("cosmetics").getAsJsonObject().entrySet().forEach(entry -> {
if (isAllowedToHaveCosmetic(entry.getKey(), entry.getValue().toString(), uuid.toString())) cosmetics.put(entry.getKey(), entry.getValue());
});
ClientMapping mapping = new ClientMapping(uuid, cosmetics);
exchange.broadcast(JsonParser.parse("{}").set("uuid", uuid.toString()).set("cosmetics", bakeData(cosmetics)).toString());
@ -147,6 +150,17 @@ public class CosmeticSocket implements SocketHandler, ListenerAdapter {
return object.getObject();
}
private boolean isAllowedToHaveCosmetic(String type, String value, String uuid) {
Json access = JsonParser.parse(CosmeticServer.instance().accessFile);
String path = type.replace(".", "\\.") + "." + value.replace(".", "\\.");
if (!access.contains(type.replace(".", "\\.")) || !access.contains(path)) return true;
Collection<String> allowedUUIDs = access.getStringList(path);
return allowedUUIDs.contains(uuid.toUpperCase());
}
@EventHandler
public void handleDisconnect(ClientDisconnectEvent event) {
WebSocketClient client = event.getExchange().client();