Mirror von
https://github.com/PaperMC/Velocity.git
synchronisiert 2024-12-24 15:20:35 +01:00
Cleanup, renames, check protocol version of client on handshake.
Dieser Commit ist enthalten in:
Ursprung
3722f5d664
Commit
31cc5288f5
@ -42,8 +42,7 @@ public class ConnectedPlayer {
|
||||
|
||||
public void handleConnectionException(ServerInfo info, Throwable throwable) {
|
||||
String error = ThrowableUtils.briefDescription(throwable);
|
||||
Disconnect disconnect = new Disconnect();
|
||||
disconnect.setReason(ComponentSerializers.JSON.serialize(TextComponent.of(error, TextColor.RED)));
|
||||
Disconnect disconnect = Disconnect.create(TextComponent.of(error, TextColor.RED));
|
||||
handleConnectionException(info, disconnect);
|
||||
}
|
||||
|
||||
@ -56,9 +55,7 @@ public class ConnectedPlayer {
|
||||
|
||||
if (connectedServer == null) {
|
||||
// The player isn't yet connected to a server - we should disconnect them.
|
||||
Disconnect d = new Disconnect();
|
||||
d.setReason(ComponentSerializers.JSON.serialize(component));
|
||||
connection.closeWith(d);
|
||||
connection.closeWith(Disconnect.create(component));
|
||||
} else {
|
||||
Chat chat = new Chat();
|
||||
chat.setMessage(ComponentSerializers.JSON.serialize(component));
|
||||
@ -71,8 +68,6 @@ public class ConnectedPlayer {
|
||||
}
|
||||
|
||||
public void close(TextComponent reason) {
|
||||
Disconnect disconnect = new Disconnect();
|
||||
disconnect.setReason(ComponentSerializers.JSON.serialize(reason));
|
||||
connection.closeWith(disconnect);
|
||||
connection.closeWith(Disconnect.create(reason));
|
||||
}
|
||||
}
|
||||
|
@ -2,10 +2,13 @@ package com.velocitypowered.proxy.connection.client;
|
||||
|
||||
import com.google.common.base.Preconditions;
|
||||
import com.velocitypowered.proxy.protocol.MinecraftPacket;
|
||||
import com.velocitypowered.proxy.protocol.ProtocolConstants;
|
||||
import com.velocitypowered.proxy.protocol.StateRegistry;
|
||||
import com.velocitypowered.proxy.protocol.packets.Disconnect;
|
||||
import com.velocitypowered.proxy.protocol.packets.Handshake;
|
||||
import com.velocitypowered.proxy.connection.MinecraftConnection;
|
||||
import com.velocitypowered.proxy.connection.MinecraftSessionHandler;
|
||||
import net.kyori.text.TextComponent;
|
||||
|
||||
public class HandshakeSessionHandler implements MinecraftSessionHandler {
|
||||
private final MinecraftConnection connection;
|
||||
@ -30,7 +33,12 @@ public class HandshakeSessionHandler implements MinecraftSessionHandler {
|
||||
break;
|
||||
case 2:
|
||||
connection.setState(StateRegistry.LOGIN);
|
||||
connection.setSessionHandler(new LoginSessionHandler(connection));
|
||||
if (!ProtocolConstants.isSupported(handshake.getProtocolVersion())) {
|
||||
connection.closeWith(Disconnect.create(TextComponent.of("Unsupported client")));
|
||||
return;
|
||||
} else {
|
||||
connection.setSessionHandler(new LoginSessionHandler(connection));
|
||||
}
|
||||
break;
|
||||
default:
|
||||
throw new IllegalArgumentException("Invalid state " + handshake.getNextStatus());
|
||||
|
@ -1,8 +1,18 @@
|
||||
package com.velocitypowered.proxy.protocol;
|
||||
|
||||
import java.util.Arrays;
|
||||
|
||||
public enum ProtocolConstants { ;
|
||||
public static final int MINECRAFT_1_12 = 340;
|
||||
|
||||
private static final int[] SUPPORTED_VERSIONS = new int[] {
|
||||
MINECRAFT_1_12
|
||||
};
|
||||
|
||||
public static boolean isSupported(int version) {
|
||||
return Arrays.binarySearch(SUPPORTED_VERSIONS, version) >= 0;
|
||||
}
|
||||
|
||||
public enum Direction {
|
||||
TO_SERVER,
|
||||
TO_CLIENT
|
||||
|
@ -1,13 +1,23 @@
|
||||
package com.velocitypowered.proxy.protocol.packets;
|
||||
|
||||
import com.google.common.base.Preconditions;
|
||||
import com.velocitypowered.proxy.protocol.ProtocolConstants;
|
||||
import com.velocitypowered.proxy.protocol.MinecraftPacket;
|
||||
import com.velocitypowered.proxy.protocol.ProtocolUtils;
|
||||
import io.netty.buffer.ByteBuf;
|
||||
import net.kyori.text.TextComponent;
|
||||
import net.kyori.text.serializer.ComponentSerializers;
|
||||
|
||||
public class Disconnect implements MinecraftPacket {
|
||||
private String reason;
|
||||
|
||||
public Disconnect() {
|
||||
}
|
||||
|
||||
public Disconnect(String reason) {
|
||||
this.reason = reason;
|
||||
}
|
||||
|
||||
public String getReason() {
|
||||
return reason;
|
||||
}
|
||||
@ -32,4 +42,9 @@ public class Disconnect implements MinecraftPacket {
|
||||
public void encode(ByteBuf buf, ProtocolConstants.Direction direction, int protocolVersion) {
|
||||
ProtocolUtils.writeString(buf, reason);
|
||||
}
|
||||
|
||||
public static Disconnect create(TextComponent component) {
|
||||
Preconditions.checkNotNull(component, "component");
|
||||
return new Disconnect(ComponentSerializers.JSON.serialize(component));
|
||||
}
|
||||
}
|
||||
|
@ -3,7 +3,7 @@ package com.velocitypowered.proxy.util;
|
||||
import java.math.BigInteger;
|
||||
|
||||
public enum EncryptionUtils { ;
|
||||
public static String twoComplementsSha1Digest(byte[] digest) {
|
||||
public static String twosComplementSha1Digest(byte[] digest) {
|
||||
return new BigInteger(digest).toString(16);
|
||||
}
|
||||
}
|
||||
|
@ -9,7 +9,7 @@ import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
|
||||
class EncryptionUtilsTest {
|
||||
@Test
|
||||
void twoComplementsSha1Digest() throws Exception {
|
||||
void twosComplementSha1Digest() throws Exception {
|
||||
String notchHash = hexDigest("Notch");
|
||||
assertEquals("4ed1f46bbe04bc756bcb17c0c7ce3e4632f06a48", notchHash);
|
||||
|
||||
@ -21,6 +21,6 @@ class EncryptionUtilsTest {
|
||||
MessageDigest digest = MessageDigest.getInstance("SHA-1");
|
||||
digest.update(str.getBytes(StandardCharsets.UTF_8));
|
||||
byte[] digested = digest.digest();
|
||||
return EncryptionUtils.twoComplementsSha1Digest(digested);
|
||||
return EncryptionUtils.twosComplementSha1Digest(digested);
|
||||
}
|
||||
}
|
||||
|
Laden…
In neuem Issue referenzieren
Einen Benutzer sperren