192 lines
6.8 KiB
Java
192 lines
6.8 KiB
Java
package okhttp3.internal.platform;
|
|
|
|
import java.io.IOException;
|
|
import java.lang.reflect.Field;
|
|
import java.net.InetSocketAddress;
|
|
import java.net.Socket;
|
|
import java.security.NoSuchAlgorithmException;
|
|
import java.security.Security;
|
|
import java.util.ArrayList;
|
|
import java.util.List;
|
|
import java.util.logging.Level;
|
|
import java.util.logging.Logger;
|
|
import javax.net.ssl.SSLContext;
|
|
import javax.net.ssl.SSLSocket;
|
|
import javax.net.ssl.SSLSocketFactory;
|
|
import javax.net.ssl.X509TrustManager;
|
|
import okhttp3.OkHttpClient;
|
|
import okhttp3.Protocol;
|
|
import okhttp3.internal.tls.BasicCertificateChainCleaner;
|
|
import okhttp3.internal.tls.BasicTrustRootIndex;
|
|
import okhttp3.internal.tls.CertificateChainCleaner;
|
|
import okhttp3.internal.tls.TrustRootIndex;
|
|
import okio.Buffer;
|
|
|
|
/* loaded from: classes2.dex */
|
|
public class Platform {
|
|
public static final int INFO = 4;
|
|
public static final int WARN = 5;
|
|
private static final Platform PLATFORM = findPlatform();
|
|
private static final Logger logger = Logger.getLogger(OkHttpClient.class.getName());
|
|
|
|
public static List<String> alpnProtocolNames(List<Protocol> list) {
|
|
ArrayList arrayList = new ArrayList(list.size());
|
|
int size = list.size();
|
|
for (int i = 0; i < size; i++) {
|
|
Protocol protocol = list.get(i);
|
|
if (protocol != Protocol.HTTP_1_0) {
|
|
arrayList.add(protocol.toString());
|
|
}
|
|
}
|
|
return arrayList;
|
|
}
|
|
|
|
static byte[] concatLengthPrefixed(List<Protocol> list) {
|
|
Buffer buffer = new Buffer();
|
|
int size = list.size();
|
|
for (int i = 0; i < size; i++) {
|
|
Protocol protocol = list.get(i);
|
|
if (protocol != Protocol.HTTP_1_0) {
|
|
buffer.writeByte(protocol.toString().length());
|
|
buffer.writeUtf8(protocol.toString());
|
|
}
|
|
}
|
|
return buffer.readByteArray();
|
|
}
|
|
|
|
private static Platform findPlatform() {
|
|
Platform buildIfSupported;
|
|
Platform buildIfSupported2 = AndroidPlatform.buildIfSupported();
|
|
if (buildIfSupported2 != null) {
|
|
return buildIfSupported2;
|
|
}
|
|
if (isConscryptPreferred() && (buildIfSupported = ConscryptPlatform.buildIfSupported()) != null) {
|
|
return buildIfSupported;
|
|
}
|
|
Jdk9Platform buildIfSupported3 = Jdk9Platform.buildIfSupported();
|
|
if (buildIfSupported3 != null) {
|
|
return buildIfSupported3;
|
|
}
|
|
Platform buildIfSupported4 = JdkWithJettyBootPlatform.buildIfSupported();
|
|
return buildIfSupported4 != null ? buildIfSupported4 : new Platform();
|
|
}
|
|
|
|
public static Platform get() {
|
|
return PLATFORM;
|
|
}
|
|
|
|
public static boolean isConscryptPreferred() {
|
|
if ("conscrypt".equals(System.getProperty("okhttp.platform"))) {
|
|
return true;
|
|
}
|
|
return "Conscrypt".equals(Security.getProviders()[0].getName());
|
|
}
|
|
|
|
static <T> T readFieldOrNull(Object obj, Class<T> cls, String str) {
|
|
Object readFieldOrNull;
|
|
for (Class<?> cls2 = obj.getClass(); cls2 != Object.class; cls2 = cls2.getSuperclass()) {
|
|
try {
|
|
Field declaredField = cls2.getDeclaredField(str);
|
|
declaredField.setAccessible(true);
|
|
Object obj2 = declaredField.get(obj);
|
|
if (obj2 != null && cls.isInstance(obj2)) {
|
|
return cls.cast(obj2);
|
|
}
|
|
return null;
|
|
} catch (IllegalAccessException unused) {
|
|
throw new AssertionError();
|
|
} catch (NoSuchFieldException unused2) {
|
|
}
|
|
}
|
|
if (str.equals("delegate") || (readFieldOrNull = readFieldOrNull(obj, Object.class, "delegate")) == null) {
|
|
return null;
|
|
}
|
|
return (T) readFieldOrNull(readFieldOrNull, cls, str);
|
|
}
|
|
|
|
public void afterHandshake(SSLSocket sSLSocket) {
|
|
}
|
|
|
|
public CertificateChainCleaner buildCertificateChainCleaner(X509TrustManager x509TrustManager) {
|
|
return new BasicCertificateChainCleaner(buildTrustRootIndex(x509TrustManager));
|
|
}
|
|
|
|
public TrustRootIndex buildTrustRootIndex(X509TrustManager x509TrustManager) {
|
|
return new BasicTrustRootIndex(x509TrustManager.getAcceptedIssuers());
|
|
}
|
|
|
|
public void configureSslSocketFactory(SSLSocketFactory sSLSocketFactory) {
|
|
}
|
|
|
|
public void configureTlsExtensions(SSLSocket sSLSocket, String str, List<Protocol> list) {
|
|
}
|
|
|
|
public void connectSocket(Socket socket, InetSocketAddress inetSocketAddress, int i) throws IOException {
|
|
socket.connect(inetSocketAddress, i);
|
|
}
|
|
|
|
public String getPrefix() {
|
|
return "OkHttp";
|
|
}
|
|
|
|
public SSLContext getSSLContext() {
|
|
if ("1.7".equals(System.getProperty("java.specification.version"))) {
|
|
try {
|
|
return SSLContext.getInstance("TLSv1.2");
|
|
} catch (NoSuchAlgorithmException unused) {
|
|
}
|
|
}
|
|
try {
|
|
return SSLContext.getInstance("TLS");
|
|
} catch (NoSuchAlgorithmException e) {
|
|
throw new IllegalStateException("No TLS provider", e);
|
|
}
|
|
}
|
|
|
|
public String getSelectedProtocol(SSLSocket sSLSocket) {
|
|
return null;
|
|
}
|
|
|
|
public Object getStackTraceForCloseable(String str) {
|
|
if (logger.isLoggable(Level.FINE)) {
|
|
return new Throwable(str);
|
|
}
|
|
return null;
|
|
}
|
|
|
|
public boolean isCleartextTrafficPermitted(String str) {
|
|
return true;
|
|
}
|
|
|
|
public void log(int i, String str, Throwable th) {
|
|
logger.log(i == 5 ? Level.WARNING : Level.INFO, str, th);
|
|
}
|
|
|
|
public void logCloseableLeak(String str, Object obj) {
|
|
if (obj == null) {
|
|
str = str + " To see where this was allocated, set the OkHttpClient logger level to FINE: Logger.getLogger(OkHttpClient.class.getName()).setLevel(Level.FINE);";
|
|
}
|
|
log(5, str, (Throwable) obj);
|
|
}
|
|
|
|
protected X509TrustManager trustManager(SSLSocketFactory sSLSocketFactory) {
|
|
try {
|
|
Object readFieldOrNull = readFieldOrNull(sSLSocketFactory, Class.forName("sun.security.ssl.SSLContextImpl"), "context");
|
|
if (readFieldOrNull == null) {
|
|
return null;
|
|
}
|
|
return (X509TrustManager) readFieldOrNull(readFieldOrNull, X509TrustManager.class, "trustManager");
|
|
} catch (ClassNotFoundException unused) {
|
|
return null;
|
|
}
|
|
}
|
|
|
|
public CertificateChainCleaner buildCertificateChainCleaner(SSLSocketFactory sSLSocketFactory) {
|
|
X509TrustManager trustManager = trustManager(sSLSocketFactory);
|
|
if (trustManager != null) {
|
|
return buildCertificateChainCleaner(trustManager);
|
|
}
|
|
throw new IllegalStateException("Unable to extract the trust manager on " + get() + ", sslSocketFactory is " + sSLSocketFactory.getClass());
|
|
}
|
|
}
|