slight client refactor
This commit is contained in:
parent
9daf6158f7
commit
680c2fb8d3
4 changed files with 50 additions and 58 deletions
1
.gitignore
vendored
1
.gitignore
vendored
|
@ -1,4 +1,5 @@
|
||||||
.gradle
|
.gradle
|
||||||
.idea
|
.idea
|
||||||
|
.notes
|
||||||
build
|
build
|
||||||
client-fabric/run
|
client-fabric/run
|
||||||
|
|
|
@ -1,16 +1,21 @@
|
||||||
package eu.e99.svc.client.fabric;
|
package eu.e99.svc.client.fabric;
|
||||||
|
|
||||||
|
import eu.e99.svc.AllTrustManager;
|
||||||
import net.fabricmc.api.ClientModInitializer;
|
import net.fabricmc.api.ClientModInitializer;
|
||||||
import net.minecraft.client.MinecraftClient;
|
import net.minecraft.client.MinecraftClient;
|
||||||
import net.minecraft.client.session.Session;
|
import net.minecraft.client.session.Session;
|
||||||
|
|
||||||
|
import javax.net.ssl.SSLContext;
|
||||||
import javax.net.ssl.SSLSocketFactory;
|
import javax.net.ssl.SSLSocketFactory;
|
||||||
|
import javax.net.ssl.TrustManager;
|
||||||
import java.security.KeyManagementException;
|
import java.security.KeyManagementException;
|
||||||
import java.security.NoSuchAlgorithmException;
|
import java.security.NoSuchAlgorithmException;
|
||||||
|
import java.security.SecureRandom;
|
||||||
|
|
||||||
public class FabricSimplerVoiceChat implements ClientModInitializer {
|
public class FabricSimplerVoiceChat implements ClientModInitializer {
|
||||||
|
|
||||||
public static final String NAMESPACE = "svc";
|
public static final String NAMESPACE = "svc";
|
||||||
|
public static SVCClient VOICE_CLIENT;
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onInitializeClient() {
|
public void onInitializeClient() {
|
||||||
|
@ -18,22 +23,26 @@ public class FabricSimplerVoiceChat implements ClientModInitializer {
|
||||||
|
|
||||||
SSLSocketFactory socketFactory;
|
SSLSocketFactory socketFactory;
|
||||||
try {
|
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) {
|
} catch (NoSuchAlgorithmException | KeyManagementException e) {
|
||||||
System.out.printf("Failed to create socket factory:%n");
|
System.out.printf("Failed to create socket factory:%n");
|
||||||
e.printStackTrace(System.out);
|
e.printStackTrace(System.out);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
MinecraftClient client = MinecraftClient.getInstance();
|
VOICE_CLIENT = new SVCClient(
|
||||||
Session session = client.getSession();
|
|
||||||
|
|
||||||
SVCClient voiceChat = new SVCClient(
|
|
||||||
"localhost",
|
"localhost",
|
||||||
6969,
|
6969,
|
||||||
client,
|
MinecraftClient.getInstance(),
|
||||||
socketFactory
|
socketFactory
|
||||||
);
|
);
|
||||||
Thread.ofPlatform().start(voiceChat::start);
|
Thread.ofPlatform().start(VOICE_CLIENT::start);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,6 +1,5 @@
|
||||||
package eu.e99.svc.client.fabric;
|
package eu.e99.svc.client.fabric;
|
||||||
|
|
||||||
import eu.e99.svc.AllTrustManager;
|
|
||||||
import eu.e99.svc.Connection;
|
import eu.e99.svc.Connection;
|
||||||
import eu.e99.svc.SimplerVoiceChat;
|
import eu.e99.svc.SimplerVoiceChat;
|
||||||
import eu.e99.svc.auth.MojangAPI;
|
import eu.e99.svc.auth.MojangAPI;
|
||||||
|
@ -9,30 +8,13 @@ import eu.e99.svc.packet.*;
|
||||||
import net.minecraft.client.MinecraftClient;
|
import net.minecraft.client.MinecraftClient;
|
||||||
import net.minecraft.client.session.Session;
|
import net.minecraft.client.session.Session;
|
||||||
|
|
||||||
import javax.net.ssl.SSLContext;
|
|
||||||
import javax.net.ssl.SSLSocketFactory;
|
import javax.net.ssl.SSLSocketFactory;
|
||||||
import javax.net.ssl.TrustManager;
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.net.InetSocketAddress;
|
import java.net.InetSocketAddress;
|
||||||
import java.net.Socket;
|
import java.net.Socket;
|
||||||
import java.security.KeyManagementException;
|
|
||||||
import java.security.NoSuchAlgorithmException;
|
|
||||||
import java.security.SecureRandom;
|
|
||||||
import java.util.UUID;
|
|
||||||
|
|
||||||
public class SVCClient {
|
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 String host;
|
||||||
private final int port;
|
private final int port;
|
||||||
private final MinecraftClient client;
|
private final MinecraftClient client;
|
||||||
|
@ -46,28 +28,8 @@ public class SVCClient {
|
||||||
}
|
}
|
||||||
|
|
||||||
public void start() {
|
public void start() {
|
||||||
// Connect and auth
|
|
||||||
|
|
||||||
while (true) {
|
while (true) {
|
||||||
try (Socket socket = this.socketFactory.createSocket()) {
|
this.connect();
|
||||||
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);
|
|
||||||
}
|
|
||||||
|
|
||||||
System.out.printf("Disconnected. Attempting reconnection in 30 seconds.%n");
|
System.out.printf("Disconnected. Attempting reconnection in 30 seconds.%n");
|
||||||
try {
|
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 {
|
private boolean handleHandshake(Connection conn) throws IOException {
|
||||||
ClientHelloPacket clientHello = new ClientHelloPacket();
|
ClientHelloPacket clientHello = new ClientHelloPacket();
|
||||||
clientHello.version = SimplerVoiceChat.PROTOCOL_VERSION;
|
clientHello.version = SimplerVoiceChat.PROTOCOL_VERSION;
|
||||||
|
@ -93,9 +80,8 @@ public class SVCClient {
|
||||||
|
|
||||||
Session session = this.client.getSession();
|
Session session = this.client.getSession();
|
||||||
|
|
||||||
boolean ok;
|
|
||||||
try {
|
try {
|
||||||
ok = MojangAPI.joinServer(
|
MojangAPI.joinServer(
|
||||||
session.getAccessToken(),
|
session.getAccessToken(),
|
||||||
session.getUuidOrNull(),
|
session.getUuidOrNull(),
|
||||||
authRequest.serverId
|
authRequest.serverId
|
||||||
|
@ -106,16 +92,11 @@ public class SVCClient {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!ok) {
|
|
||||||
System.out.printf("Failed to join server.%n");
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
AuthResponsePacket authResponse = new AuthResponsePacket();
|
AuthResponsePacket authResponse = new AuthResponsePacket();
|
||||||
authResponse.username = session.getUsername();
|
authResponse.username = session.getUsername();
|
||||||
conn.writePacket(authResponse);
|
conn.writePacket(authResponse);
|
||||||
}
|
}
|
||||||
case AuthSuccessPacket authComplete -> {
|
case AuthSuccessPacket success -> {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
case DisconnectPacket disconnect -> {
|
case DisconnectPacket disconnect -> {
|
||||||
|
@ -127,6 +108,9 @@ public class SVCClient {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private void handle(Connection conn) {
|
||||||
|
System.out.printf("Successfully authenticated.%n");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -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()) {
|
try (HttpClient client = HttpClient.newHttpClient()) {
|
||||||
String loc = "https://sessionserver.mojang.com/session/minecraft/join";
|
String loc = "https://sessionserver.mojang.com/session/minecraft/join";
|
||||||
URI uri = URI.create(loc);
|
URI uri = URI.create(loc);
|
||||||
|
@ -58,10 +58,8 @@ public class MojangAPI {
|
||||||
HttpResponse<Void> res = client.send(req, HttpResponse.BodyHandlers.discarding());
|
HttpResponse<Void> res = client.send(req, HttpResponse.BodyHandlers.discarding());
|
||||||
System.out.printf("%d%n", res.statusCode());
|
System.out.printf("%d%n", res.statusCode());
|
||||||
if (res.statusCode() != 204) {
|
if (res.statusCode() != 204) {
|
||||||
return false;
|
throw new IllegalArgumentException("Failed to authenticate.");
|
||||||
}
|
}
|
||||||
|
|
||||||
return true;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue