3
0
Mirror von https://github.com/PaperMC/Velocity.git synchronisiert 2024-11-16 21:10:30 +01:00

feat: Add extra vHost data in ConnectionHandshakeEvent

Dieser Commit ist enthalten in:
Koneiii 2024-08-27 00:04:08 +02:00
Ursprung 6100e675af
Commit 3bb304b7e4
2 geänderte Dateien mit 50 neuen und 19 gelöschten Zeilen

Datei anzeigen

@ -20,10 +20,19 @@ public final class ConnectionHandshakeEvent {
private final InboundConnection connection; private final InboundConnection connection;
private final HandshakeIntent intent; private final HandshakeIntent intent;
private final String extraDataInHandshake;
public ConnectionHandshakeEvent(InboundConnection connection, HandshakeIntent intent) { /**
* Creates a new event.
*
* @param connection the inbound connection
* @param intent the intent of the handshake
* @param extraDataInHandshake the extra data in the handshake
*/
public ConnectionHandshakeEvent(InboundConnection connection, HandshakeIntent intent, String extraDataInHandshake) {
this.connection = Preconditions.checkNotNull(connection, "connection"); this.connection = Preconditions.checkNotNull(connection, "connection");
this.intent = Preconditions.checkNotNull(intent, "intent"); this.intent = Preconditions.checkNotNull(intent, "intent");
this.extraDataInHandshake = Preconditions.checkNotNull(extraDataInHandshake, "extraDataInHandshake");
} }
/** /**
@ -31,12 +40,13 @@ public final class ConnectionHandshakeEvent {
* that have not yet updated their integration tests. * that have not yet updated their integration tests.
* *
* @param connection the inbound connection * @param connection the inbound connection
* @deprecated use {@link #ConnectionHandshakeEvent(InboundConnection, HandshakeIntent)} * @deprecated use {@link #ConnectionHandshakeEvent(InboundConnection, HandshakeIntent, String)}
*/ */
@Deprecated(forRemoval = true) @Deprecated(forRemoval = true)
public ConnectionHandshakeEvent(InboundConnection connection) { public ConnectionHandshakeEvent(InboundConnection connection) {
this.connection = Preconditions.checkNotNull(connection, "connection"); this.connection = Preconditions.checkNotNull(connection, "connection");
this.intent = HandshakeIntent.LOGIN; this.intent = HandshakeIntent.LOGIN;
this.extraDataInHandshake = "";
} }
public InboundConnection getConnection() { public InboundConnection getConnection() {
@ -47,11 +57,15 @@ public final class ConnectionHandshakeEvent {
return this.intent; return this.intent;
} }
public String getExtraDataInHandshake() {
return this.extraDataInHandshake;
}
@Override @Override
public String toString() { public String toString() {
return "ConnectionHandshakeEvent{" return "ConnectionHandshakeEvent{"
+ "connection=" + connection + "connection=" + connection
+ ", intent=" + intent + ", intent=" + intent
+ '}'; + '}';
} }
} }

Datei anzeigen

@ -70,7 +70,7 @@ public class HandshakeSessionHandler implements MinecraftSessionHandler {
public boolean handle(LegacyPingPacket packet) { public boolean handle(LegacyPingPacket packet) {
connection.setProtocolVersion(ProtocolVersion.LEGACY); connection.setProtocolVersion(ProtocolVersion.LEGACY);
final StatusSessionHandler handler = final StatusSessionHandler handler =
new StatusSessionHandler(server, new LegacyInboundConnection(connection, packet)); new StatusSessionHandler(server, new LegacyInboundConnection(connection, packet));
connection.setActiveSessionHandler(StateRegistry.STATUS, handler); connection.setActiveSessionHandler(StateRegistry.STATUS, handler);
handler.handle(packet); handler.handle(packet);
return true; return true;
@ -79,8 +79,8 @@ public class HandshakeSessionHandler implements MinecraftSessionHandler {
@Override @Override
public boolean handle(LegacyHandshakePacket packet) { public boolean handle(LegacyHandshakePacket packet) {
connection.closeWith(LegacyDisconnect.from(Component.text( connection.closeWith(LegacyDisconnect.from(Component.text(
"Your client is extremely old. Please update to a newer version of Minecraft.", "Your client is extremely old. Please update to a newer version of Minecraft.",
NamedTextColor.RED) NamedTextColor.RED)
)); ));
return true; return true;
} }
@ -104,8 +104,8 @@ public class HandshakeSessionHandler implements MinecraftSessionHandler {
switch (nextState) { switch (nextState) {
case STATUS -> connection.setActiveSessionHandler(StateRegistry.STATUS, case STATUS -> connection.setActiveSessionHandler(StateRegistry.STATUS,
new StatusSessionHandler(server, ic)); new StatusSessionHandler(server, ic));
case LOGIN -> this.handleLogin(handshake, ic); case LOGIN -> this.handleLogin(handshake, ic, getExtraVhostData(handshake.getServerAddress()));
default -> default ->
// If you get this, it's a bug in Velocity. // If you get this, it's a bug in Velocity.
throw new AssertionError("getStateForProtocol provided invalid state!"); throw new AssertionError("getStateForProtocol provided invalid state!");
@ -123,7 +123,7 @@ public class HandshakeSessionHandler implements MinecraftSessionHandler {
}; };
} }
private void handleLogin(HandshakePacket handshake, InitialInboundConnection ic) { private void handleLogin(HandshakePacket handshake, InitialInboundConnection ic, String extraData) {
if (!handshake.getProtocolVersion().isSupported()) { if (!handshake.getProtocolVersion().isSupported()) {
// Bump connection into correct protocol state so that we can send the disconnect packet. // Bump connection into correct protocol state so that we can send the disconnect packet.
connection.setState(StateRegistry.LOGIN); connection.setState(StateRegistry.LOGIN);
@ -147,19 +147,19 @@ public class HandshakeSessionHandler implements MinecraftSessionHandler {
// If the proxy is configured for modern forwarding, we must deny connections from 1.12.2 // If the proxy is configured for modern forwarding, we must deny connections from 1.12.2
// and lower, otherwise IP information will never get forwarded. // and lower, otherwise IP information will never get forwarded.
if (server.getConfiguration().getPlayerInfoForwardingMode() == PlayerInfoForwarding.MODERN if (server.getConfiguration().getPlayerInfoForwardingMode() == PlayerInfoForwarding.MODERN
&& handshake.getProtocolVersion().lessThan(ProtocolVersion.MINECRAFT_1_13)) { && handshake.getProtocolVersion().lessThan(ProtocolVersion.MINECRAFT_1_13)) {
// Bump connection into correct protocol state so that we can send the disconnect packet. // Bump connection into correct protocol state so that we can send the disconnect packet.
connection.setState(StateRegistry.LOGIN); connection.setState(StateRegistry.LOGIN);
ic.disconnectQuietly( ic.disconnectQuietly(
Component.translatable("velocity.error.modern-forwarding-needs-new-client")); Component.translatable("velocity.error.modern-forwarding-needs-new-client"));
return; return;
} }
final LoginInboundConnection lic = new LoginInboundConnection(ic); final LoginInboundConnection lic = new LoginInboundConnection(ic);
server.getEventManager().fireAndForget( server.getEventManager().fireAndForget(
new ConnectionHandshakeEvent(lic, handshake.getIntent())); new ConnectionHandshakeEvent(lic, handshake.getIntent(), extraData));
connection.setActiveSessionHandler(StateRegistry.LOGIN, connection.setActiveSessionHandler(StateRegistry.LOGIN,
new InitialLoginSessionHandler(server, connection, lic)); new InitialLoginSessionHandler(server, connection, lic));
} }
private ConnectionType getHandshakeConnectionType(HandshakePacket handshake) { private ConnectionType getHandshakeConnectionType(HandshakePacket handshake) {
@ -169,7 +169,7 @@ public class HandshakeSessionHandler implements MinecraftSessionHandler {
} }
// Determine if we're using Forge (1.8 to 1.12, may not be the case in 1.13). // Determine if we're using Forge (1.8 to 1.12, may not be the case in 1.13).
if (handshake.getServerAddress().endsWith(LegacyForgeConstants.HANDSHAKE_HOSTNAME_TOKEN) if (handshake.getServerAddress().endsWith(LegacyForgeConstants.HANDSHAKE_HOSTNAME_TOKEN)
&& handshake.getProtocolVersion().lessThan(ProtocolVersion.MINECRAFT_1_13)) { && handshake.getProtocolVersion().lessThan(ProtocolVersion.MINECRAFT_1_13)) {
return ConnectionTypes.LEGACY_FORGE; return ConnectionTypes.LEGACY_FORGE;
} else if (handshake.getProtocolVersion().noGreaterThan(ProtocolVersion.MINECRAFT_1_7_6)) { } else if (handshake.getProtocolVersion().noGreaterThan(ProtocolVersion.MINECRAFT_1_7_6)) {
// 1.7 Forge will not notify us during handshake. UNDETERMINED will listen for incoming // 1.7 Forge will not notify us during handshake. UNDETERMINED will listen for incoming
@ -206,6 +206,23 @@ public class HandshakeSessionHandler implements MinecraftSessionHandler {
return cleaned; return cleaned;
} }
/**
* Extracts the extra data from the specified hostname.
*
* @param hostname the host name to extract the extra data from
* @return the extra data
*/
@VisibleForTesting
static String getExtraVhostData(String hostname) {
// Add extra data support
String data = "";
if (hostname.contains("\0")) {
String[] split = hostname.split("\0", 2);
data = "\0" + split[1];
}
return data;
}
@Override @Override
public void handleGeneric(MinecraftPacket packet) { public void handleGeneric(MinecraftPacket packet) {
// Unknown packet received. Better to close the connection. // Unknown packet received. Better to close the connection.
@ -256,10 +273,10 @@ public class HandshakeSessionHandler implements MinecraftSessionHandler {
@Override @Override
public String toString() { public String toString() {
final boolean isPlayerAddressLoggingEnabled = connection.server.getConfiguration() final boolean isPlayerAddressLoggingEnabled = connection.server.getConfiguration()
.isPlayerAddressLoggingEnabled(); .isPlayerAddressLoggingEnabled();
final String playerIp = final String playerIp =
isPlayerAddressLoggingEnabled isPlayerAddressLoggingEnabled
? this.getRemoteAddress().toString() : "<ip address withheld>"; ? this.getRemoteAddress().toString() : "<ip address withheld>";
return "[legacy connection] " + playerIp; return "[legacy connection] " + playerIp;
} }