From d221e529294e4c004e54d79f01f7c2c6a1d8276b Mon Sep 17 00:00:00 2001 From: Kevin Ludwig Date: Sat, 2 Jul 2022 10:26:28 +1000 Subject: [PATCH] #3241: Support ping passthrough for legacy pings --- .../bungee/api/AbstractReconnectHandler.java | 9 +-- .../bungee/connection/InitialHandler.java | 74 ++++++++++++------- 2 files changed, 49 insertions(+), 34 deletions(-) diff --git a/api/src/main/java/net/md_5/bungee/api/AbstractReconnectHandler.java b/api/src/main/java/net/md_5/bungee/api/AbstractReconnectHandler.java index 2adcd5739..e99fe7648 100644 --- a/api/src/main/java/net/md_5/bungee/api/AbstractReconnectHandler.java +++ b/api/src/main/java/net/md_5/bungee/api/AbstractReconnectHandler.java @@ -28,18 +28,13 @@ public abstract class AbstractReconnectHandler implements ReconnectHandler public static ServerInfo getForcedHost(PendingConnection con) { - if ( con.getVirtualHost() == null ) - { - return null; - } - - String forced = con.getListener().getForcedHosts().get( con.getVirtualHost().getHostString() ); + String forced = ( con.getVirtualHost() == null ) ? null : con.getListener().getForcedHosts().get( con.getVirtualHost().getHostString() ); if ( forced == null && con.getListener().isForceDefault() ) { forced = con.getListener().getDefaultServer(); } - return ProxyServer.getInstance().getServerInfo( forced ); + return ( forced == null ) ? null : ProxyServer.getInstance().getServerInfo( forced ); } protected abstract ServerInfo getStoredServer(ProxiedPlayer player); 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 0558e4d88..ce7b64d67 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 @@ -25,7 +25,6 @@ import net.md_5.bungee.Util; import net.md_5.bungee.api.AbstractReconnectHandler; import net.md_5.bungee.api.Callback; import net.md_5.bungee.api.ChatColor; -import net.md_5.bungee.api.Favicon; import net.md_5.bungee.api.ServerPing; import net.md_5.bungee.api.chat.BaseComponent; import net.md_5.bungee.api.chat.TextComponent; @@ -175,44 +174,65 @@ public class InitialHandler extends PacketHandler implements PendingConnection this.legacy = true; final boolean v1_5 = ping.isV1_5(); - ServerPing legacy = new ServerPing( new ServerPing.Protocol( bungee.getName() + " " + bungee.getGameVersion(), bungee.getProtocolVersion() ), - new ServerPing.Players( listener.getMaxPlayers(), bungee.getOnlineCount(), null ), - new TextComponent( TextComponent.fromLegacyText( listener.getMotd() ) ), (Favicon) null ); + ServerInfo forced = AbstractReconnectHandler.getForcedHost( this ); + final String motd = ( forced != null ) ? forced.getMotd() : listener.getMotd(); + final int protocol = bungee.getProtocolVersion(); - Callback callback = new Callback() + Callback pingBack = new Callback() { @Override - public void done(ProxyPingEvent result, Throwable error) + public void done(ServerPing result, Throwable error) { - if ( ch.isClosed() ) + if ( error != null ) { - return; + result = getPingInfo( bungee.getTranslation( "ping_cannot_connect" ), protocol ); + bungee.getLogger().log( Level.WARNING, "Error pinging remote server", error ); } - ServerPing legacy = result.getResponse(); - String kickMessage; - - if ( v1_5 ) + Callback callback = new Callback() { - kickMessage = ChatColor.DARK_BLUE - + "\00" + 127 - + '\00' + legacy.getVersion().getName() - + '\00' + getFirstLine( legacy.getDescription() ) - + '\00' + legacy.getPlayers().getOnline() - + '\00' + legacy.getPlayers().getMax(); - } else - { - // Clients <= 1.3 don't support colored motds because the color char is used as delimiter - kickMessage = ChatColor.stripColor( getFirstLine( legacy.getDescription() ) ) - + '\u00a7' + legacy.getPlayers().getOnline() - + '\u00a7' + legacy.getPlayers().getMax(); - } + @Override + public void done(ProxyPingEvent result, Throwable error) + { + if ( ch.isClosed() ) + { + return; + } - ch.close( kickMessage ); + ServerPing legacy = result.getResponse(); + String kickMessage; + + if ( v1_5 ) + { + kickMessage = ChatColor.DARK_BLUE + + "\00" + 127 + + '\00' + legacy.getVersion().getName() + + '\00' + getFirstLine( legacy.getDescription() ) + + '\00' + legacy.getPlayers().getOnline() + + '\00' + legacy.getPlayers().getMax(); + } else + { + // Clients <= 1.3 don't support colored motds because the color char is used as delimiter + kickMessage = ChatColor.stripColor( getFirstLine( legacy.getDescription() ) ) + + '\u00a7' + legacy.getPlayers().getOnline() + + '\u00a7' + legacy.getPlayers().getMax(); + } + + ch.close( kickMessage ); + } + }; + + bungee.getPluginManager().callEvent( new ProxyPingEvent( InitialHandler.this, result, callback ) ); } }; - bungee.getPluginManager().callEvent( new ProxyPingEvent( this, legacy, callback ) ); + if ( forced != null && listener.isPingPassthrough() ) + { + ( (BungeeServerInfo) forced ).ping( pingBack, bungee.getProtocolVersion() ); + } else + { + pingBack.done( getPingInfo( motd, protocol ), null ); + } } private static String getFirstLine(String str)