diff options
author | Christoph Schulthess | 2017-02-22 14:52:21 +0100 |
---|---|---|
committer | Christoph Schulthess | 2017-02-22 14:52:21 +0100 |
commit | 3f74578130e8a58a22749468f2448250c37f5bd9 (patch) | |
tree | 2c82c7ac4a971641dc3abf0469a9162114f7a98d | |
parent | fixed Dispatch Relay (diff) | |
download | tmlite-bwlp-3f74578130e8a58a22749468f2448250c37f5bd9.tar.gz tmlite-bwlp-3f74578130e8a58a22749468f2448250c37f5bd9.tar.xz tmlite-bwlp-3f74578130e8a58a22749468f2448250c37f5bd9.zip |
DispatchSSLRelay added
-rw-r--r-- | src/main/java/org/openslx/taskmanager/tasks/DispatchRelay.java | 23 | ||||
-rw-r--r-- | src/main/java/org/openslx/taskmanager/tasks/DispatchSSLRelay.java | 40 |
2 files changed, 55 insertions, 8 deletions
diff --git a/src/main/java/org/openslx/taskmanager/tasks/DispatchRelay.java b/src/main/java/org/openslx/taskmanager/tasks/DispatchRelay.java index 73bea44..6f16ab5 100644 --- a/src/main/java/org/openslx/taskmanager/tasks/DispatchRelay.java +++ b/src/main/java/org/openslx/taskmanager/tasks/DispatchRelay.java @@ -1,6 +1,9 @@ package org.openslx.taskmanager.tasks; import java.net.Socket; + +import javax.net.SocketFactory; + import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; @@ -11,7 +14,7 @@ import com.google.gson.annotations.Expose; public class DispatchRelay extends AbstractTask { - private Output status = new Output(); + protected Output status = new Output(); @Expose public String[] hosts; @@ -26,10 +29,10 @@ public class DispatchRelay extends AbstractTask { Socket[] sockets; Thread[] relayThreads; try { - sockets = createSockets(); + sockets = createSockets( getSocketFactory() ); status.addMessage( "Sockets connected." ); - } catch ( IOException iox ) { - status.addMessage( "Failed to connect sockets. " + iox.getMessage() ); + } catch ( Exception e ) { + status.addMessage( "Failed to connect sockets. " + e.getMessage() ); return false; } @@ -80,7 +83,7 @@ public class DispatchRelay extends AbstractTask { protected Thread[] createRelayThreads ( Socket[] sockets) throws IOException { Thread[] t = new Thread[2]; - Socket[][] s = {sockets, {sockets[1], sockets[0]}}; + Socket[][] s = { sockets, { sockets[1], sockets[0] }}; for ( int i = 0; i < 2; i++ ) { t[i] = new Thread( new Relay( s[i], status, descs[i] )); @@ -88,16 +91,20 @@ public class DispatchRelay extends AbstractTask { return t; } - protected Socket[] createSockets () throws IOException + protected SocketFactory getSocketFactory () throws Exception { + return SocketFactory.getDefault(); + } + + protected Socket[] createSockets ( SocketFactory sf ) throws IOException { Socket[] s = new Socket[2]; for ( int i = 0; i < 2; i++ ) { InetSocketAddress addr = new InetSocketAddress( hosts[i], ports[i] ); - s[i] = new Socket(); + s[i] = sf.createSocket(); this.status.addMessage( "trying to connect socket to " + addr.toString() ); s[i].connect( addr, 1200 ); - this.status.addMessage( "connected."); + this.status.addMessage( "connected." ); } return s; } diff --git a/src/main/java/org/openslx/taskmanager/tasks/DispatchSSLRelay.java b/src/main/java/org/openslx/taskmanager/tasks/DispatchSSLRelay.java new file mode 100644 index 0000000..fc1382e --- /dev/null +++ b/src/main/java/org/openslx/taskmanager/tasks/DispatchSSLRelay.java @@ -0,0 +1,40 @@ +package org.openslx.taskmanager.tasks; + +import java.io.IOException; +import java.security.cert.X509Certificate; + +import javax.net.SocketFactory; +import javax.net.ssl.SSLContext; +import javax.net.ssl.SSLSocketFactory; +import javax.net.ssl.TrustManager; +import javax.net.ssl.X509TrustManager; +import javax.net.ssl.SSLSocket; + +import org.openslx.taskmanager.tasks.DispatchRelay; + +public class DispatchSSLRelay extends DispatchRelay { + + @Override + protected SSLSocket[] createSockets ( SocketFactory sf ) throws IOException { + return ( SSLSocket[] ) super.createSockets( sf ); + } + + @Override + protected SSLSocketFactory getSocketFactory () throws Exception { + return trustAll().getSocketFactory(); + } + + protected SSLContext trustAll () throws Exception { + TrustManager[] trustAllMan = new TrustManager[] { new X509TrustManager() { + public java.security.cert.X509Certificate[] getAcceptedIssuers() { + return null; + } + public void checkClientTrusted( X509Certificate[] certs, String authType ) {} + public void checkServerTrusted( X509Certificate[] certs, String authType ) {} + } + }; + SSLContext ctx = SSLContext.getInstance( "SSL" ); + ctx.init( null, trustAllMan, new java.security.SecureRandom() ); + return ctx; + } +} |