package g.i0.f;

import com.seewo.vcommons.constants.Platform;
import g.d0;
import g.g0;
import g.h;
import g.i;
import g.i0.h.a;
import g.i0.i.g;
import g.i0.i.p;
import g.n;
import g.q;
import g.r;
import g.t;
import g.w;
import g.x;
import g.z;
import h.r;
import h.s;
import h.y;
import java.io.IOException;
import java.lang.ref.Reference;
import java.net.ConnectException;
import java.net.InetSocketAddress;
import java.net.Proxy;
import java.net.Socket;
import java.net.SocketException;
import java.security.cert.X509Certificate;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.concurrent.TimeUnit;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.annotation.Nullable;
import javax.net.ssl.SSLPeerUnverifiedException;
import javax.net.ssl.SSLSession;
import javax.net.ssl.SSLSocket;
import javax.net.ssl.SSLSocketFactory;

/* JADX INFO: loaded from: classes.dex */
public final class c extends g.d {

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

    /* JADX INFO: renamed from: c, reason: collision with root package name */
    public final g0 f6160c;

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

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

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

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

    /* JADX INFO: renamed from: h, reason: collision with root package name */
    public g.i0.i.g f6165h;

    /* JADX INFO: renamed from: i, reason: collision with root package name */
    public h.h f6166i;

    /* JADX INFO: renamed from: j, reason: collision with root package name */
    public h.g f6167j;
    public boolean k;
    public int l;
    public int m = 1;
    public final List<Reference<g>> n = new ArrayList();
    public long o = Long.MAX_VALUE;

    public c(h hVar, g0 g0Var) {
        this.f6159b = hVar;
        this.f6160c = g0Var;
    }

    @Override // g.i0.i.g.d
    public void a(g.i0.i.g gVar) {
        synchronized (this.f6159b) {
            this.m = gVar.z();
        }
    }

    @Override // g.i0.i.g.d
    public void b(p pVar) {
        pVar.c(g.i0.i.b.REFUSED_STREAM);
    }

    /* JADX WARN: Removed duplicated region for block: B:103:0x0137 A[SYNTHETIC] */
    /* JADX WARN: Removed duplicated region for block: B:37:0x00ae  */
    /* JADX WARN: Removed duplicated region for block: B:45:0x00be A[ORIG_RETURN, RETURN] */
    /* JADX WARN: Removed duplicated region for block: B:54:0x00ee  */
    /* JADX WARN: Removed duplicated region for block: B:55:0x00f4  */
    /* JADX WARN: Removed duplicated region for block: B:60:0x0105  */
    /*
        Code decompiled incorrectly, please refer to instructions dump.
        To view partially-correct add '--show-bad-code' argument
    */
    public void c(int r16, int r17, int r18, int r19, boolean r20, g.d r21, g.n r22) throws java.lang.Throwable {
        /*
            Method dump skipped, instruction units count: 333
            To view this dump add '--comments-level debug' option
        */
        throw new UnsupportedOperationException("Method not decompiled: g.i0.f.c.c(int, int, int, int, boolean, g.d, g.n):void");
    }

    public final void d(int i2, int i3, g.d dVar, n nVar) throws IOException {
        g0 g0Var = this.f6160c;
        Proxy proxy = g0Var.f6116b;
        this.f6161d = (proxy.type() == Proxy.Type.DIRECT || proxy.type() == Proxy.Type.HTTP) ? g0Var.f6115a.f6050c.createSocket() : new Socket(proxy);
        InetSocketAddress inetSocketAddress = this.f6160c.f6117c;
        Objects.requireNonNull(nVar);
        this.f6161d.setSoTimeout(i3);
        try {
            g.i0.j.f.f6405a.g(this.f6161d, this.f6160c.f6117c, i2);
            try {
                this.f6166i = new s(h.p.d(this.f6161d));
                this.f6167j = new r(h.p.b(this.f6161d));
            } catch (NullPointerException e2) {
                if ("throw with null exception".equals(e2.getMessage())) {
                    throw new IOException(e2);
                }
            }
        } catch (ConnectException e3) {
            StringBuilder sbC = a.b.c.a.a.C("Failed to connect to ");
            sbC.append(this.f6160c.f6117c);
            ConnectException connectException = new ConnectException(sbC.toString());
            connectException.initCause(e3);
            throw connectException;
        }
    }

    public final void e(int i2, int i3, int i4, g.d dVar, n nVar) throws IOException {
        z.a aVar = new z.a();
        aVar.e(this.f6160c.f6115a.f6048a);
        aVar.c("CONNECT", null);
        aVar.b("Host", g.i0.c.m(this.f6160c.f6115a.f6048a, true));
        aVar.b("Proxy-Connection", "Keep-Alive");
        aVar.b("User-Agent", "okhttp/3.12.0");
        z zVarA = aVar.a();
        d0.a aVar2 = new d0.a();
        aVar2.f6084a = zVarA;
        aVar2.f6085b = x.HTTP_1_1;
        aVar2.f6086c = 407;
        aVar2.f6087d = "Preemptive Authenticate";
        aVar2.f6090g = g.i0.c.f6142c;
        aVar2.k = -1L;
        aVar2.l = -1L;
        r.a aVar3 = aVar2.f6089f;
        Objects.requireNonNull(aVar3);
        g.r.a("Proxy-Authenticate");
        g.r.b("OkHttp-Preemptive", "Proxy-Authenticate");
        aVar3.c("Proxy-Authenticate");
        aVar3.f6438a.add("Proxy-Authenticate");
        aVar3.f6438a.add("OkHttp-Preemptive");
        aVar2.a();
        Objects.requireNonNull(this.f6160c.f6115a.f6051d);
        g.s sVar = zVarA.f6504a;
        d(i2, i3, dVar, nVar);
        String str = "CONNECT " + g.i0.c.m(sVar, true) + " HTTP/1.1";
        h.h hVar = this.f6166i;
        h.g gVar = this.f6167j;
        g.i0.h.a aVar4 = new g.i0.h.a(null, null, hVar, gVar);
        TimeUnit timeUnit = TimeUnit.MILLISECONDS;
        hVar.e().g(i3, timeUnit);
        this.f6167j.e().g(i4, timeUnit);
        aVar4.l(zVarA.f6506c, str);
        gVar.flush();
        d0.a aVarG = aVar4.g(false);
        aVarG.f6084a = zVarA;
        d0 d0VarA = aVarG.a();
        long jA = g.i0.g.e.a(d0VarA);
        if (jA == -1) {
            jA = 0;
        }
        h.x xVarI = aVar4.i(jA);
        g.i0.c.t(xVarI, Platform.UNKNOWN, timeUnit);
        ((a.f) xVarI).close();
        int i5 = d0VarA.f6077d;
        if (i5 == 200) {
            if (!this.f6166i.b().q() || !this.f6167j.b().q()) {
                throw new IOException("TLS tunnel buffered too many bytes!");
            }
        } else {
            if (i5 == 407) {
                Objects.requireNonNull(this.f6160c.f6115a.f6051d);
                throw new IOException("Failed to authenticate with proxy");
            }
            StringBuilder sbC = a.b.c.a.a.C("Unexpected response code for CONNECT: ");
            sbC.append(d0VarA.f6077d);
            throw new IOException(sbC.toString());
        }
    }

    public final void f(b bVar, int i2, g.d dVar, n nVar) throws Throwable {
        SSLSocket sSLSocket;
        g.a aVar = this.f6160c.f6115a;
        if (aVar.f6056i == null) {
            List<x> list = aVar.f6052e;
            x xVar = x.H2_PRIOR_KNOWLEDGE;
            if (!list.contains(xVar)) {
                this.f6162e = this.f6161d;
                this.f6164g = x.HTTP_1_1;
                return;
            } else {
                this.f6162e = this.f6161d;
                this.f6164g = xVar;
                j(i2);
                return;
            }
        }
        Objects.requireNonNull(nVar);
        g.a aVar2 = this.f6160c.f6115a;
        SSLSocketFactory sSLSocketFactory = aVar2.f6056i;
        try {
            try {
                Socket socket = this.f6161d;
                g.s sVar = aVar2.f6048a;
                sSLSocket = (SSLSocket) sSLSocketFactory.createSocket(socket, sVar.f6443e, sVar.f6444f, true);
            } catch (Throwable th) {
                th = th;
                sSLSocket = null;
            }
        } catch (AssertionError e2) {
            e = e2;
        }
        try {
            i iVarA = bVar.a(sSLSocket);
            if (iVarA.f6131f) {
                g.i0.j.f.f6405a.f(sSLSocket, aVar2.f6048a.f6443e, aVar2.f6052e);
            }
            sSLSocket.startHandshake();
            SSLSession session = sSLSocket.getSession();
            q qVarA = q.a(session);
            if (!aVar2.f6057j.verify(aVar2.f6048a.f6443e, session)) {
                X509Certificate x509Certificate = (X509Certificate) qVarA.f6435c.get(0);
                throw new SSLPeerUnverifiedException("Hostname " + aVar2.f6048a.f6443e + " not verified:\n    certificate: " + g.f.b(x509Certificate) + "\n    DN: " + x509Certificate.getSubjectDN().getName() + "\n    subjectAltNames: " + g.i0.l.d.a(x509Certificate));
            }
            aVar2.k.a(aVar2.f6048a.f6443e, qVarA.f6435c);
            String strI = iVarA.f6131f ? g.i0.j.f.f6405a.i(sSLSocket) : null;
            this.f6162e = sSLSocket;
            this.f6166i = new s(h.p.d(sSLSocket));
            this.f6167j = new h.r(h.p.b(this.f6162e));
            this.f6163f = qVarA;
            this.f6164g = strI != null ? x.get(strI) : x.HTTP_1_1;
            g.i0.j.f.f6405a.a(sSLSocket);
            if (this.f6164g == x.HTTP_2) {
                j(i2);
            }
        } catch (AssertionError e3) {
            e = e3;
            if (!g.i0.c.r(e)) {
                throw e;
            }
            throw new IOException(e);
        } catch (Throwable th2) {
            th = th2;
            if (sSLSocket != null) {
                g.i0.j.f.f6405a.a(sSLSocket);
            }
            g.i0.c.e(sSLSocket);
            throw th;
        }
    }

    public boolean g(g.a aVar, @Nullable g0 g0Var) {
        if (this.n.size() < this.m && !this.k) {
            g.i0.a aVar2 = g.i0.a.f6138a;
            g.a aVar3 = this.f6160c.f6115a;
            Objects.requireNonNull((w.a) aVar2);
            if (!aVar3.a(aVar)) {
                return false;
            }
            if (aVar.f6048a.f6443e.equals(this.f6160c.f6115a.f6048a.f6443e)) {
                return true;
            }
            if (this.f6165h == null || g0Var == null || g0Var.f6116b.type() != Proxy.Type.DIRECT || this.f6160c.f6116b.type() != Proxy.Type.DIRECT || !this.f6160c.f6117c.equals(g0Var.f6117c) || g0Var.f6115a.f6057j != g.i0.l.d.f6409a || !k(aVar.f6048a)) {
                return false;
            }
            try {
                aVar.k.a(aVar.f6048a.f6443e, this.f6163f.f6435c);
                return true;
            } catch (SSLPeerUnverifiedException unused) {
            }
        }
        return false;
    }

    public boolean h() {
        return this.f6165h != null;
    }

    public g.i0.g.c i(w wVar, t.a aVar, g gVar) throws SocketException {
        if (this.f6165h != null) {
            return new g.i0.i.f(wVar, aVar, gVar, this.f6165h);
        }
        g.i0.g.f fVar = (g.i0.g.f) aVar;
        this.f6162e.setSoTimeout(fVar.f6207j);
        y yVarE = this.f6166i.e();
        long j2 = fVar.f6207j;
        TimeUnit timeUnit = TimeUnit.MILLISECONDS;
        yVarE.g(j2, timeUnit);
        this.f6167j.e().g(fVar.k, timeUnit);
        return new g.i0.h.a(wVar, gVar, this.f6166i, this.f6167j);
    }

    public final void j(int i2) throws SocketException {
        this.f6162e.setSoTimeout(0);
        g.c cVar = new g.c(true);
        Socket socket = this.f6162e;
        String str = this.f6160c.f6115a.f6048a.f6443e;
        h.h hVar = this.f6166i;
        h.g gVar = this.f6167j;
        cVar.f6297a = socket;
        cVar.f6298b = str;
        cVar.f6299c = hVar;
        cVar.f6300d = gVar;
        cVar.f6301e = this;
        cVar.f6302f = i2;
        g.i0.i.g gVar2 = new g.i0.i.g(cVar);
        this.f6165h = gVar2;
        g.i0.i.q qVar = gVar2.t;
        synchronized (qVar) {
            if (qVar.f6366g) {
                throw new IOException("closed");
            }
            if (qVar.f6363d) {
                Logger logger = g.i0.i.q.f6361b;
                if (logger.isLoggable(Level.FINE)) {
                    logger.fine(g.i0.c.l(">> CONNECTION %s", g.i0.i.e.f6268a.g()));
                }
                qVar.f6362c.f(g.i0.i.e.f6268a.n());
                qVar.f6362c.flush();
            }
        }
        g.i0.i.q qVar2 = gVar2.t;
        g.i0.i.t tVar = gVar2.p;
        synchronized (qVar2) {
            if (qVar2.f6366g) {
                throw new IOException("closed");
            }
            qVar2.o(0, Integer.bitCount(tVar.f6376a) * 6, (byte) 4, (byte) 0);
            int i3 = 0;
            while (i3 < 10) {
                if (((1 << i3) & tVar.f6376a) != 0) {
                    qVar2.f6362c.m(i3 == 4 ? 3 : i3 == 7 ? 4 : i3);
                    qVar2.f6362c.d(tVar.f6377b[i3]);
                }
                i3++;
            }
            qVar2.f6362c.flush();
        }
        if (gVar2.p.a() != 65535) {
            gVar2.t.L(0, r0 - 65535);
        }
        new Thread(gVar2.u).start();
    }

    public boolean k(g.s sVar) {
        int i2 = sVar.f6444f;
        g.s sVar2 = this.f6160c.f6115a.f6048a;
        if (i2 != sVar2.f6444f) {
            return false;
        }
        if (sVar.f6443e.equals(sVar2.f6443e)) {
            return true;
        }
        q qVar = this.f6163f;
        return qVar != null && g.i0.l.d.f6409a.c(sVar.f6443e, (X509Certificate) qVar.f6435c.get(0));
    }

    public String toString() {
        StringBuilder sbC = a.b.c.a.a.C("Connection{");
        sbC.append(this.f6160c.f6115a.f6048a.f6443e);
        sbC.append(":");
        sbC.append(this.f6160c.f6115a.f6048a.f6444f);
        sbC.append(", proxy=");
        sbC.append(this.f6160c.f6116b);
        sbC.append(" hostAddress=");
        sbC.append(this.f6160c.f6117c);
        sbC.append(" cipherSuite=");
        q qVar = this.f6163f;
        sbC.append(qVar != null ? qVar.f6434b : "none");
        sbC.append(" protocol=");
        sbC.append(this.f6164g);
        sbC.append('}');
        return sbC.toString();
    }
}
