package o;

import com.google.android.gms.common.api.Api;
import java.io.IOException;
import java.io.InterruptedIOException;
import java.net.ConnectException;
import java.net.InetSocketAddress;
import java.net.ProtocolException;
import java.net.Proxy;
import java.net.Socket;
import java.net.SocketException;
import java.net.SocketTimeoutException;
import java.net.UnknownServiceException;
import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.concurrent.TimeUnit;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.net.ssl.SSLException;
import javax.net.ssl.SSLHandshakeException;
import javax.net.ssl.SSLPeerUnverifiedException;
import javax.net.ssl.SSLSession;
import javax.net.ssl.SSLSocket;
import javax.net.ssl.SSLSocketFactory;

/* compiled from: r8-map-id-3b7cc9102578c509342fa2f0fabed2bfc72239cd258d579fc679bfcbc198b048 */
/* renamed from: o.t00, reason: case insensitive filesystem */
/* loaded from: classes.dex */
public final class C2486t00 extends CF {
    public final C2587u30 b;
    public Socket c;
    public Socket d;
    public DE e;
    public EnumC2910xZ f;
    public KF g;
    public C1822m00 h;
    public C1632k00 i;
    public boolean j;
    public boolean k;
    public int l;
    public int m;
    public int n;

    /* renamed from: o, reason: collision with root package name */
    public int f723o = 1;
    public final ArrayList p = new ArrayList();
    public long q = Long.MAX_VALUE;

    public C2486t00(C2587u30 c2587u30) {
        this.b = c2587u30;
    }

    public static void d(C2429sT c2429sT, C2587u30 c2587u30, IOException iOException) {
        if (c2587u30.b.type() != Proxy.Type.DIRECT) {
            U1 u1 = c2587u30.a;
            u1.g.connectFailed(u1.h.g(), c2587u30.b.address(), iOException);
        }
        WT wt = c2429sT.J;
        synchronized (wt) {
            ((LinkedHashSet) wt.m).add(c2587u30);
        }
    }

    @Override // o.CF
    public final synchronized void a(C2880x70 c2880x70) {
        this.f723o = (c2880x70.a & 16) != 0 ? c2880x70.b[4] : Api.BaseClientBuilder.API_PRIORITY_OTHER;
    }

    @Override // o.CF
    public final void b(RF rf) {
        rf.c(8, null);
    }

    public final void c(int i, int i2, int i3, boolean z, C2202q00 c2202q00) throws Throwable {
        C2587u30 c2587u30;
        if (this.f != null) {
            throw new IllegalStateException("already connected");
        }
        U1 u1 = this.b.a;
        List list = u1.j;
        C1984nl c1984nl = new C1984nl(list);
        if (u1.c == null) {
            if (!list.contains(C1889ml.f)) {
                throw new C2682v30(new UnknownServiceException("CLEARTEXT communication not enabled for client"));
            }
            String str = this.b.a.h.d;
            ZW zw = ZW.a;
            if (!ZW.a.h(str)) {
                throw new C2682v30(new UnknownServiceException(AbstractC2462sn.j("CLEARTEXT communication to ", str, " not permitted by network security policy")));
            }
        } else if (u1.i.contains(EnumC2910xZ.q)) {
            throw new C2682v30(new UnknownServiceException("H2_PRIOR_KNOWLEDGE cannot be used with HTTPS"));
        }
        C2682v30 c2682v30 = null;
        do {
            try {
                C2587u30 c2587u302 = this.b;
                if (c2587u302.a.c != null && c2587u302.b.type() == Proxy.Type.HTTP) {
                    f(i, i2, i3);
                    if (this.c == null) {
                    }
                    c2587u30 = this.b;
                    if (c2587u30.a.c == null && c2587u30.b.type() == Proxy.Type.HTTP && this.c == null) {
                        throw new C2682v30(new ProtocolException("Too many tunnel connections attempted: 21"));
                    }
                    this.q = System.nanoTime();
                    return;
                }
                e(i, i2);
                g(c1984nl);
                InetSocketAddress inetSocketAddress = this.b.c;
                c2587u30 = this.b;
                if (c2587u30.a.c == null) {
                }
                this.q = System.nanoTime();
                return;
            } catch (IOException e) {
                Socket socket = this.d;
                if (socket != null) {
                    AbstractC2264qi0.e(socket);
                }
                Socket socket2 = this.c;
                if (socket2 != null) {
                    AbstractC2264qi0.e(socket2);
                }
                this.d = null;
                this.c = null;
                this.h = null;
                this.i = null;
                this.e = null;
                this.f = null;
                this.g = null;
                this.f723o = 1;
                InetSocketAddress inetSocketAddress2 = this.b.c;
                if (c2682v30 == null) {
                    c2682v30 = new C2682v30(e);
                } else {
                    C80.i(c2682v30.l, e);
                    c2682v30.m = e;
                }
                if (!z) {
                    throw c2682v30;
                }
                c1984nl.c = true;
                if (!c1984nl.b) {
                    throw c2682v30;
                }
                if (e instanceof ProtocolException) {
                    throw c2682v30;
                }
                if (e instanceof InterruptedIOException) {
                    throw c2682v30;
                }
                if ((e instanceof SSLHandshakeException) && (e.getCause() instanceof CertificateException)) {
                    throw c2682v30;
                }
                if (e instanceof SSLPeerUnverifiedException) {
                    throw c2682v30;
                }
            }
        } while (e instanceof SSLException);
        throw c2682v30;
    }

    public final void e(int i, int i2) throws IOException {
        C2587u30 c2587u30 = this.b;
        Proxy proxy = c2587u30.b;
        U1 u1 = c2587u30.a;
        Proxy.Type type = proxy.type();
        int i3 = type == null ? -1 : AbstractC2296r00.a[type.ordinal()];
        int i4 = 1;
        Socket socketCreateSocket = (i3 == 1 || i3 == 2) ? u1.b.createSocket() : new Socket(proxy);
        this.c = socketCreateSocket;
        InetSocketAddress inetSocketAddress = this.b.c;
        socketCreateSocket.setSoTimeout(i2);
        try {
            ZW zw = ZW.a;
            ZW.a.e(socketCreateSocket, this.b.c, i);
            try {
                Logger logger = AbstractC2714vT.a;
                L90 l90 = new L90(socketCreateSocket);
                int i5 = 0;
                this.h = new C1822m00(new M9(i5, l90, new M9(i4, socketCreateSocket.getInputStream(), l90)));
                L90 l902 = new L90(socketCreateSocket);
                this.i = new C1632k00(new L9(i5, l902, new L9(i4, socketCreateSocket.getOutputStream(), l902)));
            } catch (NullPointerException e) {
                if (F3.c(e.getMessage(), "throw with null exception")) {
                    throw new IOException(e);
                }
            }
        } catch (ConnectException e2) {
            ConnectException connectException = new ConnectException("Failed to connect to " + this.b.c);
            connectException.initCause(e2);
            throw connectException;
        }
    }

    public final void f(int i, int i2, int i3) throws IOException, NumberFormatException {
        Q1 q1 = new Q1();
        C2587u30 c2587u30 = this.b;
        q1.f292o = c2587u30.a.h;
        q1.v("CONNECT", null);
        U1 u1 = c2587u30.a;
        ((KE) q1.n).p("Host", AbstractC2264qi0.v(u1.h, true));
        ((KE) q1.n).p("Proxy-Connection", "Keep-Alive");
        ((KE) q1.n).p("User-Agent", "okhttp/4.12.0");
        U7 u7G = q1.g();
        KE ke = new KE(0);
        ke.p("Proxy-Authenticate", "OkHttp-Preemptive");
        ke.d();
        u1.f.getClass();
        ZF zf = (ZF) u7G.m;
        e(i, i2);
        String str = "CONNECT " + AbstractC2264qi0.v(zf, true) + " HTTP/1.1";
        C1822m00 c1822m00 = this.h;
        C1632k00 c1632k00 = this.i;
        C2689v7 c2689v7 = new C2689v7(null, this, c1822m00, c1632k00);
        Cf0 cf0A = c1822m00.l.a();
        long j = i2;
        TimeUnit timeUnit = TimeUnit.MILLISECONDS;
        cf0A.g(j);
        c1632k00.l.a().g(i3);
        c2689v7.t((LE) u7G.f352o, str);
        c2689v7.b();
        B20 b20G = c2689v7.g(false);
        b20G.a = u7G;
        C20 c20A = b20G.a();
        int i4 = c20A.f63o;
        long j2 = AbstractC2264qi0.j(c20A);
        if (j2 != -1) {
            C2890xF c2890xFN = c2689v7.n(j2);
            AbstractC2264qi0.t(c2890xFN, Api.BaseClientBuilder.API_PRIORITY_OTHER);
            c2890xFN.close();
        }
        if (i4 != 200) {
            if (i4 != 407) {
                throw new IOException(AbstractC0126Ew.j(i4, "Unexpected response code for CONNECT: "));
            }
            u1.f.getClass();
            throw new IOException("Failed to authenticate with proxy");
        }
        if (!c1822m00.m.f() || !c1632k00.m.f()) {
            throw new IOException("TLS tunnel buffered too many bytes!");
        }
    }

    public final void g(C1984nl c1984nl) throws Throwable {
        SSLSocket sSLSocket;
        int i;
        SSLSocket sSLSocket2;
        String strF;
        EnumC2910xZ enumC2910xZ = EnumC2910xZ.p;
        EnumC2910xZ enumC2910xZ2 = EnumC2910xZ.n;
        EnumC2910xZ enumC2910xZ3 = EnumC2910xZ.q;
        U1 u1 = this.b.a;
        SSLSocketFactory sSLSocketFactory = u1.c;
        if (sSLSocketFactory == null) {
            if (!u1.i.contains(enumC2910xZ3)) {
                this.d = this.c;
                this.f = enumC2910xZ2;
                return;
            } else {
                this.d = this.c;
                this.f = enumC2910xZ3;
                l();
                return;
            }
        }
        try {
            Socket socket = this.c;
            ZF zf = u1.h;
            i = 1;
            sSLSocket2 = (SSLSocket) sSLSocketFactory.createSocket(socket, zf.d, zf.e, true);
        } catch (Throwable th) {
            th = th;
            sSLSocket = null;
        }
        try {
            C1889ml c1889mlA = c1984nl.a(sSLSocket2);
            if (c1889mlA.b) {
                ZW zw = ZW.a;
                ZW.a.d(sSLSocket2, u1.h.d, u1.i);
            }
            sSLSocket2.startHandshake();
            SSLSession session = sSLSocket2.getSession();
            DE deW = Co0.w(session);
            int i2 = 0;
            if (!u1.d.verify(u1.h.d, session)) {
                List listA = deW.a();
                if (listA.isEmpty()) {
                    throw new SSLPeerUnverifiedException("Hostname " + u1.h.d + " not verified (no certificates)");
                }
                X509Certificate x509Certificate = (X509Certificate) listA.get(0);
                StringBuilder sb = new StringBuilder("\n              |Hostname ");
                sb.append(u1.h.d);
                sb.append(" not verified:\n              |    certificate: ");
                C0395Pg c0395Pg = C0395Pg.c;
                sb.append(C80.H(x509Certificate));
                sb.append("\n              |    DN: ");
                sb.append(x509Certificate.getSubjectDN().getName());
                sb.append("\n              |    subjectAltNames: ");
                sb.append(AbstractC3117zi.l0(C2240qT.a(x509Certificate, 7), C2240qT.a(x509Certificate, 2)));
                sb.append("\n              ");
                throw new SSLPeerUnverifiedException(AbstractC0725ab0.H(sb.toString()));
            }
            C0395Pg c0395Pg2 = u1.e;
            this.e = new DE(deW.a, deW.b, deW.c, new C2391s00(c0395Pg2, deW, u1, i2));
            String str = u1.h.d;
            Iterator it = c0395Pg2.a.iterator();
            if (it.hasNext()) {
                AbstractC2462sn.s(it.next());
                throw null;
            }
            if (c1889mlA.b) {
                ZW zw2 = ZW.a;
                strF = ZW.a.f(sSLSocket2);
            } else {
                strF = null;
            }
            this.d = sSLSocket2;
            Logger logger = AbstractC2714vT.a;
            L90 l90 = new L90(sSLSocket2);
            this.h = new C1822m00(new M9(i2, l90, new M9(i, sSLSocket2.getInputStream(), l90)));
            L90 l902 = new L90(sSLSocket2);
            this.i = new C1632k00(new L9(i2, l902, new L9(i, sSLSocket2.getOutputStream(), l902)));
            if (strF != null) {
                EnumC2910xZ enumC2910xZ4 = EnumC2910xZ.m;
                if (strF.equals("http/1.0")) {
                    enumC2910xZ2 = enumC2910xZ4;
                } else if (!strF.equals("http/1.1")) {
                    if (strF.equals("h2_prior_knowledge")) {
                        enumC2910xZ2 = enumC2910xZ3;
                    } else if (strF.equals("h2")) {
                        enumC2910xZ2 = enumC2910xZ;
                    } else {
                        enumC2910xZ2 = EnumC2910xZ.f792o;
                        if (!strF.equals("spdy/3.1")) {
                            enumC2910xZ2 = EnumC2910xZ.r;
                            if (!strF.equals("quic")) {
                                throw new IOException("Unexpected protocol: ".concat(strF));
                            }
                        }
                    }
                }
            }
            this.f = enumC2910xZ2;
            ZW zw3 = ZW.a;
            ZW.a.a(sSLSocket2);
            if (this.f == enumC2910xZ) {
                l();
            }
        } catch (Throwable th2) {
            th = th2;
            sSLSocket = sSLSocket2;
            if (sSLSocket != null) {
                ZW zw4 = ZW.a;
                ZW.a.a(sSLSocket);
            }
            if (sSLSocket != null) {
                AbstractC2264qi0.e(sSLSocket);
            }
            throw th;
        }
    }

    /* JADX WARN: Removed duplicated region for block: B:54:0x00a8 A[EXC_TOP_SPLITTER, SYNTHETIC] */
    /*
        Code decompiled incorrectly, please refer to instructions dump.
        To view partially-correct add '--show-bad-code' argument
    */
    public final boolean h(o.U1 r8, java.util.List r9) {
        /*
            r7 = this;
            o.ZF r0 = r8.h
            byte[] r1 = o.AbstractC2264qi0.a
            java.util.ArrayList r1 = r7.p
            int r1 = r1.size()
            int r2 = r7.f723o
            r3 = 0
            if (r1 >= r2) goto Lc8
            boolean r1 = r7.j
            if (r1 == 0) goto L15
            goto Lc8
        L15:
            o.u30 r1 = r7.b
            o.U1 r2 = r1.a
            o.U1 r4 = r1.a
            boolean r2 = r2.a(r8)
            if (r2 != 0) goto L23
            goto Lc8
        L23:
            java.lang.String r2 = r0.d
            o.ZF r5 = r4.h
            java.lang.String r5 = r5.d
            boolean r2 = o.F3.c(r2, r5)
            if (r2 == 0) goto L31
            goto Lbd
        L31:
            o.KF r2 = r7.g
            if (r2 != 0) goto L37
            goto Lc8
        L37:
            if (r9 == 0) goto Lc8
            boolean r2 = r9.isEmpty()
            if (r2 == 0) goto L41
            goto Lc8
        L41:
            java.util.Iterator r9 = r9.iterator()
        L45:
            boolean r2 = r9.hasNext()
            if (r2 == 0) goto Lc8
            java.lang.Object r2 = r9.next()
            o.u30 r2 = (o.C2587u30) r2
            java.net.Proxy r5 = r2.b
            java.net.Proxy$Type r5 = r5.type()
            java.net.Proxy$Type r6 = java.net.Proxy.Type.DIRECT
            if (r5 != r6) goto L45
            java.net.Proxy r5 = r1.b
            java.net.Proxy$Type r5 = r5.type()
            if (r5 != r6) goto L45
            java.net.InetSocketAddress r5 = r1.c
            java.net.InetSocketAddress r2 = r2.c
            boolean r2 = o.F3.c(r5, r2)
            if (r2 == 0) goto L45
            javax.net.ssl.HostnameVerifier r9 = r8.d
            o.qT r1 = o.C2240qT.a
            if (r9 == r1) goto L74
            goto Lc8
        L74:
            byte[] r9 = o.AbstractC2264qi0.a
            o.ZF r9 = r4.h
            int r1 = r0.e
            java.lang.String r0 = r0.d
            int r2 = r9.e
            if (r1 == r2) goto L81
            goto Lc8
        L81:
            java.lang.String r9 = r9.d
            boolean r9 = o.F3.c(r0, r9)
            if (r9 == 0) goto L8a
            goto La8
        L8a:
            boolean r9 = r7.k
            if (r9 != 0) goto Lc8
            o.DE r9 = r7.e
            if (r9 == 0) goto Lc8
            java.util.List r9 = r9.a()
            boolean r1 = r9.isEmpty()
            if (r1 != 0) goto Lc8
            java.lang.Object r9 = r9.get(r3)
            java.security.cert.X509Certificate r9 = (java.security.cert.X509Certificate) r9
            boolean r9 = o.C2240qT.c(r0, r9)
            if (r9 == 0) goto Lc8
        La8:
            o.Pg r8 = r8.e     // Catch: javax.net.ssl.SSLPeerUnverifiedException -> Lc8
            o.DE r9 = r7.e     // Catch: javax.net.ssl.SSLPeerUnverifiedException -> Lc8
            r9.a()     // Catch: javax.net.ssl.SSLPeerUnverifiedException -> Lc8
            java.util.Set r8 = r8.a     // Catch: javax.net.ssl.SSLPeerUnverifiedException -> Lc8
            java.lang.Iterable r8 = (java.lang.Iterable) r8     // Catch: javax.net.ssl.SSLPeerUnverifiedException -> Lc8
            java.util.Iterator r8 = r8.iterator()     // Catch: javax.net.ssl.SSLPeerUnverifiedException -> Lc8
            boolean r9 = r8.hasNext()     // Catch: javax.net.ssl.SSLPeerUnverifiedException -> Lc8
            if (r9 != 0) goto Lbf
        Lbd:
            r8 = 1
            return r8
        Lbf:
            java.lang.Object r8 = r8.next()     // Catch: javax.net.ssl.SSLPeerUnverifiedException -> Lc8
            o.AbstractC2462sn.s(r8)     // Catch: javax.net.ssl.SSLPeerUnverifiedException -> Lc8
            r8 = 0
            throw r8     // Catch: javax.net.ssl.SSLPeerUnverifiedException -> Lc8
        Lc8:
            return r3
        */
        throw new UnsupportedOperationException("Method not decompiled: o.C2486t00.h(o.U1, java.util.List):boolean");
    }

    public final boolean i(boolean z) throws SocketException {
        long j;
        byte[] bArr = AbstractC2264qi0.a;
        long jNanoTime = System.nanoTime();
        Socket socket = this.c;
        Socket socket2 = this.d;
        C1822m00 c1822m00 = this.h;
        if (socket.isClosed() || socket2.isClosed() || socket2.isInputShutdown() || socket2.isOutputShutdown()) {
            return false;
        }
        KF kf = this.g;
        if (kf != null) {
            synchronized (kf) {
                if (kf.q) {
                    return false;
                }
                if (kf.y < kf.x) {
                    if (jNanoTime >= kf.z) {
                        return false;
                    }
                }
                return true;
            }
        }
        synchronized (this) {
            j = jNanoTime - this.q;
        }
        if (j < 10000000000L || !z) {
            return true;
        }
        try {
            int soTimeout = socket2.getSoTimeout();
            try {
                socket2.setSoTimeout(1);
                return !c1822m00.b();
            } finally {
                socket2.setSoTimeout(soTimeout);
            }
        } catch (SocketTimeoutException unused) {
            return true;
        } catch (IOException unused2) {
            return false;
        }
    }

    public final InterfaceC0593Ww j(C2429sT c2429sT, A00 a00) {
        int i = a00.g;
        Socket socket = this.d;
        C1822m00 c1822m00 = this.h;
        C1632k00 c1632k00 = this.i;
        KF kf = this.g;
        if (kf != null) {
            return new LF(c2429sT, this, a00, kf);
        }
        socket.setSoTimeout(i);
        Cf0 cf0A = c1822m00.l.a();
        long j = i;
        TimeUnit timeUnit = TimeUnit.MILLISECONDS;
        cf0A.g(j);
        c1632k00.l.a().g(a00.h);
        return new C2689v7(c2429sT, this, c1822m00, c1632k00);
    }

    public final synchronized void k() {
        this.j = true;
    }

    public final void l() throws SocketException {
        Socket socket = this.d;
        C1822m00 c1822m00 = this.h;
        C1632k00 c1632k00 = this.i;
        socket.setSoTimeout(0);
        Uc0 uc0 = Uc0.i;
        U7 u7 = new U7(uc0);
        String str = this.b.a.h.d;
        u7.n = socket;
        u7.f352o = AbstractC2264qi0.g + ' ' + str;
        u7.p = c1822m00;
        u7.q = c1632k00;
        u7.r = this;
        KF kf = new KF(u7);
        this.g = kf;
        C2880x70 c2880x70 = KF.K;
        this.f723o = (c2880x70.a & 16) != 0 ? c2880x70.b[4] : Api.BaseClientBuilder.API_PRIORITY_OTHER;
        SF sf = kf.H;
        synchronized (sf) {
            try {
                if (sf.f327o) {
                    throw new IOException("closed");
                }
                Logger logger = SF.q;
                if (logger.isLoggable(Level.FINE)) {
                    logger.fine(AbstractC2264qi0.h(">> CONNECTION " + AF.a.c(), new Object[0]));
                }
                sf.l.o(AF.a);
                sf.l.flush();
            } finally {
            }
        }
        SF sf2 = kf.H;
        C2880x70 c2880x702 = kf.A;
        synchronized (sf2) {
            try {
                if (sf2.f327o) {
                    throw new IOException("closed");
                }
                sf2.d(0, Integer.bitCount(c2880x702.a) * 6, 4, 0);
                int i = 0;
                while (i < 10) {
                    boolean z = true;
                    if (((1 << i) & c2880x702.a) == 0) {
                        z = false;
                    }
                    if (z) {
                        sf2.l.writeShort(i != 4 ? i != 7 ? i : 4 : 3);
                        sf2.l.writeInt(c2880x702.b[i]);
                    }
                    i++;
                }
                sf2.l.flush();
            } finally {
            }
        }
        if (kf.A.a() != 65535) {
            kf.H.m(0, r1 - 65535);
        }
        uc0.e().c(new IF(kf.n, kf.I, 2), 0L);
    }

    public final String toString() {
        Object obj;
        StringBuilder sb = new StringBuilder("Connection{");
        C2587u30 c2587u30 = this.b;
        sb.append(c2587u30.a.h.d);
        sb.append(':');
        sb.append(c2587u30.a.h.e);
        sb.append(", proxy=");
        sb.append(c2587u30.b);
        sb.append(" hostAddress=");
        sb.append(c2587u30.c);
        sb.append(" cipherSuite=");
        DE de = this.e;
        if (de == null || (obj = de.b) == null) {
            obj = "none";
        }
        sb.append(obj);
        sb.append(" protocol=");
        sb.append(this.f);
        sb.append('}');
        return sb.toString();
    }
}
