3
0
Mirror von https://github.com/PaperMC/Velocity.git synchronisiert 2024-11-06 00:00:47 +01:00

Clean up HandshakeSessionHandler

Dieser Commit ist enthalten in:
Andrew Steinborn 2019-09-27 22:37:42 -04:00
Ursprung 546307b0d6
Commit 62cd2c661a
3 geänderte Dateien mit 64 neuen und 62 gelöschten Zeilen

Datei anzeigen

@ -34,14 +34,4 @@ public interface ConnectionType {
*/ */
GameProfile addGameProfileTokensIfRequired(GameProfile original, GameProfile addGameProfileTokensIfRequired(GameProfile original,
PlayerInfoForwarding forwardingType); PlayerInfoForwarding forwardingType);
/**
* Tests whether the hostname is the handshake packet is valid.
*
* @param address The address to check
* @return true if valid.
*/
default boolean checkServerAddressIsValid(String address) {
return true;
}
} }

Datei anzeigen

@ -28,6 +28,7 @@ import net.kyori.text.TranslatableComponent;
import net.kyori.text.format.TextColor; import net.kyori.text.format.TextColor;
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.checkerframework.checker.nullness.qual.Nullable;
public class HandshakeSessionHandler implements MinecraftSessionHandler { public class HandshakeSessionHandler implements MinecraftSessionHandler {
@ -62,39 +63,57 @@ public class HandshakeSessionHandler implements MinecraftSessionHandler {
public boolean handle(Handshake handshake) { public boolean handle(Handshake handshake) {
InitialInboundConnection ic = new InitialInboundConnection(connection, InitialInboundConnection ic = new InitialInboundConnection(connection,
cleanVhost(handshake.getServerAddress()), handshake); cleanVhost(handshake.getServerAddress()), handshake);
switch (handshake.getNextStatus()) { StateRegistry nextState = getStateForProtocol(handshake.getNextStatus());
case StateRegistry.STATUS_ID: if (nextState == null) {
connection.setState(StateRegistry.STATUS); LOGGER.error("{} provided invalid protocol {}", ic, handshake.getNextStatus());
connection.close();
} else {
connection.setState(nextState);
connection.setProtocolVersion(handshake.getProtocolVersion()); connection.setProtocolVersion(handshake.getProtocolVersion());
connection.setAssociation(ic); connection.setAssociation(ic);
connection.setSessionHandler(new StatusSessionHandler(server, connection, ic));
return true;
case StateRegistry.LOGIN_ID:
connection.setState(StateRegistry.LOGIN);
connection.setProtocolVersion(handshake.getProtocolVersion());
switch (nextState) {
case STATUS:
connection.setSessionHandler(new StatusSessionHandler(server, connection, ic));
break;
case LOGIN:
this.handleLogin(handshake, ic);
break;
default:
// If you get this, it's a bug in Velocity.
throw new AssertionError("getStateForProtocol provided invalid state!");
}
}
return true;
}
private static @Nullable StateRegistry getStateForProtocol(int status) {
switch (status) {
case StateRegistry.STATUS_ID:
return StateRegistry.STATUS;
case StateRegistry.LOGIN_ID:
return StateRegistry.LOGIN;
default:
return null;
}
}
private void handleLogin(Handshake handshake, InitialInboundConnection ic) {
if (!ProtocolVersion.isSupported(handshake.getProtocolVersion())) { if (!ProtocolVersion.isSupported(handshake.getProtocolVersion())) {
connection.closeWith(Disconnect connection.closeWith(Disconnect
.create(TranslatableComponent.of("multiplayer.disconnect.outdated_client"))); .create(TranslatableComponent.of("multiplayer.disconnect.outdated_client")));
return true; return;
} }
InetAddress address = ((InetSocketAddress) connection.getRemoteAddress()).getAddress(); InetAddress address = ((InetSocketAddress) connection.getRemoteAddress()).getAddress();
if (!server.getIpAttemptLimiter().attempt(address)) { if (!server.getIpAttemptLimiter().attempt(address)) {
connection.closeWith( connection.closeWith(
Disconnect.create(TextComponent.of("You are logging in too fast, try again later."))); Disconnect.create(TextComponent.of("You are logging in too fast, try again later.")));
return true; return;
} }
ConnectionType type = checkForForge(handshake); connection.setType(getHandshakeConnectionType(handshake));
connection.setType(type);
// Make sure legacy forwarding is not in use on this connection.
if (!type.checkServerAddressIsValid(handshake.getServerAddress())) {
connection.closeWith(Disconnect
.create(TextComponent.of("Running Velocity behind Velocity is unsupported.")));
return true;
}
// 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.
@ -102,21 +121,14 @@ public class HandshakeSessionHandler implements MinecraftSessionHandler {
&& handshake.getProtocolVersion().compareTo(ProtocolVersion.MINECRAFT_1_13) < 0) { && handshake.getProtocolVersion().compareTo(ProtocolVersion.MINECRAFT_1_13) < 0) {
connection.closeWith(Disconnect connection.closeWith(Disconnect
.create(TextComponent.of("This server is only compatible with 1.13 and above."))); .create(TextComponent.of("This server is only compatible with 1.13 and above.")));
return true; return;
} }
connection.setAssociation(ic);
server.getEventManager().fireAndForget(new ConnectionHandshakeEvent(ic)); server.getEventManager().fireAndForget(new ConnectionHandshakeEvent(ic));
connection.setSessionHandler(new LoginSessionHandler(server, connection, ic)); connection.setSessionHandler(new LoginSessionHandler(server, connection, ic));
return true;
default:
LOGGER.error("{} provided invalid protocol {}", ic, handshake.getNextStatus());
connection.close();
return true;
}
} }
private ConnectionType checkForForge(Handshake handshake) { private ConnectionType getHandshakeConnectionType(Handshake handshake) {
// 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().compareTo(ProtocolVersion.MINECRAFT_1_13) < 0) { && handshake.getProtocolVersion().compareTo(ProtocolVersion.MINECRAFT_1_13) < 0) {
@ -126,8 +138,8 @@ public class HandshakeSessionHandler implements MinecraftSessionHandler {
// forge handshake attempts. Also sends a reset handshake packet on every transition. // forge handshake attempts. Also sends a reset handshake packet on every transition.
return ConnectionTypes.UNDETERMINED_17; return ConnectionTypes.UNDETERMINED_17;
} else { } else {
// For later: See if we can determine Forge 1.13+ here, else this will need to be UNDETERMINED // Note for future implementation: Forge 1.13+ identifies itself using a slightly different
// until later in the cycle (most likely determinable during the LOGIN phase) // hostname token.
return ConnectionTypes.VANILLA; return ConnectionTypes.VANILLA;
} }
} }

Datei anzeigen

@ -6,7 +6,7 @@ package com.velocitypowered.proxy.connection.forge.legacy;
public class LegacyForgeConstants { public class LegacyForgeConstants {
/** /**
* Clients attempting to connect to 1.8+ Forge servers will have * Clients attempting to connect to 1.8-1.12.2 Forge servers will have
* this token appended to the hostname in the initial handshake * this token appended to the hostname in the initial handshake
* packet. * packet.
*/ */