From 31933ee7183436584a069d82e8e06cde68e53ea3 Mon Sep 17 00:00:00 2001 From: Adrian Bergqvist Date: Mon, 20 Jun 2022 17:25:27 +0200 Subject: [PATCH] Config file works almost --- .../org/adde0109/ambassador/Ambassador.java | 90 +------------ .../adde0109/ambassador/AmbassadorConfig.java | 124 ++++++++++++++++++ .../ambassador/ForgeConnectionHandler.java | 20 --- .../ambassador/ForgeHandshakeDataHandler.java | 80 ++++++----- 4 files changed, 178 insertions(+), 136 deletions(-) create mode 100644 src/main/java/org/adde0109/ambassador/AmbassadorConfig.java delete mode 100644 src/main/java/org/adde0109/ambassador/ForgeConnectionHandler.java diff --git a/src/main/java/org/adde0109/ambassador/Ambassador.java b/src/main/java/org/adde0109/ambassador/Ambassador.java index b94642a..3ecc1bb 100644 --- a/src/main/java/org/adde0109/ambassador/Ambassador.java +++ b/src/main/java/org/adde0109/ambassador/Ambassador.java @@ -31,6 +31,7 @@ public class Ambassador { private final Logger logger; private final Path dataDirectory; private Optional forgeServer; + private AmbassadorConfig config; private static ForgeHandshakeDataHandler forgeHandshakeDataHandler; @@ -43,97 +44,16 @@ public class Ambassador { @Subscribe public void onProxyInitialization(ProxyInitializeEvent event) { - if(readOrCreateConfig()) { - forgeHandshakeDataHandler = new ForgeHandshakeDataHandler(forgeServer.get(),logger); + config = AmbassadorConfig.readOrCreateConfig(dataDirectory,server,logger); + if(config != null) { + forgeHandshakeDataHandler = new ForgeHandshakeDataHandler(config,logger); server.getEventManager().register(this, forgeHandshakeDataHandler); } else { - logger.info("Ambassador will be disabled because of errors"); + logger.warn("Ambassador will be disabled because of errors"); } } - private boolean readOrCreateConfig() { - try { - Files.createDirectories(dataDirectory); - Files.createFile(dataDirectory.resolve("forgeServer.toml")); - } - catch (FileAlreadyExistsException ignored) { - - } - catch (IOException e) { - logger.error("Config related error: " + e.toString()); - return false; - } - - try { - CommentedFileConfig config = CommentedFileConfig.builder(dataDirectory.resolve("forgeServer.toml")) - .defaultData(Ambassador.class.getClassLoader().getResource("default-ambassador.toml")) - .autosave() - .preserveInsertionOrder() - .sync() - .build(); - config.load(); - - CommentedConfig settingsConfig = config.get("Differentiators"); - - - Differentiators settings = new Differentiators(settingsConfig); - - logger.info(settings.differentiators.get("758").handshakeServer); - - config.save(); - } - catch (ParsingException e) { - logger.error("Config related error: " + e.toString()); - return false; - } - - - - //758 - 1.18.2 - - //754 - 1.16.5 - - forgeServer = server.getServer("lobby"); - return true; - } - - private static class Differentiators { - private Map differentiators = ImmutableMap.of( - "758", new DifferentiatorSettings(), - "754", new DifferentiatorSettings() - ); - private Differentiators(){ - } - - private Differentiators(CommentedConfig config) { - if (config != null) { - Map differentiators = new HashMap<>(); - for (UnmodifiableConfig.Entry entry : config.entrySet()) { - if (entry.getValue() instanceof CommentedConfig) { - differentiators.put(entry.getKey(),new DifferentiatorSettings(entry.getValue())); - } - } - this.differentiators = ImmutableMap.copyOf(differentiators); - } - } - } - - private static class DifferentiatorSettings { - private String handshakeServer = ""; - private boolean forced = false; - - private DifferentiatorSettings(){ - } - - private DifferentiatorSettings(CommentedConfig config) { - if (config != null) { - this.handshakeServer = config.getOrElse("forge-server", handshakeServer); - this.forced = config.getOrElse("forced",forced); - } - } - - } } diff --git a/src/main/java/org/adde0109/ambassador/AmbassadorConfig.java b/src/main/java/org/adde0109/ambassador/AmbassadorConfig.java new file mode 100644 index 0000000..9ec75c9 --- /dev/null +++ b/src/main/java/org/adde0109/ambassador/AmbassadorConfig.java @@ -0,0 +1,124 @@ +package org.adde0109.ambassador; + +import com.electronwill.nightconfig.core.CommentedConfig; +import com.electronwill.nightconfig.core.UnmodifiableConfig; +import com.electronwill.nightconfig.core.file.CommentedFileConfig; +import com.electronwill.nightconfig.core.io.ParsingException; +import com.google.common.collect.ImmutableMap; +import com.velocitypowered.api.proxy.ProxyServer; +import com.velocitypowered.api.proxy.server.RegisteredServer; +import org.slf4j.Logger; + +import java.io.IOException; +import java.nio.file.FileAlreadyExistsException; +import java.nio.file.Files; +import java.nio.file.Path; +import java.rmi.NoSuchObjectException; +import java.util.HashMap; +import java.util.Map; +import java.util.Objects; +import java.util.Optional; + +public class AmbassadorConfig { + + + private final ProxyServer server; + private final Logger logger; + private Differentiators settings; + + + private AmbassadorConfig(ProxyServer server,Logger logger) { + this.server = server; + this.logger = logger; + } + + + + public RegisteredServer getServer(int protocolVersion) { + return settings.differentiators.get(protocolVersion).handshakeServer; + } + + public boolean shouldHandle(int protocolVersion) { + return settings.differentiators.containsKey(protocolVersion); + } + + + + public static AmbassadorConfig readOrCreateConfig(Path dataDirectory,ProxyServer server, Logger logger) { + AmbassadorConfig ambassadorConfig = new AmbassadorConfig(server,logger); + try { + Files.createDirectories(dataDirectory); + + } + catch (FileAlreadyExistsException ignored) { + + } + catch (IOException e) { + logger.error("Config related error: " + e.toString()); + return null; + } + + try { + CommentedFileConfig config = CommentedFileConfig.builder(dataDirectory.resolve("ambassador.toml")) + .defaultData(Ambassador.class.getClassLoader().getResource("default-ambassador.toml")) + .autosave() + .preserveInsertionOrder() + .sync() + .build(); + config.load(); + + CommentedConfig settingsConfig = config.get("Differentiators"); + + + ambassadorConfig.settings = ambassadorConfig.new Differentiators(settingsConfig); + + + config.save(); + return ambassadorConfig; + } + catch (Exception e) { + logger.error("Config related error: " + e.toString()); + return null; + } + } + + private class Differentiators { + private Map differentiators = ImmutableMap.of( + 758, new DifferentiatorSettings(), + 754, new DifferentiatorSettings() + ); + private Differentiators(){ + } + + private Differentiators(CommentedConfig config) throws Exception { + if (config != null) { + Map differentiators = new HashMap<>(); + for (UnmodifiableConfig.Entry entry : config.entrySet()) { + if (entry.getValue() instanceof CommentedConfig) { + differentiators.put(Integer.decode(entry.getKey()),new DifferentiatorSettings(entry.getValue())); + } + } + this.differentiators = ImmutableMap.copyOf(differentiators); + } + } + } + + private class DifferentiatorSettings { + private RegisteredServer handshakeServer = null; + private boolean forced = false; + + private DifferentiatorSettings(){ + } + + private DifferentiatorSettings(CommentedConfig config) throws Exception { + if (config != null) { + String serverName = config.getOrElse("forge-server", ""); + if (!Objects.equals(serverName, "")) + this.handshakeServer = server.getServer(serverName) + .orElseThrow(() -> new Exception(serverName + "is not a registered server!")); + this.forced = config.getOrElse("forced",forced); + } + } + + } +} diff --git a/src/main/java/org/adde0109/ambassador/ForgeConnectionHandler.java b/src/main/java/org/adde0109/ambassador/ForgeConnectionHandler.java deleted file mode 100644 index 1a8d657..0000000 --- a/src/main/java/org/adde0109/ambassador/ForgeConnectionHandler.java +++ /dev/null @@ -1,20 +0,0 @@ -package org.adde0109.ambassador; - -import com.velocitypowered.api.event.Continuation; -import com.velocitypowered.api.event.connection.PreLoginEvent; -import com.velocitypowered.api.network.ProtocolVersion; - -public class ForgeConnectionHandler { - - - - public void onPreLogin(PreLoginEvent event, Continuation continuation) { - if(event.getConnection().getProtocolVersion() == ProtocolVersion.MINECRAFT_1_16_4) { - - } - else { - continuation.resume(); - } - } - -} diff --git a/src/main/java/org/adde0109/ambassador/ForgeHandshakeDataHandler.java b/src/main/java/org/adde0109/ambassador/ForgeHandshakeDataHandler.java index 81c5242..d2de2fd 100644 --- a/src/main/java/org/adde0109/ambassador/ForgeHandshakeDataHandler.java +++ b/src/main/java/org/adde0109/ambassador/ForgeHandshakeDataHandler.java @@ -5,60 +5,59 @@ import com.velocitypowered.api.event.Continuation; import com.velocitypowered.api.event.PostOrder; import com.velocitypowered.api.event.Subscribe; import com.velocitypowered.api.event.connection.PreLoginEvent; +import com.velocitypowered.api.event.permission.PermissionsSetupEvent; import com.velocitypowered.api.event.player.ServerLoginPluginMessageEvent; +import com.velocitypowered.api.proxy.InboundConnection; import com.velocitypowered.api.proxy.LoginPhaseConnection; +import com.velocitypowered.api.proxy.Player; import com.velocitypowered.api.proxy.messages.MinecraftChannelIdentifier; import com.velocitypowered.api.proxy.server.RegisteredServer; import com.velocitypowered.api.proxy.server.ServerPing; import com.velocitypowered.api.util.ModInfo; import java.io.EOFException; -import org.checkerframework.checker.nullness.qual.Nullable; +import java.util.*; +import java.util.concurrent.atomic.AtomicInteger; import org.slf4j.Logger; import java.nio.charset.StandardCharsets; -import java.util.ArrayList; -import java.util.Arrays; -import java.util.List; -import java.util.Objects; import java.util.concurrent.CompletableFuture; public class ForgeHandshakeDataHandler { - public handshakeReceiver.CachedServerHandshake cachedServerHandshake; + public Map cachedServerHandshake = new HashMap(); public byte[] recivedClientACK; - public byte[] recivedClientModlist; + public Map recivedClientModlist = new HashMap(); + public LoginPhaseConnection connection; + + private Collection pendingConnections = new HashSet(); private static final int PACKET_LENGTH_INDEX = 14; //length of "fml:handshake"+1 + private final AmbassadorConfig config; private final Logger logger; - private RegisteredServer handshakeServer; - public ForgeHandshakeDataHandler(RegisteredServer handshakeServer,Logger logger) { - this.handshakeServer = handshakeServer; + public ForgeHandshakeDataHandler(AmbassadorConfig config,Logger logger) { + this.config = config; this.logger = logger; } - @Subscribe(order = PostOrder.EARLY) - public void onPreLogin(PreLoginEvent event, Continuation continuation) { - if(handshakeServer.getPlayersConnected().isEmpty()) { - handshakeReceiver receiver = new handshakeReceiver(handshakeServer, logger); - receiver.downloadHandshake().thenAccept((p) -> { - cachedServerHandshake = p; - sendModlist(p.modListPacket,(LoginPhaseConnection) event.getConnection()); - sendOther(p.otherPackets,(LoginPhaseConnection) event.getConnection()); - continuation.resume(); - }); - } - else if(cachedServerHandshake != null) { - sendModlist(cachedServerHandshake.modListPacket, (LoginPhaseConnection) event.getConnection()); - sendOther(cachedServerHandshake.otherPackets,(LoginPhaseConnection) event.getConnection()); + @Subscribe + public void onPreLoginEvent(PreLoginEvent event, Continuation continuation) { + if (!config.shouldHandle(event.getConnection().getProtocolVersion().getProtocol())) { continuation.resume(); + return; } + RegisteredServer server = config.getServer(event.getConnection().getProtocolVersion().getProtocol()); + getHandshake(server).thenAccept(p -> { + sendModlist(p.modListPacket,(LoginPhaseConnection) event.getConnection(),server); + sendOther(p.otherPackets,(LoginPhaseConnection) event.getConnection()); + continuation.resume(); + }); } @Subscribe public void onServerLoginPluginMessageEvent(ServerLoginPluginMessageEvent event, Continuation continuation) { - if((recivedClientModlist == null) || (recivedClientACK == null) || (!Objects.equals(event.getIdentifier().getId(), "fml:loginwrapper"))) { + if((!recivedClientModlist.containsKey(event.getConnection().getServer())) || (recivedClientACK == null)) { continuation.resume(); return; } @@ -71,7 +70,7 @@ public class ForgeHandshakeDataHandler { int packetID = readVarInt(data); if(packetID == 1) { - event.setResult(ServerLoginPluginMessageEvent.ResponseResult.reply(recivedClientModlist)); + event.setResult(ServerLoginPluginMessageEvent.ResponseResult.reply(recivedClientModlist.get(event.getConnection().getServer()))); } else { event.setResult(ServerLoginPluginMessageEvent.ResponseResult.reply(recivedClientACK)); @@ -81,15 +80,34 @@ public class ForgeHandshakeDataHandler { } + public CompletableFuture getHandshake(RegisteredServer handshakeServer) { + CompletableFuture future; + if((handshakeServer.getPlayersConnected().isEmpty()) || (!cachedServerHandshake.containsKey(handshakeServer))) { + handshakeReceiver receiver = new handshakeReceiver(handshakeServer, logger); + future = receiver.downloadHandshake(); + future.thenAccept(p -> { + cachedServerHandshake.put(handshakeServer,p); + }); + return future; + } + else { + future = new CompletableFuture<>(); + future.complete(cachedServerHandshake.get(handshakeServer)); + return future; + } + } - private void sendModlist(byte[] modListPacket, LoginPhaseConnection connection) { - connection.sendLoginPluginMessage(MinecraftChannelIdentifier.create("fml","loginwrapper"), modListPacket, responseBody -> recivedClientModlist = responseBody); + private void sendModlist(byte[] modListPacket, LoginPhaseConnection connection, RegisteredServer server) { + connection.sendLoginPluginMessage(MinecraftChannelIdentifier.create("fml","loginwrapper"), modListPacket, responseBody -> recivedClientModlist.put(server,responseBody)); } private void sendOther(List otherPackets, LoginPhaseConnection connection) { - otherPackets.forEach((packet) -> { - connection.sendLoginPluginMessage(MinecraftChannelIdentifier.create("fml","loginwrapper"), packet, responseBody -> recivedClientACK = responseBody); - }); + for (int i = 0;i recivedClientACK = responseBody : responseBody -> { + pendingConnections.add(connection); + }); + } } public static int readVarInt(ByteArrayDataInput stream) {