mirror of
https://github.com/Minestom/Minestom.git
synced 2025-03-10 13:49:04 +01:00
Improve velocity forwarding
This commit is contained in:
parent
e9e688b50a
commit
f489f95bb7
@ -11,6 +11,7 @@ import java.net.InetAddress;
|
||||
import java.net.UnknownHostException;
|
||||
import java.nio.ByteBuffer;
|
||||
import java.security.InvalidKeyException;
|
||||
import java.security.Key;
|
||||
import java.security.MessageDigest;
|
||||
import java.security.NoSuchAlgorithmException;
|
||||
|
||||
@ -20,12 +21,12 @@ import java.security.NoSuchAlgorithmException;
|
||||
* Can be enabled by simply calling {@link #enable(String)}.
|
||||
*/
|
||||
public final class VelocityProxy {
|
||||
|
||||
public static final String PLAYER_INFO_CHANNEL = "velocity:player_info";
|
||||
private static final int SUPPORTED_FORWARDING_VERSION = 1;
|
||||
private static final String MAC_ALGORITHM = "HmacSHA256";
|
||||
|
||||
private static volatile boolean enabled;
|
||||
private static byte[] secret;
|
||||
private static Key key;
|
||||
|
||||
/**
|
||||
* Enables velocity modern forwarding.
|
||||
@ -35,7 +36,7 @@ public final class VelocityProxy {
|
||||
*/
|
||||
public static void enable(@NotNull String secret) {
|
||||
VelocityProxy.enabled = true;
|
||||
VelocityProxy.secret = secret.getBytes();
|
||||
VelocityProxy.key = new SecretKeySpec(secret.getBytes(), MAC_ALGORITHM);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -48,29 +49,21 @@ public final class VelocityProxy {
|
||||
}
|
||||
|
||||
public static boolean checkIntegrity(@NotNull BinaryReader reader) {
|
||||
if (!enabled) {
|
||||
return false;
|
||||
}
|
||||
|
||||
final byte[] signature = reader.readBytes(32);
|
||||
|
||||
ByteBuffer buf = reader.getBuffer();
|
||||
buf.mark();
|
||||
final byte[] data = new byte[buf.remaining()];
|
||||
buf.get(data);
|
||||
final byte[] data = reader.readRemainingBytes();
|
||||
buf.reset();
|
||||
|
||||
try {
|
||||
final Mac mac = Mac.getInstance("HmacSHA256");
|
||||
mac.init(new SecretKeySpec(secret, "HmacSHA256"));
|
||||
Mac mac = Mac.getInstance(MAC_ALGORITHM);
|
||||
mac.init(key);
|
||||
final byte[] mySignature = mac.doFinal(data);
|
||||
if (!MessageDigest.isEqual(signature, mySignature)) {
|
||||
return false;
|
||||
}
|
||||
} catch (final InvalidKeyException | NoSuchAlgorithmException e) {
|
||||
throw new AssertionError(e);
|
||||
} catch (NoSuchAlgorithmException | InvalidKeyException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
final int version = reader.readVarInt();
|
||||
return version == SUPPORTED_FORWARDING_VERSION;
|
||||
}
|
||||
@ -87,9 +80,8 @@ public final class VelocityProxy {
|
||||
public static PlayerSkin readSkin(@NotNull BinaryReader reader) {
|
||||
String skinTexture = null;
|
||||
String skinSignature = null;
|
||||
|
||||
final int properties = reader.readVarInt();
|
||||
for (int i1 = 0; i1 < properties; i1++) {
|
||||
for (int i = 0; i < properties; i++) {
|
||||
final String name = reader.readSizedString(Short.MAX_VALUE);
|
||||
final String value = reader.readSizedString(Short.MAX_VALUE);
|
||||
final String signature = reader.readBoolean() ? reader.readSizedString(Short.MAX_VALUE) : null;
|
||||
@ -106,5 +98,4 @@ public final class VelocityProxy {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user