package okhttp3.n0.connection;

import g.a.a.a.a;
import g.h.a.i;
import java.net.UnknownServiceException;
import java.util.Arrays;
import java.util.Comparator;
import java.util.List;
import javax.net.ssl.SSLSocket;
import kotlin.Metadata;
import kotlin.comparisons.NaturalOrderComparator;
import kotlin.jvm.internal.o;
import okhttp3.CipherSuite;
import okhttp3.ConnectionSpec;
import okhttp3.n0.c;

/* JADX INFO: renamed from: y.n0.g.b, reason: from Kotlin metadata */
/* JADX INFO: loaded from: classes2.dex */
@Metadata(d1 = {"\u00006\n\u0002\u0018\u0002\n\u0002\u0010\u0000\n\u0000\n\u0002\u0010 \n\u0002\u0018\u0002\n\u0002\b\u0002\n\u0002\u0010\u000b\n\u0002\b\u0002\n\u0002\u0010\b\n\u0002\b\u0002\n\u0002\u0018\u0002\n\u0002\b\u0002\n\u0002\u0018\u0002\n\u0002\b\u0002\b\u0000\u0018\u00002\u00020\u0001B\u0013\u0012\f\u0010\u0002\u001a\b\u0012\u0004\u0012\u00020\u00040\u0003¢\u0006\u0002\u0010\u0005J\u000e\u0010\u000b\u001a\u00020\u00042\u0006\u0010\f\u001a\u00020\rJ\u000e\u0010\u000e\u001a\u00020\u00072\u0006\u0010\u000f\u001a\u00020\u0010J\u0010\u0010\b\u001a\u00020\u00072\u0006\u0010\u0011\u001a\u00020\rH\u0002R\u0014\u0010\u0002\u001a\b\u0012\u0004\u0012\u00020\u00040\u0003X\u0082\u0004¢\u0006\u0002\n\u0000R\u000e\u0010\u0006\u001a\u00020\u0007X\u0082\u000e¢\u0006\u0002\n\u0000R\u000e\u0010\b\u001a\u00020\u0007X\u0082\u000e¢\u0006\u0002\n\u0000R\u000e\u0010\t\u001a\u00020\nX\u0082\u000e¢\u0006\u0002\n\u0000¨\u0006\u0012"}, d2 = {"Lokhttp3/internal/connection/ConnectionSpecSelector;", "", "connectionSpecs", "", "Lokhttp3/ConnectionSpec;", "(Ljava/util/List;)V", "isFallback", "", "isFallbackPossible", "nextModeIndex", "", "configureSecureSocket", "sslSocket", "Ljavax/net/ssl/SSLSocket;", "connectionFailed", "e", "Ljava/io/IOException;", "socket", "okhttp"}, k = 1, mv = {1, 6, 0}, xi = 48)
public final class ConnectionSpecSelector {
    public final List<ConnectionSpec> a;

    /* JADX INFO: renamed from: b, reason: collision with root package name */
    public int f9740b;

    /* JADX INFO: renamed from: c, reason: collision with root package name */
    public boolean f9741c;

    /* JADX INFO: renamed from: d, reason: collision with root package name */
    public boolean f9742d;

    public ConnectionSpecSelector(List<ConnectionSpec> list) {
        o.f(list, "connectionSpecs");
        this.a = list;
    }

    public final ConnectionSpec a(SSLSocket sSLSocket) throws UnknownServiceException {
        ConnectionSpec connectionSpec;
        boolean z2;
        String[] enabledCipherSuites;
        String[] enabledProtocols;
        o.f(sSLSocket, "sslSocket");
        int i2 = this.f9740b;
        int size = this.a.size();
        while (true) {
            if (i2 >= size) {
                connectionSpec = null;
                break;
            }
            int i3 = i2 + 1;
            connectionSpec = this.a.get(i2);
            if (connectionSpec.b(sSLSocket)) {
                this.f9740b = i3;
                break;
            }
            i2 = i3;
        }
        if (connectionSpec == null) {
            StringBuilder sbF = a.F("Unable to find acceptable protocols. isFallback=");
            sbF.append(this.f9742d);
            sbF.append(", modes=");
            sbF.append(this.a);
            sbF.append(", supported protocols=");
            String[] enabledProtocols2 = sSLSocket.getEnabledProtocols();
            o.c(enabledProtocols2);
            String string = Arrays.toString(enabledProtocols2);
            o.e(string, "toString(this)");
            sbF.append(string);
            throw new UnknownServiceException(sbF.toString());
        }
        int i4 = this.f9740b;
        int size2 = this.a.size();
        while (true) {
            if (i4 >= size2) {
                z2 = false;
                break;
            }
            int i5 = i4 + 1;
            if (this.a.get(i4).b(sSLSocket)) {
                z2 = true;
                break;
            }
            i4 = i5;
        }
        this.f9741c = z2;
        boolean z3 = this.f9742d;
        o.f(sSLSocket, "sslSocket");
        if (connectionSpec.f9705g != null) {
            String[] enabledCipherSuites2 = sSLSocket.getEnabledCipherSuites();
            o.e(enabledCipherSuites2, "sslSocket.enabledCipherSuites");
            String[] strArr = connectionSpec.f9705g;
            CipherSuite.b bVar = CipherSuite.a;
            CipherSuite.b bVar2 = CipherSuite.a;
            enabledCipherSuites = c.q(enabledCipherSuites2, strArr, CipherSuite.f9670b);
        } else {
            enabledCipherSuites = sSLSocket.getEnabledCipherSuites();
        }
        if (connectionSpec.f9706h != null) {
            String[] enabledProtocols3 = sSLSocket.getEnabledProtocols();
            o.e(enabledProtocols3, "sslSocket.enabledProtocols");
            String[] strArr2 = connectionSpec.f9706h;
            NaturalOrderComparator naturalOrderComparator = NaturalOrderComparator.f6125f;
            o.d(naturalOrderComparator, "null cannot be cast to non-null type java.util.Comparator<T of kotlin.comparisons.ComparisonsKt__ComparisonsKt.naturalOrder>{ kotlin.TypeAliasesKt.Comparator<T of kotlin.comparisons.ComparisonsKt__ComparisonsKt.naturalOrder> }");
            enabledProtocols = c.q(enabledProtocols3, strArr2, naturalOrderComparator);
        } else {
            enabledProtocols = sSLSocket.getEnabledProtocols();
        }
        String[] supportedCipherSuites = sSLSocket.getSupportedCipherSuites();
        o.e(supportedCipherSuites, "supportedCipherSuites");
        CipherSuite.b bVar3 = CipherSuite.a;
        CipherSuite.b bVar4 = CipherSuite.a;
        Comparator<String> comparator = CipherSuite.f9670b;
        byte[] bArr = c.a;
        o.f(supportedCipherSuites, "<this>");
        o.f("TLS_FALLBACK_SCSV", "value");
        o.f(comparator, "comparator");
        int length = supportedCipherSuites.length;
        int i6 = 0;
        while (true) {
            if (i6 >= length) {
                i6 = -1;
                break;
            }
            if (((CipherSuite.a) comparator).compare(supportedCipherSuites[i6], "TLS_FALLBACK_SCSV") == 0) {
                break;
            }
            i6++;
        }
        if (z3 && i6 != -1) {
            o.e(enabledCipherSuites, "cipherSuitesIntersection");
            String str = supportedCipherSuites[i6];
            o.e(str, "supportedCipherSuites[indexOfFallbackScsv]");
            o.f(enabledCipherSuites, "<this>");
            o.f(str, "value");
            Object[] objArrCopyOf = Arrays.copyOf(enabledCipherSuites, enabledCipherSuites.length + 1);
            o.e(objArrCopyOf, "copyOf(this, newSize)");
            enabledCipherSuites = (String[]) objArrCopyOf;
            enabledCipherSuites[i.u1(enabledCipherSuites)] = str;
        }
        ConnectionSpec.a aVar = new ConnectionSpec.a(connectionSpec);
        o.e(enabledCipherSuites, "cipherSuitesIntersection");
        aVar.b((String[]) Arrays.copyOf(enabledCipherSuites, enabledCipherSuites.length));
        o.e(enabledProtocols, "tlsVersionsIntersection");
        aVar.e((String[]) Arrays.copyOf(enabledProtocols, enabledProtocols.length));
        ConnectionSpec connectionSpecA = aVar.a();
        if (connectionSpecA.c() != null) {
            sSLSocket.setEnabledProtocols(connectionSpecA.f9706h);
        }
        if (connectionSpecA.a() != null) {
            sSLSocket.setEnabledCipherSuites(connectionSpecA.f9705g);
        }
        return connectionSpec;
    }
}
