Skip to content
Open
Show file tree
Hide file tree
Changes from 3 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 @@ -12,6 +12,7 @@
import com.velocitypowered.api.network.ProtocolVersion;
import java.net.InetSocketAddress;
import java.util.Optional;
import java.util.UUID;

/**
* Represents an incoming connection to the proxy.
Expand Down Expand Up @@ -68,4 +69,14 @@ public interface InboundConnection {
* @return the intent of the connection
*/
HandshakeIntent getHandshakeIntent();

/**
* Returns the unique, stable session UUID for this connection, generated when
* the client initially connects to the proxy. The session id remains consistent
* across all login-phase events and all proxy-to-backend connections for the
* same player session.
Comment thread
xfy2412 marked this conversation as resolved.
Outdated
*
* @return the session UUID, never {@code null}
*/
UUID getSessionId();
}
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@
import java.util.EnumMap;
import java.util.Map;
import java.util.Objects;
import java.util.UUID;
import java.util.concurrent.TimeUnit;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;
Expand All @@ -87,6 +88,7 @@ public class MinecraftConnection extends ChannelInboundHandlerAdapter {

private static final Logger logger = LogManager.getLogger(MinecraftConnection.class);

private final @Nullable UUID sessionId;
private final Channel channel;
public boolean pendingConfigurationSwitch = false;
private SocketAddress remoteAddress;
Expand All @@ -105,10 +107,11 @@ public class MinecraftConnection extends ChannelInboundHandlerAdapter {
* @param channel the channel on the connection
* @param server the Velocity instance
*/
public MinecraftConnection(Channel channel, VelocityServer server) {
public MinecraftConnection(Channel channel, VelocityServer server, @Nullable UUID sessionId) {
this.channel = channel;
this.remoteAddress = channel.remoteAddress();
this.server = server;
this.sessionId = sessionId;
this.state = StateRegistry.HANDSHAKE;

this.sessionHandlers = new EnumMap<>(StateRegistry.class);
Expand Down Expand Up @@ -322,6 +325,17 @@ public Channel getChannel() {
return channel;
}

/**
* Returns the session UUID for this connection. Client connections have a
* unique session UUID; backend connections share the client's session UUID;
* ping connections have {@code null}.
Comment thread
xfy2412 marked this conversation as resolved.
Outdated
*
* @return the session UUID, or {@code null} for ping connections
*/
public @Nullable UUID getSessionId() {
return sessionId;
}

public boolean isClosed() {
return !channel.isActive();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,8 @@ public CompletableFuture<Impl> connect() {
.connect(registeredServer.getServerInfo().getAddress())
.addListener((ChannelFutureListener) future -> {
if (future.isSuccess()) {
connection = new MinecraftConnection(future.channel(), server);
connection = new MinecraftConnection(future.channel(), server,
proxyPlayer.getConnection().getSessionId());
connection.setAssociation(VelocityServerConnection.this);
future.channel().pipeline().addLast(HANDLER, connection);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -321,6 +321,11 @@ public MinecraftConnection getConnection() {
return connection;
}

@Override
public UUID getSessionId() {
return connection.getSessionId();
}

@Override
public long getPing() {
return this.ping;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
import java.net.InetSocketAddress;
import java.util.Optional;
import java.util.Queue;
import java.util.UUID;
import java.util.concurrent.ConcurrentLinkedQueue;
import java.util.concurrent.atomic.AtomicIntegerFieldUpdater;
import net.kyori.adventure.text.Component;
Expand Down Expand Up @@ -183,4 +184,9 @@ public ProtocolState getProtocolState() {
public HandshakeIntent getHandshakeIntent() {
return delegate.getHandshakeIntent();
}

@Override
public UUID getSessionId() {
return delegate.getSessionId();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,17 @@

import com.velocitypowered.api.proxy.InboundConnection;
import com.velocitypowered.proxy.connection.MinecraftConnection;
import java.util.UUID;

/**
* Base internal interface for a {@link InboundConnection}.
*/
public interface VelocityInboundConnection extends InboundConnection {

MinecraftConnection getConnection();

@Override
default UUID getSessionId() {
return getConnection().getSessionId();
Comment thread
xfy2412 marked this conversation as resolved.
Outdated
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
import io.netty.channel.ChannelInitializer;
import io.netty.handler.codec.haproxy.HAProxyMessageDecoder;
import io.netty.handler.timeout.ReadTimeoutHandler;
import java.util.UUID;
import java.util.concurrent.TimeUnit;

/**
Expand Down Expand Up @@ -69,7 +70,7 @@ protected void initChannel(final Channel ch) {
.addLast(MINECRAFT_DECODER, new MinecraftDecoder(ProtocolUtils.Direction.SERVERBOUND))
.addLast(MINECRAFT_ENCODER, new MinecraftEncoder(ProtocolUtils.Direction.CLIENTBOUND));

final MinecraftConnection connection = new MinecraftConnection(ch, this.server);
final MinecraftConnection connection = new MinecraftConnection(ch, this.server, UUID.randomUUID());
connection.setActiveSessionHandler(StateRegistry.HANDSHAKE,
new HandshakeSessionHandler(connection, this.server));
ch.pipeline().addLast(Connections.HANDLER, connection);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ protected void initChannel(Channel ch) {
.addLast(MINECRAFT_DECODER, new MinecraftDecoder(ProtocolUtils.Direction.CLIENTBOUND))
.addLast(MINECRAFT_ENCODER, new MinecraftEncoder(ProtocolUtils.Direction.SERVERBOUND));

ch.pipeline().addLast(HANDLER, new MinecraftConnection(ch, server));
ch.pipeline().addLast(HANDLER, new MinecraftConnection(ch, server, null));
}
}).connect(serverInfo.getAddress()).addListener((ChannelFutureListener) future -> {
if (future.isSuccess()) {
Expand Down