slight client refactor

This commit is contained in:
1e99 2024-11-17 19:00:37 +01:00
parent 9daf6158f7
commit 680c2fb8d3
4 changed files with 50 additions and 58 deletions

1
.gitignore vendored
View file

@ -1,4 +1,5 @@
.gradle
.idea
.notes
build
client-fabric/run

View file

@ -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);
}
}

View file

@ -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");
}
}

View file

@ -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<Void> 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;
}
}
}