From 95e0a50b1eafd079fb0b66204a5e7540491ee44f Mon Sep 17 00:00:00 2001 From: marci4 Date: Mon, 10 Sep 2018 19:32:30 +0200 Subject: [PATCH] Use a SocketFactory to support reconnecting Fixes #764 --- .gitignore | 1 - src/main/example/SSLClientExample.java | 2 +- .../client/WebSocketClient.java | 24 ++- .../java_websocket/issues/AllIssueTests.java | 4 +- .../java_websocket/issues/Issue764Test.java | 142 ++++++++++++++++++ src/test/java/org/java_websocket/keystore.jks | Bin 0 -> 2251 bytes 6 files changed, 168 insertions(+), 5 deletions(-) create mode 100644 src/test/java/org/java_websocket/issues/Issue764Test.java create mode 100644 src/test/java/org/java_websocket/keystore.jks diff --git a/.gitignore b/.gitignore index 27c091f7..845b9273 100644 --- a/.gitignore +++ b/.gitignore @@ -16,6 +16,5 @@ bin /doc *.xml .idea/.name -*.jks lib/junit-4.7.jar *.jar \ No newline at end of file diff --git a/src/main/example/SSLClientExample.java b/src/main/example/SSLClientExample.java index 5b3361be..e24ea90d 100644 --- a/src/main/example/SSLClientExample.java +++ b/src/main/example/SSLClientExample.java @@ -104,7 +104,7 @@ public static void main( String[] args ) throws Exception { SSLSocketFactory factory = sslContext.getSocketFactory();// (SSLSocketFactory) SSLSocketFactory.getDefault(); - chatclient.setSocket( factory.createSocket() ); + chatclient.setSocketFactory( factory ); chatclient.connectBlocking(); diff --git a/src/main/java/org/java_websocket/client/WebSocketClient.java b/src/main/java/org/java_websocket/client/WebSocketClient.java index c4e9b0e4..d749f531 100644 --- a/src/main/java/org/java_websocket/client/WebSocketClient.java +++ b/src/main/java/org/java_websocket/client/WebSocketClient.java @@ -40,6 +40,7 @@ import java.util.concurrent.CountDownLatch; import java.util.concurrent.TimeUnit; +import javax.net.SocketFactory; import javax.net.ssl.SSLContext; import javax.net.ssl.SSLException; import javax.net.ssl.SSLSocketFactory; @@ -79,6 +80,12 @@ public abstract class WebSocketClient extends AbstractWebSocket implements Runna */ private Socket socket = null; + /** + * The SocketFactory for this WebSocketClient + * @since 1.4.0 + */ + private SocketFactory socketFactory = null; + /** * The used OutputStream */ @@ -372,8 +379,9 @@ public void run() { InputStream istream; try { boolean isNewSocket = false; - - if( socket == null ) { + if (socketFactory != null) { + socket = socketFactory.createSocket(); + } else if( socket == null ) { socket = new Socket( proxy ); isNewSocket = true; @@ -691,7 +699,9 @@ public void setProxy( Proxy proxy ) { * This method must be called before connect. * If the given socket is not yet bound it will be bound to the uri specified in the constructor. * @param socket The socket which should be used for the connection + * @deprecated use setSocketFactory */ + @Deprecated public void setSocket( Socket socket ) { if( this.socket != null ) { throw new IllegalStateException( "socket has already been set" ); @@ -699,6 +709,16 @@ public void setSocket( Socket socket ) { this.socket = socket; } + /** + * Accepts a SocketFactory.
+ * This method must be called before connect. + * The socket will be bound to the uri specified in the constructor. + * @param socketFactory The socket factory which should be used for the connection. + */ + public void setSocketFactory(SocketFactory socketFactory) { + this.socketFactory = socketFactory; + } + @Override public void sendFragmentedFrame(Opcode op, ByteBuffer buffer, boolean fin ) { engine.sendFragmentedFrame( op, buffer, fin ); diff --git a/src/test/java/org/java_websocket/issues/AllIssueTests.java b/src/test/java/org/java_websocket/issues/AllIssueTests.java index 61726299..b79c2ef4 100644 --- a/src/test/java/org/java_websocket/issues/AllIssueTests.java +++ b/src/test/java/org/java_websocket/issues/AllIssueTests.java @@ -37,7 +37,9 @@ org.java_websocket.issues.Issue661Test.class, org.java_websocket.issues.Issue666Test.class, org.java_websocket.issues.Issue677Test.class, - org.java_websocket.issues.Issue732Test.class + org.java_websocket.issues.Issue732Test.class, + org.java_websocket.issues.Issue764Test.class, + org.java_websocket.issues.Issue765Test.class }) /** * Start all tests for issues diff --git a/src/test/java/org/java_websocket/issues/Issue764Test.java b/src/test/java/org/java_websocket/issues/Issue764Test.java new file mode 100644 index 00000000..055d76bf --- /dev/null +++ b/src/test/java/org/java_websocket/issues/Issue764Test.java @@ -0,0 +1,142 @@ +/* + * Copyright (c) 2010-2018 Nathan Rajlich + * + * Permission is hereby granted, free of charge, to any person + * obtaining a copy of this software and associated documentation + * files (the "Software"), to deal in the Software without + * restriction, including without limitation the rights to use, + * copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the + * Software is furnished to do so, subject to the following + * conditions: + * + * The above copyright notice and this permission notice shall be + * included in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, + * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES + * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND + * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT + * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, + * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING + * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR + * OTHER DEALINGS IN THE SOFTWARE. + * + */ + +package org.java_websocket.issues; + +import org.java_websocket.WebSocket; +import org.java_websocket.client.WebSocketClient; +import org.java_websocket.handshake.ClientHandshake; +import org.java_websocket.handshake.ServerHandshake; +import org.java_websocket.server.DefaultSSLWebSocketServerFactory; +import org.java_websocket.server.WebSocketServer; +import org.java_websocket.util.SocketUtil; +import org.junit.Test; + +import javax.net.ssl.KeyManagerFactory; +import javax.net.ssl.SSLContext; +import javax.net.ssl.TrustManagerFactory; +import java.io.File; +import java.io.FileInputStream; +import java.io.IOException; +import java.net.InetSocketAddress; +import java.net.URI; +import java.net.URISyntaxException; +import java.security.*; +import java.security.cert.CertificateException; +import java.util.concurrent.CountDownLatch; + +public class Issue764Test { + private CountDownLatch countClientDownLatch = new CountDownLatch(2); + private CountDownLatch countServerDownLatch = new CountDownLatch(1); + + @Test(timeout = 2000) + public void testIssue() throws IOException, URISyntaxException, KeyStoreException, NoSuchAlgorithmException, KeyManagementException, UnrecoverableKeyException, CertificateException, InterruptedException { + int port = SocketUtil.getAvailablePort(); + final WebSocketClient webSocket = new WebSocketClient(new URI("wss://localhost:" + port)) { + @Override + public void onOpen(ServerHandshake handshakedata) { + countClientDownLatch.countDown(); + countServerDownLatch.countDown(); + } + + @Override + public void onMessage(String message) { + } + + @Override + public void onClose(int code, String reason, boolean remote) { + } + + @Override + public void onError(Exception ex) { + } + }; + WebSocketServer server = new MyWebSocketServer(port, webSocket, countServerDownLatch); + + // load up the key store + String STORETYPE = "JKS"; + String KEYSTORE = String.format("src%1$stest%1$1sjava%1$1sorg%1$1sjava_websocket%1$1skeystore.jks", File.separator); + String STOREPASSWORD = "storepassword"; + String KEYPASSWORD = "keypassword"; + + KeyStore ks = KeyStore.getInstance(STORETYPE); + File kf = new File(KEYSTORE); + ks.load(new FileInputStream(kf), STOREPASSWORD.toCharArray()); + + KeyManagerFactory kmf = KeyManagerFactory.getInstance("SunX509"); + kmf.init(ks, KEYPASSWORD.toCharArray()); + TrustManagerFactory tmf = TrustManagerFactory.getInstance("SunX509"); + tmf.init(ks); + + SSLContext sslContext = null; + sslContext = SSLContext.getInstance("TLS"); + sslContext.init(kmf.getKeyManagers(), tmf.getTrustManagers(), null); + + server.setWebSocketFactory(new DefaultSSLWebSocketServerFactory(sslContext)); + webSocket.setSocketFactory(sslContext.getSocketFactory()); + server.start(); + countServerDownLatch.await(); + webSocket.connectBlocking(); + webSocket.reconnectBlocking(); + countClientDownLatch.await(); + } + + + private static class MyWebSocketServer extends WebSocketServer { + private final WebSocketClient webSocket; + private final CountDownLatch countServerDownLatch; + + + public MyWebSocketServer(int port, WebSocketClient webSocket, CountDownLatch countServerDownLatch) { + super(new InetSocketAddress(port)); + this.webSocket = webSocket; + this.countServerDownLatch = countServerDownLatch; + } + + @Override + public void onOpen(WebSocket conn, ClientHandshake handshake) { + } + + @Override + public void onClose(WebSocket conn, int code, String reason, boolean remote) { + } + + @Override + public void onMessage(WebSocket conn, String message) { + + } + + @Override + public void onError(WebSocket conn, Exception ex) { + ex.printStackTrace(); + } + + @Override + public void onStart() { + countServerDownLatch.countDown(); + } + } +} diff --git a/src/test/java/org/java_websocket/keystore.jks b/src/test/java/org/java_websocket/keystore.jks new file mode 100644 index 0000000000000000000000000000000000000000..743add50e6fe50f8e00c182085024bab565743b2 GIT binary patch literal 2251 zcmc&#_fr#y5>7%ANa&zIATd%^YC;K3kRsB9R4IZW<`~JyE{AI?(F_t`?&@H06;7U{_X5ugx@^E z{7EeAqH;Mn699k!FfrgG3=b482L*zGvLJpS5CQu|>lMdZZ4Q-QTUKZSkYw$? zMhlIp<#S^$aw%#k?oOW3TvBLh>NmuzM1x_YOXFjxij_r5{np@3eKpuSn7TT1+upaZCGT|jzX_htTF19C(MM`-S0n+AT20&jL2`mb?)vtmC18jWjK7Pb%j!Y z%Jha!+(57)YCfj4(Li{nbY)2HG~m%?<~P>>r#ibyyD`8_teman9=Il2y> z&_YZ6o_&%C1UL;9MNjEQSB&SE(~=_3)gHWc&_&ret;)>zIQfv|y`L(s8hEVZN`8;b zs}(Ix4c<%{)gi({3obJwIsW_#n~R%`It*$|$9XSVZ+-?0NGt;nS7!QV%%ST z{YjUl`0(?<2@msou^uhWt=xkIF8I8gUg9;y9e2;1pfjd`T)l<6(LSOQAN}e@dm`Q# zU@S~Mv-#YT*T-(pg))F2Mh#vTr)VclaeQ!*)!Nq%O$F7q>`p)EMym(HZ5<1!BSYy5 zI{7L@{|I-imza;J=ySC$k?S9Yi(HMS3QK!zI!x~4BGx}x>DLJ!it|HaJA6%EHF(Ym z=;!ft#A$lTg_e%B6i)4P%kV^hq^swb)Zadzjk=prrlS2#qqkblEsPdKE`xW=RlKz; zl$8^Q4?02K=fu+PTvOCMmqIdM-DA`U^d^_7(c^N^hlRX9lJ+Q**1Jbmf6;D^x!VqI zH7wuT@r{&8dO?3<7cZAqXV^l?cknZwYTC|>ax7skMk$?tSd4Bo_B)%s{<0tpmzkin zJ|T4iu+{fh{ESU^8x`#%4Ii<>GCTCV=;5I3F}szCR)ddrpX>MYenlhhUaG=M?T z@601y8*=98h{20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