Skip to content

Commit

Permalink
expose raw vhost (#1423)
Browse files Browse the repository at this point in the history
  • Loading branch information
456dev authored Oct 7, 2024
1 parent ef1f500 commit 99aaf3c
Show file tree
Hide file tree
Showing 6 changed files with 34 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,20 @@ public interface InboundConnection {

/**
* Returns the hostname that the user entered into the client, if applicable.
*
* <br/>
* This is partially processed, including removing a trailing dot, and discarding data after a null byte.
* @return the hostname from the client
*/
Optional<InetSocketAddress> getVirtualHost();

/**
* Returns the raw hostname that the client sent, if applicable.
*
* @return the raw hostname from the client
*/
Optional<String> getRawVirtualHost();

/**
* Determine whether or not the player remains online.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ public void activated() {

// Initiate a regular connection and move over to it.
ConnectedPlayer player = new ConnectedPlayer(server, profileEvent.getGameProfile(),
mcConnection, inbound.getVirtualHost().orElse(null), onlineMode,
mcConnection, inbound.getVirtualHost().orElse(null), inbound.getRawVirtualHost().orElse(null), onlineMode,
inbound.getIdentifiedKey());
this.connectedPlayer = player;
if (!server.canRegisterConnection(player)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,7 @@ public class ConnectedPlayer implements MinecraftConnectionAssociation, Player,
*/
private final MinecraftConnection connection;
private final @Nullable InetSocketAddress virtualHost;
private final @Nullable String rawVirtualHost;
private GameProfile profile;
private PermissionFunction permissionFunction;
private int tryIndex = 0;
Expand Down Expand Up @@ -191,12 +192,13 @@ public class ConnectedPlayer implements MinecraftConnectionAssociation, Player,
private final ChatBuilderFactory chatBuilderFactory;

ConnectedPlayer(VelocityServer server, GameProfile profile, MinecraftConnection connection,
@Nullable InetSocketAddress virtualHost, boolean onlineMode,
@Nullable InetSocketAddress virtualHost, @Nullable String rawVirtualHost, boolean onlineMode,
@Nullable IdentifiedKey playerKey) {
this.server = server;
this.profile = profile;
this.connection = connection;
this.virtualHost = virtualHost;
this.rawVirtualHost = rawVirtualHost;
this.permissionFunction = PermissionFunction.ALWAYS_UNDEFINED;
this.connectionPhase = connection.getType().getInitialClientPhase();
this.onlineMode = onlineMode;
Expand Down Expand Up @@ -356,6 +358,11 @@ public Optional<InetSocketAddress> getVirtualHost() {
return Optional.ofNullable(virtualHost);
}

@Override
public Optional<String> getRawVirtualHost() {
return Optional.ofNullable(rawVirtualHost);
}

void setPermissionFunction(PermissionFunction permissionFunction) {
this.permissionFunction = permissionFunction;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,11 @@ public Optional<InetSocketAddress> getVirtualHost() {
return Optional.ofNullable(ping.getVhost());
}

@Override
public Optional<String> getRawVirtualHost() {
return getVirtualHost().map(InetSocketAddress::getHostName);
}

@Override
public boolean isActive() {
return !connection.isClosed();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,11 @@ public Optional<InetSocketAddress> getVirtualHost() {
return Optional.of(InetSocketAddress.createUnresolved(cleanedAddress, handshake.getPort()));
}

@Override
public Optional<String> getRawVirtualHost() {
return Optional.of(handshake.getServerAddress());
}

@Override
public boolean isActive() {
return connection.getChannel().isActive();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,11 @@ public Optional<InetSocketAddress> getVirtualHost() {
return delegate.getVirtualHost();
}

@Override
public Optional<String> getRawVirtualHost() {
return delegate.getRawVirtualHost();
}

@Override
public boolean isActive() {
return delegate.isActive();
Expand Down

0 comments on commit 99aaf3c

Please sign in to comment.