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 f6173b;

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

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

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

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

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

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

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

    /* JADX INFO: renamed from: j, reason: collision with root package name */
    public h.g f6181j;
    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.f6173b = hVar;
        this.f6174c = g0Var;
    }

    @Override // g.i0.i.g.d
    public void a(g.i0.i.g gVar) {
        synchronized (this.f6173b) {
            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.f6174c;
        Proxy proxy = g0Var.f6130b;
        this.f6175d = (proxy.type() == Proxy.Type.DIRECT || proxy.type() == Proxy.Type.HTTP) ? g0Var.f6129a.f6064c.createSocket() : new Socket(proxy);
        InetSocketAddress inetSocketAddress = this.f6174c.f6131c;
        Objects.requireNonNull(nVar);
        this.f6175d.setSoTimeout(i3);
        try {
            g.i0.j.f.f6419a.g(this.f6175d, this.f6174c.f6131c, i2);
            try {
                this.f6180i = new s(h.p.d(this.f6175d));
                this.f6181j = new r(h.p.b(this.f6175d));
            } 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.f6174c.f6131c);
            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.f6174c.f6129a.f6062a);
        aVar.c("CONNECT", null);
        aVar.b("Host", g.i0.c.m(this.f6174c.f6129a.f6062a, 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.f6098a = zVarA;
        aVar2.f6099b = x.HTTP_1_1;
        aVar2.f6100c = 407;
        aVar2.f6101d = "Preemptive Authenticate";
        aVar2.f6104g = g.i0.c.f6156c;
        aVar2.k = -1L;
        aVar2.l = -1L;
        r.a aVar3 = aVar2.f6103f;
        Objects.requireNonNull(aVar3);
        g.r.a("Proxy-Authenticate");
        g.r.b("OkHttp-Preemptive", "Proxy-Authenticate");
        aVar3.c("Proxy-Authenticate");
        aVar3.f6452a.add("Proxy-Authenticate");
        aVar3.f6452a.add("OkHttp-Preemptive");
        aVar2.a();
        Objects.requireNonNull(this.f6174c.f6129a.f6065d);
        g.s sVar = zVarA.f6518a;
        d(i2, i3, dVar, nVar);
        String str = "CONNECT " + g.i0.c.m(sVar, true) + " HTTP/1.1";
        h.h hVar = this.f6180i;
        h.g gVar = this.f6181j;
        g.i0.h.a aVar4 = new g.i0.h.a(null, null, hVar, gVar);
        TimeUnit timeUnit = TimeUnit.MILLISECONDS;
        hVar.e().g(i3, timeUnit);
        this.f6181j.e().g(i4, timeUnit);
        aVar4.l(zVarA.f6520c, str);
        gVar.flush();
        d0.a aVarG = aVar4.g(false);
        aVarG.f6098a = 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.f6091d;
        if (i5 == 200) {
            if (!this.f6180i.b().q() || !this.f6181j.b().q()) {
                throw new IOException("TLS tunnel buffered too many bytes!");
            }
        } else {
            if (i5 == 407) {
                Objects.requireNonNull(this.f6174c.f6129a.f6065d);
                throw new IOException("Failed to authenticate with proxy");
            }
            StringBuilder sbC = a.b.c.a.a.C("Unexpected response code for CONNECT: ");
            sbC.append(d0VarA.f6091d);
            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.f6174c.f6129a;
        if (aVar.f6070i == null) {
            List<x> list = aVar.f6066e;
            x xVar = x.H2_PRIOR_KNOWLEDGE;
            if (!list.contains(xVar)) {
                this.f6176e = this.f6175d;
                this.f6178g = x.HTTP_1_1;
                return;
            } else {
                this.f6176e = this.f6175d;
                this.f6178g = xVar;
                j(i2);
                return;
            }
        }
        Objects.requireNonNull(nVar);
        g.a aVar2 = this.f6174c.f6129a;
        SSLSocketFactory sSLSocketFactory = aVar2.f6070i;
        try {
            try {
                Socket socket = this.f6175d;
                g.s sVar = aVar2.f6062a;
                sSLSocket = (SSLSocket) sSLSocketFactory.createSocket(socket, sVar.f6457e, sVar.f6458f, true);
            } catch (Throwable th) {
                th = th;
                sSLSocket = null;
            }
        } catch (AssertionError e2) {
            e = e2;
        }
        try {
            i iVarA = bVar.a(sSLSocket);
            if (iVarA.f6145f) {
                g.i0.j.f.f6419a.f(sSLSocket, aVar2.f6062a.f6457e, aVar2.f6066e);
            }
            sSLSocket.startHandshake();
            SSLSession session = sSLSocket.getSession();
            q qVarA = q.a(session);
            if (!aVar2.f6071j.verify(aVar2.f6062a.f6457e, session)) {
                X509Certificate x509Certificate = (X509Certificate) qVarA.f6449c.get(0);
                throw new SSLPeerUnverifiedException("Hostname " + aVar2.f6062a.f6457e + " 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.f6062a.f6457e, qVarA.f6449c);
            String strI = iVarA.f6145f ? g.i0.j.f.f6419a.i(sSLSocket) : null;
            this.f6176e = sSLSocket;
            this.f6180i = new s(h.p.d(sSLSocket));
            this.f6181j = new h.r(h.p.b(this.f6176e));
            this.f6177f = qVarA;
            this.f6178g = strI != null ? x.get(strI) : x.HTTP_1_1;
            g.i0.j.f.f6419a.a(sSLSocket);
            if (this.f6178g == 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.f6419a.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.f6152a;
            g.a aVar3 = this.f6174c.f6129a;
            Objects.requireNonNull((w.a) aVar2);
            if (!aVar3.a(aVar)) {
                return false;
            }
            if (aVar.f6062a.f6457e.equals(this.f6174c.f6129a.f6062a.f6457e)) {
                return true;
            }
            if (this.f6179h == null || g0Var == null || g0Var.f6130b.type() != Proxy.Type.DIRECT || this.f6174c.f6130b.type() != Proxy.Type.DIRECT || !this.f6174c.f6131c.equals(g0Var.f6131c) || g0Var.f6129a.f6071j != g.i0.l.d.f6423a || !k(aVar.f6062a)) {
                return false;
            }
            try {
                aVar.k.a(aVar.f6062a.f6457e, this.f6177f.f6449c);
                return true;
            } catch (SSLPeerUnverifiedException unused) {
            }
        }
        return false;
    }

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

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

    public final void j(int i2) throws SocketException {
        this.f6176e.setSoTimeout(0);
        g.c cVar = new g.c(true);
        Socket socket = this.f6176e;
        String str = this.f6174c.f6129a.f6062a.f6457e;
        h.h hVar = this.f6180i;
        h.g gVar = this.f6181j;
        cVar.f6311a = socket;
        cVar.f6312b = str;
        cVar.f6313c = hVar;
        cVar.f6314d = gVar;
        cVar.f6315e = this;
        cVar.f6316f = i2;
        g.i0.i.g gVar2 = new g.i0.i.g(cVar);
        this.f6179h = gVar2;
        g.i0.i.q qVar = gVar2.t;
        synchronized (qVar) {
            if (qVar.f6380g) {
                throw new IOException("closed");
            }
            if (qVar.f6377d) {
                Logger logger = g.i0.i.q.f6375b;
                if (logger.isLoggable(Level.FINE)) {
                    logger.fine(g.i0.c.l(">> CONNECTION %s", g.i0.i.e.f6282a.g()));
                }
                qVar.f6376c.f(g.i0.i.e.f6282a.n());
                qVar.f6376c.flush();
            }
        }
        g.i0.i.q qVar2 = gVar2.t;
        g.i0.i.t tVar = gVar2.p;
        synchronized (qVar2) {
            if (qVar2.f6380g) {
                throw new IOException("closed");
            }
            qVar2.o(0, Integer.bitCount(tVar.f6390a) * 6, (byte) 4, (byte) 0);
            int i3 = 0;
            while (i3 < 10) {
                if (((1 << i3) & tVar.f6390a) != 0) {
                    qVar2.f6376c.m(i3 == 4 ? 3 : i3 == 7 ? 4 : i3);
                    qVar2.f6376c.d(tVar.f6391b[i3]);
                }
                i3++;
            }
            qVar2.f6376c.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.f6458f;
        g.s sVar2 = this.f6174c.f6129a.f6062a;
        if (i2 != sVar2.f6458f) {
            return false;
        }
        if (sVar.f6457e.equals(sVar2.f6457e)) {
            return true;
        }
        q qVar = this.f6177f;
        return qVar != null && g.i0.l.d.f6423a.c(sVar.f6457e, (X509Certificate) qVar.f6449c.get(0));
    }

    public String toString() {
        StringBuilder sbC = a.b.c.a.a.C("Connection{");
        sbC.append(this.f6174c.f6129a.f6062a.f6457e);
        sbC.append(":");
        sbC.append(this.f6174c.f6129a.f6062a.f6458f);
        sbC.append(", proxy=");
        sbC.append(this.f6174c.f6130b);
        sbC.append(" hostAddress=");
        sbC.append(this.f6174c.f6131c);
        sbC.append(" cipherSuite=");
        q qVar = this.f6177f;
        sbC.append(qVar != null ? qVar.f6448b : "none");
        sbC.append(" protocol=");
        sbC.append(this.f6178g);
        sbC.append('}');
        return sbC.toString();
    }
}
