diff --git a/.gitignore b/.gitignore index ffdc958..8e244e5 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,5 @@ .gradle .idea +.notes build client-fabric/run diff --git a/client-fabric/src/eu/e99/svc/client/fabric/FabricSimplerVoiceChat.java b/client-fabric/src/eu/e99/svc/client/fabric/FabricSimplerVoiceChat.java index 3fe4aa7..65ce4f8 100644 --- a/client-fabric/src/eu/e99/svc/client/fabric/FabricSimplerVoiceChat.java +++ b/client-fabric/src/eu/e99/svc/client/fabric/FabricSimplerVoiceChat.java @@ -1,16 +1,21 @@ package eu.e99.svc.client.fabric; +import eu.e99.svc.AllTrustManager; import net.fabricmc.api.ClientModInitializer; import net.minecraft.client.MinecraftClient; import net.minecraft.client.session.Session; +import javax.net.ssl.SSLContext; import javax.net.ssl.SSLSocketFactory; +import javax.net.ssl.TrustManager; import java.security.KeyManagementException; import java.security.NoSuchAlgorithmException; +import java.security.SecureRandom; public class FabricSimplerVoiceChat implements ClientModInitializer { public static final String NAMESPACE = "svc"; + public static SVCClient VOICE_CLIENT; @Override public void onInitializeClient() { @@ -18,22 +23,26 @@ public class FabricSimplerVoiceChat implements ClientModInitializer { SSLSocketFactory socketFactory; try { - socketFactory = SVCClient.createSocketFactory(); + TrustManager[] trustManagers = new TrustManager[]{ + new AllTrustManager(), + }; + + SSLContext sslContext = SSLContext.getInstance("TLS"); + sslContext.init(null, trustManagers, new SecureRandom()); + + socketFactory = sslContext.getSocketFactory(); } catch (NoSuchAlgorithmException | KeyManagementException e) { System.out.printf("Failed to create socket factory:%n"); e.printStackTrace(System.out); return; } - MinecraftClient client = MinecraftClient.getInstance(); - Session session = client.getSession(); - - SVCClient voiceChat = new SVCClient( + VOICE_CLIENT = new SVCClient( "localhost", 6969, - client, + MinecraftClient.getInstance(), socketFactory ); - Thread.ofPlatform().start(voiceChat::start); + Thread.ofPlatform().start(VOICE_CLIENT::start); } } diff --git a/client-fabric/src/eu/e99/svc/client/fabric/SVCClient.java b/client-fabric/src/eu/e99/svc/client/fabric/SVCClient.java index b3c7b1f..c10c809 100644 --- a/client-fabric/src/eu/e99/svc/client/fabric/SVCClient.java +++ b/client-fabric/src/eu/e99/svc/client/fabric/SVCClient.java @@ -1,6 +1,5 @@ package eu.e99.svc.client.fabric; -import eu.e99.svc.AllTrustManager; import eu.e99.svc.Connection; import eu.e99.svc.SimplerVoiceChat; import eu.e99.svc.auth.MojangAPI; @@ -9,30 +8,13 @@ import eu.e99.svc.packet.*; import net.minecraft.client.MinecraftClient; import net.minecraft.client.session.Session; -import javax.net.ssl.SSLContext; import javax.net.ssl.SSLSocketFactory; -import javax.net.ssl.TrustManager; import java.io.IOException; import java.net.InetSocketAddress; import java.net.Socket; -import java.security.KeyManagementException; -import java.security.NoSuchAlgorithmException; -import java.security.SecureRandom; -import java.util.UUID; public class SVCClient { - public static SSLSocketFactory createSocketFactory() throws NoSuchAlgorithmException, KeyManagementException { - TrustManager[] trustManagers = new TrustManager[]{ - new AllTrustManager(), - }; - - SSLContext sslContext = SSLContext.getInstance("TLS"); - sslContext.init(null, trustManagers, new SecureRandom()); - - return sslContext.getSocketFactory(); - } - private final String host; private final int port; private final MinecraftClient client; @@ -46,28 +28,8 @@ public class SVCClient { } public void start() { - // Connect and auth - while (true) { - try (Socket socket = this.socketFactory.createSocket()) { - System.out.printf("Connecting to %s.%n", this.host); - socket.connect(new InetSocketAddress(this.host, this.port)); - Connection conn = new Connection(socket); - System.out.printf("Connected."); - - boolean ok = this.handleHandshake(conn); - if (!ok) { - System.out.printf("Handshake failed.%n"); - break; - } - - System.out.printf("Successfully authenticated.%n"); - - while (true) {} - } catch (IOException e) { - System.out.printf("Failed to connect.%n"); - e.printStackTrace(System.out); - } + this.connect(); System.out.printf("Disconnected. Attempting reconnection in 30 seconds.%n"); try { @@ -79,6 +41,31 @@ public class SVCClient { } } + private void connect() { + try (Socket socket = this.socketFactory.createSocket()) { + try { + System.out.printf("Connecting to %s.%n", this.host); + socket.connect(new InetSocketAddress(this.host, this.port)); + System.out.printf("Connected.%n"); + } catch (IOException e) { + System.out.printf("Connection failed:%n"); + e.printStackTrace(System.out); + } + Connection conn = new Connection(socket); + + boolean ok = this.handleHandshake(conn); + if (!ok) { + System.out.printf("Handshake failed.%n"); + return; + } + + this.handle(conn); + } catch (Exception e) { + System.out.printf("Failed to connect.%n"); + e.printStackTrace(System.out); + } + } + private boolean handleHandshake(Connection conn) throws IOException { ClientHelloPacket clientHello = new ClientHelloPacket(); clientHello.version = SimplerVoiceChat.PROTOCOL_VERSION; @@ -93,9 +80,8 @@ public class SVCClient { Session session = this.client.getSession(); - boolean ok; try { - ok = MojangAPI.joinServer( + MojangAPI.joinServer( session.getAccessToken(), session.getUuidOrNull(), authRequest.serverId @@ -106,16 +92,11 @@ public class SVCClient { return false; } - if (!ok) { - System.out.printf("Failed to join server.%n"); - return false; - } - AuthResponsePacket authResponse = new AuthResponsePacket(); authResponse.username = session.getUsername(); conn.writePacket(authResponse); } - case AuthSuccessPacket authComplete -> { + case AuthSuccessPacket success -> { return true; } case DisconnectPacket disconnect -> { @@ -127,6 +108,9 @@ public class SVCClient { } } } + } + private void handle(Connection conn) { + System.out.printf("Successfully authenticated.%n"); } } diff --git a/common/src/eu/e99/svc/auth/MojangAPI.java b/common/src/eu/e99/svc/auth/MojangAPI.java index 446b5ae..345c738 100644 --- a/common/src/eu/e99/svc/auth/MojangAPI.java +++ b/common/src/eu/e99/svc/auth/MojangAPI.java @@ -40,7 +40,7 @@ public class MojangAPI { } } - public static boolean joinServer(String accessToken, UUID uuid, String serverId) throws IOException, InterruptedException { + public static void joinServer(String accessToken, UUID uuid, String serverId) throws IOException, InterruptedException { try (HttpClient client = HttpClient.newHttpClient()) { String loc = "https://sessionserver.mojang.com/session/minecraft/join"; URI uri = URI.create(loc); @@ -58,10 +58,8 @@ public class MojangAPI { HttpResponse res = client.send(req, HttpResponse.BodyHandlers.discarding()); System.out.printf("%d%n", res.statusCode()); if (res.statusCode() != 204) { - return false; + throw new IllegalArgumentException("Failed to authenticate."); } - - return true; } } }