3
0
Mirror von https://github.com/PaperMC/Velocity.git synchronisiert 2024-11-17 05:20:14 +01:00

Make ServerboundHandshakePacket immutable

Dieser Commit ist enthalten in:
Andrew Steinborn 2021-04-23 19:16:47 -04:00
Ursprung 3dc3c40c33
Commit f00b9b73ea
9 geänderte Dateien mit 52 neuen und 75 gelöschten Zeilen

Datei anzeigen

@ -29,7 +29,7 @@ public interface InboundConnection {
* *
* @return the hostname from the client * @return the hostname from the client
*/ */
Optional<InetSocketAddress> connectedHost(); Optional<InetSocketAddress> connectedHostname();
/** /**
* Determine whether or not the player remains online. * Determine whether or not the player remains online.

Datei anzeigen

@ -118,8 +118,8 @@ public class VelocityServerConnection implements MinecraftConnectionAssociation,
return result; return result;
} }
private String getHandshakeRemoteAddress() { private String playerConnectedHostname() {
return proxyPlayer.connectedHost().map(InetSocketAddress::getHostString).orElse(""); return proxyPlayer.connectedHostname().map(InetSocketAddress::getHostString).orElse("");
} }
private String createLegacyForwardingAddress(UnaryOperator<List<Property>> propertiesTransform) { private String createLegacyForwardingAddress(UnaryOperator<List<Property>> propertiesTransform) {
@ -128,10 +128,10 @@ public class VelocityServerConnection implements MinecraftConnectionAssociation,
// UUID (undashed), and if you are in online-mode, their login properties (from Mojang). // UUID (undashed), and if you are in online-mode, their login properties (from Mojang).
SocketAddress playerRemoteAddress = proxyPlayer.remoteAddress(); SocketAddress playerRemoteAddress = proxyPlayer.remoteAddress();
if (!(playerRemoteAddress instanceof InetSocketAddress)) { if (!(playerRemoteAddress instanceof InetSocketAddress)) {
return getHandshakeRemoteAddress(); return playerConnectedHostname();
} }
StringBuilder data = new StringBuilder() StringBuilder data = new StringBuilder()
.append(getHandshakeRemoteAddress()) .append(playerConnectedHostname())
.append('\0') .append('\0')
.append(((InetSocketAddress) proxyPlayer.remoteAddress()).getHostString()) .append(((InetSocketAddress) proxyPlayer.remoteAddress()).getHostString())
.append('\0') .append('\0')
@ -162,24 +162,12 @@ public class VelocityServerConnection implements MinecraftConnectionAssociation,
// Initiate the handshake. // Initiate the handshake.
ProtocolVersion protocolVersion = proxyPlayer.getConnection().getProtocolVersion(); ProtocolVersion protocolVersion = proxyPlayer.getConnection().getProtocolVersion();
ServerboundHandshakePacket handshake = new ServerboundHandshakePacket(); String address = getHandshakeAddressField(forwardingMode);
handshake.setNextStatus(StateRegistry.LOGIN_ID);
handshake.setProtocolVersion(protocolVersion);
if (forwardingMode == PlayerInfoForwarding.LEGACY) {
handshake.setServerAddress(createLegacyForwardingAddress());
} else if (forwardingMode == PlayerInfoForwarding.BUNGEEGUARD) {
byte[] secret = server.configuration().getForwardingSecret();
handshake.setServerAddress(createBungeeGuardForwardingAddress(secret));
} else if (proxyPlayer.getConnection().getType() == ConnectionTypes.LEGACY_FORGE) {
handshake.setServerAddress(getHandshakeRemoteAddress() + HANDSHAKE_HOSTNAME_TOKEN);
} else {
handshake.setServerAddress(getHandshakeRemoteAddress());
}
SocketAddress destinationAddr = registeredServer.serverInfo().address(); SocketAddress destinationAddr = registeredServer.serverInfo().address();
if (destinationAddr instanceof InetSocketAddress) { int port = destinationAddr instanceof InetSocketAddress
handshake.setPort(((InetSocketAddress) destinationAddr).getPort()); ? ((InetSocketAddress) destinationAddr).getPort() : 0;
} ServerboundHandshakePacket handshake = new ServerboundHandshakePacket(protocolVersion,
address, port, StateRegistry.LOGIN_ID);
mc.delayedWrite(handshake); mc.delayedWrite(handshake);
mc.setProtocolVersion(protocolVersion); mc.setProtocolVersion(protocolVersion);
@ -188,6 +176,19 @@ public class VelocityServerConnection implements MinecraftConnectionAssociation,
mc.flush(); mc.flush();
} }
private String getHandshakeAddressField(PlayerInfoForwarding forwardingMode) {
if (forwardingMode == PlayerInfoForwarding.LEGACY) {
return createLegacyForwardingAddress();
} else if (forwardingMode == PlayerInfoForwarding.BUNGEEGUARD) {
byte[] secret = server.configuration().getForwardingSecret();
return createBungeeGuardForwardingAddress(secret);
} else if (proxyPlayer.getConnection().getType() == ConnectionTypes.LEGACY_FORGE) {
return playerConnectedHostname() + HANDSHAKE_HOSTNAME_TOKEN;
} else {
return playerConnectedHostname();
}
}
public @Nullable MinecraftConnection getConnection() { public @Nullable MinecraftConnection getConnection() {
return connection; return connection;
} }

Datei anzeigen

@ -50,7 +50,6 @@ import com.velocitypowered.api.proxy.server.RegisteredServer;
import com.velocitypowered.api.util.GameProfile; import com.velocitypowered.api.util.GameProfile;
import com.velocitypowered.api.util.ModInfo; import com.velocitypowered.api.util.ModInfo;
import com.velocitypowered.proxy.VelocityServer; import com.velocitypowered.proxy.VelocityServer;
import com.velocitypowered.proxy.config.VelocityConfiguration;
import com.velocitypowered.proxy.connection.MinecraftConnection; import com.velocitypowered.proxy.connection.MinecraftConnection;
import com.velocitypowered.proxy.connection.MinecraftConnectionAssociation; import com.velocitypowered.proxy.connection.MinecraftConnectionAssociation;
import com.velocitypowered.proxy.connection.backend.VelocityServerConnection; import com.velocitypowered.proxy.connection.backend.VelocityServerConnection;
@ -238,7 +237,7 @@ public class ConnectedPlayer implements MinecraftConnectionAssociation, Player {
} }
@Override @Override
public Optional<InetSocketAddress> connectedHost() { public Optional<InetSocketAddress> connectedHostname() {
return Optional.ofNullable(virtualHost); return Optional.ofNullable(virtualHost);
} }
@ -616,7 +615,7 @@ public class ConnectedPlayer implements MinecraftConnectionAssociation, Player {
*/ */
private Optional<RegisteredServer> getNextServerToTry(@Nullable RegisteredServer current) { private Optional<RegisteredServer> getNextServerToTry(@Nullable RegisteredServer current) {
if (serversToTry == null) { if (serversToTry == null) {
String virtualHostStr = connectedHost().map(InetSocketAddress::getHostString) String virtualHostStr = connectedHostname().map(InetSocketAddress::getHostString)
.orElse("") .orElse("")
.toLowerCase(Locale.ROOT); .toLowerCase(Locale.ROOT);
serversToTry = server.configuration().getForcedHosts().getOrDefault(virtualHostStr, serversToTry = server.configuration().getForcedHosts().getOrDefault(virtualHostStr,

Datei anzeigen

@ -213,7 +213,7 @@ public class HandshakeSessionHandler implements MinecraftSessionHandler {
} }
@Override @Override
public Optional<InetSocketAddress> connectedHost() { public Optional<InetSocketAddress> connectedHostname() {
return Optional.ofNullable(ping.getVhost()); return Optional.ofNullable(ping.getVhost());
} }

Datei anzeigen

@ -54,7 +54,7 @@ public final class InitialInboundConnection implements InboundConnection,
} }
@Override @Override
public Optional<InetSocketAddress> connectedHost() { public Optional<InetSocketAddress> connectedHostname() {
return Optional.of(InetSocketAddress.createUnresolved(cleanedAddress, handshake.getPort())); return Optional.of(InetSocketAddress.createUnresolved(cleanedAddress, handshake.getPort()));
} }

Datei anzeigen

@ -58,7 +58,6 @@ import java.security.GeneralSecurityException;
import java.security.KeyPair; import java.security.KeyPair;
import java.security.MessageDigest; import java.security.MessageDigest;
import java.util.Arrays; import java.util.Arrays;
import java.util.Locale;
import java.util.Optional; import java.util.Optional;
import java.util.UUID; import java.util.UUID;
import java.util.concurrent.CompletableFuture; import java.util.concurrent.CompletableFuture;
@ -66,7 +65,6 @@ import java.util.concurrent.ExecutionException;
import java.util.concurrent.ThreadLocalRandom; import java.util.concurrent.ThreadLocalRandom;
import net.kyori.adventure.text.Component; import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.format.NamedTextColor; import net.kyori.adventure.text.format.NamedTextColor;
import net.kyori.adventure.translation.GlobalTranslator;
import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger; import org.apache.logging.log4j.Logger;
import org.asynchttpclient.ListenableFuture; import org.asynchttpclient.ListenableFuture;
@ -240,7 +238,7 @@ public class LoginSessionHandler implements MinecraftSessionHandler {
// Initiate a regular connection and move over to it. // Initiate a regular connection and move over to it.
ConnectedPlayer player = new ConnectedPlayer(server, profileEvent.gameProfile(), ConnectedPlayer player = new ConnectedPlayer(server, profileEvent.gameProfile(),
mcConnection, inbound.connectedHost().orElse(null), onlineMode); mcConnection, inbound.connectedHostname().orElse(null), onlineMode);
this.connectedPlayer = player; this.connectedPlayer = player;
if (!server.canRegisterConnection(player)) { if (!server.canRegisterConnection(player)) {
player.disconnect0(Component.translatable("velocity.error.already-connected-proxy", player.disconnect0(Component.translatable("velocity.error.already-connected-proxy",

Datei anzeigen

@ -168,7 +168,7 @@ public class StatusSessionHandler implements MinecraftSessionHandler {
if (passthrough == PingPassthroughMode.DISABLED) { if (passthrough == PingPassthroughMode.DISABLED) {
return CompletableFuture.completedFuture(constructLocalPing(shownVersion)); return CompletableFuture.completedFuture(constructLocalPing(shownVersion));
} else { } else {
String virtualHostStr = inbound.connectedHost().map(InetSocketAddress::getHostString) String virtualHostStr = inbound.connectedHostname().map(InetSocketAddress::getHostString)
.map(str -> str.toLowerCase(Locale.ROOT)) .map(str -> str.toLowerCase(Locale.ROOT))
.orElse(""); .orElse("");
List<String> serversToTry = server.configuration().getForcedHosts().getOrDefault( List<String> serversToTry = server.configuration().getForcedHosts().getOrDefault(

Datei anzeigen

@ -35,15 +35,17 @@ public class ServerboundHandshakePacket implements Packet {
final int nextStatus = ProtocolUtils.readVarInt(buf); final int nextStatus = ProtocolUtils.readVarInt(buf);
return new ServerboundHandshakePacket(protocolVersion, hostname, port, nextStatus); return new ServerboundHandshakePacket(protocolVersion, hostname, port, nextStatus);
}; };
public static final PacketWriter<ServerboundHandshakePacket> ENCODER = PacketWriter.deprecatedEncode(); public static final PacketWriter<ServerboundHandshakePacket> ENCODER = (out, packet, version) -> {
ProtocolUtils.writeVarInt(out, packet.protocolVersion.protocol());
ProtocolUtils.writeString(out, packet.serverAddress);
out.writeShort(packet.port);
ProtocolUtils.writeVarInt(out, packet.nextStatus);
};
private ProtocolVersion protocolVersion; private final ProtocolVersion protocolVersion;
private String serverAddress = ""; private final String serverAddress;
private int port; private final int port;
private int nextStatus; private final int nextStatus;
public ServerboundHandshakePacket() {
}
public ServerboundHandshakePacket(final ProtocolVersion protocolVersion, final String hostname, final int port, final int nextStatus) { public ServerboundHandshakePacket(final ProtocolVersion protocolVersion, final String hostname, final int port, final int nextStatus) {
this.protocolVersion = protocolVersion; this.protocolVersion = protocolVersion;
@ -52,14 +54,6 @@ public class ServerboundHandshakePacket implements Packet {
this.nextStatus = nextStatus; this.nextStatus = nextStatus;
} }
@Override
public void encode(ByteBuf buf, ProtocolVersion ignored) {
ProtocolUtils.writeVarInt(buf, this.protocolVersion.protocol());
ProtocolUtils.writeString(buf, this.serverAddress);
buf.writeShort(this.port);
ProtocolUtils.writeVarInt(buf, this.nextStatus);
}
@Override @Override
public boolean handle(PacketHandler handler) { public boolean handle(PacketHandler handler) {
return handler.handle(this); return handler.handle(this);
@ -69,38 +63,18 @@ public class ServerboundHandshakePacket implements Packet {
return protocolVersion; return protocolVersion;
} }
@Deprecated
public void setProtocolVersion(ProtocolVersion protocolVersion) {
this.protocolVersion = protocolVersion;
}
public String getServerAddress() { public String getServerAddress() {
return serverAddress; return serverAddress;
} }
@Deprecated
public void setServerAddress(String serverAddress) {
this.serverAddress = serverAddress;
}
public int getPort() { public int getPort() {
return port; return port;
} }
@Deprecated
public void setPort(int port) {
this.port = port;
}
public int getNextStatus() { public int getNextStatus() {
return nextStatus; return nextStatus;
} }
@Deprecated
public void setNextStatus(int nextStatus) {
this.nextStatus = nextStatus;
}
@Override @Override
public String toString() { public String toString() {
return MoreObjects.toStringHelper(this) return MoreObjects.toStringHelper(this)

Datei anzeigen

@ -50,20 +50,25 @@ public class PingSessionHandler implements MinecraftSessionHandler {
@Override @Override
public void activated() { public void activated() {
ServerboundHandshakePacket handshake = new ServerboundHandshakePacket();
handshake.setNextStatus(StateRegistry.STATUS_ID);
SocketAddress address = server.serverInfo().address(); SocketAddress address = server.serverInfo().address();
String hostname;
int port;
if (address instanceof InetSocketAddress) { if (address instanceof InetSocketAddress) {
InetSocketAddress socketAddr = (InetSocketAddress) address; InetSocketAddress socketAddr = (InetSocketAddress) address;
handshake.setServerAddress(socketAddr.getHostString()); hostname = socketAddr.getHostString();
handshake.setPort(socketAddr.getPort()); port = socketAddr.getPort();
} else { } else {
// Just fake it // Just fake it
handshake.setServerAddress("127.0.0.1"); hostname = "127.0.0.1";
port = 25565;
} }
handshake.setProtocolVersion(version);
connection.delayedWrite(handshake); connection.delayedWrite(new ServerboundHandshakePacket(
version,
hostname,
port,
StateRegistry.STATUS_ID
));
connection.setState(StateRegistry.STATUS); connection.setState(StateRegistry.STATUS);
connection.delayedWrite(ServerboundStatusRequestPacket.INSTANCE); connection.delayedWrite(ServerboundStatusRequestPacket.INSTANCE);