Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update SSLSocketChannel2.java #1253

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
25 changes: 22 additions & 3 deletions src/main/java/org/java_websocket/SSLSocketChannel2.java
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;
}
}
}
}