package m4;

import com.revenuecat.purchases.common.Constants;
import java.net.Authenticator;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.MalformedURLException;
import java.net.PasswordAuthentication;
import java.net.Proxy;
import java.net.ProxySelector;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import k4.C2385D;

/* JADX INFO: renamed from: m4.u1, reason: case insensitive filesystem */
/* JADX INFO: loaded from: classes3.dex */
public final class C2549u1 {

    /* JADX INFO: renamed from: d, reason: collision with root package name */
    public static final Logger f21195d = Logger.getLogger(C2549u1.class.getName());

    /* JADX INFO: renamed from: e, reason: collision with root package name */
    public static final i2 f21196e = new i2(14);

    /* JADX INFO: renamed from: f, reason: collision with root package name */
    public static final C2498d0 f21197f = new C2498d0(1);

    /* JADX INFO: renamed from: a, reason: collision with root package name */
    public final C2498d0 f21198a;

    /* JADX INFO: renamed from: b, reason: collision with root package name */
    public final i2 f21199b;

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

    public C2549u1() {
        String str = System.getenv("GRPC_PROXY_EXP");
        C2498d0 c2498d0 = f21197f;
        c2498d0.getClass();
        this.f21198a = c2498d0;
        i2 i2Var = f21196e;
        i2Var.getClass();
        this.f21199b = i2Var;
        if (str == null) {
            this.f21200c = null;
            return;
        }
        String[] strArrSplit = str.split(Constants.SUBS_ID_BASE_PLAN_ID_SEPARATOR, 2);
        int i6 = strArrSplit.length > 1 ? Integer.parseInt(strArrSplit[1]) : 80;
        f21195d.warning("Detected GRPC_PROXY_EXP and will honor it, but this feature will be removed in a future release. Use the JVM flags \"-Dhttps.proxyHost=HOST -Dhttps.proxyPort=PORT\" to set the https proxy for this JVM.");
        this.f21200c = new InetSocketAddress(strArrSplit[0], i6);
    }

    public final C2385D a(InetSocketAddress inetSocketAddress) {
        URL url;
        C2385D c2385d;
        if (inetSocketAddress != null) {
            InetSocketAddress inetSocketAddress2 = this.f21200c;
            if (inetSocketAddress2 != null) {
                int i6 = C2385D.f19787e;
                A4.g.o(inetSocketAddress, "targetAddress");
                return new C2385D(inetSocketAddress2, inetSocketAddress, null, null);
            }
            Logger logger = f21195d;
            try {
                try {
                    URI uri = new URI("https", null, AbstractC2504f0.d(inetSocketAddress), inetSocketAddress.getPort(), null, null, null);
                    this.f21198a.getClass();
                    ProxySelector proxySelector = ProxySelector.getDefault();
                    if (proxySelector == null) {
                        logger.log(Level.FINE, "proxy selector is null, so continuing without proxy lookup");
                        return null;
                    }
                    List<Proxy> listSelect = proxySelector.select(uri);
                    if (listSelect.size() > 1) {
                        logger.warning("More than 1 proxy detected, gRPC will select the first one");
                    }
                    Proxy proxy = listSelect.get(0);
                    if (proxy.type() != Proxy.Type.DIRECT) {
                        InetSocketAddress inetSocketAddress3 = (InetSocketAddress) proxy.address();
                        String strD = AbstractC2504f0.d(inetSocketAddress3);
                        InetAddress address = inetSocketAddress3.getAddress();
                        int port = inetSocketAddress3.getPort();
                        this.f21199b.getClass();
                        try {
                            url = new URL("https", strD, port, "");
                        } catch (MalformedURLException unused) {
                            logger.log(Level.WARNING, "failed to create URL for Authenticator: {0} {1}", new Object[]{"https", strD});
                            url = null;
                        }
                        PasswordAuthentication passwordAuthenticationRequestPasswordAuthentication = Authenticator.requestPasswordAuthentication(strD, address, port, "https", "", null, url, Authenticator.RequestorType.PROXY);
                        if (inetSocketAddress3.isUnresolved()) {
                            inetSocketAddress3 = new InetSocketAddress(InetAddress.getByName(inetSocketAddress3.getHostName()), inetSocketAddress3.getPort());
                        }
                        int i7 = C2385D.f19787e;
                        if (passwordAuthenticationRequestPasswordAuthentication == null) {
                            c2385d = new C2385D(inetSocketAddress3, inetSocketAddress, null, null);
                        } else {
                            c2385d = new C2385D(inetSocketAddress3, inetSocketAddress, passwordAuthenticationRequestPasswordAuthentication.getUserName(), passwordAuthenticationRequestPasswordAuthentication.getPassword() != null ? new String(passwordAuthenticationRequestPasswordAuthentication.getPassword()) : null);
                        }
                        return c2385d;
                    }
                } catch (URISyntaxException e6) {
                    logger.log(Level.WARNING, "Failed to construct URI for proxy lookup, proceeding without proxy", (Throwable) e6);
                    return null;
                }
            } catch (Throwable th) {
                logger.log(Level.WARNING, "Failed to get host for proxy lookup, proceeding without proxy", th);
                return null;
            }
        }
        return null;
    }
}
