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