From 30a27d3ed5d046132d18aca12d1c2b09125b46c0 Mon Sep 17 00:00:00 2001 From: Christoph Schulthess Date: Thu, 1 Dec 2016 16:53:15 +0100 Subject: cleaned task execute method --- .../openslx/taskmanager/tasks/SSLRelayTask.java | 151 +++++++++++++-------- 1 file changed, 91 insertions(+), 60 deletions(-) diff --git a/src/main/java/org/openslx/taskmanager/tasks/SSLRelayTask.java b/src/main/java/org/openslx/taskmanager/tasks/SSLRelayTask.java index 8377dc6..a9c0214 100644 --- a/src/main/java/org/openslx/taskmanager/tasks/SSLRelayTask.java +++ b/src/main/java/org/openslx/taskmanager/tasks/SSLRelayTask.java @@ -58,77 +58,107 @@ public class SSLRelayTask extends AbstractTask { this.setStatusObject(status); return true; } - + @Override protected boolean execute() { - SSLSocketFactory ssf; + SSLSocketFactory ssf = initSSLSocketFactory(); + if (ssf==null) { + status.error = "Could not initialize SSLSocketFactory"; + return false; + } + try { + initSockets(ssf); + initRelays(); + } catch (UnknownHostException uhx) { + status.error = "One of the hosts to relay to/from is unknown: " + uhx.getMessage(); + return close(); + } catch (IOException iox) { + status.error = iox.getMessage(); + return close(); + } + + Thread aToBThread = initThread('a'); + Thread bToAThread = initThread('b'); + + if (aToBThread == null || bToAThread == null) { + status.error = "Could not initialize Threads."; + return close(); + } + + while(enabled) { + aToBThread.start(); + bToAThread.start(); + } + return close(); + } + + private SSLSocketFactory initSSLSocketFactory () { if (auth) { + System.setProperty("javax.net.ssl.keyStore", "keystore.jks"); + System.setProperty("javax.net.ssl.trustStore", "cacerts.jks"); + + return (SSLSocketFactory) SSLSocketFactory.getDefault(); + } + else { try { - ssf = trustAll().getSocketFactory(); + return trustAll().getSocketFactory(); } catch (NoSuchAlgorithmException nax) { status.error = nax.getMessage(); - return false; + return null; } catch (KeyManagementException kmx) { status.error = kmx.getMessage(); - return false; + return null; } } - else { - ssf = (SSLSocketFactory) SSLSocketFactory.getDefault(); - System.setProperty("javax.net.ssl.keyStore", "keystore.jks"); - System.setProperty("javax.net.ssl.trustStore", "cacerts.jks"); - } - - try { - sockA = (SSLSocket) ssf.createSocket(clientAIp, clientAPort); - System.out.println("connected to " + clientAIp + " on port " + Integer.toString(clientAPort)); - sockB = (SSLSocket) ssf.createSocket(clientBIp, clientBPort); - System.out.println("connected to " + clientBIp + " on port " + Integer.toString(clientBPort)); - aToB = new Relay(sockA, sockB); - System.out.println("relay created from " + clientAIp + " to " + clientBIp); - bToA = new Relay(sockB, sockA); - System.out.println("relay created from " + clientBIp + " to " + clientAIp); - } catch (UnknownHostException uhx) { - close(); - status.error = uhx.getMessage(); - return false; - } catch (IOException iox) { - status.error = iox.getMessage(); - return false; - } - - Thread aToBThread = new Thread() { - public void run() { - try { - aToB.relay(); - } catch (IOException iox) { - status.error = iox.getMessage(); - return; - } catch (InterruptedException ix) { - status.error = ix.getMessage(); - return; - } + } + + private Thread initThread (char source) { + if (source == 'a') { + return new Thread() { + public void run() { + try { + aToB.relay(); + } catch (IOException iox) { + status.error = iox.getMessage(); + return; + } catch (InterruptedException ix) { + status.error = ix.getMessage(); + return; + } + }; }; - }; - Thread bToAThread = new Thread() { - public void run() { - try { - bToA.relay(); - } catch (IOException iox) { - status.error = iox.getMessage(); - return; - } catch (InterruptedException ix) { - status.error = ix.getMessage(); - return; - } + } + else if (source == 'b') { + return new Thread() { + public void run() { + try { + bToA.relay(); + } catch (IOException iox) { + status.error = iox.getMessage(); + return; + } catch (InterruptedException ix) { + status.error = ix.getMessage(); + return; + } + }; }; - }; - while(enabled) { - aToBThread.start(); - bToAThread.start(); } - close(); - return true; + else + return null; + } + + private void initSockets (SSLSocketFactory ssf) throws IOException, UnknownHostException { + sockA = (SSLSocket) ssf.createSocket(clientAIp, clientAPort); + System.out.println("connected to " + clientAIp + " on port " + Integer.toString(clientAPort)); + sockB = (SSLSocket) ssf.createSocket(clientBIp, clientBPort); + System.out.println("connected to " + clientBIp + " on port " + Integer.toString(clientBPort)); + } + + private void initRelays () throws IOException { + aToB = new Relay(sockA, sockB); + System.out.println("relay created from " + clientAIp + " to " + clientBIp); + bToA = new Relay(sockB, sockA); + System.out.println("relay created from " + clientBIp + " to " + clientAIp); } /** @@ -148,7 +178,7 @@ public class SSLRelayTask extends AbstractTask { return ctx; } - private void close() { + private boolean close() { try { if (aToB != null) aToB.close(); @@ -158,8 +188,9 @@ public class SSLRelayTask extends AbstractTask { sockB.close(); } catch (IOException iox) { status.error = iox.getMessage(); - System.exit(1); + return false; } + return true; } /** -- cgit v1.2.3-55-g7522