Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Virtualhost support for server list pings #1265

Open
wants to merge 2 commits into
base: dev/3.0.0
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -50,4 +50,14 @@ public interface RegisteredServer extends ChannelMessageSink, Audience {
* @since 3.2.0
*/
CompletableFuture<ServerPing> 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<ServerPing> ping(PingOptions pingOptions, String virtualHostString);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of adding another ping method, add a virtual host field to PingOptions

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sounds good! Should I make this change for the private internal API as well?

}
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ private ServerPing constructLocalPing(ProtocolVersion version) {
}

private CompletableFuture<ServerPing> attemptPingPassthrough(VelocityInboundConnection connection,
PingPassthroughMode mode, List<String> servers, ProtocolVersion responseProtocolVersion) {
PingPassthroughMode mode, List<String> servers, ProtocolVersion responseProtocolVersion, String virtualHostStr) {
ServerPing fallback = constructLocalPing(connection.getProtocolVersion());
List<CompletableFuture<ServerPing>> pings = new ArrayList<>();
for (String s : servers) {
Expand All @@ -73,7 +73,7 @@ private CompletableFuture<ServerPing> 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);
Expand Down Expand Up @@ -155,7 +155,7 @@ public CompletableFuture<ServerPing> getInitialPing(VelocityInboundConnection co
.orElse("");
List<String> serversToTry = server.getConfiguration().getForcedHosts().getOrDefault(
virtualHostStr, server.getConfiguration().getAttemptConnectionOrder());
return attemptPingPassthrough(connection, passthroughMode, serversToTry, shownVersion);
return attemptPingPassthrough(connection, passthroughMode, serversToTry, shownVersion, virtualHostStr);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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<ServerPing> 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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,12 +86,21 @@ public Collection<Player> getPlayersConnected() {

@Override
public CompletableFuture<ServerPing> ping(PingOptions pingOptions) {
return ping(null, pingOptions);
return ping(null, pingOptions, "");
}

@Override
public CompletableFuture<ServerPing> ping(PingOptions pingOptions, String virtualHostString) {
return ping(null, pingOptions, virtualHostString);
}

@Override
public CompletableFuture<ServerPing> ping() {
return ping(null, PingOptions.DEFAULT);
return ping(null, PingOptions.DEFAULT, "");
}

public CompletableFuture<ServerPing> ping(@Nullable EventLoop loop, PingOptions pingOptions) {
return ping(loop, pingOptions, "");
}

/**
Expand All @@ -102,7 +111,7 @@ public CompletableFuture<ServerPing> ping() {
* @param pingOptions the options to apply to this ping
* @return the server list ping response
*/
public CompletableFuture<ServerPing> ping(@Nullable EventLoop loop, PingOptions pingOptions) {
public CompletableFuture<ServerPing> ping(@Nullable EventLoop loop, PingOptions pingOptions, String virtualHostString) {
if (server == null) {
throw new IllegalStateException("No Velocity proxy instance available");
}
Expand All @@ -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());
Expand Down