move client to fabric client

This commit is contained in:
1e99 2024-11-17 13:49:04 +01:00
parent f8777ad035
commit df825fa87b
2 changed files with 89 additions and 78 deletions

View file

@ -0,0 +1,89 @@
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.io.BinaryMessage;
import eu.e99.svc.packet.AuthRequestPacket;
import eu.e99.svc.packet.AuthResponsePacket;
import eu.e99.svc.packet.AuthSuccessPacket;
import eu.e99.svc.packet.ClientHelloPacket;
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;
public class SVCClient {
public static void main(String[] args) throws NoSuchAlgorithmException, KeyManagementException {
TrustManager[] trustManagers = new TrustManager[]{
new AllTrustManager(),
};
SSLContext sslContext = SSLContext.getInstance("TLS");
sslContext.init(null, trustManagers, new SecureRandom());
SSLSocketFactory socketFactory = sslContext.getSocketFactory();
SVCClient voiceChat = new SVCClient("localhost", 6969, socketFactory);
voiceChat.start();
}
private final String host;
private final int port;
private final SSLSocketFactory socketFactory;
public SVCClient(String host, int port, SSLSocketFactory socketFactory) {
this.host = host;
this.port = port;
this.socketFactory = socketFactory;
}
public void start() {
// Connect and auth
try (Socket socket = this.socketFactory.createSocket()) {
socket.connect(new InetSocketAddress(this.host, this.port));
Connection conn = new Connection(socket);
this.handleHandshake(conn);
} catch (IOException e) {
System.out.printf("Failed to connect.%n");
e.printStackTrace(System.out);
}
}
private void handleHandshake(Connection conn) throws IOException {
ClientHelloPacket clientHello = new ClientHelloPacket();
clientHello.version = SimplerVoiceChat.PROTOCOL_VERSION;
conn.writePacket(clientHello);
while (true) {
BinaryMessage packet = conn.readPacket();
switch (packet) {
case AuthRequestPacket authRequest -> {
System.out.printf("Joining fake server to authenticate.%n");
AuthResponsePacket authResponse = new AuthResponsePacket();
authResponse.username = "Test";
conn.writePacket(authResponse);
}
case AuthSuccessPacket authComplete -> {
System.out.printf("Successfully authenticated.%n");
return;
}
default -> {
System.out.printf("Got unexpected packet.%n");
}
}
}
}
}

View file

@ -1,19 +1,8 @@
package eu.e99.svc; package eu.e99.svc;
import eu.e99.svc.io.BinaryMessage;
import eu.e99.svc.io.MessageRegistry; import eu.e99.svc.io.MessageRegistry;
import eu.e99.svc.packet.*; import eu.e99.svc.packet.*;
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;
public class SimplerVoiceChat { public class SimplerVoiceChat {
public static final int PROTOCOL_VERSION = 0; public static final int PROTOCOL_VERSION = 0;
@ -30,71 +19,4 @@ public class SimplerVoiceChat {
PACKETS.registerMessage(100, MessagePacket::new); PACKETS.registerMessage(100, MessagePacket::new);
PACKETS.registerMessage(255, DisconnectPacket::new); PACKETS.registerMessage(255, DisconnectPacket::new);
} }
public static void main(String[] args) throws NoSuchAlgorithmException, KeyManagementException {
TrustManager[] trustManagers = new TrustManager[]{
new AllTrustManager(),
};
SSLContext sslContext = SSLContext.getInstance("TLS");
sslContext.init(null, trustManagers, new SecureRandom());
SSLSocketFactory socketFactory = sslContext.getSocketFactory();
SimplerVoiceChat voiceChat = new SimplerVoiceChat("localhost", 6969, socketFactory);
voiceChat.start();
}
private final String host;
private final int port;
private final SSLSocketFactory socketFactory;
public SimplerVoiceChat(String host, int port, SSLSocketFactory socketFactory) {
this.host = host;
this.port = port;
this.socketFactory = socketFactory;
}
public void start() {
// Connect and auth
try (Socket socket = this.socketFactory.createSocket()) {
socket.connect(new InetSocketAddress(this.host, this.port));
Connection conn = new Connection(socket);
this.handleHandshake(conn);
} catch (IOException e) {
System.out.printf("Failed to connect.%n");
e.printStackTrace(System.out);
}
}
private void handleHandshake(Connection conn) throws IOException {
ClientHelloPacket clientHello = new ClientHelloPacket();
clientHello.version = PROTOCOL_VERSION;
conn.writePacket(clientHello);
while (true) {
BinaryMessage packet = conn.readPacket();
switch (packet) {
case AuthRequestPacket authRequest -> {
System.out.printf("Joining fake server to authenticate.%n");
AuthResponsePacket authResponse = new AuthResponsePacket();
authResponse.username = "Test";
conn.writePacket(authResponse);
}
case AuthSuccessPacket authComplete -> {
System.out.printf("Successfully authenticated.%n");
return;
}
default -> {
System.out.printf("Got unexpected packet.%n");
}
}
}
}
} }