Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix intent issues in InitialHandler #3532

Open
wants to merge 9 commits into
base: master
Choose a base branch
from
52 changes: 35 additions & 17 deletions proxy/src/main/java/net/md_5/bungee/connection/InitialHandler.java
Expand Up @@ -121,8 +121,7 @@ public boolean shouldHandle(PacketWrapper packet) throws Exception

private enum State
{

HANDSHAKE, STATUS, PING, USERNAME, ENCRYPT, FINISHING;
HANDSHAKE, STATUS, PROCESSING_PING, PING, USERNAME, ENCRYPT, FINISHING;
}

private boolean canSendKickMessage()
Expand Down Expand Up @@ -166,13 +165,18 @@ public void handle(PluginMessage pluginMessage) throws Exception
@Override
public void handle(LegacyHandshake legacyHandshake) throws Exception
{
Preconditions.checkState( thisState == State.HANDSHAKE, "Not expecting LEGACY HANDSHAKE" );

this.legacy = true;
ch.close( bungee.getTranslation( "outdated_client", bungee.getGameVersion() ) );
}

@Override
public void handle(LegacyPing ping) throws Exception
{
Preconditions.checkState( thisState == State.HANDSHAKE, "Not expecting LEGACY PING" );

thisState = State.PROCESSING_PING;
this.legacy = true;
final boolean v1_5 = ping.isV1_5();

Expand Down Expand Up @@ -260,6 +264,7 @@ public void handle(StatusRequest statusRequest) throws Exception
ServerInfo forced = AbstractReconnectHandler.getForcedHost( this );
final String motd = ( forced != null ) ? forced.getMotd() : listener.getMotd();
final int protocol = ( ProtocolConstants.SUPPORTED_VERSION_IDS.contains( handshake.getProtocolVersion() ) ) ? handshake.getProtocolVersion() : bungee.getProtocolVersion();
thisState = State.PROCESSING_PING;

Callback<ServerPing> pingBack = new Callback<ServerPing>()
{
Expand All @@ -277,12 +282,17 @@ public void done(ServerPing result, Throwable error)
@Override
public void done(ProxyPingEvent pingResult, Throwable error)
{
if ( ch.isClosed() )
{
return;
}
Gson gson = BungeeCord.getInstance().gson;
unsafe.sendPacket( new StatusResponse( gson.toJson( pingResult.getResponse() ) ) );
if ( bungee.getConnectionThrottle() != null )
{
bungee.getConnectionThrottle().unthrottle( getSocketAddress() );
}
thisState = State.PING;
}
};

Expand All @@ -297,8 +307,6 @@ public void done(ProxyPingEvent pingResult, Throwable error)
{
pingBack.done( getPingInfo( motd, protocol ), null );
}

thisState = State.PING;
}

@Override
Expand Down Expand Up @@ -339,6 +347,11 @@ public void handle(Handshake handshake) throws Exception

bungee.getPluginManager().callEvent( new PlayerHandshakeEvent( InitialHandler.this, handshake ) );

if ( ch.isClosing() )
{
return;
}

switch ( handshake.getRequestedProtocol() )
{
case 1:
Expand Down Expand Up @@ -438,7 +451,7 @@ public void done(PreLoginEvent result, Throwable error)
disconnect( ( reason != null ) ? reason : TextComponent.fromLegacyText( bungee.getTranslation( "kick_message" ) ) );
return;
}
if ( ch.isClosed() )
if ( ch.isClosing() )
{
return;
}
Expand All @@ -448,12 +461,12 @@ public void done(PreLoginEvent result, Throwable error)
unsafe().sendPacket( request = EncryptionUtil.encryptRequest() );
} else
{
thisState = State.FINISHING;
finish();
}
}
};

thisState = State.FINISHING;
// fire pre login event
bungee.getPluginManager().callEvent( new PreLoginEvent( InitialHandler.this, callback ) );
}
Expand Down Expand Up @@ -492,6 +505,10 @@ public void done(String result, Throwable error)
{
if ( error == null )
{
if ( ch.isClosing() )
{
return;
}
LoginResult obj = BungeeCord.getInstance().gson.fromJson( result, LoginResult.class );
if ( obj != null && obj.getId() != null )
{
Expand Down Expand Up @@ -582,7 +599,7 @@ public void done(LoginEvent result, Throwable error)
disconnect( ( reason != null ) ? reason : TextComponent.fromLegacyText( bungee.getTranslation( "kick_message" ) ) );
return;
}
if ( ch.isClosed() )
if ( ch.isClosing() )
{
return;
}
Expand All @@ -592,18 +609,19 @@ public void done(LoginEvent result, Throwable error)
@Override
public void run()
{
if ( !ch.isClosing() )
if ( ch.isClosing() )
{
return;
}
userCon = new UserConnection( bungee, ch, getName(), InitialHandler.this );
userCon.setCompressionThreshold( BungeeCord.getInstance().config.getCompressionThreshold() );

if ( getVersion() < ProtocolConstants.MINECRAFT_1_20_2 )
{
userCon = new UserConnection( bungee, ch, getName(), InitialHandler.this );
userCon.setCompressionThreshold( BungeeCord.getInstance().config.getCompressionThreshold() );

if ( getVersion() < ProtocolConstants.MINECRAFT_1_20_2 )
{
unsafe.sendPacket( new LoginSuccess( getUniqueId(), getName(), ( loginProfile == null ) ? null : loginProfile.getProperties() ) );
ch.setProtocol( Protocol.GAME );
}
finish2();
unsafe.sendPacket( new LoginSuccess( getUniqueId(), getName(), ( loginProfile == null ) ? null : loginProfile.getProperties() ) );
ch.setProtocol( Protocol.GAME );
}
finish2();
}
} );
}
Expand Down