package okhttp3.internal.connection;

/* JADX INFO: compiled from: r8-map-id-ae6aab8d9a1033945587ffdd1bc6222159522c1d66e66a2c4cecb065134dd0a6 */
/* JADX INFO: loaded from: E:\ios\vf_dex\classes3.dex */
@kotlin.Metadata(d1 = {"\u0000d\n\u0002\u0018\u0002\n\u0002\u0010\u0000\n\u0000\n\u0002\u0018\u0002\n\u0000\n\u0002\u0018\u0002\n\u0000\n\u0002\u0018\u0002\n\u0000\n\u0002\u0018\u0002\n\u0002\b\u0002\n\u0002\u0010 \n\u0002\u0018\u0002\n\u0000\n\u0002\u0010\b\n\u0000\n\u0002\u0010!\n\u0002\u0018\u0002\n\u0000\n\u0002\u0018\u0002\n\u0000\n\u0002\u0010\u000b\n\u0002\b\u0002\n\u0002\u0018\u0002\n\u0002\b\u0002\n\u0002\u0010\u0002\n\u0002\b\u0003\n\u0002\u0018\u0002\n\u0002\b\u0003\u0018\u0000 !2\u00020\u0001:\u0002!\"B%\u0012\u0006\u0010\u0002\u001a\u00020\u0003\u0012\u0006\u0010\u0004\u001a\u00020\u0005\u0012\u0006\u0010\u0006\u001a\u00020\u0007\u0012\u0006\u0010\b\u001a\u00020\t¢\u0006\u0002\u0010\nJ\t\u0010\u0015\u001a\u00020\u0016H\u0086\u0002J\b\u0010\u0017\u001a\u00020\u0016H\u0002J\t\u0010\u0018\u001a\u00020\u0019H\u0086\u0002J\b\u0010\u001a\u001a\u00020\u0014H\u0002J\u0010\u0010\u001b\u001a\u00020\u001c2\u0006\u0010\u001d\u001a\u00020\u0014H\u0002J\u001a\u0010\u001e\u001a\u00020\u001c2\u0006\u0010\u001f\u001a\u00020 2\b\u0010\u001d\u001a\u0004\u0018\u00010\u0014H\u0002R\u000e\u0010\u0002\u001a\u00020\u0003X\u0082\u0004¢\u0006\u0002\n\u0000R\u000e\u0010\u0006\u001a\u00020\u0007X\u0082\u0004¢\u0006\u0002\n\u0000R\u000e\u0010\b\u001a\u00020\tX\u0082\u0004¢\u0006\u0002\n\u0000R\u0014\u0010\u000b\u001a\b\u0012\u0004\u0012\u00020\r0\fX\u0082\u000e¢\u0006\u0002\n\u0000R\u000e\u0010\u000e\u001a\u00020\u000fX\u0082\u000e¢\u0006\u0002\n\u0000R\u0014\u0010\u0010\u001a\b\u0012\u0004\u0012\u00020\u00120\u0011X\u0082\u0004¢\u0006\u0002\n\u0000R\u0014\u0010\u0013\u001a\b\u0012\u0004\u0012\u00020\u00140\fX\u0082\u000e¢\u0006\u0002\n\u0000R\u000e\u0010\u0004\u001a\u00020\u0005X\u0082\u0004¢\u0006\u0002\n\u0000¨\u0006#"}, d2 = {"Lokhttp3/internal/connection/RouteSelector;", okhttp3.HttpUrl.FRAGMENT_ENCODE_SET, "address", "Lokhttp3/Address;", "routeDatabase", "Lokhttp3/internal/connection/RouteDatabase;", "call", "Lokhttp3/Call;", "eventListener", "Lokhttp3/EventListener;", "(Lokhttp3/Address;Lokhttp3/internal/connection/RouteDatabase;Lokhttp3/Call;Lokhttp3/EventListener;)V", "inetSocketAddresses", okhttp3.HttpUrl.FRAGMENT_ENCODE_SET, "Ljava/net/InetSocketAddress;", "nextProxyIndex", okhttp3.HttpUrl.FRAGMENT_ENCODE_SET, "postponedRoutes", okhttp3.HttpUrl.FRAGMENT_ENCODE_SET, "Lokhttp3/Route;", "proxies", "Ljava/net/Proxy;", "hasNext", okhttp3.HttpUrl.FRAGMENT_ENCODE_SET, "hasNextProxy", "next", "Lokhttp3/internal/connection/RouteSelector$Selection;", "nextProxy", "resetNextInetSocketAddress", okhttp3.HttpUrl.FRAGMENT_ENCODE_SET, "proxy", "resetNextProxy", "url", "Lokhttp3/HttpUrl;", "Companion", "Selection", "okhttp"}, k = 1, mv = {1, 8, 0}, xi = 48)
public final class RouteSelector {

    /* JADX INFO: renamed from: Companion, reason: from kotlin metadata */
    @org.jetbrains.annotations.NotNull
    public static final okhttp3.internal.connection.RouteSelector.Companion INSTANCE = new okhttp3.internal.connection.RouteSelector.Companion(null);

    @org.jetbrains.annotations.NotNull
    private final okhttp3.Address address;

    @org.jetbrains.annotations.NotNull
    private final okhttp3.Call call;

    @org.jetbrains.annotations.NotNull
    private final okhttp3.EventListener eventListener;

    @org.jetbrains.annotations.NotNull
    private java.util.List<? extends java.net.InetSocketAddress> inetSocketAddresses;
    private int nextProxyIndex;

    @org.jetbrains.annotations.NotNull
    private final java.util.List<okhttp3.Route> postponedRoutes;

    @org.jetbrains.annotations.NotNull
    private java.util.List<? extends java.net.Proxy> proxies;

    @org.jetbrains.annotations.NotNull
    private final okhttp3.internal.connection.RouteDatabase routeDatabase;

    /* JADX INFO: compiled from: r8-map-id-ae6aab8d9a1033945587ffdd1bc6222159522c1d66e66a2c4cecb065134dd0a6 */
    @kotlin.Metadata(d1 = {"\u0000\u0018\n\u0002\u0018\u0002\n\u0002\u0010\u0000\n\u0002\b\u0002\n\u0002\u0010\u000e\n\u0002\u0018\u0002\n\u0002\b\u0003\b\u0086\u0003\u0018\u00002\u00020\u0001B\u0007\b\u0002¢\u0006\u0002\u0010\u0002R\u0015\u0010\u0003\u001a\u00020\u0004*\u00020\u00058F¢\u0006\u0006\u001a\u0004\b\u0006\u0010\u0007¨\u0006\b"}, d2 = {"Lokhttp3/internal/connection/RouteSelector$Companion;", okhttp3.HttpUrl.FRAGMENT_ENCODE_SET, "()V", "socketHost", okhttp3.HttpUrl.FRAGMENT_ENCODE_SET, "Ljava/net/InetSocketAddress;", "getSocketHost", "(Ljava/net/InetSocketAddress;)Ljava/lang/String;", "okhttp"}, k = 1, mv = {1, 8, 0}, xi = 48)
    public static final class Companion {
        public /* synthetic */ Companion(kotlin.jvm.internal.DefaultConstructorMarker defaultConstructorMarker) {
            this();
        }

        @org.jetbrains.annotations.NotNull
        public final java.lang.String getSocketHost(@org.jetbrains.annotations.NotNull java.net.InetSocketAddress inetSocketAddress) {
            kotlin.jvm.internal.Intrinsics.l(inetSocketAddress, "<this>");
            java.net.InetAddress address = inetSocketAddress.getAddress();
            if (address == null) {
                java.lang.String hostName = inetSocketAddress.getHostName();
                kotlin.jvm.internal.Intrinsics.k(hostName, "hostName");
                return hostName;
            }
            java.lang.String hostAddress = address.getHostAddress();
            kotlin.jvm.internal.Intrinsics.k(hostAddress, "address.hostAddress");
            return hostAddress;
        }

        private Companion() {
        }
    }

    /* JADX INFO: compiled from: r8-map-id-ae6aab8d9a1033945587ffdd1bc6222159522c1d66e66a2c4cecb065134dd0a6 */
    @kotlin.Metadata(d1 = {"\u0000&\n\u0002\u0018\u0002\n\u0002\u0010\u0000\n\u0000\n\u0002\u0010 \n\u0002\u0018\u0002\n\u0002\b\u0002\n\u0002\u0010\b\n\u0002\b\u0003\n\u0002\u0010\u000b\n\u0002\b\u0002\u0018\u00002\u00020\u0001B\u0013\u0012\f\u0010\u0002\u001a\b\u0012\u0004\u0012\u00020\u00040\u0003¢\u0006\u0002\u0010\u0005J\t\u0010\n\u001a\u00020\u000bH\u0086\u0002J\t\u0010\f\u001a\u00020\u0004H\u0086\u0002R\u000e\u0010\u0006\u001a\u00020\u0007X\u0082\u000e¢\u0006\u0002\n\u0000R\u0017\u0010\u0002\u001a\b\u0012\u0004\u0012\u00020\u00040\u0003¢\u0006\b\n\u0000\u001a\u0004\b\b\u0010\t¨\u0006\r"}, d2 = {"Lokhttp3/internal/connection/RouteSelector$Selection;", okhttp3.HttpUrl.FRAGMENT_ENCODE_SET, "routes", okhttp3.HttpUrl.FRAGMENT_ENCODE_SET, "Lokhttp3/Route;", "(Ljava/util/List;)V", "nextRouteIndex", okhttp3.HttpUrl.FRAGMENT_ENCODE_SET, "getRoutes", "()Ljava/util/List;", "hasNext", okhttp3.HttpUrl.FRAGMENT_ENCODE_SET, "next", "okhttp"}, k = 1, mv = {1, 8, 0}, xi = 48)
    public static final class Selection {
        private int nextRouteIndex;

        @org.jetbrains.annotations.NotNull
        private final java.util.List<okhttp3.Route> routes;

        public Selection(@org.jetbrains.annotations.NotNull java.util.List<okhttp3.Route> list) {
            kotlin.jvm.internal.Intrinsics.l(list, "routes");
            this.routes = list;
        }

        @org.jetbrains.annotations.NotNull
        public final java.util.List<okhttp3.Route> getRoutes() {
            return this.routes;
        }

        public final boolean hasNext() {
            return this.nextRouteIndex < this.routes.size();
        }

        @org.jetbrains.annotations.NotNull
        public final okhttp3.Route next() {
            if (!hasNext()) {
                throw new java.util.NoSuchElementException();
            }
            java.util.List<okhttp3.Route> list = this.routes;
            int i = this.nextRouteIndex;
            this.nextRouteIndex = i + 1;
            return list.get(i);
        }
    }

    public RouteSelector(@org.jetbrains.annotations.NotNull okhttp3.Address address, @org.jetbrains.annotations.NotNull okhttp3.internal.connection.RouteDatabase routeDatabase, @org.jetbrains.annotations.NotNull okhttp3.Call call, @org.jetbrains.annotations.NotNull okhttp3.EventListener eventListener) {
        kotlin.jvm.internal.Intrinsics.l(address, "address");
        kotlin.jvm.internal.Intrinsics.l(routeDatabase, "routeDatabase");
        kotlin.jvm.internal.Intrinsics.l(call, "call");
        kotlin.jvm.internal.Intrinsics.l(eventListener, "eventListener");
        this.address = address;
        this.routeDatabase = routeDatabase;
        this.call = call;
        this.eventListener = eventListener;
        this.proxies = kotlin.collections.CollectionsKt.o();
        this.inetSocketAddresses = kotlin.collections.CollectionsKt.o();
        this.postponedRoutes = new java.util.ArrayList();
        resetNextProxy(address.url(), address.proxy());
    }

    private final boolean hasNextProxy() {
        return this.nextProxyIndex < this.proxies.size();
    }

    private final java.net.Proxy nextProxy() throws java.io.IOException {
        if (hasNextProxy()) {
            java.util.List<? extends java.net.Proxy> list = this.proxies;
            int i = this.nextProxyIndex;
            this.nextProxyIndex = i + 1;
            java.net.Proxy proxy = list.get(i);
            resetNextInetSocketAddress(proxy);
            return proxy;
        }
        throw new java.net.SocketException("No route to " + this.address.url().host() + "; exhausted proxy configurations: " + this.proxies);
    }

    private final void resetNextInetSocketAddress(java.net.Proxy proxy) throws java.io.IOException {
        java.lang.String strHost;
        int iPort;
        java.util.List<java.net.InetAddress> listE;
        java.util.ArrayList arrayList = new java.util.ArrayList();
        this.inetSocketAddresses = arrayList;
        if (proxy.type() == java.net.Proxy.Type.DIRECT || proxy.type() == java.net.Proxy.Type.SOCKS) {
            strHost = this.address.url().host();
            iPort = this.address.url().port();
        } else {
            java.net.SocketAddress socketAddressAddress = proxy.address();
            if (!(socketAddressAddress instanceof java.net.InetSocketAddress)) {
                throw new java.lang.IllegalArgumentException(("Proxy.address() is not an InetSocketAddress: " + socketAddressAddress.getClass()).toString());
            }
            okhttp3.internal.connection.RouteSelector.Companion companion = INSTANCE;
            kotlin.jvm.internal.Intrinsics.k(socketAddressAddress, "proxyAddress");
            java.net.InetSocketAddress inetSocketAddress = (java.net.InetSocketAddress) socketAddressAddress;
            strHost = companion.getSocketHost(inetSocketAddress);
            iPort = inetSocketAddress.getPort();
        }
        if (1 > iPort || iPort >= 65536) {
            throw new java.net.SocketException("No route to " + strHost + ':' + iPort + "; port is out of range");
        }
        if (proxy.type() == java.net.Proxy.Type.SOCKS) {
            arrayList.add(java.net.InetSocketAddress.createUnresolved(strHost, iPort));
            return;
        }
        if (okhttp3.internal.Util.canParseAsIpAddress(strHost)) {
            listE = kotlin.collections.CollectionsKt.e(java.net.InetAddress.getByName(strHost));
        } else {
            this.eventListener.dnsStart(this.call, strHost);
            java.util.List<java.net.InetAddress> listLookup = this.address.dns().lookup(strHost);
            if (listLookup.isEmpty()) {
                throw new java.net.UnknownHostException(this.address.dns() + " returned no addresses for " + strHost);
            }
            this.eventListener.dnsEnd(this.call, strHost, listLookup);
            listE = listLookup;
        }
        java.util.Iterator<java.net.InetAddress> it = listE.iterator();
        while (it.hasNext()) {
            arrayList.add(new java.net.InetSocketAddress(it.next(), iPort));
        }
    }

    private final void resetNextProxy(okhttp3.HttpUrl url, java.net.Proxy proxy) {
        this.eventListener.proxySelectStart(this.call, url);
        java.util.List<java.net.Proxy> listResetNextProxy$selectProxies = resetNextProxy$selectProxies(proxy, url, this);
        this.proxies = listResetNextProxy$selectProxies;
        this.nextProxyIndex = 0;
        this.eventListener.proxySelectEnd(this.call, url, listResetNextProxy$selectProxies);
    }

    private static final java.util.List<java.net.Proxy> resetNextProxy$selectProxies(java.net.Proxy proxy, okhttp3.HttpUrl httpUrl, okhttp3.internal.connection.RouteSelector routeSelector) {
        if (proxy != null) {
            return kotlin.collections.CollectionsKt.e(proxy);
        }
        java.net.URI uri = httpUrl.uri();
        if (uri.getHost() == null) {
            return okhttp3.internal.Util.immutableListOf(java.net.Proxy.NO_PROXY);
        }
        java.util.List<java.net.Proxy> listSelect = routeSelector.address.proxySelector().select(uri);
        if (listSelect == null || listSelect.isEmpty()) {
            return okhttp3.internal.Util.immutableListOf(java.net.Proxy.NO_PROXY);
        }
        kotlin.jvm.internal.Intrinsics.k(listSelect, "proxiesOrNull");
        return okhttp3.internal.Util.toImmutableList(listSelect);
    }

    public final boolean hasNext() {
        return hasNextProxy() || !this.postponedRoutes.isEmpty();
    }

    @org.jetbrains.annotations.NotNull
    public final okhttp3.internal.connection.RouteSelector.Selection next() throws java.io.IOException {
        if (!hasNext()) {
            throw new java.util.NoSuchElementException();
        }
        java.util.ArrayList arrayList = new java.util.ArrayList();
        while (hasNextProxy()) {
            java.net.Proxy proxyNextProxy = nextProxy();
            java.util.Iterator<? extends java.net.InetSocketAddress> it = this.inetSocketAddresses.iterator();
            while (it.hasNext()) {
                okhttp3.Route route = new okhttp3.Route(this.address, proxyNextProxy, it.next());
                if (this.routeDatabase.shouldPostpone(route)) {
                    this.postponedRoutes.add(route);
                } else {
                    arrayList.add(route);
                }
            }
            if (!arrayList.isEmpty()) {
                break;
            }
        }
        if (arrayList.isEmpty()) {
            kotlin.collections.CollectionsKt.E(arrayList, this.postponedRoutes);
            this.postponedRoutes.clear();
        }
        return new okhttp3.internal.connection.RouteSelector.Selection(arrayList);
    }
}
