diff --git a/src/main/java/com/velocitypowered/proxy/Velocity.java b/src/main/java/com/velocitypowered/proxy/Velocity.java index 6e3330b31..8f7396d26 100644 --- a/src/main/java/com/velocitypowered/proxy/Velocity.java +++ b/src/main/java/com/velocitypowered/proxy/Velocity.java @@ -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(); diff --git a/src/main/java/com/velocitypowered/proxy/connection/VelocityServer.java b/src/main/java/com/velocitypowered/proxy/VelocityServer.java similarity index 79% rename from src/main/java/com/velocitypowered/proxy/connection/VelocityServer.java rename to src/main/java/com/velocitypowered/proxy/VelocityServer.java index c7d2df6aa..524bf4e5b 100644 --- a/src/main/java/com/velocitypowered/proxy/connection/VelocityServer.java +++ b/src/main/java/com/velocitypowered/proxy/VelocityServer.java @@ -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; diff --git a/src/main/java/com/velocitypowered/proxy/connection/backend/ServerConnection.java b/src/main/java/com/velocitypowered/proxy/connection/backend/ServerConnection.java index f0aa79050..b5f69b1d6 100644 --- a/src/main/java/com/velocitypowered/proxy/connection/backend/ServerConnection.java +++ b/src/main/java/com/velocitypowered/proxy/connection/backend/ServerConnection.java @@ -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.*; diff --git a/src/main/java/com/velocitypowered/proxy/connection/client/LoginSessionHandler.java b/src/main/java/com/velocitypowered/proxy/connection/client/LoginSessionHandler.java index 76b4fa0af..8fe8bd57b 100644 --- a/src/main/java/com/velocitypowered/proxy/connection/client/LoginSessionHandler.java +++ b/src/main/java/com/velocitypowered/proxy/connection/client/LoginSessionHandler.java @@ -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)); - } } diff --git a/src/main/java/com/velocitypowered/proxy/util/UuidUtils.java b/src/main/java/com/velocitypowered/proxy/util/UuidUtils.java new file mode 100644 index 000000000..23553631e --- /dev/null +++ b/src/main/java/com/velocitypowered/proxy/util/UuidUtils.java @@ -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)); + } +} diff --git a/src/test/java/com/velocitypowered/proxy/util/UuidUtilsTest.java b/src/test/java/com/velocitypowered/proxy/util/UuidUtilsTest.java new file mode 100644 index 000000000..cc62f37e4 --- /dev/null +++ b/src/test/java/com/velocitypowered/proxy/util/UuidUtilsTest.java @@ -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)); + } +} \ No newline at end of file