From b8037e132818388a1022ee6d64e53296eb963d64 Mon Sep 17 00:00:00 2001 From: BoomEaro <21033866+BoomEaro@users.noreply.github.com> Date: Wed, 18 Sep 2024 22:21:25 +0300 Subject: [PATCH 1/3] Fix possible server connection race condition & do not send StartConfiguration when the connection is in Configuration phase --- .../java/net/md_5/bungee/ServerConnector.java | 5 ++++- .../main/java/net/md_5/bungee/UserConnection.java | 15 ++++++++++----- 2 files changed, 14 insertions(+), 6 deletions(-) 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 8283286563..90c5237cad 100644 --- a/proxy/src/main/java/net/md_5/bungee/ServerConnector.java +++ b/proxy/src/main/java/net/md_5/bungee/ServerConnector.java @@ -361,7 +361,10 @@ private void cutThrough(ServerConnection server) if ( user.getServer() != null ) { // Begin config mode - user.unsafe().sendPacket( new StartConfiguration() ); + if ( user.getCh().getEncodeProtocol() != Protocol.CONFIGURATION ) + { + user.unsafe().sendPacket( new StartConfiguration() ); + } } else { LoginResult loginProfile = user.getPendingConnection().getLoginProfile(); 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 54842ca0ee..ad42105166 100644 --- a/proxy/src/main/java/net/md_5/bungee/UserConnection.java +++ b/proxy/src/main/java/net/md_5/bungee/UserConnection.java @@ -306,6 +306,11 @@ public void connect(final ServerConnectRequest request) { Preconditions.checkNotNull( request, "request" ); + ch.getHandle().eventLoop().execute( () -> connect0( request ) ); + } + + private void connect0(final ServerConnectRequest request) + { final Callback callback = request.getCallback(); ServerConnectEvent event = new ServerConnectEvent( this, request.getTarget(), request.getReason(), request ); if ( bungee.getPluginManager().callEvent( event ).isCancelled() ) @@ -390,11 +395,11 @@ public void operationComplete(ChannelFuture future) throws Exception } }; Bootstrap b = new Bootstrap() - .channel( PipelineUtils.getChannel( target.getAddress() ) ) - .group( ch.getHandle().eventLoop() ) - .handler( initializer ) - .option( ChannelOption.CONNECT_TIMEOUT_MILLIS, request.getConnectTimeout() ) - .remoteAddress( target.getAddress() ); + .channel( PipelineUtils.getChannel( target.getAddress() ) ) + .group( ch.getHandle().eventLoop() ) + .handler( initializer ) + .option( ChannelOption.CONNECT_TIMEOUT_MILLIS, request.getConnectTimeout() ) + .remoteAddress( target.getAddress() ); // Windows is bugged, multi homed users will just have to live with random connecting IPs if ( getPendingConnection().getListener().isSetLocalAddress() && !PlatformDependent.isWindows() && getPendingConnection().getListener().getSocketAddress() instanceof InetSocketAddress ) { From bf742b1e299802e778a030774662e7a837b410f7 Mon Sep 17 00:00:00 2001 From: BoomEaro <21033866+BoomEaro@users.noreply.github.com> Date: Thu, 19 Sep 2024 01:09:47 +0300 Subject: [PATCH 2/3] Fix formatting... --- .../src/main/java/net/md_5/bungee/UserConnection.java | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) 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 ad42105166..16e83d35b9 100644 --- a/proxy/src/main/java/net/md_5/bungee/UserConnection.java +++ b/proxy/src/main/java/net/md_5/bungee/UserConnection.java @@ -395,11 +395,11 @@ public void operationComplete(ChannelFuture future) throws Exception } }; Bootstrap b = new Bootstrap() - .channel( PipelineUtils.getChannel( target.getAddress() ) ) - .group( ch.getHandle().eventLoop() ) - .handler( initializer ) - .option( ChannelOption.CONNECT_TIMEOUT_MILLIS, request.getConnectTimeout() ) - .remoteAddress( target.getAddress() ); + .channel( PipelineUtils.getChannel( target.getAddress() ) ) + .group( ch.getHandle().eventLoop() ) + .handler( initializer ) + .option( ChannelOption.CONNECT_TIMEOUT_MILLIS, request.getConnectTimeout() ) + .remoteAddress( target.getAddress() ); // Windows is bugged, multi homed users will just have to live with random connecting IPs if ( getPendingConnection().getListener().isSetLocalAddress() && !PlatformDependent.isWindows() && getPendingConnection().getListener().getSocketAddress() instanceof InetSocketAddress ) { From 851e76d7d266a663c8e7a2e591f639b546a33e61 Mon Sep 17 00:00:00 2001 From: BoomEaro <21033866+BoomEaro@users.noreply.github.com> Date: Thu, 19 Sep 2024 12:50:06 +0300 Subject: [PATCH 3/3] Do not throw IllegalStateException if ServerConnectEvent was cancelled and server is null. --- proxy/src/main/java/net/md_5/bungee/UserConnection.java | 4 ---- 1 file changed, 4 deletions(-) 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 16e83d35b9..2359e86472 100644 --- a/proxy/src/main/java/net/md_5/bungee/UserConnection.java +++ b/proxy/src/main/java/net/md_5/bungee/UserConnection.java @@ -320,10 +320,6 @@ private void connect0(final ServerConnectRequest request) callback.done( ServerConnectRequest.Result.EVENT_CANCEL, null ); } - if ( getServer() == null && !ch.isClosing() ) - { - throw new IllegalStateException( "Cancelled ServerConnectEvent with no server or disconnect." ); - } return; }