package a.b.a.k;

import a.a.a.b;
import java.net.Inet4Address;
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Enumeration;
import java.util.Iterator;
import java.util.List;
import java.util.regex.Pattern;

public class c {

    /* renamed from: a  reason: collision with root package name */
    private static final String f45a = "NetworkInterfaceUtils";

    private static String a(NetworkInterface networkInterface) {
        String displayName = networkInterface.getDisplayName();
        return displayName == null ? "" : displayName;
    }

    public static String a(List<String> list) {
        NetworkInterface b = b(list);
        if (b != null) {
            return b(b);
        }
        b.e(f45a, "getSelfHostAddress: mostFitInterface is null");
        return null;
    }

    public static String b(NetworkInterface networkInterface) {
        Enumeration<InetAddress> inetAddresses = networkInterface.getInetAddresses();
        while (inetAddresses != null && inetAddresses.hasMoreElements()) {
            InetAddress nextElement = inetAddresses.nextElement();
            if (nextElement.isLoopbackAddress() || !(nextElement instanceof Inet4Address)) {
                b.e(f45a, "networkInterface=" + networkInterface.getDisplayName() + ",s InetAddress=" + nextElement + " not matches.");
            } else {
                String hostAddress = nextElement.getHostAddress();
                b.c(f45a, "getHostAddressByInterface: networkInterface=" + networkInterface.getDisplayName() + ", hostAddress=" + hostAddress);
                return hostAddress;
            }
        }
        return null;
    }

    private static NetworkInterface b(List<String> list) {
        b.c(f45a, "getNetworkInterfaceByRegex: regex=" + list);
        ArrayList arrayList = new ArrayList();
        for (String str : list) {
            arrayList.add(Pattern.compile(str));
        }
        try {
            Enumeration<NetworkInterface> networkInterfaces = NetworkInterface.getNetworkInterfaces();
            if (networkInterfaces == null) {
                return null;
            }
            ArrayList list2 = Collections.list(networkInterfaces);
            Iterator it = arrayList.iterator();
            while (it.hasNext()) {
                Pattern pattern = (Pattern) it.next();
                Iterator it2 = list2.iterator();
                while (true) {
                    if (it2.hasNext()) {
                        NetworkInterface networkInterface = (NetworkInterface) it2.next();
                        if (pattern.matcher(a(networkInterface)).matches()) {
                            return networkInterface;
                        }
                    }
                }
            }
            return null;
        } catch (SocketException e) {
            b.b(f45a, "Get NetworkInterfaces failed", e);
            return null;
        }
    }
}
