-
-
Notifications
You must be signed in to change notification settings - Fork 814
/
ConnectionThread.java
703 lines (626 loc) · 22.9 KB
/
ConnectionThread.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
/**
* Copyright (c) 2013, Redsolution LTD. All rights reserved.
*
* This file is part of Xabber project; you can redistribute it and/or
* modify it under the terms of the GNU General Public License, Version 3.
*
* Xabber is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
* See the GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License,
* along with this program. If not, see http://www.gnu.org/licenses/.
*/
package com.xabber.android.data.connection;
import android.widget.Toast;
import com.xabber.android.R;
import com.xabber.android.data.Application;
import com.xabber.android.data.LogManager;
import com.xabber.android.data.NetworkException;
import com.xabber.android.data.SettingsManager;
import com.xabber.android.data.account.AccountItem;
import com.xabber.android.data.account.AccountProtocol;
import com.xabber.android.data.account.OAuthManager;
import com.xabber.android.data.account.OAuthResult;
import com.xabber.android.data.roster.AccountRosterListener;
import org.jivesoftware.smack.AbstractXMPPConnection;
import org.jivesoftware.smack.ConnectionListener;
import org.jivesoftware.smack.SASLAuthentication;
import org.jivesoftware.smack.SmackException;
import org.jivesoftware.smack.StanzaListener;
import org.jivesoftware.smack.XMPPConnection;
import org.jivesoftware.smack.XMPPException;
import org.jivesoftware.smack.filter.StanzaFilter;
import org.jivesoftware.smack.packet.Stanza;
import org.jivesoftware.smack.packet.StreamError;
import org.jivesoftware.smack.parsing.ExceptionLoggingCallback;
import org.jivesoftware.smack.roster.Roster;
import org.jivesoftware.smack.sasl.provided.SASLPlainMechanism;
import org.jivesoftware.smack.tcp.XMPPTCPConnection;
import org.jivesoftware.smack.tcp.XMPPTCPConnectionConfiguration;
import org.jivesoftware.smack.util.TLSUtils;
import org.jivesoftware.smackx.iqregister.AccountManager;
import org.jivesoftware.smackx.ping.PingFailedListener;
import java.io.IOException;
import java.security.KeyManagementException;
import java.security.NoSuchAlgorithmException;
import java.security.cert.CertificateException;
import java.util.Map;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.ThreadFactory;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLException;
import javax.net.ssl.X509TrustManager;
import de.duenndns.ssl.MemorizingTrustManager;
/**
* Provides connection workflow.
* <p/>
* Warning: SMACK with its connection is going to be removed!
*
* @author alexander.ivanov
*/
public class ConnectionThread implements
ConnectionListener,
StanzaListener, PingFailedListener {
private static Pattern ADDRESS_AND_PORT = Pattern.compile("^(.*):(\\d+)$");
/**
* Filter to process all packets.
*/
private final AcceptAll ACCEPT_ALL = new AcceptAll();
private final ConnectionItem connectionItem;
/**
* SMACK connection.
*/
private XMPPTCPConnection xmppConnection;
/**
* Thread holder for this connection.
*/
private final ExecutorService executorService;
private final AccountProtocol protocol;
private final String serverName;
private final String login;
/**
* Refresh token for OAuth or regular password.
*/
private final String token;
private final String resource;
private final boolean saslEnabled;
private final TLSMode tlsMode;
private final boolean compression;
private final ProxyType proxyType;
private final String proxyHost;
private final int proxyPort;
private final String proxyUser;
private final String proxyPassword;
private boolean started;
private boolean registerNewAccount;
public ConnectionThread(final ConnectionItem connectionItem) {
LogManager.i(this, "NEW connection thread " + connectionItem.getRealJid());
this.connectionItem = connectionItem;
executorService = Executors.newSingleThreadExecutor(new ThreadFactory() {
@Override
public Thread newThread(Runnable runnable) {
Thread thread = new Thread(
runnable,
"Connection thread for "
+ (connectionItem instanceof AccountItem ? ((AccountItem) connectionItem)
.getAccount() : connectionItem));
thread.setPriority(Thread.MIN_PRIORITY);
thread.setDaemon(true);
return thread;
}
});
ConnectionManager.getInstance().onConnection(this);
ConnectionSettings connectionSettings = connectionItem.getConnectionSettings();
protocol = connectionSettings.getProtocol();
serverName = connectionSettings.getServerName();
token = connectionSettings.getPassword();
resource = connectionSettings.getResource();
saslEnabled = connectionSettings.isSaslEnabled();
tlsMode = connectionSettings.getTlsMode();
compression = connectionSettings.useCompression();
if (saslEnabled && protocol == AccountProtocol.gtalk) {
login = connectionSettings.getUserName() + "@" + connectionSettings.getServerName();
} else {
login = connectionSettings.getUserName();
}
proxyType = connectionSettings.getProxyType();
proxyHost = connectionSettings.getProxyHost();
proxyPort = connectionSettings.getProxyPort();
proxyUser = connectionSettings.getProxyUser();
proxyPassword = connectionSettings.getProxyPassword();
started = false;
}
public AbstractXMPPConnection getXMPPConnection() {
return xmppConnection;
}
public ConnectionItem getConnectionItem() {
return connectionItem;
}
private void createConnection(String fqdn, int port, boolean useSRVLookup) {
XMPPTCPConnectionConfiguration.Builder builder = XMPPTCPConnectionConfiguration.builder();
if (useSRVLookup) {
builder.setServiceName(serverName);
} else {
builder.setHost(fqdn);
builder.setPort(port);
builder.setServiceName(serverName);
}
onReady(builder);
}
private void onReady(XMPPTCPConnectionConfiguration.Builder builder) {
builder.setSecurityMode(tlsMode.getSecurityMode());
builder.setCompressionEnabled(compression);
builder.setSendPresence(false);
try {
if (SettingsManager.securityCheckCertificate()) {
SSLContext sslContext = SSLContext.getInstance("TLS");
MemorizingTrustManager mtm = new MemorizingTrustManager(Application.getInstance());
sslContext.init(null, new X509TrustManager[]{mtm}, new java.security.SecureRandom());
builder.setCustomSSLContext(sslContext);
builder.setHostnameVerifier(
mtm.wrapHostnameVerifier(new org.apache.http.conn.ssl.StrictHostnameVerifier()));
} else {
TLSUtils.acceptAllCertificates(builder);
TLSUtils.disableHostnameVerificationForTlsCertificicates(builder);
}
} catch (NoSuchAlgorithmException | KeyManagementException e) {
e.printStackTrace();
}
setUpSASL();
xmppConnection = new XMPPTCPConnection(builder.build());
xmppConnection.addAsyncStanzaListener(this, ACCEPT_ALL);
xmppConnection.addConnectionListener(this);
// enable Stream Management support. SMACK will only enable SM if supported by the server,
// so no additional checks are required.
xmppConnection.setUseStreamManagement(true);
// by default Smack disconnects in case of parsing errors
xmppConnection.setParsingExceptionCallback(new ExceptionLoggingCallback());
AccountRosterListener rosterListener = new AccountRosterListener(((AccountItem)connectionItem).getAccount());
final Roster roster = Roster.getInstanceFor(xmppConnection);
roster.addRosterListener(rosterListener);
roster.addRosterLoadedListener(rosterListener);
roster.setSubscriptionMode(Roster.SubscriptionMode.manual);
org.jivesoftware.smackx.ping.PingManager.getInstanceFor(xmppConnection).registerPingFailedListener(this);
connectionItem.onSRVResolved(this);
final String password = OAuthManager.getInstance().getPassword(protocol, token);
if (password != null) {
runOnConnectionThread(new Runnable() {
@Override
public void run() {
connect(password);
}
});
} else {
runOnConnectionThread(new Runnable() {
@Override
public void run() {
passwordRequest();
}
});
}
}
private void setUpSASL() {
if (SettingsManager.connectionUsePlainTextAuth()) {
final Map<String, String> registeredSASLMechanisms = SASLAuthentication.getRegisterdSASLMechanisms();
for (String mechanism : registeredSASLMechanisms.values()) {
SASLAuthentication.blacklistSASLMechanism(mechanism);
}
SASLAuthentication.unBlacklistSASLMechanism(SASLPlainMechanism.NAME);
} else {
final Map<String, String> registeredSASLMechanisms = SASLAuthentication.getRegisterdSASLMechanisms();
for (String mechanism : registeredSASLMechanisms.values()) {
SASLAuthentication.unBlacklistSASLMechanism(mechanism);
}
}
}
/**
* Request to renew password using OAuth.
*/
private void passwordRequest() {
final OAuthResult oAuthResult;
try {
oAuthResult = OAuthManager.getInstance().requestAccessToken(
protocol, token);
} catch (NetworkException e) {
throw new RuntimeException(e);
}
runOnUiThread(new Runnable() {
@Override
public void run() {
onPasswordReceived(oAuthResult);
}
});
}
/**
* Called when password has been renewed.
*
* @param oAuthResult
*/
private void onPasswordReceived(final OAuthResult oAuthResult) {
OAuthManager.getInstance().onAccessTokenReceived(oAuthResult);
if (oAuthResult == null) {
connectionItem.onAuthFailed();
return;
}
connectionItem.onPasswordChanged(oAuthResult.getRefreshToken());
final String password = oAuthResult.getAccessToken();
runOnConnectionThread(new Runnable() {
@Override
public void run() {
connect(password);
}
});
}
/**
* Server requests us to see another host.
*
* @param target
*/
private void seeOtherHost(String target) {
Matcher matcher = ADDRESS_AND_PORT.matcher(target);
int defaultPort = 5222;
if (matcher.matches()) {
String value = matcher.group(2);
try {
defaultPort = Integer.valueOf(value);
target = matcher.group(1);
} catch (NumberFormatException e2) {
}
}
final String fqdn = target;
final int port = defaultPort;
// TODO: Check for the same address.
runOnUiThread(new Runnable() {
@Override
public void run() {
connectionItem.onSeeOtherHost(ConnectionThread.this, fqdn, port, true);
}
});
}
/**
* Connect to the server.
*
* @param password
*/
private void connect(final String password) {
try {
xmppConnection.connect();
} catch (SmackException | IOException | XMPPException e) {
e.printStackTrace();
checkForCertificateError(e);
if (!checkForSeeOtherHost(e)) {
// There is no connection listeners yet, so we call onClose.
throw new RuntimeException(e);
}
}
// try {
// xmppConnection.connect();
// } catch (XMPPException e) {
// checkForCertificateError(e);
// if (!checkForSeeOtherHost(e)) {
// // There is no connection listeners yet, so we call onClose.
// throw new RuntimeException(e);
// }
// } catch (IllegalStateException e) {
// // There is no connection listeners yet, so we call onClose.
// // connectionCreated() in other modules can fail.
// throw new RuntimeException(e);
// }
runOnUiThread(new Runnable() {
@Override
public void run() {
onConnected(password);
}
});
}
/**
* Check for certificate exception.
*
* @param e
*/
private void checkForCertificateError(Exception e) {
if (!(e instanceof SSLException)) {
return;
}
Throwable e2 = e.getCause();
if (!(e2 instanceof CertificateException)) {
return;
}
runOnUiThread(new Runnable() {
@Override
public void run() {
connectionItem.onInvalidCertificate();
}
});
}
/**
* Check for see other host exception.
*
* @param e
* @return <code>true</code> if {@link StreamError.Condition#see_other_host} has
* been found.
*/
private boolean checkForSeeOtherHost(Exception e) {
if (e instanceof XMPPException.StreamErrorException) {
XMPPException.StreamErrorException streamErrorException = (XMPPException.StreamErrorException) e;
if (streamErrorException.getStreamError().getCondition() == StreamError.Condition.see_other_host) {
String target = streamErrorException.getStreamError().getConditionText();
if (target == null || "".equals(target))
return false;
LogManager.i(this, "See other host: " + target);
seeOtherHost(target);
return true;
}
}
return false;
}
/**
* Connection to the server has been established.
*
* @param password
*/
private void onConnected(final String password) {
connectionItem.onConnected(this);
ConnectionManager.getInstance().onConnected(this);
if(registerNewAccount) {
runOnConnectionThread(new Runnable() {
@Override
public void run() {
registerAccount(password);
}
});
}
else {
runOnConnectionThread(new Runnable() {
@Override
public void run() {
authorization(password);
}
});
}
}
/**
* Register new account.
*
* @param password
*/
private void registerAccount(final String password) {
try {
AccountManager.getInstance(xmppConnection).createAccount(login, password);
} catch (SmackException.NoResponseException | SmackException.NotConnectedException | XMPPException.XMPPErrorException e) {
LogManager.exception(connectionItem, e);
connectionClosedOnError(e);
// Server will destroy connection, but we can speedup
// it.
xmppConnection.disconnect();
return;
}
runOnUiThread(new Runnable() {
@Override
public void run() {
onAccountRegistered(password);
}
});
}
/**
* New account has been registerd on the server.
*
* @param password
*/
private void onAccountRegistered(final String password) {
LogManager.i(this, "Account registered");
connectionItem.onAccountRegistered(this);
runOnConnectionThread(new Runnable() {
@Override
public void run() {
authorization(password);
}
});
}
/**
* Login.
*
* @param password
*/
private void authorization(String password) {
try {
xmppConnection.login(login, password, resource);
} catch (IOException | SmackException | XMPPException e) {
e.printStackTrace();
connectionClosedOnError(e);
xmppConnection.disconnect();
return;
}
// } catch (XMPPException e) {
// LogManager.exception(connectionItem, e);
// // SASLAuthentication#authenticate(String,String,String)
// boolean SASLfailed = e.getMessage() != null
// && e.getMessage().startsWith("SASL authentication ")
// && !e.getMessage().endsWith("temporary-auth-failure");
// // NonSASLAuthentication#authenticate(String,String,String)
// // Authentication request failed (doesn't supported),
// // error was returned after negotiation or
// // authentication failed.
// boolean NonSASLfailed = e.getXMPPError() != null
// && "Authentication failed.".equals(e.getMessage());
// if (SASLfailed || NonSASLfailed) {
// // connectionClosed can be called before from reader thread,
// // so don't check whether connection is managed.
// Application.getInstance().runOnUiThread(new Runnable() {
// @Override
// public void run() {
// // Login failed. We don`t want to reconnect.
// connectionItem.onAuthFailed();
// }
// });
// connectionClosed();
// } else
// connectionClosedOnError(e);
// // Server will destroy connection, but we can speedup
// // it.
// xmppConnection.disconnect();
// return;
// } catch (SmackException | IOException e) {
// e.printStackTrace();
// return;
// }
runOnUiThread(new Runnable() {
@Override
public void run() {
onAuthorized();
}
});
}
/**
* Authorization passed.
*/
private void onAuthorized() {
connectionItem.onAuthorized(this);
ConnectionManager.getInstance().onAuthorized(this);
if (connectionItem instanceof AccountItem) {
com.xabber.android.data.account.AccountManager.getInstance().removeAuthorizationError(
((AccountItem) connectionItem).getAccount());
}
shutdown();
}
@Override
public void connected(XMPPConnection connection) {
}
@Override
public void authenticated(XMPPConnection connection, boolean resumed) {
}
@Override
public void connectionClosed() {
// Can be called on error, e.g. XMPPConnection#initConnection().
runOnUiThread(new Runnable() {
@Override
public void run() {
connectionItem.onClose(ConnectionThread.this);
}
});
}
@Override
public void connectionClosedOnError(final Exception e) {
checkForCertificateError(e);
if (checkForSeeOtherHost(e))
return;
if (SettingsManager.showConnectionErrors()) {
runOnUiThread(new Runnable() {
@Override
public void run() {
Toast.makeText(Application.getInstance(),
Application.getInstance().getString(R.string.CONNECTION_FAILED) + ": " + e.getMessage(),
Toast.LENGTH_SHORT
).show();
}
});
}
connectionClosed();
}
@Override
public void reconnectingIn(int seconds) {
}
@Override
public void reconnectionSuccessful() {
}
@Override
public void reconnectionFailed(Exception e) {
}
@Override
public void processPacket(final Stanza packet) throws SmackException.NotConnectedException {
Application.getInstance().runOnUiThread(new Runnable() {
@Override
public void run() {
ConnectionManager.getInstance().processPacket(ConnectionThread.this, packet);
}
});
}
@Override
public void pingFailed() {
LogManager.i(this, "pingFailed for " + getConnectionItem().getRealJid());
getConnectionItem().forceReconnect();
}
/**
* Filter to accept all packets.
*
* @author alexander.ivanov
*/
static class AcceptAll implements StanzaFilter {
@Override
public boolean accept(Stanza packet) {
return true;
}
}
/**
* Start connection.
* <p/>
* This function can be called only once.
*
* @param fqdn Fully Qualified Domain Names.
* @param port Preferred port.
* @param useSRVLookup Whether SRV lookup should be used.
*/
synchronized void start(final String fqdn, final int port,
final boolean useSRVLookup,
final boolean registerNewAccount) {
LogManager.i(this, "start: " + fqdn);
if (started)
throw new IllegalStateException();
started = true;
this.registerNewAccount = registerNewAccount;
runOnConnectionThread(new Runnable() {
@Override
public void run() {
createConnection(fqdn, port, useSRVLookup);
}
});
}
/**
* Stop connection.
* <p/>
* start MUST BE CALLED FIRST.
*/
void shutdown() {
executorService.shutdownNow();
}
/**
* Submit task to be executed in connection thread.
*
* @param runnable
*/
private void runOnConnectionThread(final Runnable runnable) {
executorService.submit(new Runnable() {
@Override
public void run() {
if (!connectionItem.isManaged(ConnectionThread.this))
return;
try {
runnable.run();
} catch (RuntimeException e) {
LogManager.exception(connectionItem, e);
connectionClosedOnError(e);
}
}
});
}
/**
* Commit changes received from connection thread in UI thread.
*
* @param runnable
*/
private void runOnUiThread(final Runnable runnable) {
Application.getInstance().runOnUiThread(new Runnable() {
@Override
public void run() {
if (!connectionItem.isManaged(ConnectionThread.this))
return;
runnable.run();
}
});
}
}