diff --git a/api/src/main/java/com/velocitypowered/api/proxy/server/PingOptions.java b/api/src/main/java/com/velocitypowered/api/proxy/server/PingOptions.java index 51be358e4..a3cd0ac17 100644 --- a/api/src/main/java/com/velocitypowered/api/proxy/server/PingOptions.java +++ b/api/src/main/java/com/velocitypowered/api/proxy/server/PingOptions.java @@ -15,6 +15,7 @@ import java.util.Objects; import java.util.concurrent.TimeUnit; import net.kyori.adventure.builder.AbstractBuilder; import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; /** * Contains the parameters used to ping a {@link RegisteredServer}. @@ -30,10 +31,12 @@ public final class PingOptions { public static final PingOptions DEFAULT = PingOptions.builder().build(); private final ProtocolVersion protocolVersion; private final long timeout; + private final String virtualHost; private PingOptions(final Builder builder) { this.protocolVersion = builder.protocolVersion; this.timeout = builder.timeout; + this.virtualHost = builder.virtualHost; } /** @@ -54,6 +57,16 @@ public final class PingOptions { return this.timeout; } + /** + * The virtual host to pass to the server for the ping. + * + * @return the virtual hostname to pass to the server for the ping + * @since 3.4.0 + */ + public @Nullable String getVirtualHost() { + return this.virtualHost; + } + /** * Create a new builder to assign values to a new PingOptions. * @@ -68,10 +81,9 @@ public final class PingOptions { if (o == null) { return false; } - if (!(o instanceof PingOptions)) { + if (!(o instanceof final PingOptions other)) { return false; } - final PingOptions other = (PingOptions) o; return Objects.equals(this.protocolVersion, other.protocolVersion) && Objects.equals(this.timeout, other.timeout); } @@ -97,6 +109,7 @@ public final class PingOptions { public static final class Builder implements AbstractBuilder { private ProtocolVersion protocolVersion = ProtocolVersion.UNKNOWN; private long timeout = 0; + private String virtualHost = null; private Builder() { } @@ -146,6 +159,18 @@ public final class PingOptions { return this; } + /** + * Sets the virtual host to pass to the server for the ping. + * + * @param virtualHost the virtual hostname to pass to the server for the ping + * @return this builder + * @since 3.4.0 + */ + public Builder virtualHost(final @Nullable String virtualHost) { + this.virtualHost = virtualHost; + return this; + } + /** * Create a new {@link PingOptions} with the values of this Builder. * 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 951d60986..97947eb6b 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 @@ public class ServerListPingHandler { } 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 @@ public class ServerListPingHandler { } VelocityRegisteredServer vrs = (VelocityRegisteredServer) rs.get(); pings.add(vrs.ping(connection.getConnection().eventLoop(), PingOptions.builder() - .version(responseProtocolVersion).build())); + .version(responseProtocolVersion).virtualHost(virtualHostStr).build())); } if (pings.isEmpty()) { return CompletableFuture.completedFuture(fallback); @@ -155,7 +155,7 @@ public class ServerListPingHandler { .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 89760344a..e58c72b44 100644 --- a/proxy/src/main/java/com/velocitypowered/proxy/server/PingSessionHandler.java +++ b/proxy/src/main/java/com/velocitypowered/proxy/server/PingSessionHandler.java @@ -43,20 +43,23 @@ 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.setIntent(HandshakeIntent.STATUS); - handshake.setServerAddress(server.getServerInfo().getAddress().getHostString()); + handshake.setServerAddress(this.virtualHostString == null || 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 a3ddd71ca..697135e38 100644 --- a/proxy/src/main/java/com/velocitypowered/proxy/server/VelocityRegisteredServer.java +++ b/proxy/src/main/java/com/velocitypowered/proxy/server/VelocityRegisteredServer.java @@ -129,7 +129,7 @@ public class VelocityRegisteredServer implements RegisteredServer, ForwardingAud 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(), pingOptions.getVirtualHost()); conn.setActiveSessionHandler(StateRegistry.HANDSHAKE, handler); } else { pingFuture.completeExceptionally(future.cause());