Skip to content

Commit

Permalink
Minor bug fixes
Browse files Browse the repository at this point in the history
Removed proxy import so configuration works.
  • Loading branch information
LXGaming committed Jun 13, 2017
1 parent fa50c03 commit f87b151
Show file tree
Hide file tree
Showing 6 changed files with 33 additions and 11 deletions.
1 change: 0 additions & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
23 changes: 19 additions & 4 deletions src/main/java/nz/co/lolnet/bungeeproxy/BungeeProxy.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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<Channel> bungeeChannelInitializer = PipelineUtils.SERVER_CHILD;
Object bungeeChannelInitializer = serverChildField.get(null);
Method initChannelMethod = ChannelInitializer.class.getDeclaredMethod("initChannel", Channel.class);
initChannelMethod.setAccessible(true);

Expand All @@ -48,11 +50,24 @@ 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;
}

public Config getConfig() {
return config;
}

public Configuration getConfiguration() {
if (getConfig() != null) {
return getConfig().getConfiguration();
}
return null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,27 +10,31 @@

public class ChannelHandler extends ChannelInitializer<Channel> {

private final ChannelInitializer<Channel> bungeeChannelInitializer;
private final Object bungeeChannelInitializer;
private final Method initChannelMethod;

public ChannelHandler(ChannelInitializer<Channel> bungeeChannelInitializer, Method initChannelMethod) {
public ChannelHandler(Object bungeeChannelInitializer, Method initChannelMethod) {
this.bungeeChannelInitializer = bungeeChannelInitializer;
this.initChannelMethod = initChannelMethod;
}

@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());
ex.printStackTrace();
}
}

public ChannelInitializer<Channel> getBungeeChannelInitializer() {
private Object getBungeeChannelInitializer() {
return bungeeChannelInitializer;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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;
}

Expand All @@ -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());
}

Expand Down
3 changes: 2 additions & 1 deletion src/main/resources/config.yml
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
BungeeProxy:
BungeeProxy:
Debug: False
2 changes: 1 addition & 1 deletion src/main/resources/plugin.yml
Original file line number Diff line number Diff line change
@@ -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
Expand Down

0 comments on commit f87b151

Please sign in to comment.