package o7;

import d3.X;
import d5.AbstractC0844a;
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.HostnameVerifier;
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;
import k7.C;
import k7.C1109a;
import k7.C1114f;
import k7.F;
import k7.n;
import k7.q;
import k7.w;
import k7.x;
import k7.y;
import l6.C1139a;
import r7.o;
import r7.p;
import v0.AbstractC1664c;
import x7.A;
import x7.B;
import x7.J;

/* JADX INFO: loaded from: classes.dex */
public final class l extends r7.h {

    /* JADX INFO: renamed from: b, reason: collision with root package name */
    public final F f15983b;

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

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

    /* JADX INFO: renamed from: e, reason: collision with root package name */
    public n f15986e;

    /* JADX INFO: renamed from: f, reason: collision with root package name */
    public x f15987f;

    /* JADX INFO: renamed from: g, reason: collision with root package name */
    public o f15988g;

    /* JADX INFO: renamed from: h, reason: collision with root package name */
    public B f15989h;
    public A i;

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

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

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

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

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

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

    /* JADX INFO: renamed from: p, reason: collision with root package name */
    public final ArrayList f15996p;

    /* JADX INFO: renamed from: q, reason: collision with root package name */
    public long f15997q;

    public l(R0.m mVar, F f3) {
        P6.g.e(mVar, "connectionPool");
        P6.g.e(f3, "route");
        this.f15983b = f3;
        this.f15995o = 1;
        this.f15996p = new ArrayList();
        this.f15997q = Long.MAX_VALUE;
    }

    public static void d(w wVar, F f3, IOException iOException) {
        P6.g.e(wVar, "client");
        P6.g.e(f3, "failedRoute");
        P6.g.e(iOException, "failure");
        if (f3.f14025b.type() != Proxy.Type.DIRECT) {
            C1109a c1109a = f3.f14024a;
            c1109a.f14040g.connectFailed(c1109a.f14041h.h(), f3.f14025b.address(), iOException);
        }
        C1139a c1139a = wVar.f14183P;
        synchronized (c1139a) {
            ((LinkedHashSet) c1139a.f14409q).add(f3);
        }
    }

    @Override // r7.h
    public final synchronized void a(o oVar, r7.A a3) {
        P6.g.e(oVar, "connection");
        P6.g.e(a3, "settings");
        this.f15995o = (a3.f16580a & 16) != 0 ? a3.f16581b[4] : Integer.MAX_VALUE;
    }

    @Override // r7.h
    public final void b(r7.w wVar) {
        wVar.c(8, null);
    }

    public final void c(int i, int i5, int i8, boolean z8, i iVar) throws Throwable {
        F f3;
        P6.g.e(iVar, "call");
        if (this.f15987f != null) {
            throw new IllegalStateException("already connected");
        }
        List list = this.f15983b.f14024a.f14042j;
        X x8 = new X(list);
        C1109a c1109a = this.f15983b.f14024a;
        if (c1109a.f14036c == null) {
            if (!list.contains(k7.j.f14085f)) {
                throw new m(new UnknownServiceException("CLEARTEXT communication not enabled for client"));
            }
            String str = this.f15983b.f14024a.f14041h.f14124d;
            s7.n nVar = s7.n.f16942a;
            if (!s7.n.f16942a.h(str)) {
                throw new m(new UnknownServiceException(I1.a.p("CLEARTEXT communication to ", str, " not permitted by network security policy")));
            }
        } else if (c1109a.i.contains(x.f14198v)) {
            throw new m(new UnknownServiceException("H2_PRIOR_KNOWLEDGE cannot be used with HTTPS"));
        }
        m mVar = null;
        do {
            try {
                F f7 = this.f15983b;
                if (f7.f14024a.f14036c != null && f7.f14025b.type() == Proxy.Type.HTTP) {
                    f(i, i5, i8, iVar);
                    if (this.f15984c == null) {
                    }
                    f3 = this.f15983b;
                    if (f3.f14024a.f14036c == null && f3.f14025b.type() == Proxy.Type.HTTP && this.f15984c == null) {
                        throw new m(new ProtocolException("Too many tunnel connections attempted: 21"));
                    }
                    this.f15997q = System.nanoTime();
                    return;
                }
                e(i, i5, iVar);
                g(x8, iVar);
                P6.g.e(this.f15983b.f14026c, "inetSocketAddress");
                f3 = this.f15983b;
                if (f3.f14024a.f14036c == null) {
                }
                this.f15997q = System.nanoTime();
                return;
            } catch (IOException e8) {
                Socket socket = this.f15985d;
                if (socket != null) {
                    l7.b.e(socket);
                }
                Socket socket2 = this.f15984c;
                if (socket2 != null) {
                    l7.b.e(socket2);
                }
                this.f15985d = null;
                this.f15984c = null;
                this.f15989h = null;
                this.i = null;
                this.f15986e = null;
                this.f15987f = null;
                this.f15988g = null;
                this.f15995o = 1;
                P6.g.e(this.f15983b.f14026c, "inetSocketAddress");
                if (mVar == null) {
                    mVar = new m(e8);
                } else {
                    android.support.v4.media.session.a.a(mVar.f15998q, e8);
                    mVar.f15999r = e8;
                }
                if (!z8) {
                    throw mVar;
                }
                x8.f11807c = true;
                if (!x8.f11806b) {
                    throw mVar;
                }
                if (e8 instanceof ProtocolException) {
                    throw mVar;
                }
                if (e8 instanceof InterruptedIOException) {
                    throw mVar;
                }
                if ((e8 instanceof SSLHandshakeException) && (e8.getCause() instanceof CertificateException)) {
                    throw mVar;
                }
                if (e8 instanceof SSLPeerUnverifiedException) {
                    throw mVar;
                }
            }
        } while (e8 instanceof SSLException);
        throw mVar;
    }

    public final void e(int i, int i5, i iVar) throws IOException {
        Socket socketCreateSocket;
        F f3 = this.f15983b;
        Proxy proxy = f3.f14025b;
        C1109a c1109a = f3.f14024a;
        Proxy.Type type = proxy.type();
        int i8 = type == null ? -1 : j.f15979a[type.ordinal()];
        if (i8 == 1 || i8 == 2) {
            socketCreateSocket = c1109a.f14035b.createSocket();
            P6.g.b(socketCreateSocket);
        } else {
            socketCreateSocket = new Socket(proxy);
        }
        this.f15984c = socketCreateSocket;
        InetSocketAddress inetSocketAddress = this.f15983b.f14026c;
        P6.g.e(iVar, "call");
        P6.g.e(inetSocketAddress, "inetSocketAddress");
        socketCreateSocket.setSoTimeout(i5);
        try {
            s7.n nVar = s7.n.f16942a;
            s7.n.f16942a.e(socketCreateSocket, this.f15983b.f14026c, i);
            try {
                this.f15989h = s7.l.c(s7.l.A(socketCreateSocket));
                this.i = s7.l.b(s7.l.y(socketCreateSocket));
            } catch (NullPointerException e8) {
                if (P6.g.a(e8.getMessage(), "throw with null exception")) {
                    throw new IOException(e8);
                }
            }
        } catch (ConnectException e9) {
            ConnectException connectException = new ConnectException("Failed to connect to " + this.f15983b.f14026c);
            connectException.initCause(e9);
            throw connectException;
        }
    }

    public final void f(int i, int i5, int i8, i iVar) throws IOException {
        K2.e eVar = new K2.e();
        F f3 = this.f15983b;
        q qVar = f3.f14024a.f14041h;
        P6.g.e(qVar, "url");
        eVar.f3552q = qVar;
        eVar.J("CONNECT", null);
        C1109a c1109a = f3.f14024a;
        eVar.G("Host", l7.b.w(c1109a.f14041h, true));
        eVar.G("Proxy-Connection", "Keep-Alive");
        eVar.G("User-Agent", "okhttp/4.12.0");
        y yVarQ = eVar.q();
        N0.c cVar = new N0.c(1);
        byte[] bArr = l7.b.f14412a;
        W3.b.f("Proxy-Authenticate");
        W3.b.g("OkHttp-Preemptive", "Proxy-Authenticate");
        cVar.k("Proxy-Authenticate");
        cVar.g("Proxy-Authenticate", "OkHttp-Preemptive");
        cVar.i();
        c1109a.f14039f.getClass();
        e(i, i5, iVar);
        String str = "CONNECT " + l7.b.w(yVarQ.f14202a, true) + " HTTP/1.1";
        B b8 = this.f15989h;
        P6.g.b(b8);
        A a3 = this.i;
        P6.g.b(a3);
        F4.a aVar = new F4.a(null, this, b8, a3);
        J jF = b8.f18724q.f();
        long j8 = i5;
        TimeUnit timeUnit = TimeUnit.MILLISECONDS;
        jF.g(j8);
        a3.f18721q.f().g(i8);
        aVar.m(yVarQ.f14204c, str);
        aVar.e();
        k7.B bG = aVar.g(false);
        P6.g.b(bG);
        bG.f13995a = yVarQ;
        C cA = bG.a();
        long jK = l7.b.k(cA);
        if (jK != -1) {
            q7.d dVarK = aVar.k(jK);
            l7.b.u(dVarK, Integer.MAX_VALUE);
            dVarK.close();
        }
        int i9 = cA.f14013t;
        if (i9 != 200) {
            if (i9 != 407) {
                throw new IOException(AbstractC0844a.j("Unexpected response code for CONNECT: ", i9));
            }
            c1109a.f14039f.getClass();
            throw new IOException("Failed to authenticate with proxy");
        }
        if (!b8.f18725r.u() || !a3.f18722r.u()) {
            throw new IOException("TLS tunnel buffered too many bytes!");
        }
    }

    /* JADX WARN: Type inference fix 'apply assigned field type' failed
    java.lang.UnsupportedOperationException: ArgType.getObject(), call class: class jadx.core.dex.instructions.args.ArgType$UnknownArg
    	at jadx.core.dex.instructions.args.ArgType.getObject(ArgType.java:593)
    	at jadx.core.dex.attributes.nodes.ClassTypeVarsAttr.getTypeVarsMapFor(ClassTypeVarsAttr.java:35)
    	at jadx.core.dex.nodes.utils.TypeUtils.replaceClassGenerics(TypeUtils.java:177)
    	at jadx.core.dex.visitors.typeinference.FixTypesVisitor.insertExplicitUseCast(FixTypesVisitor.java:397)
    	at jadx.core.dex.visitors.typeinference.FixTypesVisitor.tryFieldTypeWithNewCasts(FixTypesVisitor.java:359)
    	at jadx.core.dex.visitors.typeinference.FixTypesVisitor.applyFieldType(FixTypesVisitor.java:309)
    	at jadx.core.dex.visitors.typeinference.FixTypesVisitor.visit(FixTypesVisitor.java:94)
     */
    public final void g(X x8, i iVar) throws Throwable {
        C1109a c1109a = this.f15983b.f14024a;
        SSLSocketFactory sSLSocketFactory = c1109a.f14036c;
        x xVarJ = x.f14195s;
        if (sSLSocketFactory == null) {
            List list = c1109a.i;
            x xVar = x.f14198v;
            if (!list.contains(xVar)) {
                this.f15985d = this.f15984c;
                this.f15987f = xVarJ;
                return;
            } else {
                this.f15985d = this.f15984c;
                this.f15987f = xVar;
                m();
                return;
            }
        }
        P6.g.e(iVar, "call");
        C1109a c1109a2 = this.f15983b.f14024a;
        SSLSocketFactory sSLSocketFactory2 = c1109a2.f14036c;
        SSLSocket sSLSocket = null;
        String strF = null;
        try {
            P6.g.b(sSLSocketFactory2);
            Socket socket = this.f15984c;
            q qVar = c1109a2.f14041h;
            Socket socketCreateSocket = sSLSocketFactory2.createSocket(socket, qVar.f14124d, qVar.f14125e, true);
            P6.g.c(socketCreateSocket, "null cannot be cast to non-null type javax.net.ssl.SSLSocket");
            SSLSocket sSLSocket2 = (SSLSocket) socketCreateSocket;
            try {
                k7.j jVarA = x8.a(sSLSocket2);
                if (jVarA.f14087b) {
                    s7.n nVar = s7.n.f16942a;
                    s7.n.f16942a.d(sSLSocket2, c1109a2.f14041h.f14124d, c1109a2.i);
                }
                sSLSocket2.startHandshake();
                SSLSession session = sSLSocket2.getSession();
                P6.g.d(session, "sslSocketSession");
                n nVarR = W2.g.r(session);
                HostnameVerifier hostnameVerifier = c1109a2.f14037d;
                P6.g.b(hostnameVerifier);
                if (!hostnameVerifier.verify(c1109a2.f14041h.f14124d, session)) {
                    List listA = nVarR.a();
                    if (listA.isEmpty()) {
                        throw new SSLPeerUnverifiedException("Hostname " + c1109a2.f14041h.f14124d + " not verified (no certificates)");
                    }
                    Object obj = listA.get(0);
                    P6.g.c(obj, "null cannot be cast to non-null type java.security.cert.X509Certificate");
                    X509Certificate x509Certificate = (X509Certificate) obj;
                    StringBuilder sb = new StringBuilder("\n              |Hostname ");
                    sb.append(c1109a2.f14041h.f14124d);
                    sb.append(" not verified:\n              |    certificate: ");
                    C1114f c1114f = C1114f.f14058c;
                    sb.append(AbstractC1664c.x(x509Certificate));
                    sb.append("\n              |    DN: ");
                    sb.append(x509Certificate.getSubjectDN().getName());
                    sb.append("\n              |    subjectAltNames: ");
                    sb.append(B6.k.q0(w7.c.a(x509Certificate, 7), w7.c.a(x509Certificate, 2)));
                    sb.append("\n              ");
                    throw new SSLPeerUnverifiedException(X6.g.D(sb.toString()));
                }
                C1114f c1114f2 = c1109a2.f14038e;
                P6.g.b(c1114f2);
                this.f15986e = new n(nVarR.f14107a, nVarR.f14108b, nVarR.f14109c, new k(c1114f2, nVarR, c1109a2));
                P6.g.e(c1109a2.f14041h.f14124d, "hostname");
                Iterator it = c1114f2.f14059a.iterator();
                if (it.hasNext()) {
                    it.next().getClass();
                    throw new ClassCastException();
                }
                if (jVarA.f14087b) {
                    s7.n nVar2 = s7.n.f16942a;
                    strF = s7.n.f16942a.f(sSLSocket2);
                }
                this.f15985d = sSLSocket2;
                this.f15989h = s7.l.c(s7.l.A(sSLSocket2));
                this.i = s7.l.b(s7.l.y(sSLSocket2));
                if (strF != null) {
                    xVarJ = android.support.v4.media.session.a.j(strF);
                }
                this.f15987f = xVarJ;
                s7.n nVar3 = s7.n.f16942a;
                s7.n.f16942a.a(sSLSocket2);
                if (this.f15987f == x.f14197u) {
                    m();
                }
            } catch (Throwable th) {
                th = th;
                sSLSocket = sSLSocket2;
                if (sSLSocket != null) {
                    s7.n nVar4 = s7.n.f16942a;
                    s7.n.f16942a.a(sSLSocket);
                }
                if (sSLSocket != null) {
                    l7.b.e(sSLSocket);
                }
                throw th;
            }
        } catch (Throwable th2) {
            th = th2;
        }
    }

    public final synchronized void h() {
        this.f15993m++;
    }

    /* JADX WARN: Removed duplicated region for block: B:53:0x00af A[EXC_TOP_SPLITTER, SYNTHETIC] */
    /*
        Code decompiled incorrectly, please refer to instructions dump.
    */
    public final boolean i(C1109a c1109a, List list) {
        n nVar;
        byte[] bArr = l7.b.f14412a;
        if (this.f15996p.size() < this.f15995o && !this.f15990j) {
            F f3 = this.f15983b;
            if (f3.f14024a.a(c1109a)) {
                q qVar = c1109a.f14041h;
                String str = qVar.f14124d;
                C1109a c1109a2 = f3.f14024a;
                if (P6.g.a(str, c1109a2.f14041h.f14124d)) {
                    return true;
                }
                if (this.f15988g != null && list != null && !list.isEmpty()) {
                    Iterator it = list.iterator();
                    while (true) {
                        if (!it.hasNext()) {
                            break;
                        }
                        F f7 = (F) it.next();
                        Proxy.Type type = f7.f14025b.type();
                        Proxy.Type type2 = Proxy.Type.DIRECT;
                        if (type == type2 && f3.f14025b.type() == type2) {
                            if (P6.g.a(f3.f14026c, f7.f14026c)) {
                                if (c1109a.f14037d == w7.c.f18319a) {
                                    byte[] bArr2 = l7.b.f14412a;
                                    q qVar2 = c1109a2.f14041h;
                                    if (qVar.f14125e == qVar2.f14125e) {
                                        String str2 = qVar2.f14124d;
                                        String str3 = qVar.f14124d;
                                        if (!P6.g.a(str3, str2)) {
                                            if (!this.f15991k && (nVar = this.f15986e) != null) {
                                                List listA = nVar.a();
                                                if (!listA.isEmpty()) {
                                                    Object obj = listA.get(0);
                                                    P6.g.c(obj, "null cannot be cast to non-null type java.security.cert.X509Certificate");
                                                    if (w7.c.c(str3, (X509Certificate) obj)) {
                                                        try {
                                                            C1114f c1114f = c1109a.f14038e;
                                                            P6.g.b(c1114f);
                                                            n nVar2 = this.f15986e;
                                                            P6.g.b(nVar2);
                                                            List listA2 = nVar2.a();
                                                            P6.g.e(str3, "hostname");
                                                            P6.g.e(listA2, "peerCertificates");
                                                            Iterator it2 = c1114f.f14059a.iterator();
                                                            if (!it2.hasNext()) {
                                                                return true;
                                                            }
                                                            it2.next().getClass();
                                                            throw new ClassCastException();
                                                        } catch (SSLPeerUnverifiedException unused) {
                                                        }
                                                    }
                                                }
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
        return false;
    }

    public final boolean j(boolean z8) {
        long j8;
        byte[] bArr = l7.b.f14412a;
        long jNanoTime = System.nanoTime();
        Socket socket = this.f15984c;
        P6.g.b(socket);
        Socket socket2 = this.f15985d;
        P6.g.b(socket2);
        P6.g.b(this.f15989h);
        if (socket.isClosed() || socket2.isClosed() || socket2.isInputShutdown() || socket2.isOutputShutdown()) {
            return false;
        }
        o oVar = this.f15988g;
        if (oVar != null) {
            return oVar.g(jNanoTime);
        }
        synchronized (this) {
            j8 = jNanoTime - this.f15997q;
        }
        if (j8 < 10000000000L || !z8) {
            return true;
        }
        try {
            int soTimeout = socket2.getSoTimeout();
            try {
                socket2.setSoTimeout(1);
                return !r4.a();
            } finally {
                socket2.setSoTimeout(soTimeout);
            }
        } catch (SocketTimeoutException unused) {
            return true;
        } catch (IOException unused2) {
            return false;
        }
    }

    public final p7.b k(w wVar, p7.d dVar) throws SocketException {
        P6.g.e(wVar, "client");
        Socket socket = this.f15985d;
        P6.g.b(socket);
        B b8 = this.f15989h;
        P6.g.b(b8);
        A a3 = this.i;
        P6.g.b(a3);
        o oVar = this.f15988g;
        if (oVar != null) {
            return new p(wVar, this, dVar, oVar);
        }
        int i = dVar.f16163g;
        socket.setSoTimeout(i);
        TimeUnit timeUnit = TimeUnit.MILLISECONDS;
        b8.f18724q.f().g(i);
        a3.f18721q.f().g(dVar.f16164h);
        return new F4.a(wVar, this, b8, a3);
    }

    public final synchronized void l() {
        this.f15990j = true;
    }

    public final void m() throws SocketException {
        Socket socket = this.f15985d;
        P6.g.b(socket);
        B b8 = this.f15989h;
        P6.g.b(b8);
        A a3 = this.i;
        P6.g.b(a3);
        socket.setSoTimeout(0);
        n7.d dVar = n7.d.f15542h;
        P6.g.e(dVar, "taskRunner");
        O3.q qVar = new O3.q();
        qVar.f5166b = dVar;
        qVar.f5170f = r7.h.f16612a;
        String str = this.f15983b.f14024a.f14041h.f14124d;
        P6.g.e(str, "peerName");
        qVar.f5167c = socket;
        String str2 = l7.b.f14418g + ' ' + str;
        P6.g.e(str2, "<set-?>");
        qVar.f5165a = str2;
        qVar.f5168d = b8;
        qVar.f5169e = a3;
        qVar.f5170f = this;
        o oVar = new o(qVar);
        this.f15988g = oVar;
        r7.A a8 = o.f16632P;
        this.f15995o = (a8.f16580a & 16) != 0 ? a8.f16581b[4] : Integer.MAX_VALUE;
        r7.x xVar = oVar.f16643M;
        synchronized (xVar) {
            try {
                if (xVar.f16702t) {
                    throw new IOException("closed");
                }
                Logger logger = r7.x.f16698v;
                if (logger.isLoggable(Level.FINE)) {
                    logger.fine(l7.b.i(">> CONNECTION " + r7.f.f16608a.d(), new Object[0]));
                }
                xVar.f16699q.x(r7.f.f16608a);
                xVar.f16699q.flush();
            } catch (Throwable th) {
                throw th;
            }
        }
        oVar.f16643M.u(oVar.f16637F);
        if (oVar.f16637F.a() != 65535) {
            oVar.f16643M.A(r1 - 65535, 0);
        }
        dVar.e().c(new n7.b(oVar.f16648s, oVar.f16644N, 0), 0L);
    }

    public final String toString() {
        Object obj;
        StringBuilder sb = new StringBuilder("Connection{");
        F f3 = this.f15983b;
        sb.append(f3.f14024a.f14041h.f14124d);
        sb.append(':');
        sb.append(f3.f14024a.f14041h.f14125e);
        sb.append(", proxy=");
        sb.append(f3.f14025b);
        sb.append(" hostAddress=");
        sb.append(f3.f14026c);
        sb.append(" cipherSuite=");
        n nVar = this.f15986e;
        if (nVar == null || (obj = nVar.f14108b) == null) {
            obj = "none";
        }
        sb.append(obj);
        sb.append(" protocol=");
        sb.append(this.f15987f);
        sb.append('}');
        return sb.toString();
    }
}
