From 949366cf4d86f210fa406e98c0e48fbbef9eca54 Mon Sep 17 00:00:00 2001 From: Juan Cruz Linsalata Date: Thu, 3 Sep 2020 18:37:43 -0300 Subject: [PATCH] FlameCord Whitelist servers addresses diff --git a/flamecord/.factorypath b/flamecord/.factorypath index f398f41e..ed4e646c 100644 --- a/flamecord/.factorypath +++ b/flamecord/.factorypath @@ -1,4 +1,5 @@ + diff --git a/flamecord/src/main/java/dev/_2lstudios/flamecord/FlameCord.java b/flamecord/src/main/java/dev/_2lstudios/flamecord/FlameCord.java index 3a7f56ee..29c1573a 100644 --- a/flamecord/src/main/java/dev/_2lstudios/flamecord/FlameCord.java +++ b/flamecord/src/main/java/dev/_2lstudios/flamecord/FlameCord.java @@ -1,5 +1,6 @@ package dev._2lstudios.flamecord; +import java.util.Collection; import java.util.logging.Logger; import dev._2lstudios.flamecord.configuration.FlameCordConfiguration; @@ -21,21 +22,25 @@ public class FlameCord { private final ModulesConfiguration modulesConfiguration; @Getter private final MessagesConfiguration messagesConfiguration; + @Getter private final Thread thread; - public static void renew(final Logger logger) { + public static void renew(final Logger logger, final Collection whitelistedAddresses) { if (FlameCord.instance != null) { FlameCord.instance.thread.interrupt(); } - FlameCord.instance = new FlameCord(logger); + final FlameCord instance = new FlameCord(logger, whitelistedAddresses); + + FlameCord.instance = instance; } - private FlameCord(final Logger logger) { - final ConfigurationProvider configurationProvider = YamlConfiguration.getProvider(YamlConfiguration.class); + private FlameCord(final Logger logger, final Collection whitelistedAddresses) { + final ConfigurationProvider configurationProvider = ConfigurationProvider.getProvider(YamlConfiguration.class); this.flameCordConfiguration = new FlameCordConfiguration(configurationProvider); - this.firewallManager = new FirewallManager(logger, flameCordConfiguration.getFirewallSeconds()); + this.firewallManager = new FirewallManager(logger, whitelistedAddresses, + flameCordConfiguration.getFirewallSeconds()); this.modulesConfiguration = new ModulesConfiguration(configurationProvider); this.messagesConfiguration = new MessagesConfiguration(logger, configurationProvider); this.thread = new Thread() { diff --git a/flamecord/src/main/java/dev/_2lstudios/flamecord/firewall/FirewallManager.java b/flamecord/src/main/java/dev/_2lstudios/flamecord/firewall/FirewallManager.java index a90c8426..8a474758 100644 --- a/flamecord/src/main/java/dev/_2lstudios/flamecord/firewall/FirewallManager.java +++ b/flamecord/src/main/java/dev/_2lstudios/flamecord/firewall/FirewallManager.java @@ -12,20 +12,35 @@ import lombok.Getter; public class FirewallManager { private final Logger logger; + private final Collection whitelistedAddresses; private final Collection firewalled; private final int defaultSeconds; @Getter private int seconds; - public FirewallManager(final Logger logger, final int defaultSeconds) { + public FirewallManager(final Logger logger, final Collection whitelistedAddresses, + final int defaultSeconds) { this.logger = logger; + this.whitelistedAddresses = whitelistedAddresses; this.firewalled = new HashSet<>(); this.defaultSeconds = defaultSeconds; this.seconds = defaultSeconds; } + public boolean isWhitelisted(final SocketAddress address) { + final String addressString = address.toString(); + + for (final String whitelistedAddressString : whitelistedAddresses) { + if (addressString.endsWith(whitelistedAddressString)) { + return true; + } + } + + return false; + } + public void addFirewalled(final SocketAddress address) { - if (FlameCord.getInstance().getFlameCordConfiguration().isFirewallEnabled()) { + if (FlameCord.getInstance().getFlameCordConfiguration().isFirewallEnabled() && !isWhitelisted(address)) { final InetSocketAddress iNetSocketAddress = (InetSocketAddress) address; final String hostString = iNetSocketAddress.getHostString(); diff --git a/proxy/src/main/java/net/md_5/bungee/BungeeCord.java b/proxy/src/main/java/net/md_5/bungee/BungeeCord.java index d6d4cd6e..34c0b675 100644 --- a/proxy/src/main/java/net/md_5/bungee/BungeeCord.java +++ b/proxy/src/main/java/net/md_5/bungee/BungeeCord.java @@ -288,19 +288,30 @@ public class BungeeCord extends ProxyServer { workerEventLoopGroup = PipelineUtils.newEventLoopGroup(0, new ThreadFactoryBuilder().setNameFormat("Netty Worker IO Thread #%1$d").build()); + // FlameCord - Load config before + config.load(); + // FlameCord - Use own module system and reload FlameCord /* * File moduleDirectory = new File( "modules" ); moduleManager.load( this, * moduleDirectory ); pluginManager.detectPlugins( moduleDirectory ); */ - FlameCord.renew(logger); + // FlameCord - Collect ips from servers + final Collection whitelistedAddresses = new HashSet<>(); + + for (final ServerInfo serverInfo : getServers().values()) { + whitelistedAddresses.add(serverInfo.getSocketAddress().toString()); + } + + FlameCord.renew(logger, whitelistedAddresses); registerModules(); pluginsFolder.mkdir(); pluginManager.detectPlugins(pluginsFolder); pluginManager.loadPlugins(); - config.load(); + // FlameCord - Load config before + // config.load(); if (config.isForgeSupport()) { registerChannel(ForgeConstants.FML_TAG); @@ -828,7 +839,14 @@ public class BungeeCord extends ProxyServer { break; } case "reload": { - FlameCord.renew(logger); + // FlameCord - Collect ips from servers + final Collection whitelistedAddresses = new HashSet<>(); + + for (final ServerInfo serverInfo : getServers().values()) { + whitelistedAddresses.add(serverInfo.getSocketAddress().toString()); + } + + FlameCord.renew(logger, whitelistedAddresses); sender.sendMessage(TextComponent .fromLegacyText(messagesConfiguration.getTranslation("flamecord_reload"))); break; diff --git a/proxy/src/main/java/net/md_5/bungee/netty/HandlerBoss.java b/proxy/src/main/java/net/md_5/bungee/netty/HandlerBoss.java index 687bf518..0f6ac089 100644 --- a/proxy/src/main/java/net/md_5/bungee/netty/HandlerBoss.java +++ b/proxy/src/main/java/net/md_5/bungee/netty/HandlerBoss.java @@ -146,11 +146,7 @@ public class HandlerBoss extends ChannelInboundHandlerAdapter { final SocketAddress remoteAddress = channel.remoteAddress(); if (remoteAddress != null) { - final Throwable causeCause = cause.getCause(); - - if (causeCause == null || !causeCause.getMessage().contains("TO_CLIENT")) { - flameCord.getFirewallManager().addFirewalled(remoteAddress); - } + flameCord.getFirewallManager().addFirewalled(remoteAddress); } } -- 2.27.0.windows.1