Initial commit
This commit is contained in:
379
sources/okhttp3/internal/platform/AndroidPlatform.java
Normal file
379
sources/okhttp3/internal/platform/AndroidPlatform.java
Normal file
@@ -0,0 +1,379 @@
|
||||
package okhttp3.internal.platform;
|
||||
|
||||
import android.os.Build;
|
||||
import android.util.Log;
|
||||
import java.io.IOException;
|
||||
import java.lang.reflect.InvocationTargetException;
|
||||
import java.lang.reflect.Method;
|
||||
import java.net.InetSocketAddress;
|
||||
import java.net.Socket;
|
||||
import java.security.NoSuchAlgorithmException;
|
||||
import java.security.Security;
|
||||
import java.security.cert.Certificate;
|
||||
import java.security.cert.TrustAnchor;
|
||||
import java.security.cert.X509Certificate;
|
||||
import java.util.List;
|
||||
import javax.net.ssl.SSLContext;
|
||||
import javax.net.ssl.SSLPeerUnverifiedException;
|
||||
import javax.net.ssl.SSLSocket;
|
||||
import javax.net.ssl.SSLSocketFactory;
|
||||
import javax.net.ssl.X509TrustManager;
|
||||
import okhttp3.Protocol;
|
||||
import okhttp3.internal.Util;
|
||||
import okhttp3.internal.tls.CertificateChainCleaner;
|
||||
import okhttp3.internal.tls.TrustRootIndex;
|
||||
|
||||
/* loaded from: classes2.dex */
|
||||
class AndroidPlatform extends Platform {
|
||||
private static final int MAX_LOG_LENGTH = 4000;
|
||||
private final CloseGuard closeGuard = CloseGuard.get();
|
||||
private final OptionalMethod<Socket> getAlpnSelectedProtocol;
|
||||
private final OptionalMethod<Socket> setAlpnProtocols;
|
||||
private final OptionalMethod<Socket> setHostname;
|
||||
private final OptionalMethod<Socket> setUseSessionTickets;
|
||||
private final Class<?> sslParametersClass;
|
||||
|
||||
static final class AndroidCertificateChainCleaner extends CertificateChainCleaner {
|
||||
private final Method checkServerTrusted;
|
||||
private final Object x509TrustManagerExtensions;
|
||||
|
||||
AndroidCertificateChainCleaner(Object obj, Method method) {
|
||||
this.x509TrustManagerExtensions = obj;
|
||||
this.checkServerTrusted = method;
|
||||
}
|
||||
|
||||
@Override // okhttp3.internal.tls.CertificateChainCleaner
|
||||
public List<Certificate> clean(List<Certificate> list, String str) throws SSLPeerUnverifiedException {
|
||||
try {
|
||||
return (List) this.checkServerTrusted.invoke(this.x509TrustManagerExtensions, (X509Certificate[]) list.toArray(new X509Certificate[list.size()]), "RSA", str);
|
||||
} catch (IllegalAccessException e) {
|
||||
throw new AssertionError(e);
|
||||
} catch (InvocationTargetException e2) {
|
||||
SSLPeerUnverifiedException sSLPeerUnverifiedException = new SSLPeerUnverifiedException(e2.getMessage());
|
||||
sSLPeerUnverifiedException.initCause(e2);
|
||||
throw sSLPeerUnverifiedException;
|
||||
}
|
||||
}
|
||||
|
||||
public boolean equals(Object obj) {
|
||||
return obj instanceof AndroidCertificateChainCleaner;
|
||||
}
|
||||
|
||||
public int hashCode() {
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
static final class AndroidTrustRootIndex implements TrustRootIndex {
|
||||
private final Method findByIssuerAndSignatureMethod;
|
||||
private final X509TrustManager trustManager;
|
||||
|
||||
AndroidTrustRootIndex(X509TrustManager x509TrustManager, Method method) {
|
||||
this.findByIssuerAndSignatureMethod = method;
|
||||
this.trustManager = x509TrustManager;
|
||||
}
|
||||
|
||||
public boolean equals(Object obj) {
|
||||
if (obj == this) {
|
||||
return true;
|
||||
}
|
||||
if (!(obj instanceof AndroidTrustRootIndex)) {
|
||||
return false;
|
||||
}
|
||||
AndroidTrustRootIndex androidTrustRootIndex = (AndroidTrustRootIndex) obj;
|
||||
return this.trustManager.equals(androidTrustRootIndex.trustManager) && this.findByIssuerAndSignatureMethod.equals(androidTrustRootIndex.findByIssuerAndSignatureMethod);
|
||||
}
|
||||
|
||||
@Override // okhttp3.internal.tls.TrustRootIndex
|
||||
public X509Certificate findByIssuerAndSignature(X509Certificate x509Certificate) {
|
||||
try {
|
||||
TrustAnchor trustAnchor = (TrustAnchor) this.findByIssuerAndSignatureMethod.invoke(this.trustManager, x509Certificate);
|
||||
if (trustAnchor != null) {
|
||||
return trustAnchor.getTrustedCert();
|
||||
}
|
||||
return null;
|
||||
} catch (IllegalAccessException e) {
|
||||
throw Util.assertionError("unable to get issues and signature", e);
|
||||
} catch (InvocationTargetException unused) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
public int hashCode() {
|
||||
return this.trustManager.hashCode() + (this.findByIssuerAndSignatureMethod.hashCode() * 31);
|
||||
}
|
||||
}
|
||||
|
||||
static final class CloseGuard {
|
||||
private final Method getMethod;
|
||||
private final Method openMethod;
|
||||
private final Method warnIfOpenMethod;
|
||||
|
||||
CloseGuard(Method method, Method method2, Method method3) {
|
||||
this.getMethod = method;
|
||||
this.openMethod = method2;
|
||||
this.warnIfOpenMethod = method3;
|
||||
}
|
||||
|
||||
static CloseGuard get() {
|
||||
Method method;
|
||||
Method method2;
|
||||
Method method3 = null;
|
||||
try {
|
||||
Class<?> cls = Class.forName("dalvik.system.CloseGuard");
|
||||
Method method4 = cls.getMethod("get", new Class[0]);
|
||||
method2 = cls.getMethod("open", String.class);
|
||||
method = cls.getMethod("warnIfOpen", new Class[0]);
|
||||
method3 = method4;
|
||||
} catch (Exception unused) {
|
||||
method = null;
|
||||
method2 = null;
|
||||
}
|
||||
return new CloseGuard(method3, method2, method);
|
||||
}
|
||||
|
||||
Object createAndOpen(String str) {
|
||||
Method method = this.getMethod;
|
||||
if (method != null) {
|
||||
try {
|
||||
Object invoke = method.invoke(null, new Object[0]);
|
||||
this.openMethod.invoke(invoke, str);
|
||||
return invoke;
|
||||
} catch (Exception unused) {
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
boolean warnIfOpen(Object obj) {
|
||||
if (obj == null) {
|
||||
return false;
|
||||
}
|
||||
try {
|
||||
this.warnIfOpenMethod.invoke(obj, new Object[0]);
|
||||
return true;
|
||||
} catch (Exception unused) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
AndroidPlatform(Class<?> cls, OptionalMethod<Socket> optionalMethod, OptionalMethod<Socket> optionalMethod2, OptionalMethod<Socket> optionalMethod3, OptionalMethod<Socket> optionalMethod4) {
|
||||
this.sslParametersClass = cls;
|
||||
this.setUseSessionTickets = optionalMethod;
|
||||
this.setHostname = optionalMethod2;
|
||||
this.getAlpnSelectedProtocol = optionalMethod3;
|
||||
this.setAlpnProtocols = optionalMethod4;
|
||||
}
|
||||
|
||||
private boolean api23IsCleartextTrafficPermitted(String str, Class<?> cls, Object obj) throws InvocationTargetException, IllegalAccessException {
|
||||
try {
|
||||
return ((Boolean) cls.getMethod("isCleartextTrafficPermitted", new Class[0]).invoke(obj, new Object[0])).booleanValue();
|
||||
} catch (NoSuchMethodException unused) {
|
||||
return super.isCleartextTrafficPermitted(str);
|
||||
}
|
||||
}
|
||||
|
||||
private boolean api24IsCleartextTrafficPermitted(String str, Class<?> cls, Object obj) throws InvocationTargetException, IllegalAccessException {
|
||||
try {
|
||||
return ((Boolean) cls.getMethod("isCleartextTrafficPermitted", String.class).invoke(obj, str)).booleanValue();
|
||||
} catch (NoSuchMethodException unused) {
|
||||
return api23IsCleartextTrafficPermitted(str, cls, obj);
|
||||
}
|
||||
}
|
||||
|
||||
public static Platform buildIfSupported() {
|
||||
Class<?> cls;
|
||||
OptionalMethod optionalMethod;
|
||||
OptionalMethod optionalMethod2;
|
||||
try {
|
||||
try {
|
||||
cls = Class.forName("com.android.org.conscrypt.SSLParametersImpl");
|
||||
} catch (ClassNotFoundException unused) {
|
||||
cls = Class.forName("org.apache.harmony.xnet.provider.jsse.SSLParametersImpl");
|
||||
}
|
||||
Class<?> cls2 = cls;
|
||||
OptionalMethod optionalMethod3 = new OptionalMethod(null, "setUseSessionTickets", Boolean.TYPE);
|
||||
OptionalMethod optionalMethod4 = new OptionalMethod(null, "setHostname", String.class);
|
||||
if (supportsAlpn()) {
|
||||
OptionalMethod optionalMethod5 = new OptionalMethod(byte[].class, "getAlpnSelectedProtocol", new Class[0]);
|
||||
optionalMethod2 = new OptionalMethod(null, "setAlpnProtocols", byte[].class);
|
||||
optionalMethod = optionalMethod5;
|
||||
} else {
|
||||
optionalMethod = null;
|
||||
optionalMethod2 = null;
|
||||
}
|
||||
return new AndroidPlatform(cls2, optionalMethod3, optionalMethod4, optionalMethod, optionalMethod2);
|
||||
} catch (ClassNotFoundException unused2) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
private static boolean supportsAlpn() {
|
||||
if (Security.getProvider("GMSCore_OpenSSL") != null) {
|
||||
return true;
|
||||
}
|
||||
try {
|
||||
Class.forName("android.net.Network");
|
||||
return true;
|
||||
} catch (ClassNotFoundException unused) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
@Override // okhttp3.internal.platform.Platform
|
||||
public CertificateChainCleaner buildCertificateChainCleaner(X509TrustManager x509TrustManager) {
|
||||
try {
|
||||
Class<?> cls = Class.forName("android.net.http.X509TrustManagerExtensions");
|
||||
return new AndroidCertificateChainCleaner(cls.getConstructor(X509TrustManager.class).newInstance(x509TrustManager), cls.getMethod("checkServerTrusted", X509Certificate[].class, String.class, String.class));
|
||||
} catch (Exception unused) {
|
||||
return super.buildCertificateChainCleaner(x509TrustManager);
|
||||
}
|
||||
}
|
||||
|
||||
@Override // okhttp3.internal.platform.Platform
|
||||
public TrustRootIndex buildTrustRootIndex(X509TrustManager x509TrustManager) {
|
||||
try {
|
||||
Method declaredMethod = x509TrustManager.getClass().getDeclaredMethod("findTrustAnchorByIssuerAndSignature", X509Certificate.class);
|
||||
declaredMethod.setAccessible(true);
|
||||
return new AndroidTrustRootIndex(x509TrustManager, declaredMethod);
|
||||
} catch (NoSuchMethodException unused) {
|
||||
return super.buildTrustRootIndex(x509TrustManager);
|
||||
}
|
||||
}
|
||||
|
||||
@Override // okhttp3.internal.platform.Platform
|
||||
public void configureTlsExtensions(SSLSocket sSLSocket, String str, List<Protocol> list) {
|
||||
if (str != null) {
|
||||
this.setUseSessionTickets.invokeOptionalWithoutCheckedException(sSLSocket, true);
|
||||
this.setHostname.invokeOptionalWithoutCheckedException(sSLSocket, str);
|
||||
}
|
||||
OptionalMethod<Socket> optionalMethod = this.setAlpnProtocols;
|
||||
if (optionalMethod == null || !optionalMethod.isSupported(sSLSocket)) {
|
||||
return;
|
||||
}
|
||||
this.setAlpnProtocols.invokeWithoutCheckedException(sSLSocket, Platform.concatLengthPrefixed(list));
|
||||
}
|
||||
|
||||
@Override // okhttp3.internal.platform.Platform
|
||||
public void connectSocket(Socket socket, InetSocketAddress inetSocketAddress, int i) throws IOException {
|
||||
try {
|
||||
socket.connect(inetSocketAddress, i);
|
||||
} catch (AssertionError e) {
|
||||
if (!Util.isAndroidGetsocknameError(e)) {
|
||||
throw e;
|
||||
}
|
||||
throw new IOException(e);
|
||||
} catch (ClassCastException e2) {
|
||||
if (Build.VERSION.SDK_INT != 26) {
|
||||
throw e2;
|
||||
}
|
||||
IOException iOException = new IOException("Exception in connect");
|
||||
iOException.initCause(e2);
|
||||
throw iOException;
|
||||
} catch (SecurityException e3) {
|
||||
IOException iOException2 = new IOException("Exception in connect");
|
||||
iOException2.initCause(e3);
|
||||
throw iOException2;
|
||||
}
|
||||
}
|
||||
|
||||
@Override // okhttp3.internal.platform.Platform
|
||||
public SSLContext getSSLContext() {
|
||||
int i = Build.VERSION.SDK_INT;
|
||||
if (i >= 16 && i < 22) {
|
||||
try {
|
||||
return SSLContext.getInstance("TLSv1.2");
|
||||
} catch (NoSuchAlgorithmException unused) {
|
||||
}
|
||||
}
|
||||
try {
|
||||
return SSLContext.getInstance("TLS");
|
||||
} catch (NoSuchAlgorithmException e) {
|
||||
throw new IllegalStateException("No TLS provider", e);
|
||||
}
|
||||
}
|
||||
|
||||
@Override // okhttp3.internal.platform.Platform
|
||||
public String getSelectedProtocol(SSLSocket sSLSocket) {
|
||||
byte[] bArr;
|
||||
OptionalMethod<Socket> optionalMethod = this.getAlpnSelectedProtocol;
|
||||
if (optionalMethod == null || !optionalMethod.isSupported(sSLSocket) || (bArr = (byte[]) this.getAlpnSelectedProtocol.invokeWithoutCheckedException(sSLSocket, new Object[0])) == null) {
|
||||
return null;
|
||||
}
|
||||
return new String(bArr, Util.UTF_8);
|
||||
}
|
||||
|
||||
@Override // okhttp3.internal.platform.Platform
|
||||
public Object getStackTraceForCloseable(String str) {
|
||||
return this.closeGuard.createAndOpen(str);
|
||||
}
|
||||
|
||||
@Override // okhttp3.internal.platform.Platform
|
||||
public boolean isCleartextTrafficPermitted(String str) {
|
||||
try {
|
||||
Class<?> cls = Class.forName("android.security.NetworkSecurityPolicy");
|
||||
return api24IsCleartextTrafficPermitted(str, cls, cls.getMethod("getInstance", new Class[0]).invoke(null, new Object[0]));
|
||||
} catch (ClassNotFoundException | NoSuchMethodException unused) {
|
||||
return super.isCleartextTrafficPermitted(str);
|
||||
} catch (IllegalAccessException e) {
|
||||
e = e;
|
||||
throw Util.assertionError("unable to determine cleartext support", e);
|
||||
} catch (IllegalArgumentException e2) {
|
||||
e = e2;
|
||||
throw Util.assertionError("unable to determine cleartext support", e);
|
||||
} catch (InvocationTargetException e3) {
|
||||
e = e3;
|
||||
throw Util.assertionError("unable to determine cleartext support", e);
|
||||
}
|
||||
}
|
||||
|
||||
@Override // okhttp3.internal.platform.Platform
|
||||
public void log(int i, String str, Throwable th) {
|
||||
int min;
|
||||
int i2 = i != 5 ? 3 : 5;
|
||||
if (th != null) {
|
||||
str = str + '\n' + Log.getStackTraceString(th);
|
||||
}
|
||||
int i3 = 0;
|
||||
int length = str.length();
|
||||
while (i3 < length) {
|
||||
int indexOf = str.indexOf(10, i3);
|
||||
if (indexOf == -1) {
|
||||
indexOf = length;
|
||||
}
|
||||
while (true) {
|
||||
min = Math.min(indexOf, i3 + MAX_LOG_LENGTH);
|
||||
Log.println(i2, "OkHttp", str.substring(i3, min));
|
||||
if (min >= indexOf) {
|
||||
break;
|
||||
} else {
|
||||
i3 = min;
|
||||
}
|
||||
}
|
||||
i3 = min + 1;
|
||||
}
|
||||
}
|
||||
|
||||
@Override // okhttp3.internal.platform.Platform
|
||||
public void logCloseableLeak(String str, Object obj) {
|
||||
if (this.closeGuard.warnIfOpen(obj)) {
|
||||
return;
|
||||
}
|
||||
log(5, str, null);
|
||||
}
|
||||
|
||||
@Override // okhttp3.internal.platform.Platform
|
||||
protected X509TrustManager trustManager(SSLSocketFactory sSLSocketFactory) {
|
||||
Object readFieldOrNull = Platform.readFieldOrNull(sSLSocketFactory, this.sslParametersClass, "sslParameters");
|
||||
if (readFieldOrNull == null) {
|
||||
try {
|
||||
readFieldOrNull = Platform.readFieldOrNull(sSLSocketFactory, Class.forName("com.google.android.gms.org.conscrypt.SSLParametersImpl", false, sSLSocketFactory.getClass().getClassLoader()), "sslParameters");
|
||||
} catch (ClassNotFoundException unused) {
|
||||
return super.trustManager(sSLSocketFactory);
|
||||
}
|
||||
}
|
||||
X509TrustManager x509TrustManager = (X509TrustManager) Platform.readFieldOrNull(readFieldOrNull, X509TrustManager.class, "x509TrustManager");
|
||||
return x509TrustManager != null ? x509TrustManager : (X509TrustManager) Platform.readFieldOrNull(readFieldOrNull, X509TrustManager.class, "trustManager");
|
||||
}
|
||||
}
|
84
sources/okhttp3/internal/platform/ConscryptPlatform.java
Normal file
84
sources/okhttp3/internal/platform/ConscryptPlatform.java
Normal file
@@ -0,0 +1,84 @@
|
||||
package okhttp3.internal.platform;
|
||||
|
||||
import java.security.NoSuchAlgorithmException;
|
||||
import java.security.Provider;
|
||||
import java.util.List;
|
||||
import javax.net.ssl.SSLContext;
|
||||
import javax.net.ssl.SSLSocket;
|
||||
import javax.net.ssl.SSLSocketFactory;
|
||||
import javax.net.ssl.X509TrustManager;
|
||||
import okhttp3.Protocol;
|
||||
import org.conscrypt.Conscrypt;
|
||||
import org.conscrypt.OpenSSLProvider;
|
||||
|
||||
/* loaded from: classes2.dex */
|
||||
public class ConscryptPlatform extends Platform {
|
||||
private ConscryptPlatform() {
|
||||
}
|
||||
|
||||
public static Platform buildIfSupported() {
|
||||
try {
|
||||
Class.forName("org.conscrypt.ConscryptEngineSocket");
|
||||
if (Conscrypt.isAvailable()) {
|
||||
return new ConscryptPlatform();
|
||||
}
|
||||
return null;
|
||||
} catch (ClassNotFoundException unused) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
private Provider getProvider() {
|
||||
return new OpenSSLProvider();
|
||||
}
|
||||
|
||||
@Override // okhttp3.internal.platform.Platform
|
||||
public void configureSslSocketFactory(SSLSocketFactory sSLSocketFactory) {
|
||||
if (Conscrypt.isConscrypt(sSLSocketFactory)) {
|
||||
Conscrypt.setUseEngineSocket(sSLSocketFactory, true);
|
||||
}
|
||||
}
|
||||
|
||||
@Override // okhttp3.internal.platform.Platform
|
||||
public void configureTlsExtensions(SSLSocket sSLSocket, String str, List<Protocol> list) {
|
||||
if (!Conscrypt.isConscrypt(sSLSocket)) {
|
||||
super.configureTlsExtensions(sSLSocket, str, list);
|
||||
return;
|
||||
}
|
||||
if (str != null) {
|
||||
Conscrypt.setUseSessionTickets(sSLSocket, true);
|
||||
Conscrypt.setHostname(sSLSocket, str);
|
||||
}
|
||||
Conscrypt.setApplicationProtocols(sSLSocket, (String[]) Platform.alpnProtocolNames(list).toArray(new String[0]));
|
||||
}
|
||||
|
||||
@Override // okhttp3.internal.platform.Platform
|
||||
public SSLContext getSSLContext() {
|
||||
try {
|
||||
return SSLContext.getInstance("TLS", getProvider());
|
||||
} catch (NoSuchAlgorithmException e) {
|
||||
throw new IllegalStateException("No TLS provider", e);
|
||||
}
|
||||
}
|
||||
|
||||
@Override // okhttp3.internal.platform.Platform
|
||||
public String getSelectedProtocol(SSLSocket sSLSocket) {
|
||||
return Conscrypt.isConscrypt(sSLSocket) ? Conscrypt.getApplicationProtocol(sSLSocket) : super.getSelectedProtocol(sSLSocket);
|
||||
}
|
||||
|
||||
@Override // okhttp3.internal.platform.Platform
|
||||
public X509TrustManager trustManager(SSLSocketFactory sSLSocketFactory) {
|
||||
if (!Conscrypt.isConscrypt(sSLSocketFactory)) {
|
||||
return super.trustManager(sSLSocketFactory);
|
||||
}
|
||||
try {
|
||||
Object readFieldOrNull = Platform.readFieldOrNull(sSLSocketFactory, Object.class, "sslParameters");
|
||||
if (readFieldOrNull != null) {
|
||||
return (X509TrustManager) Platform.readFieldOrNull(readFieldOrNull, X509TrustManager.class, "x509TrustManager");
|
||||
}
|
||||
return null;
|
||||
} catch (Exception e) {
|
||||
throw new UnsupportedOperationException("clientBuilder.sslSocketFactory(SSLSocketFactory) not supported on Conscrypt", e);
|
||||
}
|
||||
}
|
||||
}
|
63
sources/okhttp3/internal/platform/Jdk9Platform.java
Normal file
63
sources/okhttp3/internal/platform/Jdk9Platform.java
Normal file
@@ -0,0 +1,63 @@
|
||||
package okhttp3.internal.platform;
|
||||
|
||||
import java.lang.reflect.InvocationTargetException;
|
||||
import java.lang.reflect.Method;
|
||||
import java.util.List;
|
||||
import javax.net.ssl.SSLParameters;
|
||||
import javax.net.ssl.SSLSocket;
|
||||
import javax.net.ssl.SSLSocketFactory;
|
||||
import javax.net.ssl.X509TrustManager;
|
||||
import okhttp3.Protocol;
|
||||
import okhttp3.internal.Util;
|
||||
|
||||
/* loaded from: classes2.dex */
|
||||
final class Jdk9Platform extends Platform {
|
||||
final Method getProtocolMethod;
|
||||
final Method setProtocolMethod;
|
||||
|
||||
Jdk9Platform(Method method, Method method2) {
|
||||
this.setProtocolMethod = method;
|
||||
this.getProtocolMethod = method2;
|
||||
}
|
||||
|
||||
public static Jdk9Platform buildIfSupported() {
|
||||
try {
|
||||
return new Jdk9Platform(SSLParameters.class.getMethod("setApplicationProtocols", String[].class), SSLSocket.class.getMethod("getApplicationProtocol", new Class[0]));
|
||||
} catch (NoSuchMethodException unused) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
@Override // okhttp3.internal.platform.Platform
|
||||
public void configureTlsExtensions(SSLSocket sSLSocket, String str, List<Protocol> list) {
|
||||
try {
|
||||
SSLParameters sSLParameters = sSLSocket.getSSLParameters();
|
||||
List<String> alpnProtocolNames = Platform.alpnProtocolNames(list);
|
||||
this.setProtocolMethod.invoke(sSLParameters, alpnProtocolNames.toArray(new String[alpnProtocolNames.size()]));
|
||||
sSLSocket.setSSLParameters(sSLParameters);
|
||||
} catch (IllegalAccessException | InvocationTargetException e) {
|
||||
throw Util.assertionError("unable to set ssl parameters", e);
|
||||
}
|
||||
}
|
||||
|
||||
@Override // okhttp3.internal.platform.Platform
|
||||
public String getSelectedProtocol(SSLSocket sSLSocket) {
|
||||
try {
|
||||
String str = (String) this.getProtocolMethod.invoke(sSLSocket, new Object[0]);
|
||||
if (str == null) {
|
||||
return null;
|
||||
}
|
||||
if (str.equals("")) {
|
||||
return null;
|
||||
}
|
||||
return str;
|
||||
} catch (IllegalAccessException | InvocationTargetException e) {
|
||||
throw Util.assertionError("unable to get selected protocols", e);
|
||||
}
|
||||
}
|
||||
|
||||
@Override // okhttp3.internal.platform.Platform
|
||||
public X509TrustManager trustManager(SSLSocketFactory sSLSocketFactory) {
|
||||
throw new UnsupportedOperationException("clientBuilder.sslSocketFactory(SSLSocketFactory) not supported on JDK 9+");
|
||||
}
|
||||
}
|
120
sources/okhttp3/internal/platform/JdkWithJettyBootPlatform.java
Normal file
120
sources/okhttp3/internal/platform/JdkWithJettyBootPlatform.java
Normal file
@@ -0,0 +1,120 @@
|
||||
package okhttp3.internal.platform;
|
||||
|
||||
import java.lang.reflect.InvocationHandler;
|
||||
import java.lang.reflect.InvocationTargetException;
|
||||
import java.lang.reflect.Method;
|
||||
import java.lang.reflect.Proxy;
|
||||
import java.util.List;
|
||||
import javax.net.ssl.SSLSocket;
|
||||
import okhttp3.Protocol;
|
||||
import okhttp3.internal.Util;
|
||||
|
||||
/* loaded from: classes2.dex */
|
||||
class JdkWithJettyBootPlatform extends Platform {
|
||||
private final Class<?> clientProviderClass;
|
||||
private final Method getMethod;
|
||||
private final Method putMethod;
|
||||
private final Method removeMethod;
|
||||
private final Class<?> serverProviderClass;
|
||||
|
||||
private static class JettyNegoProvider implements InvocationHandler {
|
||||
private final List<String> protocols;
|
||||
String selected;
|
||||
boolean unsupported;
|
||||
|
||||
JettyNegoProvider(List<String> list) {
|
||||
this.protocols = list;
|
||||
}
|
||||
|
||||
@Override // java.lang.reflect.InvocationHandler
|
||||
public Object invoke(Object obj, Method method, Object[] objArr) throws Throwable {
|
||||
String name = method.getName();
|
||||
Class<?> returnType = method.getReturnType();
|
||||
if (objArr == null) {
|
||||
objArr = Util.EMPTY_STRING_ARRAY;
|
||||
}
|
||||
if (name.equals("supports") && Boolean.TYPE == returnType) {
|
||||
return true;
|
||||
}
|
||||
if (name.equals("unsupported") && Void.TYPE == returnType) {
|
||||
this.unsupported = true;
|
||||
return null;
|
||||
}
|
||||
if (name.equals("protocols") && objArr.length == 0) {
|
||||
return this.protocols;
|
||||
}
|
||||
if ((!name.equals("selectProtocol") && !name.equals("select")) || String.class != returnType || objArr.length != 1 || !(objArr[0] instanceof List)) {
|
||||
if ((!name.equals("protocolSelected") && !name.equals("selected")) || objArr.length != 1) {
|
||||
return method.invoke(this, objArr);
|
||||
}
|
||||
this.selected = (String) objArr[0];
|
||||
return null;
|
||||
}
|
||||
List list = (List) objArr[0];
|
||||
int size = list.size();
|
||||
for (int i = 0; i < size; i++) {
|
||||
if (this.protocols.contains(list.get(i))) {
|
||||
String str = (String) list.get(i);
|
||||
this.selected = str;
|
||||
return str;
|
||||
}
|
||||
}
|
||||
String str2 = this.protocols.get(0);
|
||||
this.selected = str2;
|
||||
return str2;
|
||||
}
|
||||
}
|
||||
|
||||
JdkWithJettyBootPlatform(Method method, Method method2, Method method3, Class<?> cls, Class<?> cls2) {
|
||||
this.putMethod = method;
|
||||
this.getMethod = method2;
|
||||
this.removeMethod = method3;
|
||||
this.clientProviderClass = cls;
|
||||
this.serverProviderClass = cls2;
|
||||
}
|
||||
|
||||
public static Platform buildIfSupported() {
|
||||
try {
|
||||
Class<?> cls = Class.forName("org.eclipse.jetty.alpn.ALPN");
|
||||
Class<?> cls2 = Class.forName("org.eclipse.jetty.alpn.ALPN$Provider");
|
||||
return new JdkWithJettyBootPlatform(cls.getMethod("put", SSLSocket.class, cls2), cls.getMethod("get", SSLSocket.class), cls.getMethod("remove", SSLSocket.class), Class.forName("org.eclipse.jetty.alpn.ALPN$ClientProvider"), Class.forName("org.eclipse.jetty.alpn.ALPN$ServerProvider"));
|
||||
} catch (ClassNotFoundException | NoSuchMethodException unused) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
@Override // okhttp3.internal.platform.Platform
|
||||
public void afterHandshake(SSLSocket sSLSocket) {
|
||||
try {
|
||||
this.removeMethod.invoke(null, sSLSocket);
|
||||
} catch (IllegalAccessException | InvocationTargetException e) {
|
||||
throw Util.assertionError("unable to remove alpn", e);
|
||||
}
|
||||
}
|
||||
|
||||
@Override // okhttp3.internal.platform.Platform
|
||||
public void configureTlsExtensions(SSLSocket sSLSocket, String str, List<Protocol> list) {
|
||||
try {
|
||||
this.putMethod.invoke(null, sSLSocket, Proxy.newProxyInstance(Platform.class.getClassLoader(), new Class[]{this.clientProviderClass, this.serverProviderClass}, new JettyNegoProvider(Platform.alpnProtocolNames(list))));
|
||||
} catch (IllegalAccessException | InvocationTargetException e) {
|
||||
throw Util.assertionError("unable to set alpn", e);
|
||||
}
|
||||
}
|
||||
|
||||
@Override // okhttp3.internal.platform.Platform
|
||||
public String getSelectedProtocol(SSLSocket sSLSocket) {
|
||||
try {
|
||||
JettyNegoProvider jettyNegoProvider = (JettyNegoProvider) Proxy.getInvocationHandler(this.getMethod.invoke(null, sSLSocket));
|
||||
if (!jettyNegoProvider.unsupported && jettyNegoProvider.selected == null) {
|
||||
Platform.get().log(4, "ALPN callback dropped: HTTP/2 is disabled. Is alpn-boot on the boot class path?", null);
|
||||
return null;
|
||||
}
|
||||
if (jettyNegoProvider.unsupported) {
|
||||
return null;
|
||||
}
|
||||
return jettyNegoProvider.selected;
|
||||
} catch (IllegalAccessException | InvocationTargetException e) {
|
||||
throw Util.assertionError("unable to get selected protocol", e);
|
||||
}
|
||||
}
|
||||
}
|
103
sources/okhttp3/internal/platform/OptionalMethod.java
Normal file
103
sources/okhttp3/internal/platform/OptionalMethod.java
Normal file
@@ -0,0 +1,103 @@
|
||||
package okhttp3.internal.platform;
|
||||
|
||||
import java.lang.reflect.InvocationTargetException;
|
||||
import java.lang.reflect.Method;
|
||||
|
||||
/* loaded from: classes2.dex */
|
||||
class OptionalMethod<T> {
|
||||
private final String methodName;
|
||||
private final Class[] methodParams;
|
||||
private final Class<?> returnType;
|
||||
|
||||
OptionalMethod(Class<?> cls, String str, Class... clsArr) {
|
||||
this.returnType = cls;
|
||||
this.methodName = str;
|
||||
this.methodParams = clsArr;
|
||||
}
|
||||
|
||||
private Method getMethod(Class<?> cls) {
|
||||
Class<?> cls2;
|
||||
String str = this.methodName;
|
||||
if (str == null) {
|
||||
return null;
|
||||
}
|
||||
Method publicMethod = getPublicMethod(cls, str, this.methodParams);
|
||||
if (publicMethod == null || (cls2 = this.returnType) == null || cls2.isAssignableFrom(publicMethod.getReturnType())) {
|
||||
return publicMethod;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
private static Method getPublicMethod(Class<?> cls, String str, Class[] clsArr) {
|
||||
try {
|
||||
Method method = cls.getMethod(str, clsArr);
|
||||
try {
|
||||
if ((method.getModifiers() & 1) != 0) {
|
||||
return method;
|
||||
}
|
||||
} catch (NoSuchMethodException unused) {
|
||||
return method;
|
||||
}
|
||||
} catch (NoSuchMethodException unused2) {
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public Object invoke(T t, Object... objArr) throws InvocationTargetException {
|
||||
Method method = getMethod(t.getClass());
|
||||
if (method == null) {
|
||||
throw new AssertionError("Method " + this.methodName + " not supported for object " + t);
|
||||
}
|
||||
try {
|
||||
return method.invoke(t, objArr);
|
||||
} catch (IllegalAccessException e) {
|
||||
AssertionError assertionError = new AssertionError("Unexpectedly could not call: " + method);
|
||||
assertionError.initCause(e);
|
||||
throw assertionError;
|
||||
}
|
||||
}
|
||||
|
||||
public Object invokeOptional(T t, Object... objArr) throws InvocationTargetException {
|
||||
Method method = getMethod(t.getClass());
|
||||
if (method == null) {
|
||||
return null;
|
||||
}
|
||||
try {
|
||||
return method.invoke(t, objArr);
|
||||
} catch (IllegalAccessException unused) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
public Object invokeOptionalWithoutCheckedException(T t, Object... objArr) {
|
||||
try {
|
||||
return invokeOptional(t, objArr);
|
||||
} catch (InvocationTargetException e) {
|
||||
Throwable targetException = e.getTargetException();
|
||||
if (targetException instanceof RuntimeException) {
|
||||
throw ((RuntimeException) targetException);
|
||||
}
|
||||
AssertionError assertionError = new AssertionError("Unexpected exception");
|
||||
assertionError.initCause(targetException);
|
||||
throw assertionError;
|
||||
}
|
||||
}
|
||||
|
||||
public Object invokeWithoutCheckedException(T t, Object... objArr) {
|
||||
try {
|
||||
return invoke(t, objArr);
|
||||
} catch (InvocationTargetException e) {
|
||||
Throwable targetException = e.getTargetException();
|
||||
if (targetException instanceof RuntimeException) {
|
||||
throw ((RuntimeException) targetException);
|
||||
}
|
||||
AssertionError assertionError = new AssertionError("Unexpected exception");
|
||||
assertionError.initCause(targetException);
|
||||
throw assertionError;
|
||||
}
|
||||
}
|
||||
|
||||
public boolean isSupported(T t) {
|
||||
return getMethod(t.getClass()) != null;
|
||||
}
|
||||
}
|
191
sources/okhttp3/internal/platform/Platform.java
Normal file
191
sources/okhttp3/internal/platform/Platform.java
Normal file
@@ -0,0 +1,191 @@
|
||||
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());
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user