diff --git a/src/main/java/dslab/client/MessageClient.java b/src/main/java/dslab/client/MessageClient.java index 2373282..b783d4e 100644 --- a/src/main/java/dslab/client/MessageClient.java +++ b/src/main/java/dslab/client/MessageClient.java @@ -13,6 +13,7 @@ import java.util.Base64; import java.util.logging.Logger; import at.ac.tuwien.dsg.orvell.Shell; +import at.ac.tuwien.dsg.orvell.StopShellException; import at.ac.tuwien.dsg.orvell.annotation.Command; import dslab.ComponentFactory; import dslab.exception.FailedVerificationException; @@ -24,8 +25,6 @@ import javax.crypto.spec.SecretKeySpec; public class MessageClient implements IMessageClient, Runnable { private static final Logger logger = Logger.getLogger(MessageClient.class.getName()); - private final String componentId; - private final InputStream consoleIn; private final String transferHost; private final int transferPort; @@ -42,7 +41,7 @@ public class MessageClient implements IMessageClient, Runnable { private PrintWriter dmtpOut; private BufferedReader dmtpIn; - private PrintStream dmapOut; + private PrintWriter dmapOut; private BufferedReader dmapIn; private final Shell shell; @@ -68,14 +67,11 @@ public class MessageClient implements IMessageClient, Runnable { this.mailboxUser = config.getString("mailbox.user"); this.mailboxPassword = config.getString("mailbox.password"); - this.componentId = componentId; - this.consoleIn = in; - this.shell = new Shell(in, out); this.shell.register(this); - this.shell.setPrompt(this.componentId + "> "); + this.shell.setPrompt(componentId + "> "); - logger.info(String.format("TransferHost: %s\nTransferPort: %d\nMailboxHost: %s\nMailboxPort: %d\nTransferEmail: %s\nMailboxUser: %s\nMailboxPassword: %s", + logger.fine(String.format("TransferHost: %s\nTransferPort: %d\nMailboxHost: %s\nMailboxPort: %d\nTransferEmail: %s\nMailboxUser: %s\nMailboxPassword: %s", transferHost, transferPort, mailboxHost, mailboxPort, transferEmail, mailboxUser, mailboxPassword)); } @@ -85,13 +81,12 @@ public class MessageClient implements IMessageClient, Runnable { logger.info("Starting connection to MailboxHost on " + this.mailboxHost + " on port " + this.mailboxPort); this.dmapSocket = new Socket(this.mailboxHost, this.mailboxPort); this.dmapIn = new BufferedReader(new InputStreamReader(this.dmapSocket.getInputStream())); - this.dmapOut = new PrintStream(this.dmapSocket.getOutputStream(), true); + this.dmapOut = new PrintWriter(this.dmapSocket.getOutputStream(), true); String input = null; - String message = null; input = dmapIn.readLine(); - logger.info("Received message from server: " + input); + logger.fine("Received message from server: " + input); if (!input.startsWith("ok DMAP2.0")) shutdown(); @@ -123,16 +118,16 @@ public class MessageClient implements IMessageClient, Runnable { String componentId; PublicKey serverPublicKey; this.dmapOut.println("startsecure"); - logger.info("Sent command 'startsecure'"); + logger.finer("Sent command 'startsecure'"); input = this.dmapIn.readLine(); - logger.info("Server's response: " + input); + logger.finer("Server's response: " + input); if (input.startsWith("ok") && (input.split("\\s+").length == 2)) { // Get the component-id from the server componentId = input.split("\\s+")[1]; try { // Attempt to read server public key from file called _pub.der byte[] keyBytes = Files.readAllBytes(Paths.get("keys", "client", componentId + "_pub.der")); - logger.info("Read bytes from path " + Paths.get("keys", "client", componentId + "_pub.der") + ": " + Arrays.toString(keyBytes)); + logger.finer("Read bytes from path " + Paths.get("keys", "client", componentId + "_pub.der") + ": " + Arrays.toString(keyBytes)); // Create X509 spec object from key X509EncodedKeySpec spec = new X509EncodedKeySpec(keyBytes); // Create generator for RSA scheme @@ -141,14 +136,14 @@ public class MessageClient implements IMessageClient, Runnable { serverPublicKey = kf.generatePublic(spec); String clientChallenge = generateChallengeMessage(serverPublicKey); // Send clientChallenge to server - logger.info("Send clientchallenge to Server: " + clientChallenge); + logger.finer("Send clientchallenge to Server: " + clientChallenge); this.dmapOut.println(clientChallenge); // Receive AES encrypted message saying "ok " String response = this.dmapIn.readLine(); // Compare received client challenge with generated client challenge verifyChallenge(response); // Answer with AES encrypted "ok" if matching and use AES cipher for subsequent communication - logger.info("Send ciphered 'ok' to Server: " + getAesCiphertext("ok")); + logger.finer("Send ciphered 'ok' to Server: " + getAesCiphertext("ok")); this.dmapOut.println(getAesCiphertext("ok")); } catch (NoSuchAlgorithmException | InvalidKeySpecException | IllegalBlockSizeException | BadPaddingException | FailedVerificationException e) { logger.severe(e.getMessage()); @@ -190,10 +185,11 @@ public class MessageClient implements IMessageClient, Runnable { */ private void verifyChallenge(String response) throws FailedVerificationException { // Decrypt to base64 encoded byte array + logger.info("Verifying challenge..."); String plainText; try { plainText = new String(aesDecryptCipher.doFinal(Base64.getDecoder().decode(response))); - logger.info("Decrypted AES challenge: " + plainText); + logger.finer("Decrypted AES challenge: " + plainText); } catch (IllegalBlockSizeException | BadPaddingException e) { logger.severe("Error during decryption of client challenge. Aborting..."); shutdown(); @@ -324,24 +320,28 @@ public class MessageClient implements IMessageClient, Runnable { @Command @Override public void inbox() { + logger.info("Received 'inbox' command"); } @Command @Override public void delete(String id) { + logger.info("Received 'delete' command for id " + id); } @Command @Override public void verify(String id) { + logger.info("Received 'verify' command for id " + id); } @Command @Override public void msg(String to, String subject, String data) { + logger.info("Received 'msg' command:\tto: " + to + "\tsubject: " + subject + "\tdata: " + data); try { logger.info("Starting connection to TransferServer on host " + this.transferHost + " on port " + this.transferPort); this.dmtpSocket = new Socket(this.transferHost, this.transferPort); @@ -356,6 +356,7 @@ public class MessageClient implements IMessageClient, Runnable { @Command @Override public void shutdown() { + logger.info("Received 'shutdown' command"); if (dmtpSocket != null) { try { this.dmtpSocket.close(); @@ -379,6 +380,7 @@ public class MessageClient implements IMessageClient, Runnable { } Thread.currentThread().interrupt(); + throw new StopShellException(); } public static void main(String[] args) throws Exception {