1
0
mirror of https://github.com/moparisthebest/Yaaic synced 2024-11-24 18:02:20 -05:00

Set SNI hostname if we can for TLS connections

This commit is contained in:
Travis Burtrum 2015-07-24 23:47:48 -04:00
parent 2282784df2
commit ebbdc383ec

View File

@ -165,6 +165,7 @@ public abstract class PircBot implements ReplyConstants {
context.init(null, new X509TrustManager[] { new NaiveTrustManager() }, null); context.init(null, new X509TrustManager[] { new NaiveTrustManager() }, null);
SSLSocketFactory factory = context.getSocketFactory(); SSLSocketFactory factory = context.getSocketFactory();
SSLSocket ssocket = (SSLSocket) factory.createSocket(hostname, port); SSLSocket ssocket = (SSLSocket) factory.createSocket(hostname, port);
setSNIHost(factory, ssocket, hostname);
ssocket.startHandshake(); ssocket.startHandshake();
_socket = ssocket; _socket = ssocket;
} }
@ -265,6 +266,17 @@ public abstract class PircBot implements ReplyConstants {
this.onConnect(); this.onConnect();
} }
private void setSNIHost(final SSLSocketFactory factory, final SSLSocket socket, final String hostname) {
if (factory instanceof android.net.SSLCertificateSocketFactory && android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.JELLY_BEAN_MR1) {
((android.net.SSLCertificateSocketFactory)factory).setHostname(socket, hostname);
} else {
try {
socket.getClass().getMethod("setHostname", String.class).invoke(socket, hostname);
} catch (Throwable e) {
// ignore any error, we just can't set the hostname...
}
}
}
/** /**
* Reconnects to the IRC server that we were previously connected to. * Reconnects to the IRC server that we were previously connected to.