Improved connect fallback

pull/172/head
M66B 5 years ago
parent 961212e45b
commit 08098c2285

@ -12,7 +12,6 @@ import androidx.preference.PreferenceManager;
import com.sun.mail.imap.IMAPFolder;
import com.sun.mail.imap.IMAPStore;
import com.sun.mail.smtp.SMTPTransport;
import com.sun.mail.util.MailConnectException;
import net.openid.appauth.AuthState;
import net.openid.appauth.AuthorizationException;
@ -219,7 +218,10 @@ public class MailService implements AutoCloseable {
}
}
public String connect(String host, int port, int auth, String provider, String user, String password, String fingerprint) throws MessagingException {
public String connect(
String host, int port,
int auth, String provider, String user, String password,
String fingerprint) throws MessagingException {
SSLSocketFactoryService factory = null;
try {
factory = new SSLSocketFactoryService(host, insecure, fingerprint);
@ -237,16 +239,12 @@ public class MailService implements AutoCloseable {
if (auth == AUTH_TYPE_GMAIL || auth == AUTH_TYPE_OAUTH)
properties.put("mail." + protocol + ".auth.mechanisms", "XOAUTH2");
//if (BuildConfig.DEBUG)
// throw new MailConnectException(
// new SocketConnectException("Debug", new Exception("Test"), host, port, 0));
if (auth == AUTH_TYPE_OAUTH) {
AuthState authState = OAuthRefresh(context, provider, password);
_connect(context, host, port, user, authState.getAccessToken(), factory);
connect(host, port, user, authState.getAccessToken(), factory);
return authState.jsonSerializeString();
} else {
_connect(context, host, port, user, password, factory);
connect(host, port, user, password, factory);
return null;
}
} catch (AuthenticationFailedException ex) {
@ -264,7 +262,7 @@ public class MailService implements AutoCloseable {
if (token == null)
throw new IllegalArgumentException("No token on refresh");
_connect(context, host, port, user, token, factory);
connect(host, port, user, token, factory);
return token;
}
@ -275,11 +273,34 @@ public class MailService implements AutoCloseable {
}
else if (auth == AUTH_TYPE_OAUTH) {
AuthState authState = OAuthRefresh(context, provider, password);
_connect(context, host, port, user, authState.getAccessToken(), factory);
connect(host, port, user, authState.getAccessToken(), factory);
return authState.jsonSerializeString();
} else
throw ex;
} catch (MailConnectException ex) {
}
}
private void connect(
String host, int port, String user, String password,
SSLSocketFactoryService factory) throws MessagingException {
try {
//if (BuildConfig.DEBUG)
// throw new MailConnectException(
// new SocketConnectException("Debug", new IOException("Test"), host, port, 0));
_connect(host, port, user, password, factory);
} catch (MessagingException ex) {
boolean ioError = false;
Throwable ce = ex;
while (ce != null) {
if (factory != null && ce instanceof CertificateException)
throw new UntrustedException(factory.getFingerPrint(), ex);
if (ce instanceof IOException)
ioError = true;
ce = ce.getCause();
}
if (ioError) {
try {
// Some devices resolve IPv6 addresses while not having IPv6 connectivity
InetAddress[] iaddrs = InetAddress.getAllByName(host);
@ -288,24 +309,23 @@ public class MailService implements AutoCloseable {
for (InetAddress iaddr : iaddrs)
try {
Log.i("Falling back to " + iaddr.getHostAddress());
_connect(context, iaddr.getHostAddress(), port, user, password, factory);
return null;
_connect(iaddr.getHostAddress(), port, user, password, factory);
return;
} catch (MessagingException ex1) {
Log.w(ex1);
}
} catch (Throwable ex1) {
Log.w(ex1);
}
}
throw ex;
}
}
private void _connect(
Context context,
String host, int port, String user, String password,
SSLSocketFactoryService factory) throws MessagingException {
try {
isession = Session.getInstance(properties, null);
isession.setDebug(debug);
//System.setProperty("mail.socket.debug", Boolean.toString(debug));
@ -377,17 +397,6 @@ public class MailService implements AutoCloseable {
}
} else
throw new NoSuchProviderException(protocol);
} catch (MessagingException ex) {
if (factory != null) {
Throwable ce = ex;
while (ce != null) {
if (ce instanceof CertificateException)
throw new UntrustedException(factory.getFingerPrint(), ex);
ce = ce.getCause();
}
}
throw ex;
}
}
private static class ErrorHolder {

Loading…
Cancel
Save