Last active
July 5, 2020 06:07
-
-
Save aslamanver/d0c6e0acc17414ec9448d13844dc8bce to your computer and use it in GitHub Desktop.
Socket.IO Android - HTTPS/SSL - Enable HTTPS secure connection to Socket.IO and Android
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.security.KeyManagementException; | |
import java.security.NoSuchAlgorithmException; | |
import java.security.cert.CertificateException; | |
import java.security.cert.X509Certificate; | |
import javax.net.ssl.HostnameVerifier; | |
import javax.net.ssl.SSLContext; | |
import javax.net.ssl.SSLSession; | |
import javax.net.ssl.TrustManager; | |
import javax.net.ssl.X509TrustManager; | |
import io.socket.client.IO; | |
import okhttp3.OkHttpClient; | |
public class SocketSSL { | |
public static OkHttpClient getOkHttpClient() { | |
try { | |
SSLContext sc = SSLContext.getInstance("SSL"); | |
sc.init(null, new TrustManager[]{new X509TrustManager() { | |
@Override | |
public void checkClientTrusted(X509Certificate[] chain, String authType) throws CertificateException { | |
} | |
@Override | |
public void checkServerTrusted(X509Certificate[] chain, String authType) throws CertificateException { | |
} | |
@Override | |
public X509Certificate[] getAcceptedIssuers() { | |
return new X509Certificate[0]; | |
} | |
}}, new java.security.SecureRandom()); | |
OkHttpClient.Builder builder = new OkHttpClient.Builder(); | |
builder.hostnameVerifier(new HostnameVerifier() { | |
@Override | |
public boolean verify(String hostname, SSLSession session) { | |
return true; | |
} | |
}); | |
builder.sslSocketFactory(sc.getSocketFactory(), new X509TrustManager() { | |
@Override | |
public void checkClientTrusted(X509Certificate[] chain, String authType) throws CertificateException { | |
} | |
@Override | |
public void checkServerTrusted(X509Certificate[] chain, String authType) throws CertificateException { | |
} | |
@Override | |
public X509Certificate[] getAcceptedIssuers() { | |
return new X509Certificate[0]; | |
} | |
}); | |
return builder.build(); | |
} catch (NoSuchAlgorithmException | KeyManagementException ex) { | |
ex.printStackTrace(); | |
} | |
return null; | |
} | |
public static void set(IO.Options options) { | |
OkHttpClient okHttpClient = getOkHttpClient(); | |
IO.setDefaultOkHttpWebSocketFactory(okHttpClient); | |
IO.setDefaultOkHttpCallFactory(okHttpClient); | |
options.callFactory = okHttpClient; | |
options.webSocketFactory = okHttpClient; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment