package dr;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
import java.util.Arrays;
import java.util.List;
import javax.net.ssl.SSLSocket;
import kotlin.jvm.internal.k;
import kotlin.jvm.internal.t;

/* loaded from: classes3.dex */
public final class e extends h {

    /* renamed from: i, reason: collision with root package name */
    public static final b f16583i = new b(null);

    /* renamed from: d, reason: collision with root package name */
    private final Method f16584d;

    /* renamed from: e, reason: collision with root package name */
    private final Method f16585e;

    /* renamed from: f, reason: collision with root package name */
    private final Method f16586f;

    /* renamed from: g, reason: collision with root package name */
    private final Class f16587g;

    /* renamed from: h, reason: collision with root package name */
    private final Class f16588h;

    /* loaded from: classes3.dex */
    private static final class a implements InvocationHandler {

        /* renamed from: a, reason: collision with root package name */
        private final List f16589a;

        /* renamed from: b, reason: collision with root package name */
        private boolean f16590b;

        /* renamed from: c, reason: collision with root package name */
        private String f16591c;

        public a(List protocols) {
            t.g(protocols, "protocols");
            this.f16589a = protocols;
        }

        public final String a() {
            return this.f16591c;
        }

        public final boolean b() {
            return this.f16590b;
        }

        @Override // java.lang.reflect.InvocationHandler
        public Object invoke(Object proxy, Method method, Object[] objArr) {
            t.g(proxy, "proxy");
            t.g(method, "method");
            if (objArr == null) {
                objArr = new Object[0];
            }
            String name = method.getName();
            Class<?> returnType = method.getReturnType();
            if (t.b(name, "supports") && t.b(Boolean.TYPE, returnType)) {
                return Boolean.TRUE;
            }
            if (t.b(name, "unsupported") && t.b(Void.TYPE, returnType)) {
                this.f16590b = true;
                return null;
            }
            if (t.b(name, "protocols")) {
                if (objArr.length == 0) {
                    return this.f16589a;
                }
            }
            if ((t.b(name, "selectProtocol") || t.b(name, "select")) && t.b(String.class, returnType) && objArr.length == 1) {
                Object obj = objArr[0];
                if (obj instanceof List) {
                    t.e(obj, "null cannot be cast to non-null type kotlin.collections.List<*>");
                    List list = (List) obj;
                    int size = list.size();
                    if (size >= 0) {
                        int i10 = 0;
                        while (true) {
                            Object obj2 = list.get(i10);
                            t.e(obj2, "null cannot be cast to non-null type kotlin.String");
                            String str = (String) obj2;
                            if (!this.f16589a.contains(str)) {
                                if (i10 == size) {
                                    break;
                                }
                                i10++;
                            } else {
                                this.f16591c = str;
                                return str;
                            }
                        }
                    }
                    String str2 = (String) this.f16589a.get(0);
                    this.f16591c = str2;
                    return str2;
                }
            }
            if ((!t.b(name, "protocolSelected") && !t.b(name, "selected")) || objArr.length != 1) {
                return method.invoke(this, Arrays.copyOf(objArr, objArr.length));
            }
            Object obj3 = objArr[0];
            t.e(obj3, "null cannot be cast to non-null type kotlin.String");
            this.f16591c = (String) obj3;
            return null;
        }
    }

    /* loaded from: classes3.dex */
    public static final class b {
        private b() {
        }

        public /* synthetic */ b(k kVar) {
            this();
        }

        public final h a() {
            String jvmVersion = System.getProperty("java.specification.version", "unknown");
            try {
                t.f(jvmVersion, "jvmVersion");
                if (Integer.parseInt(jvmVersion) >= 9) {
                    return null;
                }
            } catch (NumberFormatException unused) {
            }
            try {
                Class<?> cls = Class.forName("org.eclipse.jetty.alpn.ALPN", true, null);
                Class<?> cls2 = Class.forName("org.eclipse.jetty.alpn.ALPN$Provider", true, null);
                Class<?> clientProviderClass = Class.forName("org.eclipse.jetty.alpn.ALPN$ClientProvider", true, null);
                Class<?> serverProviderClass = Class.forName("org.eclipse.jetty.alpn.ALPN$ServerProvider", true, null);
                Method putMethod = cls.getMethod("put", SSLSocket.class, cls2);
                Method getMethod = cls.getMethod("get", SSLSocket.class);
                Method removeMethod = cls.getMethod("remove", SSLSocket.class);
                t.f(putMethod, "putMethod");
                t.f(getMethod, "getMethod");
                t.f(removeMethod, "removeMethod");
                t.f(clientProviderClass, "clientProviderClass");
                t.f(serverProviderClass, "serverProviderClass");
                return new e(putMethod, getMethod, removeMethod, clientProviderClass, serverProviderClass);
            } catch (ClassNotFoundException | NoSuchMethodException unused2) {
                return null;
            }
        }
    }

    public e(Method putMethod, Method getMethod, Method removeMethod, Class clientProviderClass, Class serverProviderClass) {
        t.g(putMethod, "putMethod");
        t.g(getMethod, "getMethod");
        t.g(removeMethod, "removeMethod");
        t.g(clientProviderClass, "clientProviderClass");
        t.g(serverProviderClass, "serverProviderClass");
        this.f16584d = putMethod;
        this.f16585e = getMethod;
        this.f16586f = removeMethod;
        this.f16587g = clientProviderClass;
        this.f16588h = serverProviderClass;
    }

    @Override // dr.h
    public void b(SSLSocket sslSocket) {
        t.g(sslSocket, "sslSocket");
        try {
            this.f16586f.invoke(null, sslSocket);
        } catch (IllegalAccessException e10) {
            throw new AssertionError("failed to remove ALPN", e10);
        } catch (InvocationTargetException e11) {
            throw new AssertionError("failed to remove ALPN", e11);
        }
    }

    @Override // dr.h
    public void e(SSLSocket sslSocket, String str, List protocols) {
        t.g(sslSocket, "sslSocket");
        t.g(protocols, "protocols");
        try {
            this.f16584d.invoke(null, sslSocket, Proxy.newProxyInstance(h.class.getClassLoader(), new Class[]{this.f16587g, this.f16588h}, new a(h.f16598a.b(protocols))));
        } catch (IllegalAccessException e10) {
            throw new AssertionError("failed to set ALPN", e10);
        } catch (InvocationTargetException e11) {
            throw new AssertionError("failed to set ALPN", e11);
        }
    }

    @Override // dr.h
    public String g(SSLSocket sslSocket) {
        t.g(sslSocket, "sslSocket");
        try {
            InvocationHandler invocationHandler = Proxy.getInvocationHandler(this.f16585e.invoke(null, sslSocket));
            t.e(invocationHandler, "null cannot be cast to non-null type okhttp3.internal.platform.Jdk8WithJettyBootPlatform.AlpnProvider");
            a aVar = (a) invocationHandler;
            if (!aVar.b() && aVar.a() == null) {
                h.k(this, "ALPN callback dropped: HTTP/2 is disabled. Is alpn-boot on the boot class path?", 0, null, 6, null);
                return null;
            }
            if (aVar.b()) {
                return null;
            }
            return aVar.a();
        } catch (IllegalAccessException e10) {
            throw new AssertionError("failed to get ALPN selected protocol", e10);
        } catch (InvocationTargetException e11) {
            throw new AssertionError("failed to get ALPN selected protocol", e11);
        }
    }
}
