diff --git a/api/src/main/java/net/md_5/bungee/api/event/PlayerHandshakeEvent.java b/api/src/main/java/net/md_5/bungee/api/event/PlayerHandshakeEvent.java new file mode 100644 index 00000000..de927325 --- /dev/null +++ b/api/src/main/java/net/md_5/bungee/api/event/PlayerHandshakeEvent.java @@ -0,0 +1,34 @@ +package net.md_5.bungee.api.event; + +import lombok.Data; +import lombok.EqualsAndHashCode; +import lombok.ToString; +import net.md_5.bungee.api.connection.PendingConnection; +import net.md_5.bungee.protocol.packet.Packet2Handshake; +import net.md_5.bungee.api.plugin.Event; + +/** + * Event called to represent a player first making their presence and username + * known. + */ +@Data +@ToString(callSuper = false) +@EqualsAndHashCode(callSuper = false) +public class PlayerHandshakeEvent extends Event +{ + + /** + * Connection attempting to login. + */ + private final PendingConnection connection; + /** + * The handshake. + */ + private final Packet2Handshake handshake; + + public PlayerHandshakeEvent(PendingConnection connection, Packet2Handshake handshake) + { + this.connection = connection; + this.handshake = handshake; + } +} diff --git a/proxy/src/main/java/net/md_5/bungee/EncryptionUtil.java b/proxy/src/main/java/net/md_5/bungee/EncryptionUtil.java index 2331aa87..046a84d2 100644 --- a/proxy/src/main/java/net/md_5/bungee/EncryptionUtil.java +++ b/proxy/src/main/java/net/md_5/bungee/EncryptionUtil.java @@ -40,9 +40,9 @@ public class EncryptionUtil } } - public static PacketFDEncryptionRequest encryptRequest() + public static PacketFDEncryptionRequest encryptRequest(boolean onlinemode) { - String hash = ( BungeeCord.getInstance().config.isOnlineMode() ) ? Long.toString( random.nextLong(), 16 ) : "-"; + String hash = ( onlinemode ) ? Long.toString( random.nextLong(), 16 ) : "-"; byte[] pubKey = keys.getPublic().getEncoded(); byte[] verify = new byte[ 4 ]; random.nextBytes( verify ); diff --git a/proxy/src/main/java/net/md_5/bungee/connection/InitialHandler.java b/proxy/src/main/java/net/md_5/bungee/connection/InitialHandler.java index 9d58b2f6..cc20ddcd 100644 --- a/proxy/src/main/java/net/md_5/bungee/connection/InitialHandler.java +++ b/proxy/src/main/java/net/md_5/bungee/connection/InitialHandler.java @@ -52,6 +52,7 @@ import net.md_5.bungee.protocol.packet.PacketFDEncryptionRequest; import net.md_5.bungee.protocol.packet.PacketFEPing; import net.md_5.bungee.protocol.packet.PacketFFKick; import net.md_5.bungee.api.AbstractReconnectHandler; +import net.md_5.bungee.api.event.PlayerHandshakeEvent; @RequiredArgsConstructor public class InitialHandler extends PacketHandler implements PendingConnection @@ -80,6 +81,8 @@ public class InitialHandler extends PacketHandler implements PendingConnection ch.write( packet ); } }; + @Getter + private boolean onlineMode = BungeeCord.getInstance().config.isOnlineMode(); private ScheduledFuture pingFuture; private InetSocketAddress vHost; private byte version = -1; @@ -198,6 +201,8 @@ public class InitialHandler extends PacketHandler implements PendingConnection this.vHost = new InetSocketAddress( handshake.getHost(), handshake.getPort() ); bungee.getLogger().log( Level.INFO, "{0} has connected", this ); + bungee.getPluginManager().callEvent( new PlayerHandshakeEvent( InitialHandler.this, handshake ) ); + if ( handshake.getProtocolVersion() > Vanilla.PROTOCOL_VERSION ) { disconnect( bungee.getTranslation( "outdated_server" ) ); @@ -220,7 +225,7 @@ public class InitialHandler extends PacketHandler implements PendingConnection } // If offline mode and they are already on, don't allow connect - if ( !BungeeCord.getInstance().config.isOnlineMode() && bungee.getPlayer( handshake.getUsername() ) != null ) + if ( !isOnlineMode() && bungee.getPlayer( handshake.getUsername() ) != null ) { disconnect( bungee.getTranslation( "already_connected" ) ); return; @@ -228,7 +233,8 @@ public class InitialHandler extends PacketHandler implements PendingConnection unsafe().sendPacket( PacketConstants.I_AM_BUNGEE ); unsafe().sendPacket( PacketConstants.FORGE_MOD_REQUEST ); - unsafe().sendPacket( request = EncryptionUtil.encryptRequest() ); + + unsafe().sendPacket( request = EncryptionUtil.encryptRequest( this.onlineMode ) ); thisState = State.ENCRYPT; } @@ -241,7 +247,7 @@ public class InitialHandler extends PacketHandler implements PendingConnection Cipher decrypt = EncryptionUtil.getCipher( Cipher.DECRYPT_MODE, sharedKey ); ch.addBefore( PipelineUtils.PACKET_DECODE_HANDLER, PipelineUtils.DECRYPT_HANDLER, new CipherDecoder( decrypt ) ); - if ( BungeeCord.getInstance().config.isOnlineMode() ) + if ( this.onlineMode ) { String encName = URLEncoder.encode( InitialHandler.this.getName(), "UTF-8" ); @@ -402,6 +408,12 @@ public class InitialHandler extends PacketHandler implements PendingConnection return unsafe; } + public void setOnlineMode(boolean onlineMode) + { + Preconditions.checkState( thisState == State.HANDSHAKE, "Can only set online mode status whilst handshaking" ); + this.onlineMode = onlineMode; + } + @Override public String toString() {