throw proper exception before changing account into error state

This commit is contained in:
Daniel Gultsch 2015-05-08 06:30:06 +02:00
parent 90b74d4b80
commit c4a4dd2392
1 changed files with 31 additions and 11 deletions

View File

@ -167,7 +167,7 @@ public class XmppConnection implements Runnable {
try { try {
String srvRecordServer; String srvRecordServer;
try { try {
srvRecordServer=IDN.toASCII(namePort.getString("name")); srvRecordServer = IDN.toASCII(namePort.getString("name"));
} catch (final IllegalArgumentException e) { } catch (final IllegalArgumentException e) {
// TODO: Handle me?` // TODO: Handle me?`
srvRecordServer = ""; srvRecordServer = "";
@ -224,6 +224,12 @@ public class XmppConnection implements Runnable {
if (socket.isConnected()) { if (socket.isConnected()) {
socket.close(); socket.close();
} }
} catch (final IncompatibleServerException e) {
this.changeStatus(Account.State.INCOMPATIBLE_SERVER);
} catch (final SecurityException e) {
this.changeStatus(Account.State.SECURITY_ERROR);
} catch (final UnauthorizedException e) {
this.changeStatus(Account.State.UNAUTHORIZED);
} catch (final UnknownHostException | ConnectException e) { } catch (final UnknownHostException | ConnectException e) {
this.changeStatus(Account.State.SERVER_NOT_FOUND); this.changeStatus(Account.State.SERVER_NOT_FOUND);
} catch (final IOException | XmlPullParserException | NoSuchAlgorithmException e) { } catch (final IOException | XmlPullParserException | NoSuchAlgorithmException e) {
@ -231,6 +237,13 @@ public class XmppConnection implements Runnable {
this.changeStatus(Account.State.OFFLINE); this.changeStatus(Account.State.OFFLINE);
this.attempt--; //don't count attempt when reconnecting instantly anyway this.attempt--; //don't count attempt when reconnecting instantly anyway
} finally { } finally {
if (socket != null) {
try {
socket.close();
} catch (IOException e) {
}
}
if (wakeLock.isHeld()) { if (wakeLock.isHeld()) {
try { try {
wakeLock.release(); wakeLock.release();
@ -279,8 +292,7 @@ public class XmppConnection implements Runnable {
processStream(tagReader.readTag()); processStream(tagReader.readTag());
break; break;
} else if (nextTag.isStart("failure")) { } else if (nextTag.isStart("failure")) {
tagReader.readElement(nextTag); throw new UnauthorizedException();
changeStatus(Account.State.UNAUTHORIZED);
} else if (nextTag.isStart("challenge")) { } else if (nextTag.isStart("challenge")) {
final String challenge = tagReader.readElement(nextTag).getContent(); final String challenge = tagReader.readElement(nextTag).getContent();
final Element response = new Element("response"); final Element response = new Element("response");
@ -542,8 +554,7 @@ public class XmppConnection implements Runnable {
if (!verifier.verify(account.getServer().getDomainpart(),sslSocket.getSession())) { if (!verifier.verify(account.getServer().getDomainpart(),sslSocket.getSession())) {
Log.d(Config.LOGTAG,account.getJid().toBareJid()+": TLS certificate verification failed"); Log.d(Config.LOGTAG,account.getJid().toBareJid()+": TLS certificate verification failed");
disconnect(true); throw new SecurityException();
changeStatus(Account.State.SECURITY_ERROR);
} }
tagReader.setInputStream(sslSocket.getInputStream()); tagReader.setInputStream(sslSocket.getInputStream());
tagWriter.setOutputStream(sslSocket.getOutputStream()); tagWriter.setOutputStream(sslSocket.getOutputStream());
@ -554,8 +565,7 @@ public class XmppConnection implements Runnable {
sslSocket.close(); sslSocket.close();
} catch (final NoSuchAlgorithmException | KeyManagementException e1) { } catch (final NoSuchAlgorithmException | KeyManagementException e1) {
Log.d(Config.LOGTAG,account.getJid().toBareJid()+": TLS certificate verification failed"); Log.d(Config.LOGTAG,account.getJid().toBareJid()+": TLS certificate verification failed");
disconnect(true); throw new SecurityException();
changeStatus(Account.State.SECURITY_ERROR);
} }
} }
@ -594,8 +604,7 @@ public class XmppConnection implements Runnable {
" has lower priority (" + String.valueOf(saslMechanism.getPriority()) + " has lower priority (" + String.valueOf(saslMechanism.getPriority()) +
") than pinned priority (" + keys.getInt(Account.PINNED_MECHANISM_KEY) + ") than pinned priority (" + keys.getInt(Account.PINNED_MECHANISM_KEY) +
"). Possible downgrade attack?"); "). Possible downgrade attack?");
disconnect(true); throw new SecurityException();
changeStatus(Account.State.SECURITY_ERROR);
} }
} catch (final JSONException e) { } catch (final JSONException e) {
Log.d(Config.LOGTAG, "Parse error while checking pinned auth mechanism"); Log.d(Config.LOGTAG, "Parse error while checking pinned auth mechanism");
@ -607,8 +616,7 @@ public class XmppConnection implements Runnable {
} }
tagWriter.writeElement(auth); tagWriter.writeElement(auth);
} else { } else {
disconnect(true); throw new IncompatibleServerException();
changeStatus(Account.State.INCOMPATIBLE_SERVER);
} }
} else if (this.streamFeatures.hasChild("sm", "urn:xmpp:sm:" } else if (this.streamFeatures.hasChild("sm", "urn:xmpp:sm:"
+ smVersion) + smVersion)
@ -1098,6 +1106,18 @@ public class XmppConnection implements Runnable {
public final ArrayList<Pair<String,String>> identities = new ArrayList<>(); public final ArrayList<Pair<String,String>> identities = new ArrayList<>();
} }
private class UnauthorizedException extends IOException {
}
private class SecurityException extends IOException {
}
private class IncompatibleServerException extends IOException {
}
public class Features { public class Features {
XmppConnection connection; XmppConnection connection;
private boolean carbonsEnabled = false; private boolean carbonsEnabled = false;