Skip to content

Commit 4b5d78f

Browse files
authored
Update SSLSocketChannel2.java
'handshakeStartTime' long variable is added and isHandShakeComplete() function is updated for TooTallNate#896. If wss handshake is not completed in 10s, close this channel to prevent cpu overload or unexpected channel error. See TooTallNate#896.
1 parent cad989d commit 4b5d78f

File tree

1 file changed

+22
-3
lines changed

1 file changed

+22
-3
lines changed

src/main/java/org/java_websocket/SSLSocketChannel2.java

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,12 @@ public class SSLSocketChannel2 implements ByteChannel, WrappedByteChannel, ISSLC
105105
**/
106106
protected int bufferallocations = 0;
107107

108+
/**
109+
* 2022-06-17 Handshake start time in WSS for the underlying channel.
110+
* If wss handshake is not completed in 10s, close this channel to prevent cpu overload or unexpected channel error. see #896.
111+
*/
112+
protected long handshakeStartTime = System.currentTimeMillis() ;
113+
108114
public SSLSocketChannel2(SocketChannel channel, SSLEngine sslEngine, ExecutorService exec,
109115
SelectionKey key) throws IOException {
110116
if (channel == null || sslEngine == null || exec == null) {
@@ -393,8 +399,21 @@ public void close() throws IOException {
393399

394400
private boolean isHandShakeComplete() {
395401
HandshakeStatus status = sslEngine.getHandshakeStatus();
396-
return status == SSLEngineResult.HandshakeStatus.FINISHED
397-
|| status == SSLEngineResult.HandshakeStatus.NOT_HANDSHAKING;
402+
403+
// handshake status
404+
boolean ret = status == SSLEngineResult.HandshakeStatus.FINISHED
405+
|| status == SSLEngineResult.HandshakeStatus.NOT_HANDSHAKING;
406+
407+
if ( ret == false )
408+
{
409+
// 2022-06-17 If wss handshake is not completed in 10s, close this channel to prevent cpu overload or unexpected channel error. see #896.
410+
if ( handshakeStartTime > 0 && ( System.currentTimeMillis() - handshakeStartTime ) > 10000 )
411+
{
412+
try{close() ;}catch(Exception ex){} ;
413+
}
414+
}
415+
416+
return ret;
398417
}
399418

400419
public SelectableChannel configureBlocking(boolean b) throws IOException {
@@ -495,4 +514,4 @@ private void tryRestoreCryptedData() {
495514
saveCryptData = null;
496515
}
497516
}
498-
}
517+
}

0 commit comments

Comments
 (0)