Skip to content

Commit

Permalink
Do manual hostname verification only if the automatic one fails
Browse files Browse the repository at this point in the history
  • Loading branch information
KacperKluka committed Jun 22, 2022
1 parent 8831ae3 commit f35c5b6
Showing 1 changed file with 14 additions and 6 deletions.
20 changes: 14 additions & 6 deletions lib/src/main/java/io/ably/lib/transport/WebSocketTransport.java
Original file line number Diff line number Diff line change
Expand Up @@ -150,11 +150,11 @@ class WsClient extends WebSocketClient {
@Override
public void onOpen(ServerHandshake handshakedata) {
Log.d(TAG, "onOpen()");
if (isHostnameVerified(params.host)) {
if (shouldExplicitlyVerifyHostname && !isHostnameVerified(params.host)) {
close();
} else {
connectListener.onTransportAvailable(WebSocketTransport.this);
flagActivity();
} else {
close();
}
}

Expand Down Expand Up @@ -259,9 +259,16 @@ public void onError(final Exception e) {

@Override
protected void onSetSSLParameters(SSLParameters sslParameters) {
// Overriding without calling the setEndpointIdentificationAlgorithm() to solve an issue on Android below 24.
// When the minSdkVersion will be updated to 24 we should remove this empty method.
// https://github.com/TooTallNate/Java-WebSocket/wiki/No-such-method-error-setEndpointIdentificationAlgorithm#workaround
try {
super.onSetSSLParameters(sslParameters);
shouldExplicitlyVerifyHostname = false;
} catch (NoSuchMethodError exception) {
// This error will be thrown on Android below level 24.
// When the minSdkVersion will be updated to 24 we should remove this overridden method.
// https://github.com/TooTallNate/Java-WebSocket/wiki/No-such-method-error-setEndpointIdentificationAlgorithm#workaround
Log.w(TAG, "Error when trying to set SSL parameters, most likely due to an old Java API version", exception);
shouldExplicitlyVerifyHostname = true;
}
}

private synchronized void dispose() {
Expand Down Expand Up @@ -337,6 +344,7 @@ private synchronized void schedule(TimerTask task, long delay) {
private Timer timer = new Timer();
private TimerTask activityTimerTask = null;
private long lastActivityTime;
private boolean shouldExplicitlyVerifyHostname = true;
}

public String toString() {
Expand Down

0 comments on commit f35c5b6

Please sign in to comment.