package defpackage;

import java.net.Inet6Address;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.Proxy;
import java.net.Socket;
import java.net.SocketAddress;
import java.net.SocketException;
import java.net.UnknownHostException;
import java.net.UnknownServiceException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.concurrent.ConcurrentLinkedQueue;

/* JADX INFO: renamed from: ʾˌᵠ, reason: contains not printable characters */
/* JADX INFO: loaded from: classes3.dex */
public final class C0252 {

    /* JADX INFO: renamed from: ʽˈˋ, reason: contains not printable characters */
    public final C2736 f1831;

    /* JADX INFO: renamed from: ˊᵫᵗ, reason: contains not printable characters */
    public final boolean f1832;

    /* JADX INFO: renamed from: ˋˈ, reason: contains not printable characters */
    public C3005 f1833;

    /* JADX INFO: renamed from: ːˏᵟᵯ, reason: contains not printable characters */
    public final int f1834;

    /* JADX INFO: renamed from: ᵝᵶ, reason: contains not printable characters */
    public C1486 f1835;

    /* JADX INFO: renamed from: ᵟᵹᵙ, reason: contains not printable characters */
    public final C0920 f1836 = new C0920();

    /* JADX INFO: renamed from: ᵨᵣ, reason: contains not printable characters */
    public final int f1837;

    /* JADX INFO: renamed from: ᵪᵹᵥᵻ, reason: contains not printable characters */
    public C0158 f1838;

    /* JADX INFO: renamed from: ᵮᵣᵗˈ, reason: contains not printable characters */
    public final boolean f1839;

    /* JADX INFO: renamed from: ᵱᵽʡᵚ, reason: contains not printable characters */
    public final ʽˎˇˌ f1840;

    /* JADX INFO: renamed from: ᵲᵶᵳˈ, reason: contains not printable characters */
    public final C5377 f1841;

    /* JADX INFO: renamed from: ᵵᵽ, reason: contains not printable characters */
    public final boolean f1842;

    /* JADX INFO: renamed from: ᵶᵥᵝ, reason: contains not printable characters */
    public final C4778 f1843;

    /* JADX INFO: renamed from: ᵷᵺʾᵖ, reason: contains not printable characters */
    public final int f1844;

    /* JADX INFO: renamed from: ᵻʽᵯ, reason: contains not printable characters */
    public final C0095 f1845;

    /* JADX INFO: renamed from: ᵻˈᵦʼ, reason: contains not printable characters */
    public final int f1846;

    public C0252(C5377 c5377, C2736 c2736, int i, int i2, int i3, int i4, boolean z, boolean z2, C0095 c0095, ʽˎˇˌ r10, C4778 c4778, C4402 c4402) {
        this.f1841 = c5377;
        this.f1831 = c2736;
        this.f1834 = i;
        this.f1844 = i2;
        this.f1837 = i3;
        this.f1846 = i4;
        this.f1842 = z;
        this.f1832 = z2;
        this.f1845 = c0095;
        this.f1840 = r10;
        this.f1843 = c4778;
        this.f1839 = !ᵥˈᵛ.ˊᵫᵗ(c4402.f16705, "GET");
    }

    /* JADX INFO: renamed from: ʽˈˋ, reason: contains not printable characters */
    public final InterfaceC4344 m1465() throws SocketException, UnknownHostException {
        Socket socketM9965;
        C1200 c1200;
        C4013 c4013 = this.f1843.f18185;
        if (c4013 == null) {
            c1200 = null;
        } else {
            boolean zM8826 = c4013.m8826(this.f1839);
            synchronized (c4013) {
                boolean z = c4013.f15420;
                boolean z2 = true;
                try {
                    if (!zM8826) {
                        c4013.f15420 = true;
                        socketM9965 = this.f1843.m9965();
                    } else if (!z) {
                        C1695 c1695 = c4013.f15413.f1565.f1327;
                        C1695 c16952 = this.f1845.f1327;
                        if (c1695.f6697 != c16952.f6697 || !ᵥˈᵛ.ˊᵫᵗ(c1695.f6700, c16952.f6700)) {
                            z2 = false;
                        }
                        socketM9965 = !z2 ? this.f1843.m9965() : null;
                    }
                } finally {
                }
            }
            if (this.f1843.f18185 == null) {
                if (socketM9965 != null) {
                    AbstractC1190.m3664(socketM9965);
                }
                this.f1843.f18181.getClass();
                c1200 = null;
            } else {
                if (socketM9965 != null) {
                    ᵕᵩʽᵲ.ᵪᵹᵥᵻ("Check failed.");
                    return null;
                }
                c1200 = new C1200(c4013);
            }
        }
        if (c1200 != null) {
            return c1200;
        }
        C1200 c1200M1467 = m1467(null, null);
        if (c1200M1467 != null) {
            return c1200M1467;
        }
        if (!this.f1836.isEmpty()) {
            return (InterfaceC4344) this.f1836.removeFirst();
        }
        C4318 c4318M1466 = m1466();
        C1200 c1200M14672 = m1467(c4318M1466, c4318M1466.f16444);
        return c1200M14672 != null ? c1200M14672 : c4318M1466;
    }

    /* JADX INFO: renamed from: ːˏᵟᵯ, reason: contains not printable characters */
    public final C4318 m1466() throws SocketException, UnknownHostException {
        String hostName;
        int port;
        List listM8424;
        boolean zContains;
        C0158 c0158 = this.f1838;
        if (c0158 != null) {
            this.f1838 = null;
            return m1469(c0158, null);
        }
        C1486 c1486 = this.f1835;
        if (c1486 != null && c1486.f6021 < c1486.f6022.size()) {
            int i = c1486.f6021;
            ArrayList arrayList = c1486.f6022;
            if (i >= arrayList.size()) {
                C4468.m9518();
                return null;
            }
            int i2 = c1486.f6021;
            c1486.f6021 = i2 + 1;
            return m1469((C0158) arrayList.get(i2), null);
        }
        C3005 c3005 = this.f1833;
        if (c3005 == null) {
            c3005 = new C3005(this.f1845, this.f1840, this.f1843, this.f1832);
            this.f1833 = c3005;
        }
        if (!c3005.m7087()) {
            C1685.m4568("exhausted all routes");
            return null;
        }
        if (!c3005.m7087()) {
            C4468.m9518();
            return null;
        }
        ArrayList arrayList2 = new ArrayList();
        while (c3005.f11574 < c3005.f11570.size()) {
            if (c3005.f11574 >= c3005.f11570.size()) {
                throw new SocketException("No route to " + c3005.f11571.f1327.f6700 + "; exhausted proxy configurations: " + c3005.f11570);
            }
            List list = c3005.f11570;
            int i3 = c3005.f11574;
            c3005.f11574 = i3 + 1;
            Proxy proxy = (Proxy) list.get(i3);
            ArrayList arrayList3 = new ArrayList();
            c3005.f11572 = arrayList3;
            if (proxy.type() == Proxy.Type.DIRECT || proxy.type() == Proxy.Type.SOCKS) {
                C1695 c1695 = c3005.f11571.f1327;
                hostName = c1695.f6700;
                port = c1695.f6697;
            } else {
                SocketAddress socketAddressAddress = proxy.address();
                if (!(socketAddressAddress instanceof InetSocketAddress)) {
                    C4935.m10232(socketAddressAddress.getClass(), "Proxy.address() is not an InetSocketAddress: ");
                    return null;
                }
                InetSocketAddress inetSocketAddress = (InetSocketAddress) socketAddressAddress;
                InetAddress address = inetSocketAddress.getAddress();
                hostName = address == null ? inetSocketAddress.getHostName() : address.getHostAddress();
                port = inetSocketAddress.getPort();
            }
            if (1 > port || port >= 65536) {
                throw new SocketException("No route to " + hostName + ':' + port + "; port is out of range");
            }
            if (proxy.type() == Proxy.Type.SOCKS) {
                arrayList3.add(InetSocketAddress.createUnresolved(hostName, port));
            } else {
                if (AbstractC3713.f14140.f17246.matcher(hostName).matches()) {
                    listM8424 = Collections.singletonList(InetAddress.getByName(hostName));
                } else {
                    c3005.f11569.f18181.getClass();
                    c3005.f11571.f1331.getClass();
                    try {
                        listM8424 = AbstractC3761.m8424(InetAddress.getAllByName(hostName));
                        if (listM8424.isEmpty()) {
                            throw new UnknownHostException(c3005.f11571.f1331 + " returned no addresses for " + hostName);
                        }
                        c3005.f11569.f18181.getClass();
                    } catch (NullPointerException e) {
                        UnknownHostException unknownHostException = new UnknownHostException("Broken system behaviour for dns lookup of ".concat(hostName));
                        unknownHostException.initCause(e);
                        throw unknownHostException;
                    }
                }
                if (c3005.f11573 && listM8424.size() >= 2) {
                    ArrayList arrayList4 = new ArrayList();
                    ArrayList arrayList5 = new ArrayList();
                    for (Object obj : listM8424) {
                        if (((InetAddress) obj) instanceof Inet6Address) {
                            arrayList4.add(obj);
                        } else {
                            arrayList5.add(obj);
                        }
                    }
                    if (!arrayList4.isEmpty() && !arrayList5.isEmpty()) {
                        byte[] bArr = AbstractC1701.f6716;
                        Iterator it = arrayList4.iterator();
                        Iterator it2 = arrayList5.iterator();
                        C0970 c0970 = ᵽˊᵘᵬ.ᵷᵺʾᵖ();
                        while (true) {
                            if (!it.hasNext() && !it2.hasNext()) {
                                break;
                            }
                            if (it.hasNext()) {
                                c0970.add(it.next());
                            }
                            if (it2.hasNext()) {
                                c0970.add(it2.next());
                            }
                        }
                        listM8424 = ᵽˊᵘᵬ.ᵲᵶᵳˈ(c0970);
                    }
                }
                Iterator it3 = listM8424.iterator();
                while (it3.hasNext()) {
                    arrayList3.add(new InetSocketAddress((InetAddress) it3.next(), port));
                }
            }
            Iterator it4 = c3005.f11572.iterator();
            while (it4.hasNext()) {
                C0158 c01582 = new C0158(c3005.f11571, proxy, (InetSocketAddress) it4.next());
                ʽˎˇˌ r5 = c3005.f11567;
                synchronized (r5) {
                    zContains = ((LinkedHashSet) r5.ᵜᵪʾ).contains(c01582);
                }
                if (zContains) {
                    c3005.f11568.add(c01582);
                } else {
                    arrayList2.add(c01582);
                }
            }
            if (!arrayList2.isEmpty()) {
                break;
            }
        }
        if (arrayList2.isEmpty()) {
            AbstractC5477.m11101(c3005.f11568, arrayList2);
            c3005.f11568.clear();
        }
        C1486 c14862 = new C1486(arrayList2);
        this.f1835 = c14862;
        if (this.f1843.f18172) {
            C1685.m4568("Canceled");
            return null;
        }
        if (c14862.f6021 >= arrayList2.size()) {
            C4468.m9518();
            return null;
        }
        int i4 = c14862.f6021;
        c14862.f6021 = i4 + 1;
        return m1469((C0158) arrayList2.get(i4), arrayList2);
    }

    /* JADX WARN: Removed duplicated region for block: B:22:0x003a A[Catch: all -> 0x0038, TryCatch #0 {all -> 0x0038, blocks: (B:14:0x002d, B:22:0x003a, B:25:0x0041), top: B:51:0x002d }] */
    /* JADX INFO: renamed from: ᵨᵣ, reason: contains not printable characters */
    /*
        Code decompiled incorrectly, please refer to instructions dump.
    */
    public final C1200 m1467(C4318 c4318, List list) {
        C4013 c4013;
        boolean z;
        Socket socketM9965;
        C2736 c2736 = this.f1831;
        boolean z2 = this.f1839;
        C0095 c0095 = this.f1845;
        C4778 c4778 = this.f1843;
        boolean z3 = c4318 != null && c4318.mo3692();
        Iterator it = ((ConcurrentLinkedQueue) c2736.f10523).iterator();
        while (true) {
            if (!it.hasNext()) {
                c4013 = null;
                break;
            }
            c4013 = (C4013) it.next();
            synchronized (c4013) {
                if (z3) {
                    try {
                        if (!(c4013.f15424 != null)) {
                            z = false;
                        } else if (c4013.m8824(c0095, list)) {
                            c4778.m9969(c4013);
                            z = true;
                        } else {
                            z = false;
                        }
                    } catch (Throwable th) {
                        throw th;
                    }
                }
            }
            if (z) {
                if (c4013.m8826(z2)) {
                    break;
                }
                synchronized (c4013) {
                    c4013.f15420 = true;
                    socketM9965 = c4778.m9965();
                }
                if (socketM9965 != null) {
                    AbstractC1190.m3664(socketM9965);
                }
            }
        }
        if (c4013 == null) {
            return null;
        }
        if (c4318 != null) {
            this.f1838 = c4318.f16441;
            Socket socket = c4318.f16440;
            if (socket != null) {
                AbstractC1190.m3664(socket);
            }
        }
        this.f1843.f18181.getClass();
        return new C1200(c4013);
    }

    /* JADX INFO: renamed from: ᵲᵶᵳˈ, reason: contains not printable characters */
    public final boolean m1468(C4013 c4013) {
        C3005 c3005;
        C0158 c0158;
        if (this.f1836.isEmpty() && this.f1838 == null) {
            if (c4013 != null) {
                synchronized (c4013) {
                    c0158 = null;
                    if (c4013.f15418 == 0 && c4013.f15420 && AbstractC1190.m3669(c4013.f15413.f1565.f1327, this.f1845.f1327)) {
                        c0158 = c4013.f15413;
                    }
                }
                if (c0158 != null) {
                    this.f1838 = c0158;
                    return true;
                }
            }
            C1486 c1486 = this.f1835;
            if ((c1486 == null || c1486.f6021 >= c1486.f6022.size()) && (c3005 = this.f1833) != null) {
                return c3005.m7087();
            }
        }
        return true;
    }

    /* JADX INFO: renamed from: ᵷᵺʾᵖ, reason: contains not printable characters */
    public final C4318 m1469(C0158 c0158, ArrayList arrayList) throws UnknownServiceException {
        EnumC0876 enumC0876 = EnumC0876.f3880;
        C0095 c0095 = c0158.f1565;
        if (c0095.f1328 == null) {
            if (!c0095.f1330.contains(C5209.f19624)) {
                throw new UnknownServiceException("CLEARTEXT communication not enabled for client");
            }
            String str = c0158.f1565.f1327.f6700;
            AbstractC2570 abstractC2570 = AbstractC2570.f9922;
            if (!AbstractC2570.f9922.mo6289(str)) {
                throw new UnknownServiceException(AbstractC3589.m8117("CLEARTEXT communication to ", str, " not permitted by network security policy"));
            }
        } else if (c0095.f1334.contains(enumC0876)) {
            throw new UnknownServiceException("H2_PRIOR_KNOWLEDGE cannot be used with HTTPS");
        }
        C4402 c4402 = null;
        if (c0158.f1563.type() == Proxy.Type.HTTP) {
            C0095 c00952 = c0158.f1565;
            if (c00952.f1328 != null || c00952.f1334.contains(enumC0876)) {
                ᵕˁـ r0 = new ᵕˁـ(7);
                r0.ᵜᵪʾ = c0158.f1565.f1327;
                r0.ˉᵲ("CONNECT", (AbstractC0782) null);
                C0095 c00953 = c0158.f1565;
                ((C4759) r0.ᵨᵼⁱ).m9938("Host", AbstractC1190.m3665(c00953.f1327, true));
                ((C4759) r0.ᵨᵼⁱ).m9938("Proxy-Connection", "Keep-Alive");
                ((C4759) r0.ᵨᵼⁱ).m9938("User-Agent", "okhttp/5.4.0");
                c4402 = new C4402(r0);
                C2550 c2550 = AbstractC4407.f16717;
                C4759 c4759 = new C4759();
                c4759.m9938("Proxy-Authenticate", "OkHttp-Preemptive");
                c4759.m9936();
                c00953.f1335.getClass();
            }
        }
        return new C4318(this.f1841, this.f1831, this.f1834, this.f1844, this.f1837, this.f1846, this.f1842, this.f1843, this, c0158, arrayList, c4402, -1, false);
    }
}
