Skip to content

Commit

Permalink
Update SSLSocketChannel2.java
Browse files Browse the repository at this point in the history
'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.
  • Loading branch information
sahnjeok authored Jun 17, 2022
1 parent cad989d commit 4b5d78f
Showing 1 changed file with 22 additions and 3 deletions.
25 changes: 22 additions & 3 deletions src/main/java/org/java_websocket/SSLSocketChannel2.java
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,12 @@ public class SSLSocketChannel2 implements ByteChannel, WrappedByteChannel, ISSLC
**/
protected int bufferallocations = 0;

/**
* 2022-06-17 Handshake start time in WSS for the underlying channel.
* If wss handshake is not completed in 10s, close this channel to prevent cpu overload or unexpected channel error. see #896.
*/
protected long handshakeStartTime = System.currentTimeMillis() ;

public SSLSocketChannel2(SocketChannel channel, SSLEngine sslEngine, ExecutorService exec,
SelectionKey key) throws IOException {
if (channel == null || sslEngine == null || exec == null) {
Expand Down Expand Up @@ -393,8 +399,21 @@ public void close() throws IOException {

private boolean isHandShakeComplete() {
HandshakeStatus status = sslEngine.getHandshakeStatus();
return status == SSLEngineResult.HandshakeStatus.FINISHED
|| status == SSLEngineResult.HandshakeStatus.NOT_HANDSHAKING;

// handshake status
boolean ret = status == SSLEngineResult.HandshakeStatus.FINISHED
|| status == SSLEngineResult.HandshakeStatus.NOT_HANDSHAKING;

if ( ret == false )
{
// 2022-06-17 If wss handshake is not completed in 10s, close this channel to prevent cpu overload or unexpected channel error. see #896.
if ( handshakeStartTime > 0 && ( System.currentTimeMillis() - handshakeStartTime ) > 10000 )
{
try{close() ;}catch(Exception ex){} ;
}
}

return ret;
}

public SelectableChannel configureBlocking(boolean b) throws IOException {
Expand Down Expand Up @@ -495,4 +514,4 @@ private void tryRestoreCryptedData() {
saveCryptData = null;
}
}
}
}

0 comments on commit 4b5d78f

Please sign in to comment.