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

Refactor cipher logic.

Dieser Commit ist enthalten in:
Andrew Steinborn 2018-08-04 00:09:25 -04:00
Ursprung 6f3397f76f
Commit 9438d087e2
6 geänderte Dateien mit 37 neuen und 3 gelöschten Zeilen

Datei anzeigen

@ -10,10 +10,22 @@ import javax.crypto.spec.IvParameterSpec;
import java.security.GeneralSecurityException;
public class JavaVelocityCipher implements VelocityCipher {
public static final VelocityCipherFactory FACTORY = new VelocityCipherFactory() {
@Override
public VelocityCipher forEncryption(SecretKey key) throws GeneralSecurityException {
return new JavaVelocityCipher(true, key);
}
@Override
public VelocityCipher forDecryption(SecretKey key) throws GeneralSecurityException {
return new JavaVelocityCipher(false, key);
}
};
private final Cipher cipher;
private boolean disposed = false;
public JavaVelocityCipher(boolean encrypt, SecretKey key) throws GeneralSecurityException {
private JavaVelocityCipher(boolean encrypt, SecretKey key) throws GeneralSecurityException {
this.cipher = Cipher.getInstance("AES/CFB8/NoPadding");
this.cipher.init(encrypt ? Cipher.ENCRYPT_MODE : Cipher.DECRYPT_MODE, key, new IvParameterSpec(key.getEncoded()));
}

Datei anzeigen

@ -0,0 +1,10 @@
package com.velocitypowered.natives.encryption;
import javax.crypto.SecretKey;
import java.security.GeneralSecurityException;
public interface VelocityCipherFactory {
VelocityCipher forEncryption(SecretKey key) throws GeneralSecurityException;
VelocityCipher forDecryption(SecretKey key) throws GeneralSecurityException;
}

Datei anzeigen

@ -5,6 +5,8 @@ import com.velocitypowered.natives.compression.JavaVelocityCompressor;
import com.velocitypowered.natives.compression.NativeVelocityCompressor;
import com.velocitypowered.natives.compression.VelocityCompressor;
import com.velocitypowered.natives.compression.VelocityCompressorFactory;
import com.velocitypowered.natives.encryption.JavaVelocityCipher;
import com.velocitypowered.natives.encryption.VelocityCipherFactory;
import java.io.IOException;
import java.nio.file.Files;
@ -46,4 +48,10 @@ public class Natives {
new NativeCodeLoader.Variant<>(NativeCodeLoader.ALWAYS, () -> {}, "Java compression", JavaVelocityCompressor.FACTORY)
)
);
public static final NativeCodeLoader<VelocityCipherFactory> cipher = new NativeCodeLoader<>(
ImmutableList.of(
new NativeCodeLoader.Variant<>(NativeCodeLoader.ALWAYS, () -> {}, "Java cipher", JavaVelocityCipher.FACTORY)
)
);
}

Datei anzeigen

@ -54,6 +54,7 @@ public class VelocityServer {
public void start() {
logger.info("Using {}", Natives.compressor.getLoadedVariant());
logger.info("Using {}", Natives.cipher.getLoadedVariant());
// Create a key pair
logger.info("Booting up Velocity...");

Datei anzeigen

@ -2,6 +2,7 @@ package com.velocitypowered.proxy.connection;
import com.google.common.base.Preconditions;
import com.velocitypowered.natives.compression.VelocityCompressor;
import com.velocitypowered.natives.encryption.VelocityCipherFactory;
import com.velocitypowered.natives.util.Natives;
import com.velocitypowered.proxy.VelocityServer;
import com.velocitypowered.proxy.protocol.PacketWrapper;
@ -205,8 +206,9 @@ public class MinecraftConnection extends ChannelInboundHandlerAdapter {
public void enableEncryption(byte[] secret) throws GeneralSecurityException {
SecretKey key = new SecretKeySpec(secret, "AES");
VelocityCipher decryptionCipher = new JavaVelocityCipher(false, key);
VelocityCipher encryptionCipher = new JavaVelocityCipher(true, key);
VelocityCipherFactory factory = Natives.cipher.get();
VelocityCipher decryptionCipher = factory.forDecryption(key);
VelocityCipher encryptionCipher = factory.forEncryption(key);
channel.pipeline().addBefore(FRAME_DECODER, CIPHER_DECODER, new MinecraftCipherDecoder(decryptionCipher));
channel.pipeline().addBefore(FRAME_ENCODER, CIPHER_ENCODER, new MinecraftCipherEncoder(encryptionCipher));
}

Datei anzeigen

@ -101,6 +101,7 @@ public class ConnectedPlayer implements MinecraftConnectionAssociation {
}
public void handleConnectionException(ServerInfo info, Component disconnectReason) {
connectionInFlight = null;
if (connectedServer == null || connectedServer.getServerInfo().equals(info)) {
// The player isn't yet connected to a server or they are already connected to the server
// they're disconnected from.