Cleaned up client encryption logic.
This commit is contained in:
parent
ec3bfbbc09
commit
5568136d86
|
@ -34,8 +34,8 @@ import java.util.Map;
|
||||||
|
|
||||||
public class ConcordClient implements Runnable {
|
public class ConcordClient implements Runnable {
|
||||||
private final Socket socket;
|
private final Socket socket;
|
||||||
private InputStream in;
|
private final InputStream in;
|
||||||
private OutputStream out;
|
private final OutputStream out;
|
||||||
private final Serializer serializer;
|
private final Serializer serializer;
|
||||||
|
|
||||||
@Getter
|
@Getter
|
||||||
|
@ -48,9 +48,14 @@ public class ConcordClient implements Runnable {
|
||||||
public ConcordClient(String host, int port, String nickname, Path tokensFile) throws IOException {
|
public ConcordClient(String host, int port, String nickname, Path tokensFile) throws IOException {
|
||||||
this.eventManager = new EventManager(this);
|
this.eventManager = new EventManager(this);
|
||||||
this.socket = new Socket(host, port);
|
this.socket = new Socket(host, port);
|
||||||
this.in = this.socket.getInputStream();
|
|
||||||
this.out = this.socket.getOutputStream();
|
|
||||||
this.serializer = new Serializer();
|
this.serializer = new Serializer();
|
||||||
|
try {
|
||||||
|
var streams = Encryption.upgrade(socket.getInputStream(), socket.getOutputStream(), this.serializer);
|
||||||
|
this.in = streams.first();
|
||||||
|
this.out = streams.second();
|
||||||
|
} catch (GeneralSecurityException e) {
|
||||||
|
throw new IOException("Could not establish secure connection to the server.", e);
|
||||||
|
}
|
||||||
this.model = this.initializeConnectionToServer(nickname, tokensFile);
|
this.model = this.initializeConnectionToServer(nickname, tokensFile);
|
||||||
|
|
||||||
// Add event listeners.
|
// Add event listeners.
|
||||||
|
@ -75,7 +80,6 @@ public class ConcordClient implements Runnable {
|
||||||
* messages, or if the server sends an unexpected response.
|
* messages, or if the server sends an unexpected response.
|
||||||
*/
|
*/
|
||||||
private ClientModel initializeConnectionToServer(String nickname, Path tokensFile) throws IOException {
|
private ClientModel initializeConnectionToServer(String nickname, Path tokensFile) throws IOException {
|
||||||
this.establishEncryption();
|
|
||||||
String token = this.getSessionToken(tokensFile);
|
String token = this.getSessionToken(tokensFile);
|
||||||
this.serializer.writeMessage(new Identification(nickname, token), this.out);
|
this.serializer.writeMessage(new Identification(nickname, token), this.out);
|
||||||
Message reply = this.serializer.readMessage(this.in);
|
Message reply = this.serializer.readMessage(this.in);
|
||||||
|
@ -90,24 +94,6 @@ public class ConcordClient implements Runnable {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Establishes an encrypted connection to the server. This should be the
|
|
||||||
* first method which interacts with the server, since it sends and receives
|
|
||||||
* specific key information, and all subsequent traffic should be encrypted.
|
|
||||||
* @throws IOException If encryption could not be established.
|
|
||||||
*/
|
|
||||||
private void establishEncryption() throws IOException {
|
|
||||||
try {
|
|
||||||
System.out.println("Initializing end-to-end encryption with the server...");
|
|
||||||
var streams = Encryption.upgrade(this.in, this.out, this.serializer);
|
|
||||||
this.in = streams.first();
|
|
||||||
this.out = streams.second();
|
|
||||||
System.out.println("Successfully established cipher streams.");
|
|
||||||
} catch (GeneralSecurityException e) {
|
|
||||||
throw new IOException(e);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public void sendMessage(Message message) throws IOException {
|
public void sendMessage(Message message) throws IOException {
|
||||||
this.serializer.writeMessage(message, this.out);
|
this.serializer.writeMessage(message, this.out);
|
||||||
}
|
}
|
||||||
|
|
|
@ -19,6 +19,9 @@ import java.util.Arrays;
|
||||||
import java.util.Collections;
|
import java.util.Collections;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Utility class for handling the establishment of encrypted communication.
|
||||||
|
*/
|
||||||
public class Encryption {
|
public class Encryption {
|
||||||
public static Pair<CipherInputStream, CipherOutputStream> upgrade(
|
public static Pair<CipherInputStream, CipherOutputStream> upgrade(
|
||||||
InputStream in,
|
InputStream in,
|
||||||
|
|
Loading…
Reference in New Issue