package 㿎.ሽ.ඓ;

import androidx.recyclerview.widget.RecyclerView;
import com.facebook.GraphRequest;
import com.facebook.internal.security.CertificateUtil;
import java.io.IOException;
import java.io.InterruptedIOException;
import java.lang.ref.Reference;
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.UnknownServiceException;
import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;
import javax.net.ssl.SSLHandshakeException;
import javax.net.ssl.SSLPeerUnverifiedException;
import javax.net.ssl.SSLProtocolException;
import javax.net.ssl.SSLSession;
import javax.net.ssl.SSLSocket;
import javax.net.ssl.SSLSocketFactory;
import okhttp3.Protocol;
import okhttp3.internal.connection.RouteException;
import okhttp3.internal.http2.ErrorCode;
import ඓ.K.K.K.C1293;
import ᨨ.AbstractC1483;
import ᨨ.AbstractC1494;
import ᨨ.AbstractC1496;
import ᨨ.C1479;
import ᨨ.C1486;
import ᨨ.C1488;
import 㿎.AbstractC2533;
import 㿎.AbstractC2537;
import 㿎.AbstractC2635;
import 㿎.AbstractC2636;
import 㿎.AbstractC2661;
import 㿎.C2531;
import 㿎.C2536;
import 㿎.C2539;
import 㿎.C2625;
import 㿎.C2630;
import 㿎.C2638;
import 㿎.C2640;
import 㿎.C2644;
import 㿎.C2655;
import 㿎.C2671;
import 㿎.ሽ.AbstractC2570;
import 㿎.ሽ.C2540;
import 㿎.ሽ.ᨨ.C2558;
import 㿎.ሽ.ⁱ.C2562;
import 㿎.ሽ.㳳.C2575;
import 㿎.ሽ.㴹.AbstractC2576;
import 㿎.ሽ.㴹.C2577;
import 㿎.ሽ.㴹.C2583;
import 㿎.ሽ.㿎.C2591;
import 㿎.ሽ.㿎.C2619;
import 㿎.ሽ.㿎.C2621;

/* renamed from: 㿎.ሽ.ඓ.ʬ  reason: contains not printable characters */
/* compiled from: RealConnection */
public final class C2543 extends C2591.AbstractC2603 implements AbstractC2635 {

    /* renamed from: ʬ  reason: contains not printable characters */
    public final C2539 f8579;

    /* renamed from: Ԕ  reason: contains not printable characters */
    public final List<Reference<C2548>> f8580 = new ArrayList();

    /* renamed from: ඓ  reason: contains not printable characters */
    public Socket f8581;

    /* renamed from: ᕣ  reason: contains not printable characters */
    public boolean f8582;

    /* renamed from: ᨨ  reason: contains not printable characters */
    public AbstractC1496 f8583;

    /* renamed from: ῲ  reason: contains not printable characters */
    public final C2655 f8584;

    /* renamed from: ⁱ  reason: contains not printable characters */
    public Protocol f8585;

    /* renamed from: 㑃  reason: contains not printable characters */
    public int f8586;

    /* renamed from: 㗗  reason: contains not printable characters */
    public long f8587 = RecyclerView.FOREVER_NS;

    /* renamed from: 㳳  reason: contains not printable characters */
    public AbstractC1483 f8588;

    /* renamed from: 㴹  reason: contains not printable characters */
    public C2536 f8589;

    /* renamed from: 㸾  reason: contains not printable characters */
    public Socket f8590;

    /* renamed from: 㿎  reason: contains not printable characters */
    public C2591 f8591;

    /* renamed from: 䁗  reason: contains not printable characters */
    public int f8592 = 1;

    public C2543(C2655 r3, C2539 r4) {
        this.f8584 = r3;
        this.f8579 = r4;
    }

    public String toString() {
        StringBuilder r0 = C1293.m3416("Connection{");
        r0.append(this.f8579.f8559.f8969.f8542);
        r0.append(CertificateUtil.DELIMITER);
        r0.append(this.f8579.f8559.f8969.f8536);
        r0.append(", proxy=");
        r0.append(this.f8579.f8558);
        r0.append(" hostAddress=");
        r0.append(this.f8579.f8557);
        r0.append(" cipherSuite=");
        C2536 r1 = this.f8589;
        r0.append(r1 != null ? r1.f8554 : "none");
        r0.append(" protocol=");
        r0.append(this.f8585);
        r0.append('}');
        return r0.toString();
    }

    /* JADX WARNING: Code restructure failed: missing block: B:82:0x0126, code lost:
        continue;
     */
    /* JADX WARNING: Removed duplicated region for block: B:35:0x00b0  */
    /* JADX WARNING: Removed duplicated region for block: B:51:0x00ee  */
    /* JADX WARNING: Removed duplicated region for block: B:52:0x00f5  */
    /* JADX WARNING: Removed duplicated region for block: B:54:0x00fa  */
    /* JADX WARNING: Removed duplicated region for block: B:79:0x012a A[EDGE_INSN: B:79:0x012a->B:71:0x012a ?: BREAK  , SYNTHETIC] */
    /* JADX WARNING: Removed duplicated region for block: B:83:? A[ORIG_RETURN, RETURN, SYNTHETIC] */
    /* renamed from: K  reason: contains not printable characters */
    public void m6660(int i, int i2, int i3, int i4, boolean z, AbstractC2537 r19, AbstractC2533 r20) {
        IOException e;
        boolean z2;
        boolean z3;
        if (this.f8585 == null) {
            List<C2630> list = this.f8579.f8559.f8971;
            C2546 r10 = new C2546(list);
            C2640 k = this.f8579.f8559;
            if (k.f8966 == null) {
                if (list.contains(C2630.f8939)) {
                    String str = this.f8579.f8559.f8969.f8542;
                    if (!C2558.f8643.m6727(str)) {
                        throw new RouteException(new UnknownServiceException(C1293.m3413("CLEARTEXT communication to ", str, " not permitted by network security policy")));
                    }
                } else {
                    throw new RouteException(new UnknownServiceException("CLEARTEXT communication not enabled for client"));
                }
            } else if (k.f8964.contains(Protocol.H2_PRIOR_KNOWLEDGE)) {
                throw new RouteException(new UnknownServiceException("H2_PRIOR_KNOWLEDGE cannot be used with HTTPS"));
            }
            RouteException routeException = null;
            do {
                try {
                    if (this.f8579.m6628()) {
                        m6661(i, i2, i3, r19, r20);
                        if (this.f8590 == null) {
                            if (!this.f8579.m6628() && this.f8590 == null) {
                                throw new RouteException(new ProtocolException("Too many tunnel connections attempted: 21"));
                            } else if (this.f8591 == null) {
                                synchronized (this.f8584) {
                                    this.f8592 = this.f8591.m6806();
                                }
                                return;
                            } else {
                                return;
                            }
                        }
                    } else {
                        try {
                            m6662(i, i2, r19, r20);
                        } catch (IOException e2) {
                            e = e2;
                            C2540.m6652(this.f8581);
                            C2540.m6652(this.f8590);
                            this.f8581 = null;
                            this.f8590 = null;
                            this.f8583 = null;
                            this.f8588 = null;
                            this.f8589 = null;
                            this.f8585 = null;
                            this.f8591 = null;
                            C2539 r4 = this.f8579;
                            InetSocketAddress inetSocketAddress = r4.f8557;
                            Proxy proxy = r4.f8558;
                            r20.m6612();
                            if (routeException != null) {
                            }
                            if (z) {
                            }
                            throw routeException;
                        }
                    }
                    try {
                        m6663(r10, i4, r19, r20);
                        InetSocketAddress inetSocketAddress2 = this.f8579.f8557;
                        Proxy proxy2 = this.f8579.f8558;
                        r20.m6624();
                        if (!this.f8579.m6628()) {
                        }
                        if (this.f8591 == null) {
                        }
                    } catch (IOException e3) {
                        e = e3;
                    }
                } catch (IOException e4) {
                    e = e4;
                    C2540.m6652(this.f8581);
                    C2540.m6652(this.f8590);
                    this.f8581 = null;
                    this.f8590 = null;
                    this.f8583 = null;
                    this.f8588 = null;
                    this.f8589 = null;
                    this.f8585 = null;
                    this.f8591 = null;
                    C2539 r42 = this.f8579;
                    InetSocketAddress inetSocketAddress3 = r42.f8557;
                    Proxy proxy3 = r42.f8558;
                    r20.m6612();
                    if (routeException != null) {
                        routeException = new RouteException(e);
                    } else {
                        routeException.addConnectException(e);
                    }
                    if (z) {
                        break;
                    }
                    z2 = true;
                    r10.f8606 = true;
                    if (!r10.f8603 || (e instanceof ProtocolException) || (e instanceof InterruptedIOException) || ((((z3 = e instanceof SSLHandshakeException)) && (e.getCause() instanceof CertificateException)) || (e instanceof SSLPeerUnverifiedException) || (!z3 && !(e instanceof SSLProtocolException)))) {
                        z2 = false;
                        continue;
                    }
                    if (!z2) {
                    }
                    throw routeException;
                }
            } while (!z2);
            throw routeException;
        }
        throw new IllegalStateException("already connected");
    }

    /* renamed from: K  reason: contains not printable characters */
    public final void m6662(int i, int i2, AbstractC2537 r6, AbstractC2533 r7) throws IOException {
        Socket socket;
        C2539 r62 = this.f8579;
        Proxy proxy = r62.f8558;
        C2640 k = r62.f8559;
        if (proxy.type() == Proxy.Type.DIRECT || proxy.type() == Proxy.Type.HTTP) {
            socket = k.f8963.createSocket();
        } else {
            socket = new Socket(proxy);
        }
        this.f8590 = socket;
        InetSocketAddress inetSocketAddress = this.f8579.f8557;
        r7.m6623();
        this.f8590.setSoTimeout(i2);
        try {
            C2558.f8643.m6733(this.f8590, this.f8579.f8557, i);
            try {
                this.f8583 = new C1488(C1486.m3735(this.f8590));
                this.f8588 = new C1479(C1486.m3736(this.f8590));
            } catch (NullPointerException e) {
                if ("throw with null exception".equals(e.getMessage())) {
                    throw new IOException(e);
                }
            }
        } catch (ConnectException e2) {
            StringBuilder r63 = C1293.m3416("Failed to connect to ");
            r63.append(this.f8579.f8557);
            ConnectException connectException = new ConnectException(r63.toString());
            connectException.initCause(e2);
            throw connectException;
        }
    }

    /* JADX WARN: Multi-variable type inference failed */
    /* JADX WARN: Type inference failed for: r0v2 */
    /* JADX WARN: Type inference failed for: r0v3 */
    /* JADX WARNING: Removed duplicated region for block: B:39:0x011a A[Catch:{ all -> 0x0110 }] */
    /* JADX WARNING: Removed duplicated region for block: B:41:0x0120 A[Catch:{ all -> 0x0110 }] */
    /* JADX WARNING: Removed duplicated region for block: B:43:0x0123  */
    /* renamed from: K  reason: contains not printable characters */
    public final void m6663(C2546 r6, int i, AbstractC2537 r8, AbstractC2533 r9) throws IOException {
        Throwable th;
        AssertionError e;
        Protocol protocol;
        C2640 k = this.f8579.f8559;
        if (k.f8966 != null) {
            r9.m6611();
            C2640 k2 = this.f8579.f8559;
            SSLSocketFactory sSLSocketFactory = k2.f8966;
            String str = 0;
            try {
                Socket socket = this.f8590;
                C2531 r2 = k2.f8969;
                SSLSocket sSLSocket = (SSLSocket) sSLSocketFactory.createSocket(socket, r2.f8542, r2.f8536, true);
                try {
                    C2630 r62 = r6.m6674(sSLSocket);
                    if (r62.m6939()) {
                        C2558.f8643.m6735(sSLSocket, k2.f8969.f8542, k2.f8964);
                    }
                    sSLSocket.startHandshake();
                    SSLSession session = sSLSocket.getSession();
                    C2536 r22 = C2536.m6627(session);
                    if (k2.m6949().verify(k2.f8969.f8542, session)) {
                        k2.m6950().m6948(k2.f8969.f8542, r22.f8553);
                        if (r62.m6939()) {
                            str = C2558.f8643.m6724(sSLSocket);
                        }
                        this.f8581 = sSLSocket;
                        this.f8583 = new C1488(C1486.m3735(this.f8581));
                        this.f8588 = new C1479(C1486.m3736(this.f8581));
                        this.f8589 = r22;
                        if (str != null) {
                            protocol = Protocol.get(str);
                        } else {
                            protocol = Protocol.HTTP_1_1;
                        }
                        this.f8585 = protocol;
                        C2558.f8643.m6734(sSLSocket);
                        if (this.f8585 == Protocol.HTTP_2) {
                            m6659(i);
                            return;
                        }
                        return;
                    }
                    X509Certificate x509Certificate = (X509Certificate) r22.f8553.get(0);
                    throw new SSLPeerUnverifiedException("Hostname " + k2.f8969.f8542 + " not verified:\n    certificate: " + C2638.m6947(x509Certificate) + "\n    DN: " + x509Certificate.getSubjectDN().getName() + "\n    subjectAltNames: " + C2575.m6771(x509Certificate));
                } catch (AssertionError e2) {
                    e = e2;
                    str = sSLSocket;
                    try {
                        if (!C2540.m6653(e)) {
                        }
                    } catch (Throwable th2) {
                        th = th2;
                        sSLSocket = str;
                        if (sSLSocket != null) {
                            C2558.f8643.m6734(sSLSocket);
                        }
                        C2540.m6652((Socket) sSLSocket);
                        throw th;
                    }
                } catch (Throwable th3) {
                    th = th3;
                    if (sSLSocket != null) {
                    }
                    C2540.m6652((Socket) sSLSocket);
                    throw th;
                }
            } catch (AssertionError e3) {
                e = e3;
                if (!C2540.m6653(e)) {
                    throw new IOException(e);
                }
                throw e;
            }
        } else if (k.f8964.contains(Protocol.H2_PRIOR_KNOWLEDGE)) {
            this.f8581 = this.f8590;
            this.f8585 = Protocol.H2_PRIOR_KNOWLEDGE;
            m6659(i);
        } else {
            this.f8581 = this.f8590;
            this.f8585 = Protocol.HTTP_1_1;
        }
    }

    /* renamed from: K  reason: contains not printable characters */
    public final void m6659(int i) throws IOException {
        this.f8581.setSoTimeout(0);
        C2591.C2596 r0 = new C2591.C2596(true);
        Socket socket = this.f8581;
        String str = this.f8579.f8559.f8969.f8542;
        AbstractC1496 r4 = this.f8583;
        AbstractC1483 r5 = this.f8588;
        r0.f8755 = socket;
        r0.f8753 = str;
        r0.f8751 = r4;
        r0.f8757 = r5;
        r0.f8752 = this;
        r0.f8758 = i;
        this.f8591 = new C2591(r0);
        C2591 r7 = this.f8591;
        r7.f8720.m6848();
        r7.f8720.m6850(r7.f8728);
        int r02 = r7.f8728.m6884();
        if (r02 != 65535) {
            r7.f8720.m6854(0, (long) (r02 - 65535));
        }
        new Thread(r7.f8734).start();
    }

    /* renamed from: K  reason: contains not printable characters */
    public final void m6661(int i, int i2, int i3, AbstractC2537 r8, AbstractC2533 r9) throws IOException {
        C2644.C2645 k = new C2644.C2645();
        k.m6968(this.f8579.f8559.f8969);
        k.m6966("Host", C2540.m6643(this.f8579.f8559.f8969, true));
        k.m6966("Proxy-Connection", "Keep-Alive");
        k.m6966(GraphRequest.USER_AGENT_HEADER, "okhttp/3.11.0");
        C2644 r0 = k.m6970();
        C2531 r1 = r0.f8985;
        m6662(i, i2, r8, r9);
        C2562 k2 = new C2562(null, null, this.f8583, this.f8588);
        this.f8583.timeout().m3705((long) i2, TimeUnit.MILLISECONDS);
        this.f8588.timeout().m3705((long) i3, TimeUnit.MILLISECONDS);
        k2.m6751(r0.f8982, "CONNECT " + C2540.m6643(r1, true) + " HTTP/1.1");
        k2.f8658.flush();
        C2671.C2672 r5 = k2.m6747(false);
        r5.f9101 = r0;
        C2671 r52 = r5.m7010();
        long r6 = C2577.m6783(r52);
        if (r6 == -1) {
            r6 = 0;
        }
        AbstractC1494 r62 = k2.m6745(r6);
        C2540.m6634(r62, Integer.MAX_VALUE, TimeUnit.MILLISECONDS);
        r62.close();
        int i4 = r52.f9092;
        if (i4 != 200) {
            if (i4 == 407) {
                C2539 r63 = this.f8579;
                ((AbstractC2636.C2637) r63.f8559.f8972).m6946(r63, r52);
                throw new IOException("Failed to authenticate with proxy");
            }
            StringBuilder r7 = C1293.m3416("Unexpected response code for CONNECT: ");
            r7.append(r52.f9092);
            throw new IOException(r7.toString());
        } else if (!this.f8583.m3819().m3802() || !this.f8588.m3731().m3802()) {
            throw new IOException("TLS tunnel buffered too many bytes!");
        }
    }

    /* renamed from: K  reason: contains not printable characters */
    public boolean m6668(C2640 k, C2539 r6) {
        if (this.f8580.size() >= this.f8592 || this.f8582 || !AbstractC2570.f8676.m6764(this.f8579.f8559, k)) {
            return false;
        }
        if (k.f8969.f8542.equals(this.f8579.f8559.f8969.f8542)) {
            return true;
        }
        if (this.f8591 == null || r6 == null || r6.f8558.type() != Proxy.Type.DIRECT || this.f8579.f8558.type() != Proxy.Type.DIRECT || !this.f8579.f8557.equals(r6.f8557) || r6.f8559.f8970 != C2575.f8679 || !m6667(k.f8969)) {
            return false;
        }
        try {
            k.f8965.m6948(k.f8969.f8542, this.f8589.f8553);
            return true;
        } catch (SSLPeerUnverifiedException unused) {
            return false;
        }
    }

    /* renamed from: K  reason: contains not printable characters */
    public AbstractC2576 m6658(C2625 r5, AbstractC2661.AbstractC2662 k, C2548 r7) throws SocketException {
        C2591 r0 = this.f8591;
        if (r0 != null) {
            return new C2619(r5, k, r7, r0);
        }
        C2583 r6 = (C2583) k;
        this.f8581.setSoTimeout(r6.f8697);
        this.f8583.timeout().m3705((long) r6.f8697, TimeUnit.MILLISECONDS);
        this.f8588.timeout().m3705((long) r6.f8691, TimeUnit.MILLISECONDS);
        return new C2562(r5, r7, this.f8583, this.f8588);
    }

    /* renamed from: K  reason: contains not printable characters */
    public boolean m6667(C2531 r5) {
        int i = r5.f8536;
        C2531 r1 = this.f8579.f8559.f8969;
        if (i != r1.f8536) {
            return false;
        }
        if (r5.f8542.equals(r1.f8542)) {
            return true;
        }
        C2536 r0 = this.f8589;
        if (r0 == null || !C2575.f8679.m6773(r5.f8542, (X509Certificate) r0.f8553.get(0))) {
            return false;
        }
        return true;
    }

    @Override // 㿎.ሽ.㿎.C2591.AbstractC2603
    /* renamed from: K  reason: contains not printable characters */
    public void m6665(C2621 r2) throws IOException {
        r2.m6906(ErrorCode.REFUSED_STREAM);
    }

    @Override // 㿎.ሽ.㿎.C2591.AbstractC2603
    /* renamed from: K  reason: contains not printable characters */
    public void m6664(C2591 r2) {
        synchronized (this.f8584) {
            this.f8592 = r2.m6806();
        }
    }

    /* renamed from: K  reason: contains not printable characters */
    public boolean m6666() {
        return this.f8591 != null;
    }
}
