diff --git a/proxy/src/main/java/net/md_5/bungee/BungeeServerInfo.java b/proxy/src/main/java/net/md_5/bungee/BungeeServerInfo.java index cd691b7c..5cd5ca33 100644 --- a/proxy/src/main/java/net/md_5/bungee/BungeeServerInfo.java +++ b/proxy/src/main/java/net/md_5/bungee/BungeeServerInfo.java @@ -1,5 +1,7 @@ package net.md_5.bungee; +import io.netty.bootstrap.Bootstrap; +import io.netty.channel.socket.nio.NioSocketChannel; import java.net.InetSocketAddress; import java.util.Queue; import java.util.concurrent.ConcurrentLinkedQueue; @@ -42,6 +44,12 @@ public class BungeeServerInfo extends ServerInfo @Override public void ping(final Callback callback) { - PipelineUtils.connectClient( getAddress() ).channel().pipeline().get( HandlerBoss.class ).setHandler( new PingHandler( callback ) ); + new Bootstrap() + .channel( NioSocketChannel.class ) + .group( BungeeCord.getInstance().eventLoops ) + .handler( PipelineUtils.BASE ) + .remoteAddress( getAddress() ) + .connect() + .channel().pipeline().get( HandlerBoss.class ).setHandler( new PingHandler( callback ) ); } } diff --git a/proxy/src/main/java/net/md_5/bungee/ServerConnector.java b/proxy/src/main/java/net/md_5/bungee/ServerConnector.java index 19d78af4..f7e98127 100644 --- a/proxy/src/main/java/net/md_5/bungee/ServerConnector.java +++ b/proxy/src/main/java/net/md_5/bungee/ServerConnector.java @@ -2,17 +2,11 @@ package net.md_5.bungee; import com.google.common.base.Preconditions; import io.netty.channel.Channel; -import io.netty.channel.ChannelFuture; -import io.netty.channel.ChannelFutureListener; import java.util.Queue; import lombok.RequiredArgsConstructor; -import net.md_5.bungee.api.ChatColor; import net.md_5.bungee.api.ProxyServer; import net.md_5.bungee.api.config.ServerInfo; -import net.md_5.bungee.api.event.ServerConnectEvent; import net.md_5.bungee.api.event.ServerConnectedEvent; -import net.md_5.bungee.netty.HandlerBoss; -import net.md_5.bungee.netty.PipelineUtils; import net.md_5.bungee.packet.DefinedPacket; import net.md_5.bungee.packet.Packet1Login; import net.md_5.bungee.packet.Packet9Respawn; @@ -114,29 +108,4 @@ public class ServerConnector extends PacketHandler { throw new KickException( kick.message ); } - - public static void connect(final UserConnection user, ServerInfo info, final boolean retry) - { - ServerConnectEvent event = new ServerConnectEvent( user, info ); - ProxyServer.getInstance().getPluginManager().callEvent( event ); - final ServerInfo target = event.getTarget(); // Update in case the event changed target - - PipelineUtils.connectClient( info.getAddress() ).addListener( new ChannelFutureListener() - { - @Override - public void operationComplete(ChannelFuture future) throws Exception - { - if ( !future.isSuccess() ) - { - future.channel().close(); - ServerInfo def = ProxyServer.getInstance().getServers().get( user.getPendingConnection().getListener().getDefaultServer() ); - if ( retry && !target.equals( def ) ) - { - user.sendMessage( ChatColor.RED + "Could not connect to target server, you have been moved to the default server" ); - connect( user, def, false ); - } - } - } - } ).channel().pipeline().get( HandlerBoss.class).setHandler( new ServerConnector( ProxyServer.getInstance(), user, target)); - } } diff --git a/proxy/src/main/java/net/md_5/bungee/UserConnection.java b/proxy/src/main/java/net/md_5/bungee/UserConnection.java index 8cc4004d..f6095cc7 100644 --- a/proxy/src/main/java/net/md_5/bungee/UserConnection.java +++ b/proxy/src/main/java/net/md_5/bungee/UserConnection.java @@ -2,7 +2,12 @@ package net.md_5.bungee; import com.google.common.base.Preconditions; import gnu.trove.set.hash.THashSet; +import io.netty.bootstrap.Bootstrap; import io.netty.channel.Channel; +import io.netty.channel.ChannelFuture; +import io.netty.channel.ChannelFutureListener; +import io.netty.channel.ChannelInitializer; +import io.netty.channel.socket.nio.NioSocketChannel; import java.net.InetSocketAddress; import java.util.Collection; import java.util.Collections; @@ -12,11 +17,15 @@ import java.util.concurrent.ConcurrentLinkedQueue; import lombok.Getter; import lombok.Setter; import lombok.Synchronized; +import net.md_5.bungee.api.ChatColor; import net.md_5.bungee.api.ProxyServer; import net.md_5.bungee.api.config.ServerInfo; import net.md_5.bungee.api.connection.PendingConnection; import net.md_5.bungee.api.connection.ProxiedPlayer; import net.md_5.bungee.api.event.PlayerDisconnectEvent; +import net.md_5.bungee.api.event.ServerConnectEvent; +import net.md_5.bungee.netty.HandlerBoss; +import net.md_5.bungee.netty.PipelineUtils; import net.md_5.bungee.packet.*; public final class UserConnection implements ProxiedPlayer @@ -85,6 +94,44 @@ public final class UserConnection implements ProxiedPlayer @Override public void connect(ServerInfo target) { + connect( target, true ); + } + + private void connect(ServerInfo info, final boolean retry) + { + ServerConnectEvent event = new ServerConnectEvent( this, info ); + ProxyServer.getInstance().getPluginManager().callEvent( event ); + final ServerInfo target = event.getTarget(); // Update in case the event changed target + new Bootstrap() + .channel( NioSocketChannel.class ) + .group( BungeeCord.getInstance().eventLoops ) + .handler( new ChannelInitializer() + { + @Override + protected void initChannel(Channel ch) throws Exception + { + PipelineUtils.BASE.initChannel( ch ); + ch.pipeline().get( HandlerBoss.class ).setHandler( new ServerConnector( bungee, UserConnection.this, target ) ); + } + } ) + .remoteAddress( target.getAddress() ) + .connect().addListener( new ChannelFutureListener() + { + @Override + public void operationComplete(ChannelFuture future) throws Exception + { + if ( !future.isSuccess() ) + { + future.channel().close(); + ServerInfo def = ProxyServer.getInstance().getServers().get( getPendingConnection().getListener().getDefaultServer() ); + if ( retry && !target.equals( def ) ) + { + sendMessage( ChatColor.RED + "Could not connect to target server, you have been moved to the default server" ); + connect( def, false ); + } + } + } + } ); } @Override diff --git a/proxy/src/main/java/net/md_5/bungee/netty/CipherCodec.java b/proxy/src/main/java/net/md_5/bungee/netty/CipherCodec.java index 530b455e..9fd54eac 100644 --- a/proxy/src/main/java/net/md_5/bungee/netty/CipherCodec.java +++ b/proxy/src/main/java/net/md_5/bungee/netty/CipherCodec.java @@ -33,6 +33,9 @@ public class CipherCodec extends ByteToByteCodec { heapOut = ctx.alloc().heapBuffer(); } + System.out.println( "e) in: " + in ); + System.out.println( "e) heapOut: " + heapOut ); + System.out.println( "e) out: " + out ); cipher( encrypt, in, heapOut ); out.writeBytes( heapOut ); heapOut.discardSomeReadBytes(); @@ -41,6 +44,9 @@ public class CipherCodec extends ByteToByteCodec @Override public void decode(ChannelHandlerContext ctx, ByteBuf in, ByteBuf out) throws Exception { + System.out.println( "d) in: " + in ); + System.out.println( "d) heapOut: " + heapOut ); + System.out.println( "d) out: " + out ); cipher( decrypt, in, out ); } 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 bce51eaa..b27d0f52 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 @@ -4,6 +4,7 @@ import com.google.common.base.Preconditions; import io.netty.buffer.ByteBuf; import io.netty.channel.ChannelHandlerContext; import io.netty.channel.ChannelInboundMessageHandlerAdapter; +import net.md_5.bungee.Util; import net.md_5.bungee.packet.DefinedPacket; import net.md_5.bungee.packet.PacketHandler; @@ -60,7 +61,7 @@ public class HandlerBoss extends ChannelInboundMessageHandlerAdapter @Override public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception { - cause.printStackTrace(); + System.out.println( handler + " " + Util.exception( cause ) ); if ( ctx.channel().isActive() ) { ctx.close(); diff --git a/proxy/src/main/java/net/md_5/bungee/netty/PipelineUtils.java b/proxy/src/main/java/net/md_5/bungee/netty/PipelineUtils.java index 56e69bac..c54227b8 100644 --- a/proxy/src/main/java/net/md_5/bungee/netty/PipelineUtils.java +++ b/proxy/src/main/java/net/md_5/bungee/netty/PipelineUtils.java @@ -12,15 +12,20 @@ import io.netty.util.AttributeKey; import java.net.SocketAddress; import java.util.concurrent.TimeUnit; import net.md_5.bungee.BungeeCord; +import net.md_5.bungee.ServerConnector; +import net.md_5.bungee.UserConnection; import net.md_5.bungee.connection.InitialHandler; import net.md_5.bungee.api.ProxyServer; import net.md_5.bungee.api.config.ListenerInfo; +import net.md_5.bungee.api.config.ServerInfo; import net.md_5.bungee.protocol.PacketDefinitions; public class PipelineUtils { public static final AttributeKey LISTENER = new AttributeKey<>( "ListerInfo" ); + public static final AttributeKey USER = new AttributeKey<>( "User" ); + public static final AttributeKey TARGET = new AttributeKey<>( "Target" ); public static final ChannelInitializer SERVER_CHILD = new ChannelInitializer() { @Override @@ -30,9 +35,18 @@ public class PipelineUtils ch.pipeline().get( HandlerBoss.class ).setHandler( new InitialHandler( ProxyServer.getInstance(), ch.attr( LISTENER ).get() ) ); } }; - private static final Base BASE = new Base(); + public static final ChannelInitializer CLIENT = new ChannelInitializer() + { + @Override + protected void initChannel(Channel ch) throws Exception + { + BASE.initChannel( ch ); + ch.pipeline().get( HandlerBoss.class ).setHandler( new ServerConnector( ProxyServer.getInstance(), ch.attr( USER ).get(), ch.attr( TARGET ).get() ) ); + } + }; + public static final Base BASE = new Base(); - private final static class Base extends ChannelInitializer + public final static class Base extends ChannelInitializer { @Override @@ -50,14 +64,4 @@ public class PipelineUtils ch.pipeline().addLast( "handler", new HandlerBoss() ); } }; - - public static ChannelFuture connectClient(SocketAddress remoteAddress) - { - return new Bootstrap() - .channel( NioSocketChannel.class ) - .group( BungeeCord.getInstance().eventLoops ) - .handler( BASE ) - .remoteAddress( remoteAddress ) - .connect(); - } }