Closer to connecting, trying to figure out this encryption bug also present in first connect on Spigot

This commit is contained in:
md_5 2013-03-09 10:47:12 +11:00
parent bcaafc206f
commit e12bc1d92e
6 changed files with 80 additions and 45 deletions

View File

@ -1,5 +1,7 @@
package net.md_5.bungee; package net.md_5.bungee;
import io.netty.bootstrap.Bootstrap;
import io.netty.channel.socket.nio.NioSocketChannel;
import java.net.InetSocketAddress; import java.net.InetSocketAddress;
import java.util.Queue; import java.util.Queue;
import java.util.concurrent.ConcurrentLinkedQueue; import java.util.concurrent.ConcurrentLinkedQueue;
@ -42,6 +44,12 @@ public class BungeeServerInfo extends ServerInfo
@Override @Override
public void ping(final Callback<ServerPing> callback) public void ping(final Callback<ServerPing> 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 ) );
} }
} }

View File

@ -2,17 +2,11 @@ package net.md_5.bungee;
import com.google.common.base.Preconditions; import com.google.common.base.Preconditions;
import io.netty.channel.Channel; import io.netty.channel.Channel;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelFutureListener;
import java.util.Queue; import java.util.Queue;
import lombok.RequiredArgsConstructor; import lombok.RequiredArgsConstructor;
import net.md_5.bungee.api.ChatColor;
import net.md_5.bungee.api.ProxyServer; import net.md_5.bungee.api.ProxyServer;
import net.md_5.bungee.api.config.ServerInfo; 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.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.DefinedPacket;
import net.md_5.bungee.packet.Packet1Login; import net.md_5.bungee.packet.Packet1Login;
import net.md_5.bungee.packet.Packet9Respawn; import net.md_5.bungee.packet.Packet9Respawn;
@ -114,29 +108,4 @@ public class ServerConnector extends PacketHandler
{ {
throw new KickException( kick.message ); 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));
}
} }

View File

@ -2,7 +2,12 @@ package net.md_5.bungee;
import com.google.common.base.Preconditions; import com.google.common.base.Preconditions;
import gnu.trove.set.hash.THashSet; import gnu.trove.set.hash.THashSet;
import io.netty.bootstrap.Bootstrap;
import io.netty.channel.Channel; 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.net.InetSocketAddress;
import java.util.Collection; import java.util.Collection;
import java.util.Collections; import java.util.Collections;
@ -12,11 +17,15 @@ import java.util.concurrent.ConcurrentLinkedQueue;
import lombok.Getter; import lombok.Getter;
import lombok.Setter; import lombok.Setter;
import lombok.Synchronized; import lombok.Synchronized;
import net.md_5.bungee.api.ChatColor;
import net.md_5.bungee.api.ProxyServer; import net.md_5.bungee.api.ProxyServer;
import net.md_5.bungee.api.config.ServerInfo; import net.md_5.bungee.api.config.ServerInfo;
import net.md_5.bungee.api.connection.PendingConnection; import net.md_5.bungee.api.connection.PendingConnection;
import net.md_5.bungee.api.connection.ProxiedPlayer; import net.md_5.bungee.api.connection.ProxiedPlayer;
import net.md_5.bungee.api.event.PlayerDisconnectEvent; 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.*; import net.md_5.bungee.packet.*;
public final class UserConnection implements ProxiedPlayer public final class UserConnection implements ProxiedPlayer
@ -85,6 +94,44 @@ public final class UserConnection implements ProxiedPlayer
@Override @Override
public void connect(ServerInfo target) 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 @Override

View File

@ -33,6 +33,9 @@ public class CipherCodec extends ByteToByteCodec
{ {
heapOut = ctx.alloc().heapBuffer(); 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 ); cipher( encrypt, in, heapOut );
out.writeBytes( heapOut ); out.writeBytes( heapOut );
heapOut.discardSomeReadBytes(); heapOut.discardSomeReadBytes();
@ -41,6 +44,9 @@ public class CipherCodec extends ByteToByteCodec
@Override @Override
public void decode(ChannelHandlerContext ctx, ByteBuf in, ByteBuf out) throws Exception 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 ); cipher( decrypt, in, out );
} }

View File

@ -4,6 +4,7 @@ import com.google.common.base.Preconditions;
import io.netty.buffer.ByteBuf; import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext; import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundMessageHandlerAdapter; import io.netty.channel.ChannelInboundMessageHandlerAdapter;
import net.md_5.bungee.Util;
import net.md_5.bungee.packet.DefinedPacket; import net.md_5.bungee.packet.DefinedPacket;
import net.md_5.bungee.packet.PacketHandler; import net.md_5.bungee.packet.PacketHandler;
@ -60,7 +61,7 @@ public class HandlerBoss extends ChannelInboundMessageHandlerAdapter<ByteBuf>
@Override @Override
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception
{ {
cause.printStackTrace(); System.out.println( handler + " " + Util.exception( cause ) );
if ( ctx.channel().isActive() ) if ( ctx.channel().isActive() )
{ {
ctx.close(); ctx.close();

View File

@ -12,15 +12,20 @@ import io.netty.util.AttributeKey;
import java.net.SocketAddress; import java.net.SocketAddress;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;
import net.md_5.bungee.BungeeCord; 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.connection.InitialHandler;
import net.md_5.bungee.api.ProxyServer; import net.md_5.bungee.api.ProxyServer;
import net.md_5.bungee.api.config.ListenerInfo; import net.md_5.bungee.api.config.ListenerInfo;
import net.md_5.bungee.api.config.ServerInfo;
import net.md_5.bungee.protocol.PacketDefinitions; import net.md_5.bungee.protocol.PacketDefinitions;
public class PipelineUtils public class PipelineUtils
{ {
public static final AttributeKey<ListenerInfo> LISTENER = new AttributeKey<>( "ListerInfo" ); public static final AttributeKey<ListenerInfo> LISTENER = new AttributeKey<>( "ListerInfo" );
public static final AttributeKey<UserConnection> USER = new AttributeKey<>( "User" );
public static final AttributeKey<ServerInfo> TARGET = new AttributeKey<>( "Target" );
public static final ChannelInitializer<Channel> SERVER_CHILD = new ChannelInitializer<Channel>() public static final ChannelInitializer<Channel> SERVER_CHILD = new ChannelInitializer<Channel>()
{ {
@Override @Override
@ -30,9 +35,18 @@ public class PipelineUtils
ch.pipeline().get( HandlerBoss.class ).setHandler( new InitialHandler( ProxyServer.getInstance(), ch.attr( LISTENER ).get() ) ); 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<Channel> CLIENT = new ChannelInitializer<Channel>()
{
@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<Channel> public final static class Base extends ChannelInitializer<Channel>
{ {
@Override @Override
@ -50,14 +64,4 @@ public class PipelineUtils
ch.pipeline().addLast( "handler", new HandlerBoss() ); 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();
}
} }