Introduce some UUID utilities for eventual auth support.

This commit is contained in:
Andrew Steinborn 2018-07-25 21:18:04 -04:00
parent d89a4c4807
commit 8139ac35d9
6 changed files with 73 additions and 11 deletions

View File

@ -1,7 +1,5 @@
package com.velocitypowered.proxy;
import com.velocitypowered.proxy.connection.VelocityServer;
public class Velocity {
public static void main(String... args) throws InterruptedException {
VelocityServer server = new VelocityServer();

View File

@ -1,5 +1,6 @@
package com.velocitypowered.proxy.connection;
package com.velocitypowered.proxy;
import com.velocitypowered.proxy.connection.MinecraftConnection;
import com.velocitypowered.proxy.protocol.StateRegistry;
import com.velocitypowered.proxy.protocol.netty.MinecraftPipelineUtils;
import com.velocitypowered.proxy.connection.client.HandshakeSessionHandler;
@ -10,11 +11,16 @@ import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.nio.NioServerSocketChannel;
import io.netty.channel.socket.nio.NioSocketChannel;
import java.security.KeyPair;
import java.security.KeyPairGenerator;
import java.security.NoSuchAlgorithmException;
public class VelocityServer {
private static VelocityServer server;
private EventLoopGroup bossGroup;
private EventLoopGroup childGroup;
private KeyPair serverKeyPair;
public VelocityServer() {
@ -25,6 +31,16 @@ public class VelocityServer {
}
public void initialize() {
// Create a key pair
try {
KeyPairGenerator generator = KeyPairGenerator.getInstance("RSA");
generator.initialize(1024);
serverKeyPair = generator.generateKeyPair();
} catch (NoSuchAlgorithmException e) {
throw new RuntimeException("Unable to generate server encryption key", e);
}
// Start the listener
bossGroup = new NioEventLoopGroup();
childGroup = new NioEventLoopGroup();
server = this;

View File

@ -6,7 +6,7 @@ import com.velocitypowered.proxy.connection.MinecraftConnection;
import com.velocitypowered.proxy.protocol.StateRegistry;
import com.velocitypowered.proxy.data.ServerInfo;
import com.velocitypowered.proxy.protocol.netty.MinecraftPipelineUtils;
import com.velocitypowered.proxy.connection.VelocityServer;
import com.velocitypowered.proxy.VelocityServer;
import com.velocitypowered.proxy.connection.client.ConnectedPlayer;
import io.netty.channel.*;

View File

@ -7,13 +7,12 @@ import com.velocitypowered.proxy.protocol.packets.ServerLogin;
import com.velocitypowered.proxy.protocol.packets.ServerLoginSuccess;
import com.velocitypowered.proxy.connection.MinecraftConnection;
import com.velocitypowered.proxy.connection.MinecraftSessionHandler;
import com.velocitypowered.proxy.connection.VelocityServer;
import com.velocitypowered.proxy.VelocityServer;
import com.velocitypowered.proxy.connection.backend.ServerConnection;
import com.velocitypowered.proxy.data.ServerInfo;
import com.velocitypowered.proxy.util.UuidUtils;
import java.net.InetSocketAddress;
import java.nio.charset.StandardCharsets;
import java.util.UUID;
public class LoginSessionHandler implements MinecraftSessionHandler {
private final MinecraftConnection inbound;
@ -32,7 +31,7 @@ public class LoginSessionHandler implements MinecraftSessionHandler {
String username = ((ServerLogin) packet).getUsername();
ServerLoginSuccess success = new ServerLoginSuccess();
success.setUsername(username);
success.setUuid(generateOfflinePlayerUuid(username));
success.setUuid(UuidUtils.generateOfflinePlayerUuid(username));
inbound.write(success);
// Initiate a regular connection and move over to it.
@ -45,7 +44,4 @@ public class LoginSessionHandler implements MinecraftSessionHandler {
connection.connect();
}
private static UUID generateOfflinePlayerUuid(String username) {
return UUID.nameUUIDFromBytes(("OfflinePlayer:" + username).getBytes(StandardCharsets.UTF_8));
}
}

View File

@ -0,0 +1,26 @@
package com.velocitypowered.proxy.util;
import com.google.common.base.Preconditions;
import java.nio.charset.StandardCharsets;
import java.util.UUID;
public enum UuidUtils {
;
public static UUID fromMojang(String id) {
Preconditions.checkNotNull(id, "id");
Preconditions.checkArgument(id.length() == 32, "Length is incorrect");
return UUID.fromString(
id.substring(0, 8) + "-" +
id.substring(8, 12) + "-" +
id.substring(12, 16) + "-" +
id.substring(16, 20) + "-" +
id.substring(20, 32)
);
}
public static UUID generateOfflinePlayerUuid(String username) {
return UUID.nameUUIDFromBytes(("OfflinePlayer:" + username).getBytes(StandardCharsets.UTF_8));
}
}

View File

@ -0,0 +1,26 @@
package com.velocitypowered.proxy.util;
import org.junit.Assert;
import org.junit.Test;
import java.util.UUID;
import static org.junit.Assert.*;
public class UuidUtilsTest {
private static final UUID VALID_UUID = UUID.fromString("6b501978-d3be-4f33-bcf6-6e7808f37a0d");
private static final String VALID_MOJANG_UUID = "6b501978d3be4f33bcf66e7808f37a0d";
private static final UUID TEST_OFFLINE_PLAYER_UUID = UUID.fromString("708f6260-183d-3912-bbde-5e279a5e739a");
private static final String TEST_OFFLINE_PLAYER = "tuxed";
@Test
public void fromMojang() {
Assert.assertEquals("UUIDs do not match", VALID_UUID, UuidUtils.fromMojang(VALID_MOJANG_UUID));
}
@Test
public void generateOfflinePlayerUuid() {
Assert.assertEquals("UUIDs do not match", TEST_OFFLINE_PLAYER_UUID, UuidUtils.generateOfflinePlayerUuid(TEST_OFFLINE_PLAYER));
}
}