diff --git a/api/src/main/java/com/velocitypowered/api/proxy/server/RegisteredServer.java b/api/src/main/java/com/velocitypowered/api/proxy/server/RegisteredServer.java index 1766833eca..52e95d592c 100644 --- a/api/src/main/java/com/velocitypowered/api/proxy/server/RegisteredServer.java +++ b/api/src/main/java/com/velocitypowered/api/proxy/server/RegisteredServer.java @@ -50,4 +50,14 @@ public interface RegisteredServer extends ChannelMessageSink, Audience { * @since 3.2.0 */ CompletableFuture ping(PingOptions pingOptions); + + /** + * Attempts to ping the remote server with the given virtualhost and return the server list ping + * result according to the options provided. + * + * @param pingOptions the options provided for pinging the server + * @param virtualHostString the virtual hostname to pass to the server for the ping + * @return the server ping result from the server + */ + CompletableFuture ping(PingOptions pingOptions, String virtualHostString); } diff --git a/proxy/src/main/java/com/velocitypowered/proxy/connection/util/ServerListPingHandler.java b/proxy/src/main/java/com/velocitypowered/proxy/connection/util/ServerListPingHandler.java index 951d609861..c0b8e0f778 100644 --- a/proxy/src/main/java/com/velocitypowered/proxy/connection/util/ServerListPingHandler.java +++ b/proxy/src/main/java/com/velocitypowered/proxy/connection/util/ServerListPingHandler.java @@ -63,7 +63,7 @@ private ServerPing constructLocalPing(ProtocolVersion version) { } private CompletableFuture attemptPingPassthrough(VelocityInboundConnection connection, - PingPassthroughMode mode, List servers, ProtocolVersion responseProtocolVersion) { + PingPassthroughMode mode, List servers, ProtocolVersion responseProtocolVersion, String virtualHostStr) { ServerPing fallback = constructLocalPing(connection.getProtocolVersion()); List> pings = new ArrayList<>(); for (String s : servers) { @@ -73,7 +73,7 @@ private CompletableFuture attemptPingPassthrough(VelocityInboundConn } VelocityRegisteredServer vrs = (VelocityRegisteredServer) rs.get(); pings.add(vrs.ping(connection.getConnection().eventLoop(), PingOptions.builder() - .version(responseProtocolVersion).build())); + .version(responseProtocolVersion).build(), virtualHostStr)); } if (pings.isEmpty()) { return CompletableFuture.completedFuture(fallback); @@ -155,7 +155,7 @@ public CompletableFuture getInitialPing(VelocityInboundConnection co .orElse(""); List serversToTry = server.getConfiguration().getForcedHosts().getOrDefault( virtualHostStr, server.getConfiguration().getAttemptConnectionOrder()); - return attemptPingPassthrough(connection, passthroughMode, serversToTry, shownVersion); + return attemptPingPassthrough(connection, passthroughMode, serversToTry, shownVersion, virtualHostStr); } } } diff --git a/proxy/src/main/java/com/velocitypowered/proxy/server/PingSessionHandler.java b/proxy/src/main/java/com/velocitypowered/proxy/server/PingSessionHandler.java index c47d5d436c..9d8e29742a 100644 --- a/proxy/src/main/java/com/velocitypowered/proxy/server/PingSessionHandler.java +++ b/proxy/src/main/java/com/velocitypowered/proxy/server/PingSessionHandler.java @@ -42,20 +42,22 @@ public class PingSessionHandler implements MinecraftSessionHandler { private final MinecraftConnection connection; private final ProtocolVersion version; private boolean completed = false; + private final String virtualHostString; PingSessionHandler(CompletableFuture result, RegisteredServer server, - MinecraftConnection connection, ProtocolVersion version) { + MinecraftConnection connection, ProtocolVersion version, String virtualHostString) { this.result = result; this.server = server; this.connection = connection; this.version = version; + this.virtualHostString = virtualHostString; } @Override public void activated() { HandshakePacket handshake = new HandshakePacket(); handshake.setNextStatus(StateRegistry.STATUS_ID); - handshake.setServerAddress(server.getServerInfo().getAddress().getHostString()); + handshake.setServerAddress(this.virtualHostString.isEmpty() ? server.getServerInfo().getAddress().getHostString() : this.virtualHostString); handshake.setPort(server.getServerInfo().getAddress().getPort()); handshake.setProtocolVersion(version); connection.delayedWrite(handshake); diff --git a/proxy/src/main/java/com/velocitypowered/proxy/server/VelocityRegisteredServer.java b/proxy/src/main/java/com/velocitypowered/proxy/server/VelocityRegisteredServer.java index 837c46a252..291b134722 100644 --- a/proxy/src/main/java/com/velocitypowered/proxy/server/VelocityRegisteredServer.java +++ b/proxy/src/main/java/com/velocitypowered/proxy/server/VelocityRegisteredServer.java @@ -86,12 +86,21 @@ public Collection getPlayersConnected() { @Override public CompletableFuture ping(PingOptions pingOptions) { - return ping(null, pingOptions); + return ping(null, pingOptions, ""); + } + + @Override + public CompletableFuture ping(PingOptions pingOptions, String virtualHostString) { + return ping(null, pingOptions, virtualHostString); } @Override public CompletableFuture ping() { - return ping(null, PingOptions.DEFAULT); + return ping(null, PingOptions.DEFAULT, ""); + } + + public CompletableFuture ping(@Nullable EventLoop loop, PingOptions pingOptions) { + return ping(loop, pingOptions, ""); } /** @@ -102,7 +111,7 @@ public CompletableFuture ping() { * @param pingOptions the options to apply to this ping * @return the server list ping response */ - public CompletableFuture ping(@Nullable EventLoop loop, PingOptions pingOptions) { + public CompletableFuture ping(@Nullable EventLoop loop, PingOptions pingOptions, String virtualHostString) { if (server == null) { throw new IllegalStateException("No Velocity proxy instance available"); } @@ -125,7 +134,7 @@ protected void initChannel(Channel ch) throws Exception { if (future.isSuccess()) { MinecraftConnection conn = future.channel().pipeline().get(MinecraftConnection.class); PingSessionHandler handler = new PingSessionHandler(pingFuture, - VelocityRegisteredServer.this, conn, pingOptions.getProtocolVersion()); + VelocityRegisteredServer.this, conn, pingOptions.getProtocolVersion(), virtualHostString); conn.setActiveSessionHandler(StateRegistry.HANDSHAKE, handler); } else { pingFuture.completeExceptionally(future.cause());