Clean up whitelist changes

This commit is contained in:
LOOHP 2022-12-07 19:13:46 +00:00
parent f8bad7fa14
commit c720a0fd67
4 changed files with 68 additions and 104 deletions

View File

@ -433,29 +433,6 @@ public class Limbo {
return properties;
}
public void reloadAllowlist() {
properties.reloadAllowlist();
}
public boolean uuidIsAllowed(UUID requestedUuid) {
if (!properties.isEnforceAllowlist()) {
return true;
}
if (properties.uuidIsAllowed(requestedUuid)) {
if(!properties.isReducedDebugInfo()) {
Limbo.getInstance().getConsole().sendMessage(String.format("allowlist: %s allowed", requestedUuid.toString()));
}
return true;
}
if(!properties.isReducedDebugInfo()) {
Limbo.getInstance().getConsole().sendMessage(String.format("allowlist: %s not allowed", requestedUuid.toString()));
}
return false;
}
public ServerConnection getServerConnection() {
return server;
}

View File

@ -165,14 +165,16 @@ public class DefaultCommands implements CommandExecutor, TabCompletor {
}
return;
}
if (args[0].equalsIgnoreCase("allowlist")) {
if (sender.hasPermission("limboserver.allowlist")) {
if (args[0].equalsIgnoreCase("whitelist")) {
if (sender.hasPermission("limboserver.whitelist")) {
if (args.length != 2) {
sender.sendMessage(ChatColor.RED + "Invalid usage!");
} else if (!args[1].equalsIgnoreCase("reload")) {
sender.sendMessage(ChatColor.RED + "Invalid usage!");
} else {
Limbo.getInstance().reloadAllowlist();
Limbo.getInstance().getServerProperties().reloadWhitelist();
sender.sendMessage("Whitelist has been reloaded");
}
} else {
sender.sendMessage(ChatColor.RED + "You do not have permission to use that command!");

View File

@ -19,33 +19,6 @@
package com.loohp.limbo.file;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map.Entry;
import java.util.Optional;
import java.util.Properties;
import java.util.UUID;
import javax.imageio.ImageIO;
import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
import org.json.simple.parser.ParseException;
import com.google.common.collect.Lists;
import com.loohp.limbo.Console;
import com.loohp.limbo.Limbo;
@ -53,9 +26,28 @@ import com.loohp.limbo.location.Location;
import com.loohp.limbo.utils.GameMode;
import com.loohp.limbo.utils.NamespacedKey;
import com.loohp.limbo.world.World;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.serializer.gson.GsonComponentSerializer;
import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.util.HashSet;
import java.util.List;
import java.util.Map.Entry;
import java.util.Optional;
import java.util.Properties;
import java.util.Set;
import java.util.UUID;
public class ServerProperties {
@ -84,8 +76,8 @@ public class ServerProperties {
private int viewDistance;
private double ticksPerSecond;
private boolean handshakeVerbose;
private boolean enforceAllowlist;
private HashMap<UUID, Boolean> allowlist;
private boolean enforceWhitelist;
private Set<UUID> whitelist;
private String resourcePackSHA1;
private String resourcePackLink;
@ -106,7 +98,7 @@ public class ServerProperties {
defStream.close();
Properties prop = new Properties();
InputStreamReader stream = new InputStreamReader(new FileInputStream(file), StandardCharsets.UTF_8);
InputStreamReader stream = new InputStreamReader(Files.newInputStream(file.toPath()), StandardCharsets.UTF_8);
prop.load(stream);
stream.close();
@ -115,7 +107,7 @@ public class ServerProperties {
String value = entry.getValue().toString();
prop.putIfAbsent(key, value);
}
PrintWriter pw = new PrintWriter(new OutputStreamWriter(new FileOutputStream(file), StandardCharsets.UTF_8));
PrintWriter pw = new PrintWriter(new OutputStreamWriter(Files.newOutputStream(file.toPath()), StandardCharsets.UTF_8));
prop.store(pw, COMMENT);
pw.close();
@ -198,63 +190,53 @@ public class ServerProperties {
favicon = Optional.empty();
}
enforceAllowlist = Boolean.parseBoolean(prop.getProperty("enforce-allowlist"));
if (enforceAllowlist) {
reloadAllowlist();
enforceWhitelist = Boolean.parseBoolean(prop.getProperty("enforce-whitelist"));
if (enforceWhitelist) {
reloadWhitelist();
}
Limbo.getInstance().getConsole().sendMessage("Loaded server.properties");
}
public void reloadAllowlist() {
public void reloadWhitelist() {
Console console = Limbo.getInstance().getConsole();
allowlist = new HashMap<UUID, Boolean>();
whitelist = new HashSet<>();
try {
JSONParser parser = new JSONParser();
Object obj = parser.parse(new FileReader("allowlist.json"));
Object obj = parser.parse(new InputStreamReader(Files.newInputStream(new File("whitelist.json").toPath()), StandardCharsets.UTF_8));
if (!(obj instanceof JSONArray)) {
console.sendMessage("allowlist: expected [] got {}");
console.sendMessage("whitelist: expected [] got {}");
return;
}
JSONArray array = (JSONArray) obj;
Iterator<?> iter = array.iterator();
while (iter.hasNext()) {
Object o = iter.next();
if (!(o instanceof JSONObject)) {
console.sendMessage("allowlist: array element is not an object");
continue;
}
for (Object o : array) {
if (!(o instanceof JSONObject)) {
console.sendMessage("whitelist: array element is not an object");
continue;
}
JSONObject element = (JSONObject) o;
o = element.get("uuid");
if (o == null) {
console.sendMessage("allowlist: missing uuid attribute");
continue;
}
if (!(o instanceof String)) {
console.sendMessage("allowlist: uuid is not a string");
continue;
}
JSONObject element = (JSONObject) o;
o = element.get("uuid");
if (o == null) {
console.sendMessage("whitelist: missing uuid attribute");
continue;
}
if (!(o instanceof String)) {
console.sendMessage("whitelist: uuid is not a string");
continue;
}
String uuidStr = (String) o;
UUID allowedUuid = UUID.fromString(uuidStr);
allowlist.put(allowedUuid, true);
}
} catch (IllegalArgumentException e) {
console.sendMessage(e.toString());
} catch (FileNotFoundException e) {
console.sendMessage(String.format("allowlist: %s", e.toString()));
} catch (IOException e) {
console.sendMessage(String.format("allowlist: %s", e.toString()));
} catch (ParseException e) {
console.sendMessage(String.format(" allowlist: parse: %s", e.toString()));
UUID allowedUuid = UUID.fromString(uuidStr);
whitelist.add(allowedUuid);
}
} catch (Exception e) {
e.printStackTrace();
}
console.sendMessage("allowlist: reloaded");
}
public String getServerImplementationVersion() {
@ -365,15 +347,12 @@ public class ServerProperties {
return handshakeVerbose;
}
public boolean isEnforceAllowlist() {
return enforceAllowlist;
public boolean enforceWhitelist() {
return enforceWhitelist;
}
public boolean uuidIsAllowed(UUID requestedUuid) {
if (allowlist.containsKey(requestedUuid)) {
return true;
}
return false;
public boolean uuidWhitelisted(UUID uuid) {
return whitelist.contains(uuid);
}
public String getResourcePackLink() {

View File

@ -90,6 +90,7 @@ import com.loohp.limbo.world.BlockPosition;
import com.loohp.limbo.world.World;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.serializer.plain.PlainTextComponentSerializer;
import net.md_5.bungee.api.ChatColor;
import net.md_5.bungee.api.chat.BaseComponent;
import net.md_5.bungee.api.chat.TextComponent;
@ -231,6 +232,11 @@ public class ClientConnection extends Thread {
}
private void disconnectDuringLogin(Component reason) {
ServerProperties properties = Limbo.getInstance().getServerProperties();
if (!properties.isReducedDebugInfo()) {
String str = (properties.isLogPlayerIPAddresses() ? inetAddress.getHostName() : "<ip address withheld>") + ":" + clientSocket.getPort();
Limbo.getInstance().getConsole().sendMessage("[/" + str + "] <-> Player disconnected with the reason " + PlainTextComponentSerializer.plainText().serialize(reason));
}
try {
PacketLoginOutDisconnect packet = new PacketLoginOutDisconnect(reason);
sendPacket(packet);
@ -470,8 +476,8 @@ public class ClientConnection extends Thread {
UUID uuid = isBungeecord || isBungeeGuard ? bungeeUUID : UUID.nameUUIDFromBytes(("OfflinePlayer:" + username).getBytes(StandardCharsets.UTF_8));
if (!Limbo.getInstance().uuidIsAllowed(uuid)) {
disconnectDuringLogin(TextComponent.fromLegacyText("You are not invited to this server"));
if (!properties.enforceWhitelist() && properties.uuidWhitelisted(uuid)) {
disconnectDuringLogin(TextComponent.fromLegacyText("You are not whitelisted on the server"));
break;
}