forked from GeyserMC/Geyser
Merge Floodgate changes
This commit is contained in:
commit
2fc591e341
16 changed files with 351 additions and 24 deletions
26
common/src/main/java/org/geysermc/common/AuthType.java
Normal file
26
common/src/main/java/org/geysermc/common/AuthType.java
Normal file
|
@ -0,0 +1,26 @@
|
|||
package org.geysermc.common;
|
||||
|
||||
import lombok.Getter;
|
||||
|
||||
@Getter
|
||||
public enum AuthType {
|
||||
OFFLINE,
|
||||
ONLINE,
|
||||
FLOODGATE;
|
||||
|
||||
public static final AuthType[] VALUES = values();
|
||||
|
||||
public static AuthType getById(int id) {
|
||||
return id < VALUES.length ? VALUES[id] : OFFLINE;
|
||||
}
|
||||
|
||||
public static AuthType getByName(String name) {
|
||||
String upperCase = name.toUpperCase();
|
||||
for (AuthType type : VALUES) {
|
||||
if (type.name().equals(upperCase)) {
|
||||
return type;
|
||||
}
|
||||
}
|
||||
return OFFLINE;
|
||||
}
|
||||
}
|
|
@ -45,6 +45,8 @@ public interface IGeyserConfiguration {
|
|||
|
||||
boolean isAllowThirdPartyCapes();
|
||||
|
||||
String getFloodgateKeyFile();
|
||||
|
||||
IMetricsInfo getMetrics();
|
||||
|
||||
interface IBedrockConfiguration {
|
||||
|
|
|
@ -0,0 +1,44 @@
|
|||
package org.geysermc.floodgate.util;
|
||||
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Getter;
|
||||
|
||||
@AllArgsConstructor
|
||||
@Getter
|
||||
public class BedrockData {
|
||||
public static final int EXPECTED_LENGTH = 7;
|
||||
public static final String FLOODGATE_IDENTIFIER = "Geyser-Floodgate";
|
||||
|
||||
private String version;
|
||||
private String username;
|
||||
private String xuid;
|
||||
private int deviceId;
|
||||
private String languageCode;
|
||||
private int inputMode;
|
||||
private String ip;
|
||||
private int dataLength;
|
||||
|
||||
public BedrockData(String version, String username, String xuid, int deviceId, String languageCode, int inputMode, String ip) {
|
||||
this(version, username, xuid, deviceId, languageCode, inputMode, ip, EXPECTED_LENGTH);
|
||||
}
|
||||
|
||||
public static BedrockData fromString(String data) {
|
||||
String[] split = data.split("\0");
|
||||
if (split.length != EXPECTED_LENGTH) return null;
|
||||
|
||||
return new BedrockData(
|
||||
split[0], split[1], split[2], Integer.parseInt(split[3]),
|
||||
split[4], Integer.parseInt(split[5]), split[6], split.length
|
||||
);
|
||||
}
|
||||
|
||||
public static BedrockData fromRawData(byte[] data) {
|
||||
return fromString(new String(data));
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return version +'\0'+ username +'\0'+ xuid +'\0'+ deviceId +'\0'+ languageCode +'\0'+
|
||||
inputMode +'\0'+ ip;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,76 @@
|
|||
package org.geysermc.floodgate.util;
|
||||
|
||||
import javax.crypto.*;
|
||||
import javax.crypto.spec.SecretKeySpec;
|
||||
import java.io.IOException;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.security.*;
|
||||
import java.security.spec.EncodedKeySpec;
|
||||
import java.security.spec.InvalidKeySpecException;
|
||||
import java.security.spec.PKCS8EncodedKeySpec;
|
||||
import java.security.spec.X509EncodedKeySpec;
|
||||
import java.util.Base64;
|
||||
|
||||
public class EncryptionUtil {
|
||||
public static String encrypt(Key key, String data) throws IllegalBlockSizeException,
|
||||
InvalidKeyException, BadPaddingException, NoSuchAlgorithmException, NoSuchPaddingException {
|
||||
KeyGenerator generator = KeyGenerator.getInstance("AES");
|
||||
generator.init(128);
|
||||
SecretKey secretKey = generator.generateKey();
|
||||
|
||||
Cipher cipher = Cipher.getInstance("AES");
|
||||
cipher.init(Cipher.ENCRYPT_MODE, secretKey);
|
||||
byte[] encryptedText = cipher.doFinal(data.getBytes());
|
||||
|
||||
cipher = Cipher.getInstance("RSA/ECB/PKCS1Padding");
|
||||
cipher.init(key instanceof PublicKey ? Cipher.PUBLIC_KEY : Cipher.PRIVATE_KEY, key);
|
||||
return Base64.getEncoder().encodeToString(cipher.doFinal(secretKey.getEncoded())) + '\0' +
|
||||
Base64.getEncoder().encodeToString(encryptedText);
|
||||
}
|
||||
|
||||
public static String encryptBedrockData(Key key, BedrockData data) throws IllegalBlockSizeException,
|
||||
InvalidKeyException, BadPaddingException, NoSuchAlgorithmException, NoSuchPaddingException {
|
||||
return encrypt(key, data.toString());
|
||||
}
|
||||
|
||||
public static byte[] decrypt(Key key, String encryptedData) throws IllegalBlockSizeException,
|
||||
InvalidKeyException, BadPaddingException, NoSuchAlgorithmException, NoSuchPaddingException {
|
||||
String[] split = encryptedData.split("\0");
|
||||
if (split.length != 2) {
|
||||
throw new IllegalArgumentException("Expected two arguments, got " + split.length);
|
||||
}
|
||||
|
||||
Cipher cipher = Cipher.getInstance("RSA/ECB/PKCS1Padding");
|
||||
cipher.init(key instanceof PublicKey ? Cipher.PUBLIC_KEY : Cipher.PRIVATE_KEY, key);
|
||||
byte[] decryptedKey = cipher.doFinal(Base64.getDecoder().decode(split[0]));
|
||||
|
||||
SecretKey secretKey = new SecretKeySpec(decryptedKey, 0, decryptedKey.length, "AES");
|
||||
cipher = Cipher.getInstance("AES");
|
||||
cipher.init(Cipher.DECRYPT_MODE, secretKey);
|
||||
return cipher.doFinal(Base64.getDecoder().decode(split[1]));
|
||||
}
|
||||
|
||||
public static BedrockData decryptBedrockData(Key key, String encryptedData) throws IllegalBlockSizeException,
|
||||
InvalidKeyException, BadPaddingException, NoSuchAlgorithmException, NoSuchPaddingException {
|
||||
return BedrockData.fromRawData(decrypt(key, encryptedData));
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
public static <T extends Key> T getKeyFromFile(Path fileLocation, Class<T> keyType) throws
|
||||
IOException, InvalidKeySpecException, NoSuchAlgorithmException {
|
||||
boolean isPublicKey = keyType == PublicKey.class;
|
||||
if (!isPublicKey && keyType != PrivateKey.class) {
|
||||
throw new RuntimeException("I can only read public and private keys!");
|
||||
}
|
||||
|
||||
byte[] key = Files.readAllBytes(fileLocation);
|
||||
|
||||
KeyFactory keyFactory = KeyFactory.getInstance("RSA");
|
||||
EncodedKeySpec keySpec = isPublicKey ? new X509EncodedKeySpec(key) : new PKCS8EncodedKeySpec(key);
|
||||
return (T) (isPublicKey ?
|
||||
keyFactory.generatePublic(keySpec) :
|
||||
keyFactory.generatePrivate(keySpec)
|
||||
);
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue