From 2c4fc00ec3ea2b69eb67547cc0720a7168da47ba Mon Sep 17 00:00:00 2001 From: mohammed jasem alaajel Date: Mon, 25 Jul 2022 16:49:57 +0400 Subject: [PATCH] single class for loading config --- RedisBungee-API/pom.xml | 5 + .../redisbungee/api/config/ConfigLoader.java | 105 +++++++++++++++++- RedisBungee-Bungee/pom.xml | 5 - .../redisbungee/RedisBungeeBungeePlugin.java | 99 ++--------------- RedisBungee-Velocity/pom.xml | 23 +++- .../RedisBungeeVelocityListener.java | 1 - .../RedisBungeeVelocityPlugin.java | 86 +------------- 7 files changed, 138 insertions(+), 186 deletions(-) diff --git a/RedisBungee-API/pom.xml b/RedisBungee-API/pom.xml index 7694afd..2c4adf4 100644 --- a/RedisBungee-API/pom.xml +++ b/RedisBungee-API/pom.xml @@ -61,6 +61,11 @@ 4.13.2 test + + org.spongepowered + configurate-yaml + 3.7.2 + \ No newline at end of file diff --git a/RedisBungee-API/src/main/java/com/imaginarycode/minecraft/redisbungee/api/config/ConfigLoader.java b/RedisBungee-API/src/main/java/com/imaginarycode/minecraft/redisbungee/api/config/ConfigLoader.java index 4f099ef..4ee7eb3 100644 --- a/RedisBungee-API/src/main/java/com/imaginarycode/minecraft/redisbungee/api/config/ConfigLoader.java +++ b/RedisBungee-API/src/main/java/com/imaginarycode/minecraft/redisbungee/api/config/ConfigLoader.java @@ -1,15 +1,116 @@ package com.imaginarycode.minecraft.redisbungee.api.config; +import com.google.common.reflect.TypeToken; +import com.imaginarycode.minecraft.redisbungee.api.RedisBungeeMode; +import com.imaginarycode.minecraft.redisbungee.api.RedisBungeePlugin; +import com.imaginarycode.minecraft.redisbungee.api.summoners.ClusterJedisSummoner; +import com.imaginarycode.minecraft.redisbungee.api.summoners.JedisSummoner; +import com.imaginarycode.minecraft.redisbungee.api.summoners.Summoner; +import ninja.leaping.configurate.ConfigurationNode; +import ninja.leaping.configurate.objectmapping.ObjectMappingException; +import ninja.leaping.configurate.yaml.YAMLConfigurationLoader; +import org.apache.commons.pool2.impl.GenericObjectPoolConfig; +import redis.clients.jedis.*; + +import java.io.File; import java.io.IOException; import java.io.InputStream; import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.StandardCopyOption; -import java.util.UUID; +import java.util.*; public interface ConfigLoader { - void loadConfig() throws IOException; + + default void loadConfig(RedisBungeePlugin plugin, File dataFolder) throws IOException { + loadConfig(plugin, dataFolder.toPath()); + } + + default void loadConfig(RedisBungeePlugin plugin, Path dataFolder) throws IOException { + Path configFile = createConfigFile(dataFolder); + final YAMLConfigurationLoader yamlConfigurationFileLoader = YAMLConfigurationLoader.builder().setPath(configFile).build(); + ConfigurationNode node = yamlConfigurationFileLoader.load(); + if (node.getNode("config-version").getInt(0) != RedisBungeeConfiguration.CONFIG_VERSION) { + handleOldConfig(dataFolder); + node = yamlConfigurationFileLoader.load(); + } + final boolean useSSL = node.getNode("useSSL").getBoolean(false); + final boolean overrideBungeeCommands = node.getNode("override-bungee-commands").getBoolean(false); + final boolean registerLegacyCommands = node.getNode("register-legacy-commands").getBoolean(false); + String redisPassword = node.getNode("redis-password").getString(null); + String proxyId = node.getNode("proxy-id").getString("test-1"); + final int maxConnections = node.getNode("max-redis-connections").getInt(10); + List exemptAddresses; + try { + exemptAddresses = node.getNode("exempt-ip-addresses").getList(TypeToken.of(String.class)); + } catch (ObjectMappingException e) { + exemptAddresses = Collections.emptyList(); + } + + // check redis password + if (redisPassword != null && (redisPassword.isEmpty() || redisPassword.equals("none"))) { + redisPassword = null; + plugin.logWarn("INSECURE setup was detected Please set password for your redis instance."); + } else if (redisPassword == null) { + plugin.logWarn("INSECURE setup was detected Please set password for your redis instance."); + } + if (!useSSL) { + plugin.logWarn("INSECURE setup was detected Please setup ssl for your redis instance."); + } + // Configuration sanity checks. + if (proxyId == null || proxyId.isEmpty()) { + String genId = UUID.randomUUID().toString(); + plugin.logInfo("Generated proxy id " + genId + " and saving it to config."); + node.getNode("proxy-id").setValue(genId); + yamlConfigurationFileLoader.save(node); + proxyId = genId; + plugin.logInfo("proxy id was generated: " + proxyId); + } else { + plugin.logInfo("Loaded proxy id " + proxyId); + } + RedisBungeeConfiguration configuration = new RedisBungeeConfiguration(proxyId, exemptAddresses, registerLegacyCommands, overrideBungeeCommands); + Summoner summoner; + RedisBungeeMode redisBungeeMode; + if (node.getNode("cluster-mode-enabled").getBoolean(false)) { + plugin.logInfo("RedisBungee MODE: CLUSTER"); + Set hostAndPortSet = new HashSet<>(); + GenericObjectPoolConfig poolConfig = new GenericObjectPoolConfig<>(); + poolConfig.setMaxTotal(maxConnections); + node.getNode("redis-cluster-servers").getChildrenList().forEach((childNode) -> { + Map hostAndPort = childNode.getChildrenMap(); + String host = hostAndPort.get("host").getString(); + int port = hostAndPort.get("port").getInt(); + hostAndPortSet.add(new HostAndPort(host, port)); + }); + plugin.logInfo(hostAndPortSet.size() + " cluster nodes were specified"); + if (hostAndPortSet.isEmpty()) { + throw new RuntimeException("No redis cluster servers specified"); + } + if (redisPassword != null) { + summoner = new ClusterJedisSummoner(new JedisCluster(hostAndPortSet, 5000, 5000, 60, proxyId, redisPassword, poolConfig, useSSL)); + } else { + plugin.logWarn("SSL option is ignored in Cluster mode if no PASSWORD is set"); + summoner = new ClusterJedisSummoner(new JedisCluster(hostAndPortSet, 5000, 5000, 60, poolConfig)); + } + redisBungeeMode = RedisBungeeMode.CLUSTER; + } else { + plugin.logInfo("RedisBungee MODE: SINGLE"); + final String redisServer = node.getNode("redis-server").getString("127.0.0.1"); + final int redisPort = node.getNode("redis-port").getInt(6379); + if (redisServer != null && redisServer.isEmpty()) { + throw new RuntimeException("No redis server specified"); + } + JedisPoolConfig config = new JedisPoolConfig(); + config.setMaxTotal(maxConnections); + summoner = new JedisSummoner(new JedisPool(config, redisServer, redisPort, 0, redisPassword, useSSL)); + redisBungeeMode = RedisBungeeMode.SINGLE; + } + plugin.logInfo("Successfully connected to Redis."); + onConfigLoad(configuration, summoner, redisBungeeMode); + } + + void onConfigLoad(RedisBungeeConfiguration configuration, Summoner summoner, RedisBungeeMode mode); default Path createConfigFile(Path dataFolder) throws IOException { if (Files.notExists(dataFolder)) { diff --git a/RedisBungee-Bungee/pom.xml b/RedisBungee-Bungee/pom.xml index 8e013ab..3627920 100644 --- a/RedisBungee-Bungee/pom.xml +++ b/RedisBungee-Bungee/pom.xml @@ -116,11 +116,6 @@ jar provided - - org.spongepowered - configurate-yaml - 3.7.2 - \ No newline at end of file diff --git a/RedisBungee-Bungee/src/main/java/com/imaginarycode/minecraft/redisbungee/RedisBungeeBungeePlugin.java b/RedisBungee-Bungee/src/main/java/com/imaginarycode/minecraft/redisbungee/RedisBungeeBungeePlugin.java index 1a1c76b..8ede890 100644 --- a/RedisBungee-Bungee/src/main/java/com/imaginarycode/minecraft/redisbungee/RedisBungeeBungeePlugin.java +++ b/RedisBungee-Bungee/src/main/java/com/imaginarycode/minecraft/redisbungee/RedisBungeeBungeePlugin.java @@ -6,10 +6,7 @@ import com.google.common.collect.ImmutableList; import com.google.common.collect.ImmutableMultimap; import com.google.common.collect.ImmutableSet; import com.google.common.collect.Multimap; -import com.google.common.reflect.TypeToken; import com.imaginarycode.minecraft.redisbungee.api.config.RedisBungeeConfiguration; -import com.imaginarycode.minecraft.redisbungee.api.summoners.ClusterJedisSummoner; -import com.imaginarycode.minecraft.redisbungee.api.summoners.JedisSummoner; import com.imaginarycode.minecraft.redisbungee.api.tasks.HeartbeatTask; import com.imaginarycode.minecraft.redisbungee.api.tasks.RedisTask; import com.imaginarycode.minecraft.redisbungee.api.util.RedisUtil; @@ -33,10 +30,6 @@ import net.md_5.bungee.api.ProxyServer; import net.md_5.bungee.api.connection.ProxiedPlayer; import net.md_5.bungee.api.plugin.Event; import net.md_5.bungee.api.plugin.Plugin; -import ninja.leaping.configurate.ConfigurationNode; -import ninja.leaping.configurate.objectmapping.ObjectMappingException; -import ninja.leaping.configurate.yaml.YAMLConfigurationLoader; -import org.apache.commons.pool2.impl.GenericObjectPoolConfig; import redis.clients.jedis.*; import redis.clients.jedis.exceptions.JedisConnectionException; import redis.clients.jedis.exceptions.JedisException; @@ -44,7 +37,6 @@ import redis.clients.jedis.exceptions.JedisException; import java.io.*; import java.lang.reflect.Field; import java.net.InetAddress; -import java.nio.file.Path; import java.util.*; import java.util.concurrent.*; import java.util.concurrent.atomic.AtomicInteger; @@ -57,7 +49,7 @@ public class RedisBungeeBungeePlugin extends Plugin implements RedisBungeePlugin private RedisBungeeAPI api; private RedisBungeeMode redisBungeeMode; private PubSubListener psl = null; - private Summoner jedisSummoner; + private Summoner summoner; private UUIDTranslator uuidTranslator; private RedisBungeeConfiguration configuration; private BungeeDataManager dataManager; @@ -485,7 +477,7 @@ public class RedisBungeeBungeePlugin extends Plugin implements RedisBungeePlugin getLogger().log(Level.INFO, "skipping replacement....."); } try { - loadConfig(); + loadConfig(this, getDataFolder()); } catch (IOException e) { throw new RuntimeException("Unable to load/save config", e); } @@ -797,7 +789,7 @@ public class RedisBungeeBungeePlugin extends Plugin implements RedisBungeePlugin } }.execute(); try { - this.jedisSummoner.close(); + this.summoner.close(); } catch (IOException e) { throw new RuntimeException(e); } @@ -806,7 +798,7 @@ public class RedisBungeeBungeePlugin extends Plugin implements RedisBungeePlugin @Override public Summoner getSummoner() { - return this.jedisSummoner; + return this.summoner; } @@ -883,84 +875,9 @@ public class RedisBungeeBungeePlugin extends Plugin implements RedisBungeePlugin } @Override - public void loadConfig() throws IOException { - Path configFile = createConfigFile(getDataFolder().toPath()); - final YAMLConfigurationLoader yamlConfigurationFileLoader = YAMLConfigurationLoader.builder().setPath(configFile).build(); - ConfigurationNode node = yamlConfigurationFileLoader.load(); - if (node.getNode("config-version").getInt(0) != RedisBungeeConfiguration.CONFIG_VERSION) { - handleOldConfig(getDataFolder().toPath()); - node = yamlConfigurationFileLoader.load(); - } - final boolean useSSL = node.getNode("useSSL").getBoolean(false); - final boolean overrideBungeeCommands = node.getNode("override-bungee-commands").getBoolean(false); - final boolean registerLegacyCommands = node.getNode("register-legacy-commands").getBoolean(false); - String redisPassword = node.getNode("redis-password").getString(null); - String proxyId = node.getNode("proxy-id").getString("test-1"); - final int maxConnections = node.getNode("max-redis-connections").getInt(10); - List exemptAddresses; - try { - exemptAddresses = node.getNode("exempt-ip-addresses").getList(TypeToken.of(String.class)); - } catch (ObjectMappingException e) { - exemptAddresses = Collections.emptyList(); - } - - // check redis password - if (redisPassword != null && (redisPassword.isEmpty() || redisPassword.equals("none"))) { - redisPassword = null; - getLogger().warning("INSECURE setup was detected Please set password for your redis instance."); - } else if (redisPassword == null) { - getLogger().warning("INSECURE setup was detected Please set password for your redis instance."); - } - if (!useSSL) { - getLogger().warning("INSECURE setup was detected Please setup ssl for your redis instance."); - } - // Configuration sanity checks. - if (proxyId == null || proxyId.isEmpty()) { - String genId = UUID.randomUUID().toString(); - getLogger().info("Generated proxy id " + genId + " and saving it to config."); - node.getNode("proxy-id").setValue(genId); - yamlConfigurationFileLoader.save(node); - proxyId = genId; - getLogger().info("proxy id was generated: " + proxyId); - } else { - getLogger().info("Loaded proxy id " + proxyId); - } - this.configuration = new RedisBungeeConfiguration(proxyId, exemptAddresses, registerLegacyCommands, overrideBungeeCommands); - - if (node.getNode("cluster-mode-enabled").getBoolean(false)) { - getLogger().info("RedisBungee MODE: CLUSTER"); - Set hostAndPortSet = new HashSet<>(); - GenericObjectPoolConfig poolConfig = new GenericObjectPoolConfig<>(); - poolConfig.setMaxTotal(maxConnections); - node.getNode("redis-cluster-servers").getChildrenList().forEach((childNode) -> { - Map hostAndPort = childNode.getChildrenMap(); - String host = hostAndPort.get("host").getString(); - int port = hostAndPort.get("port").getInt(); - hostAndPortSet.add(new HostAndPort(host, port)); - }); - getLogger().info(hostAndPortSet.size() + " cluster nodes were specified"); - if (hostAndPortSet.isEmpty()) { - throw new RuntimeException("No redis cluster servers specified"); - } - if (redisPassword != null) { - this.jedisSummoner = new ClusterJedisSummoner(new JedisCluster(hostAndPortSet, 5000, 5000, 60, proxyId, redisPassword, poolConfig, useSSL)); - } else { - getLogger().warning("SSL option is ignored in Cluster mode if no PASSWORD is set"); - this.jedisSummoner = new ClusterJedisSummoner(new JedisCluster(hostAndPortSet, 5000, 5000, 60, poolConfig)); - } - this.redisBungeeMode = RedisBungeeMode.CLUSTER; - } else { - getLogger().info("RedisBungee MODE: SINGLE"); - final String redisServer = node.getNode("redis-server").getString("127.0.0.1"); - final int redisPort = node.getNode("redis-port").getInt(6379); - if (redisServer != null && redisServer.isEmpty()) { - throw new RuntimeException("No redis server specified"); - } - JedisPoolConfig config = new JedisPoolConfig(); - config.setMaxTotal(maxConnections); - this.jedisSummoner = new JedisSummoner(new JedisPool(config, redisServer, redisPort, 0, redisPassword, useSSL)); - this.redisBungeeMode = RedisBungeeMode.SINGLE; - } - getLogger().info("Successfully connected to Redis."); + public void onConfigLoad(RedisBungeeConfiguration configuration, Summoner summoner, RedisBungeeMode mode) { + this.configuration = configuration; + this.redisBungeeMode = mode; + this.summoner = summoner; } } diff --git a/RedisBungee-Velocity/pom.xml b/RedisBungee-Velocity/pom.xml index d0522d4..45bc6d5 100644 --- a/RedisBungee-Velocity/pom.xml +++ b/RedisBungee-Velocity/pom.xml @@ -69,24 +69,30 @@ package - shade + + shade + redis.clients.jedis - com.imaginarycode.minecraft.redisbungee.internal.jedis + com.imaginarycode.minecraft.redisbungee.internal.jedis + redis.clients.util - com.imaginarycode.minecraft.redisbungee.internal.jedisutil + com.imaginarycode.minecraft.redisbungee.internal.jedisutil + org.apache.commons.pool - com.imaginarycode.minecraft.redisbungee.internal.commonspool + com.imaginarycode.minecraft.redisbungee.internal.commonspool + com.squareup.okhttp - com.imaginarycode.minecraft.redisbungee.internal.okhttp + com.imaginarycode.minecraft.redisbungee.internal.okhttp + okio @@ -98,7 +104,8 @@ org.checkerframework - com.imaginarycode.minecraft.redisbungee.internal.checkframework + com.imaginarycode.minecraft.redisbungee.internal.checkframework + @@ -118,6 +125,10 @@ com.google.guava guava + + org.spongepowered + configurate-yaml + diff --git a/RedisBungee-Velocity/src/main/java/com/imaginarycode/minecraft/redisbungee/RedisBungeeVelocityListener.java b/RedisBungee-Velocity/src/main/java/com/imaginarycode/minecraft/redisbungee/RedisBungeeVelocityListener.java index 21592f6..2369c6f 100644 --- a/RedisBungee-Velocity/src/main/java/com/imaginarycode/minecraft/redisbungee/RedisBungeeVelocityListener.java +++ b/RedisBungee-Velocity/src/main/java/com/imaginarycode/minecraft/redisbungee/RedisBungeeVelocityListener.java @@ -7,7 +7,6 @@ import com.google.common.io.ByteArrayDataInput; import com.google.common.io.ByteArrayDataOutput; import com.google.common.io.ByteStreams; import com.imaginarycode.minecraft.redisbungee.api.AbstractRedisBungeeListener; -import com.imaginarycode.minecraft.redisbungee.api.AbstractDataManager; import com.imaginarycode.minecraft.redisbungee.api.GenericPlayerUtils; import com.imaginarycode.minecraft.redisbungee.api.RedisBungeePlugin; import com.imaginarycode.minecraft.redisbungee.api.tasks.RedisTask; diff --git a/RedisBungee-Velocity/src/main/java/com/imaginarycode/minecraft/redisbungee/RedisBungeeVelocityPlugin.java b/RedisBungee-Velocity/src/main/java/com/imaginarycode/minecraft/redisbungee/RedisBungeeVelocityPlugin.java index 921b8fd..4f5b088 100644 --- a/RedisBungee-Velocity/src/main/java/com/imaginarycode/minecraft/redisbungee/RedisBungeeVelocityPlugin.java +++ b/RedisBungee-Velocity/src/main/java/com/imaginarycode/minecraft/redisbungee/RedisBungeeVelocityPlugin.java @@ -98,7 +98,7 @@ public class RedisBungeeVelocityPlugin implements RedisBungeePlugin { this.logger = logger; this.dataFolder = dataDirectory; try { - loadConfig(); + loadConfig(this, dataDirectory); } catch (IOException e) { throw new RuntimeException("Unable to load/save config", e); } catch (JedisConnectionException e) { @@ -834,88 +834,12 @@ public class RedisBungeeVelocityPlugin implements RedisBungeePlugin { } @Override - public void loadConfig() throws IOException { - Path configFile = createConfigFile(getDataFolder()); - final YAMLConfigurationLoader yamlConfigurationFileLoader = YAMLConfigurationLoader.builder().setPath(configFile).build(); - ConfigurationNode node = yamlConfigurationFileLoader.load(); - if (node.getNode("config-version").getInt(0) != RedisBungeeConfiguration.CONFIG_VERSION) { - handleOldConfig(getDataFolder()); - node = yamlConfigurationFileLoader.load(); - } - final boolean useSSL = node.getNode("useSSL").getBoolean(false); - final boolean overrideBungeeCommands = node.getNode("override-bungee-commands").getBoolean(false); - final boolean registerLegacyCommands = node.getNode("register-legacy-commands").getBoolean(false); - String redisPassword = node.getNode("redis-password").getString(null); - String proxyId = node.getNode("proxy-id").getString("test-1"); - final int maxConnections = node.getNode("max-redis-connections").getInt(10); - List exemptAddresses; - try { - exemptAddresses = node.getNode("exempt-ip-addresses").getList(TypeToken.of(String.class)); - } catch (ObjectMappingException e) { - exemptAddresses = Collections.emptyList(); - } - - // check redis password - if (redisPassword != null && (redisPassword.isEmpty() || redisPassword.equals("none"))) { - redisPassword = null; - getLogger().warn("INSECURE setup was detected Please set password for your redis instance."); - } else if (redisPassword == null) { - getLogger().warn("INSECURE setup was detected Please set password for your redis instance."); - } - if (!useSSL) { - getLogger().warn("INSECURE setup was detected Please setup ssl for your redis instance."); - } - // Configuration sanity checks. - if (proxyId == null || proxyId.isEmpty()) { - String genId = UUID.randomUUID().toString(); - getLogger().info("Generated proxy id {} and saving it to config.", genId); - node.getNode("proxy-id").setValue(genId); - yamlConfigurationFileLoader.save(node); - proxyId = genId; - getLogger().info("proxy id was generated: {}", proxyId); - } else { - getLogger().info("Loaded proxy id {}.", proxyId); - } - this.configuration = new RedisBungeeConfiguration(proxyId, exemptAddresses, registerLegacyCommands, overrideBungeeCommands); - - if (node.getNode("cluster-mode-enabled").getBoolean(false)) { - getLogger().info("RedisBungee MODE: CLUSTER"); - Set hostAndPortSet = new HashSet<>(); - GenericObjectPoolConfig poolConfig = new GenericObjectPoolConfig<>(); - poolConfig.setMaxTotal(maxConnections); - node.getNode("redis-cluster-servers").getChildrenList().forEach((childNode) -> { - Map hostAndPort = childNode.getChildrenMap(); - String host = hostAndPort.get("host").getString(); - int port = hostAndPort.get("port").getInt(); - hostAndPortSet.add(new HostAndPort(host, port)); - }); - getLogger().info(hostAndPortSet.size() + " cluster nodes were specified"); - if (hostAndPortSet.isEmpty()) { - throw new RuntimeException("No redis cluster servers specified"); - } - if (redisPassword != null) { - this.jedisSummoner = new ClusterJedisSummoner(new JedisCluster(hostAndPortSet, 5000, 5000, 60, proxyId, redisPassword, poolConfig, useSSL)); - } else { - getLogger().warn("SSL option is ignored in Cluster mode if no PASSWORD is set"); - this.jedisSummoner = new ClusterJedisSummoner(new JedisCluster(hostAndPortSet, 5000, 5000, 60, poolConfig)); - } - this.redisBungeeMode = RedisBungeeMode.CLUSTER; - } else { - getLogger().info("RedisBungee MODE: SINGLE"); - final String redisServer = node.getNode("redis-server").getString("127.0.0.1"); - final int redisPort = node.getNode("redis-port").getInt(6379); - if (redisServer != null && redisServer.isEmpty()) { - throw new RuntimeException("No redis server specified"); - } - JedisPoolConfig config = new JedisPoolConfig(); - config.setMaxTotal(maxConnections); - this.jedisSummoner = new JedisSummoner(new JedisPool(config, redisServer, redisPort, 0, redisPassword, useSSL)); - this.redisBungeeMode = RedisBungeeMode.SINGLE; - } - getLogger().info("Successfully connected to Redis."); + public void onConfigLoad(RedisBungeeConfiguration configuration, Summoner summoner, RedisBungeeMode mode) { + this.jedisSummoner = summoner; + this.configuration = configuration; + this.redisBungeeMode = mode; } - @Override public void kickPlayer(UUID playerUniqueId, String message) { // first handle on origin proxy if player not found publish the payload