diff --git a/build.gradle b/build.gradle index 30f382d..c88e4ec 100644 --- a/build.gradle +++ b/build.gradle @@ -43,7 +43,6 @@ dependencies { provided("io.netty:netty-all:4.1.9.Final") compile("io.netty:netty-codec-haproxy:4.1.9.Final") provided("net.md-5:bungeecord-api:1.11-SNAPSHOT") - provided("net.md-5:bungeecord-proxy:1.4.7-SNAPSHOT") } jar { diff --git a/src/main/java/nz/co/lolnet/bungeeproxy/BungeeProxy.java b/src/main/java/nz/co/lolnet/bungeeproxy/BungeeProxy.java index d215eec..d757ade 100644 --- a/src/main/java/nz/co/lolnet/bungeeproxy/BungeeProxy.java +++ b/src/main/java/nz/co/lolnet/bungeeproxy/BungeeProxy.java @@ -7,7 +7,7 @@ import io.netty.channel.Channel; import io.netty.channel.ChannelInitializer; import net.md_5.bungee.api.plugin.Plugin; -import net.md_5.bungee.netty.PipelineUtils; +import net.md_5.bungee.config.Configuration; import nz.co.lolnet.bungeeproxy.config.Config; import nz.co.lolnet.bungeeproxy.handlers.ChannelHandler; import nz.co.lolnet.bungeeproxy.util.Reference; @@ -24,14 +24,16 @@ public void onEnable() { getConfig().loadConfig(); try { - Field serverChildField = PipelineUtils.class.getDeclaredField("SERVER_CHILD"); + Class pipelineUtilsClass = Class.forName("net.md_5.bungee.netty.PipelineUtils"); + + Field serverChildField = pipelineUtilsClass.getField("SERVER_CHILD"); serverChildField.setAccessible(true); Field modifiersField = Field.class.getDeclaredField("modifiers"); modifiersField.setAccessible(true); - modifiersField.setInt(serverChildField, Modifier.PUBLIC & Modifier.STATIC); + modifiersField.setInt(serverChildField, serverChildField.getModifiers() & ~Modifier.FINAL); - ChannelInitializer bungeeChannelInitializer = PipelineUtils.SERVER_CHILD; + Object bungeeChannelInitializer = serverChildField.get(null); Method initChannelMethod = ChannelInitializer.class.getDeclaredMethod("initChannel", Channel.class); initChannelMethod.setAccessible(true); @@ -48,6 +50,12 @@ public void onDisable() { getLogger().info(Reference.PLUGIN_NAME + " Disabled!"); } + public void debugMessage(String message) { + if (getConfiguration() != null && getConfiguration().getBoolean("BungeeProxy.Debug")) { + getLogger().info(message); + } + } + public static BungeeProxy getInstance() { return instance; } @@ -55,4 +63,11 @@ public static BungeeProxy getInstance() { public Config getConfig() { return config; } + + public Configuration getConfiguration() { + if (getConfig() != null) { + return getConfig().getConfiguration(); + } + return null; + } } \ No newline at end of file diff --git a/src/main/java/nz/co/lolnet/bungeeproxy/handlers/ChannelHandler.java b/src/main/java/nz/co/lolnet/bungeeproxy/handlers/ChannelHandler.java index cc033c7..3757aad 100644 --- a/src/main/java/nz/co/lolnet/bungeeproxy/handlers/ChannelHandler.java +++ b/src/main/java/nz/co/lolnet/bungeeproxy/handlers/ChannelHandler.java @@ -10,10 +10,10 @@ public class ChannelHandler extends ChannelInitializer { - private final ChannelInitializer bungeeChannelInitializer; + private final Object bungeeChannelInitializer; private final Method initChannelMethod; - public ChannelHandler(ChannelInitializer bungeeChannelInitializer, Method initChannelMethod) { + public ChannelHandler(Object bungeeChannelInitializer, Method initChannelMethod) { this.bungeeChannelInitializer = bungeeChannelInitializer; this.initChannelMethod = initChannelMethod; } @@ -21,8 +21,12 @@ public ChannelHandler(ChannelInitializer bungeeChannelInitializer, Meth @Override protected void initChannel(Channel channel) { try { + if (getInitChannelMethod() == null || getBungeeChannelInitializer() == null) { + throw new NullPointerException(); + } + getInitChannelMethod().invoke(getBungeeChannelInitializer(), channel); - channel.pipeline().addAfter("", Reference.DECODER_NAME, new HAProxyMessageDecoder()); + channel.pipeline().addAfter("timeout", Reference.DECODER_NAME, new HAProxyMessageDecoder()); channel.pipeline().addAfter(Reference.DECODER_NAME, Reference.HANDLER_NAME, new ProxyHandler()); } catch (Exception ex) { BungeeProxy.getInstance().getLogger().severe("Encountered an error processing 'initChannel' in '" + getClass().getSimpleName() + "' - " + ex.getMessage()); @@ -30,7 +34,7 @@ protected void initChannel(Channel channel) { } } - public ChannelInitializer getBungeeChannelInitializer() { + private Object getBungeeChannelInitializer() { return bungeeChannelInitializer; } diff --git a/src/main/java/nz/co/lolnet/bungeeproxy/handlers/ProxyHandler.java b/src/main/java/nz/co/lolnet/bungeeproxy/handlers/ProxyHandler.java index 7e00d0e..4f0a38c 100644 --- a/src/main/java/nz/co/lolnet/bungeeproxy/handlers/ProxyHandler.java +++ b/src/main/java/nz/co/lolnet/bungeeproxy/handlers/ProxyHandler.java @@ -20,6 +20,7 @@ public void channelActive(ChannelHandlerContext ctx) { try { remoteAddress = AbstractChannel.class.getDeclaredField("remoteAddress"); getRemoteAddress().setAccessible(true); + super.channelActive(ctx); } catch (Exception ex) { BungeeProxy.getInstance().getLogger().severe("Encountered an error processing 'channelActive' in '" + getClass().getSimpleName() + "' - " + ex.getMessage()); ex.printStackTrace(); @@ -32,6 +33,7 @@ public void channelRead(ChannelHandlerContext ctx, Object msg) { if (msg instanceof HAProxyMessage && getRemoteAddress() != null) { HAProxyMessage proxyMessage = (HAProxyMessage) msg; getRemoteAddress().set(ctx.channel(), new InetSocketAddress(proxyMessage.sourceAddress(), proxyMessage.sourcePort())); + BungeeProxy.getInstance().debugMessage("Successfully processed HAProxyMessage."); return; } @@ -47,6 +49,7 @@ public void exceptionCaught(ChannelHandlerContext ctx, Throwable throwable) { if (ctx.channel().isActive()) { ctx.channel().writeAndFlush(Unpooled.EMPTY_BUFFER).addListener(ChannelFutureListener.CLOSE); } + BungeeProxy.getInstance().getLogger().severe("Exception caught in '" + getClass().getSimpleName() + "' - " + throwable.getMessage()); } diff --git a/src/main/resources/config.yml b/src/main/resources/config.yml index c119b45..78fe4b9 100644 --- a/src/main/resources/config.yml +++ b/src/main/resources/config.yml @@ -1 +1,2 @@ -BungeeProxy: \ No newline at end of file +BungeeProxy: + Debug: False \ No newline at end of file diff --git a/src/main/resources/plugin.yml b/src/main/resources/plugin.yml index 57b006e..06ffb38 100644 --- a/src/main/resources/plugin.yml +++ b/src/main/resources/plugin.yml @@ -1,5 +1,5 @@ name: ${name} -main: nz.co.lolnet.BungeeProxy +main: nz.co.lolnet.bungeeproxy.BungeeProxy version: ${version} description: BungeeProxy author: Byteflux, LX_Gaming