package k5; import h5.b0; import h5.i; import h5.m0; import h5.t; import java.io.IOException; import java.net.InetAddress; import java.net.InetSocketAddress; import java.net.Proxy; import java.net.SocketAddress; import java.net.SocketException; import java.net.UnknownHostException; import java.util.ArrayList; import java.util.Collections; import java.util.List; import java.util.NoSuchElementException; /* loaded from: classes2.dex */ public final class f { /* renamed from: a, reason: collision with root package name */ private final h5.a f11241a; /* renamed from: b, reason: collision with root package name */ private final d f11242b; /* renamed from: c, reason: collision with root package name */ private final i f11243c; /* renamed from: d, reason: collision with root package name */ private final t f11244d; /* renamed from: f, reason: collision with root package name */ private int f11246f; /* renamed from: e, reason: collision with root package name */ private List f11245e = Collections.emptyList(); /* renamed from: g, reason: collision with root package name */ private List f11247g = Collections.emptyList(); /* renamed from: h, reason: collision with root package name */ private final List f11248h = new ArrayList(); public static final class a { /* renamed from: a, reason: collision with root package name */ private final List f11249a; /* renamed from: b, reason: collision with root package name */ private int f11250b = 0; a(List list) { this.f11249a = list; } public List a() { return new ArrayList(this.f11249a); } public boolean b() { return this.f11250b < this.f11249a.size(); } public m0 c() { if (!b()) { throw new NoSuchElementException(); } List list = this.f11249a; int i10 = this.f11250b; this.f11250b = i10 + 1; return (m0) list.get(i10); } } public f(h5.a aVar, d dVar, i iVar, t tVar) { this.f11241a = aVar; this.f11242b = dVar; this.f11243c = iVar; this.f11244d = tVar; b(aVar.l(), aVar.g()); } static String a(InetSocketAddress inetSocketAddress) { InetAddress address = inetSocketAddress.getAddress(); return address == null ? inetSocketAddress.getHostName() : address.getHostAddress(); } private void b(b0 b0Var, Proxy proxy) { List u10; if (proxy != null) { u10 = Collections.singletonList(proxy); } else { List select = this.f11241a.i().select(b0Var.D()); u10 = (select == null || select.isEmpty()) ? i5.c.u(Proxy.NO_PROXY) : i5.c.t(select); } this.f11245e = u10; this.f11246f = 0; } private void c(Proxy proxy) { String t10; int y10; this.f11247g = new ArrayList(); if (proxy.type() == Proxy.Type.DIRECT || proxy.type() == Proxy.Type.SOCKS) { t10 = this.f11241a.l().t(); y10 = this.f11241a.l().y(); } else { SocketAddress address = proxy.address(); if (!(address instanceof InetSocketAddress)) { throw new IllegalArgumentException("Proxy.address() is not an InetSocketAddress: " + address.getClass()); } InetSocketAddress inetSocketAddress = (InetSocketAddress) address; t10 = a(inetSocketAddress); y10 = inetSocketAddress.getPort(); } if (y10 < 1 || y10 > 65535) { throw new SocketException("No route to " + t10 + ":" + y10 + "; port is out of range"); } if (proxy.type() == Proxy.Type.SOCKS) { this.f11247g.add(InetSocketAddress.createUnresolved(t10, y10)); return; } this.f11244d.k(this.f11243c, t10); List a10 = this.f11241a.d().a(t10); if (a10.isEmpty()) { throw new UnknownHostException(this.f11241a.d() + " returned no addresses for " + t10); } this.f11244d.j(this.f11243c, t10, a10); int size = a10.size(); for (int i10 = 0; i10 < size; i10++) { this.f11247g.add(new InetSocketAddress((InetAddress) a10.get(i10), y10)); } } private boolean d() { return this.f11246f < this.f11245e.size(); } private Proxy e() { if (d()) { List list = this.f11245e; int i10 = this.f11246f; this.f11246f = i10 + 1; Proxy proxy = (Proxy) list.get(i10); c(proxy); return proxy; } throw new SocketException("No route to " + this.f11241a.l().t() + "; exhausted proxy configurations: " + this.f11245e); } public void f(m0 m0Var, IOException iOException) { if (m0Var.b().type() != Proxy.Type.DIRECT && this.f11241a.i() != null) { this.f11241a.i().connectFailed(this.f11241a.l().D(), m0Var.b().address(), iOException); } this.f11242b.b(m0Var); } public boolean g() { return d() || !this.f11248h.isEmpty(); } public a h() { if (!g()) { throw new NoSuchElementException(); } ArrayList arrayList = new ArrayList(); while (d()) { Proxy e10 = e(); int size = this.f11247g.size(); for (int i10 = 0; i10 < size; i10++) { m0 m0Var = new m0(this.f11241a, e10, (InetSocketAddress) this.f11247g.get(i10)); if (this.f11242b.c(m0Var)) { this.f11248h.add(m0Var); } else { arrayList.add(m0Var); } } if (!arrayList.isEmpty()) { break; } } if (arrayList.isEmpty()) { arrayList.addAll(this.f11248h); this.f11248h.clear(); } return new a(arrayList); } }