From fb522876d7048f691b09d56b573d4a8e22ca5495 Mon Sep 17 00:00:00 2001 From: scratchyone Date: Tue, 5 Mar 2024 12:28:15 -0500 Subject: [PATCH 1/2] Add virtualhost support for server list pings --- .../proxy/connection/util/ServerListPingHandler.java | 6 +++--- .../velocitypowered/proxy/server/PingSessionHandler.java | 6 ++++-- .../proxy/server/VelocityRegisteredServer.java | 8 ++++++-- 3 files changed, 13 insertions(+), 7 deletions(-) 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..41ff9f7018 100644 --- a/proxy/src/main/java/com/velocitypowered/proxy/server/VelocityRegisteredServer.java +++ b/proxy/src/main/java/com/velocitypowered/proxy/server/VelocityRegisteredServer.java @@ -94,6 +94,10 @@ public CompletableFuture ping() { return ping(null, PingOptions.DEFAULT); } + public CompletableFuture ping(@Nullable EventLoop loop, PingOptions pingOptions) { + return ping(loop, pingOptions, ""); + } + /** * Pings the specified server using the specified event {@code loop}, claiming to be {@code * version}. @@ -102,7 +106,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 +129,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()); From 35ccd3e869823c9008f81a3af1b8c0bf6bc96465 Mon Sep 17 00:00:00 2001 From: scratchyone Date: Thu, 7 Mar 2024 22:13:14 -0500 Subject: [PATCH 2/2] Add virtualhost support to ping public API --- .../api/proxy/server/RegisteredServer.java | 10 ++++++++++ .../proxy/server/VelocityRegisteredServer.java | 9 +++++++-- 2 files changed, 17 insertions(+), 2 deletions(-) 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/server/VelocityRegisteredServer.java b/proxy/src/main/java/com/velocitypowered/proxy/server/VelocityRegisteredServer.java index 41ff9f7018..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,17 @@ 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) {