Skip to content

Commit 18655c9

Browse files
committed
upgrade ws library to 1.5.3 and remove outdated SNI code
1 parent 37de7a0 commit 18655c9

File tree

3 files changed

+16
-106
lines changed

3 files changed

+16
-106
lines changed

tinodesdk/build.gradle

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,7 @@ dependencies {
3232
api 'com.fasterxml.jackson.core:jackson-databind:2.13.4'
3333
api 'com.fasterxml.jackson.core:jackson-core:2.13.4'
3434
api 'com.fasterxml.jackson.core:jackson-annotations:2.13.4'
35-
//noinspection GradleDependency: 1.5.0 requires Android API 24.
36-
api 'org.java-websocket:Java-WebSocket:1.4.1'
35+
api 'org.java-websocket:Java-WebSocket:1.5.3'
3736

3837
implementation 'org.jetbrains:annotations:23.0.0'
3938
testImplementation 'junit:junit:4.13.2'

tinodesdk/src/main/java/co/tinode/tinodesdk/Connection.java

Lines changed: 5 additions & 83 deletions
Original file line numberDiff line numberDiff line change
@@ -6,24 +6,13 @@
66
import org.java_websocket.drafts.Draft_6455;
77
import org.java_websocket.handshake.ServerHandshake;
88

9-
import java.io.IOException;
10-
import java.lang.reflect.InvocationTargetException;
11-
import java.lang.reflect.Method;
12-
import java.net.InetAddress;
13-
import java.net.Socket;
149
import java.net.URI;
1510
import java.net.URISyntaxException;
1611
import java.nio.ByteBuffer;
17-
import java.security.KeyManagementException;
18-
import java.security.NoSuchAlgorithmException;
1912
import java.util.HashMap;
2013
import java.util.Map;
2114
import java.util.concurrent.TimeUnit;
2215

23-
import javax.net.SocketFactory;
24-
import javax.net.ssl.SSLContext;
25-
import javax.net.ssl.SSLSocketFactory;
26-
2716
/**
2817
* A thinly wrapped websocket connection.
2918
*/
@@ -71,18 +60,6 @@ protected Connection(URI endpoint, String apikey, WsListener listener) {
7160
mStatus = State.NEW;
7261
mAutoreconnect = false;
7362
mBackground = false;
74-
75-
// Horrible hack to support SNI on API<21
76-
if ("wss".equals(getURI().getScheme())) {
77-
try {
78-
SSLContext sslContext = SSLContext.getInstance("TLSv1.2");
79-
sslContext.init(null, null, null);
80-
SSLSocketFactory factory = sslContext.getSocketFactory();
81-
setSocketFactory(new SNISocketFactory(factory));
82-
} catch (NoSuchAlgorithmException | KeyManagementException ex) {
83-
Log.w(TAG, "Failed to set up SSL", ex);
84-
}
85-
}
8663
}
8764

8865
private static Map<String,String> wrapApiKey(String apikey) {
@@ -294,72 +271,17 @@ public void onError(Exception ex) {
294271
}
295272
}
296273

297-
static class WsListener {
298-
WsListener() {}
299-
300-
protected void onConnect(Connection conn, boolean background) {
301-
}
302-
303-
protected void onMessage(Connection conn, String message) {
304-
}
305-
306-
protected void onDisconnect(Connection conn, boolean byServer, int code, String reason) {
307-
}
308-
309-
protected void onError(Connection conn, Exception err) {
310-
}
311-
}
312-
313-
private class SNISocketFactory extends SocketFactory {
314-
final SocketFactory mWrapped;
315-
316-
SNISocketFactory(SocketFactory parent) {
317-
mWrapped = parent;
318-
}
319-
320-
@Override
321-
public Socket createSocket() throws IOException {
322-
URI uri = getURI();
323-
return createSocket(uri.getHost(), uri.getPort());
274+
interface WsListener {
275+
default void onConnect(Connection conn, boolean background) {
324276
}
325277

326-
@Override
327-
public Socket createSocket(String host, int port) throws IOException {
328-
Socket socket = mWrapped.createSocket(host, port);
329-
fixHostname(socket);
330-
return socket;
278+
default void onMessage(Connection conn, String message) {
331279
}
332280

333-
@Override
334-
public Socket createSocket(String host, int port, InetAddress localHost, int localPort) throws IOException {
335-
Socket socket = mWrapped.createSocket(host, port, localHost, localPort);
336-
fixHostname(socket);
337-
return socket;
281+
default void onDisconnect(Connection conn, boolean byServer, int code, String reason) {
338282
}
339283

340-
@Override
341-
public Socket createSocket(InetAddress address, int port) throws IOException {
342-
Socket socket = mWrapped.createSocket(address, port);
343-
fixHostname(socket);
344-
return socket;
345-
}
346-
347-
@Override
348-
public Socket createSocket(InetAddress address, int port, InetAddress localAddress, int localPort) throws IOException {
349-
Socket socket = mWrapped.createSocket(address, port, localAddress, localPort);
350-
fixHostname(socket);
351-
return socket;
352-
}
353-
354-
// SNI hack for earlier versions of Android
355-
private void fixHostname(Socket socket) {
356-
try {
357-
// We don't know the actual class of the socket. Using reflection.
358-
Method method = socket.getClass().getMethod("setHostname", String.class);
359-
method.invoke(socket, getURI().getHost());
360-
} catch (NoSuchMethodException | SecurityException | IllegalAccessException | InvocationTargetException ex) {
361-
Log.w(TAG, "SNI configuration failed", ex);
362-
}
284+
default void onError(Connection conn, Exception err) {
363285
}
364286
}
365287
}

tinodesdk/src/main/java/co/tinode/tinodesdk/Tinode.java

Lines changed: 10 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -2246,17 +2246,6 @@ synchronized String nextUniqueString() {
22462246
return Long.toString(((new Date().getTime() - 1414213562373L) << 16) + (mNameCounter & 0xFFFF), 32);
22472247
}
22482248

2249-
long getTimeAdjustment() {
2250-
return mTimeAdjustment;
2251-
}
2252-
2253-
/**
2254-
* Interface for converting mime type string to Jackson's JavaType.
2255-
*/
2256-
public interface MimeTypeResolver {
2257-
JavaType resolve(String mimeType);
2258-
}
2259-
22602249
/**
22612250
* Interface to be implemented by those clients which want to fetch topics
22622251
* using {@link Tinode#getFilteredTopics}
@@ -2305,7 +2294,7 @@ default void onLogin(int code, String text) {
23052294
*
23062295
* @param msg message to be processed
23072296
*/
2308-
default void onMessage(ServerMessage msg) {
2297+
default void onMessage(@SuppressWarnings("unused") ServerMessage msg) {
23092298
}
23102299

23112300
/**
@@ -2315,15 +2304,15 @@ default void onMessage(ServerMessage msg) {
23152304
*
23162305
* @param msg message to be processed
23172306
*/
2318-
default void onRawMessage(String msg) {
2307+
default void onRawMessage(@SuppressWarnings("unused") String msg) {
23192308
}
23202309

23212310
/**
23222311
* Handle control message
23232312
*
23242313
* @param ctrl control message to process
23252314
*/
2326-
default void onCtrlMessage(MsgServerCtrl ctrl) {
2315+
default void onCtrlMessage(@SuppressWarnings("unused") MsgServerCtrl ctrl) {
23272316
}
23282317

23292318
/**
@@ -2347,15 +2336,15 @@ default void onInfoMessage(MsgServerInfo info) {
23472336
*
23482337
* @param meta meta message to process
23492338
*/
2350-
default void onMetaMessage(MsgServerMeta meta) {
2339+
default void onMetaMessage(@SuppressWarnings("unused") MsgServerMeta meta) {
23512340
}
23522341

23532342
/**
23542343
* Handle presence message
23552344
*
23562345
* @param pres control message to process
23572346
*/
2358-
default void onPresMessage(MsgServerPres pres) {
2347+
default void onPresMessage(@SuppressWarnings("unused") MsgServerPres pres) {
23592348
}
23602349
}
23612350

@@ -2502,7 +2491,7 @@ private static class FutureHolder {
25022491
}
25032492

25042493
// Class which listens for websocket to connect.
2505-
private class ConnectedWsListener extends Connection.WsListener {
2494+
private class ConnectedWsListener implements Connection.WsListener {
25062495
final Vector<PromisedReply<ServerMessage>> mCompletionPromises;
25072496

25082497
ConnectedWsListener() {
@@ -2514,7 +2503,7 @@ void addPromise(PromisedReply<ServerMessage> promise) {
25142503
}
25152504

25162505
@Override
2517-
protected void onConnect(final Connection conn, final boolean background) {
2506+
public void onConnect(final Connection conn, final boolean background) {
25182507
// Connection established, send handshake, inform listener on success
25192508
hello(background).thenApply(
25202509
new PromisedReply.SuccessListener<ServerMessage>() {
@@ -2564,7 +2553,7 @@ public PromisedReply<ServerMessage> onSuccess(ServerMessage pkt) throws Exceptio
25642553
}
25652554

25662555
@Override
2567-
protected void onMessage(Connection conn, String message) {
2556+
public void onMessage(Connection conn, String message) {
25682557
try {
25692558
dispatchPacket(message);
25702559
} catch (Exception ex) {
@@ -2573,7 +2562,7 @@ protected void onMessage(Connection conn, String message) {
25732562
}
25742563

25752564
@Override
2576-
protected void onDisconnect(Connection conn, boolean byServer, int code, String reason) {
2565+
public void onDisconnect(Connection conn, boolean byServer, int code, String reason) {
25772566
handleDisconnect(byServer, -code, reason);
25782567
// Promises may have already been rejected if onError was called first.
25792568
try {
@@ -2584,7 +2573,7 @@ protected void onDisconnect(Connection conn, boolean byServer, int code, String
25842573
}
25852574

25862575
@Override
2587-
protected void onError(Connection conn, Exception err) {
2576+
public void onError(Connection conn, Exception err) {
25882577
// No need to call handleDisconnect here. It will be called from onDisconnect().
25892578

25902579
// If the promise is waiting, reject. Otherwise it's not our problem.

0 commit comments

Comments
 (0)