package io.netty.resolver;

import g.a.a.a.a;
import io.netty.util.NetUtil;
import io.netty.util.internal.ObjectUtil;
import io.netty.util.internal.PlatformDependent;
import io.netty.util.internal.logging.InternalLogger;
import io.netty.util.internal.logging.InternalLoggerFactory;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.io.Reader;
import java.net.Inet4Address;
import java.net.Inet6Address;
import java.net.InetAddress;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Locale;
import java.util.regex.Pattern;

/* JADX INFO: loaded from: classes.dex */
public final class HostsFileParser {
    private static final String WINDOWS_DEFAULT_SYSTEM_ROOT = "C:\\Windows";
    private static final String WINDOWS_HOSTS_FILE_RELATIVE_PATH = "\\system32\\drivers\\etc\\hosts";
    private static final String X_PLATFORMS_HOSTS_FILE_PATH = "/etc/hosts";
    private static final Pattern WHITESPACES = Pattern.compile("[ \t]+");
    private static final InternalLogger logger = InternalLoggerFactory.getInstance((Class<?>) HostsFileParser.class);

    private HostsFileParser() {
    }

    private static File locateHostsFile() {
        if (!PlatformDependent.isWindows()) {
            return new File(X_PLATFORMS_HOSTS_FILE_PATH);
        }
        File file = new File(System.getenv("SystemRoot") + WINDOWS_HOSTS_FILE_RELATIVE_PATH);
        return !file.exists() ? new File("C:\\Windows\\system32\\drivers\\etc\\hosts") : file;
    }

    public static HostsFileEntries parse() {
        return parse(locateHostsFile());
    }

    public static HostsFileEntries parse(File file) {
        ObjectUtil.checkNotNull(file, "file");
        return (file.exists() && file.isFile()) ? parse(new BufferedReader(new FileReader(file))) : HostsFileEntries.EMPTY;
    }

    public static HostsFileEntries parse(Reader reader) {
        byte[] bArrCreateByteArrayFromIpAddressString;
        ObjectUtil.checkNotNull(reader, "reader");
        BufferedReader bufferedReader = new BufferedReader(reader);
        try {
            HashMap map = new HashMap();
            HashMap map2 = new HashMap();
            while (true) {
                String line = bufferedReader.readLine();
                if (line == null) {
                    break;
                }
                int iIndexOf = line.indexOf(35);
                if (iIndexOf != -1) {
                    line = line.substring(0, iIndexOf);
                }
                String strTrim = line.trim();
                if (!strTrim.isEmpty()) {
                    ArrayList arrayList = new ArrayList();
                    for (String str : WHITESPACES.split(strTrim)) {
                        if (!str.isEmpty()) {
                            arrayList.add(str);
                        }
                    }
                    if (arrayList.size() >= 2 && (bArrCreateByteArrayFromIpAddressString = NetUtil.createByteArrayFromIpAddressString((String) arrayList.get(0))) != null) {
                        for (int i2 = 1; i2 < arrayList.size(); i2++) {
                            String str2 = (String) arrayList.get(i2);
                            String lowerCase = str2.toLowerCase(Locale.ENGLISH);
                            InetAddress byAddress = InetAddress.getByAddress(str2, bArrCreateByteArrayFromIpAddressString);
                            if (byAddress instanceof Inet4Address) {
                                Inet4Address inet4Address = (Inet4Address) map.put(lowerCase, (Inet4Address) byAddress);
                                if (inet4Address != null) {
                                    map.put(lowerCase, inet4Address);
                                }
                            } else {
                                Inet6Address inet6Address = (Inet6Address) map2.put(lowerCase, (Inet6Address) byAddress);
                                if (inet6Address != null) {
                                    map2.put(lowerCase, inet6Address);
                                }
                            }
                        }
                    }
                }
            }
            return (map.isEmpty() && map2.isEmpty()) ? HostsFileEntries.EMPTY : new HostsFileEntries(map, map2);
        } finally {
            try {
                bufferedReader.close();
            } catch (IOException e2) {
                logger.warn("Failed to close a reader", (Throwable) e2);
            }
        }
    }

    public static HostsFileEntries parseSilently() {
        File fileLocateHostsFile = locateHostsFile();
        try {
            return parse(fileLocateHostsFile);
        } catch (IOException e2) {
            InternalLogger internalLogger = logger;
            StringBuilder sbF = a.F("Failed to load and parse hosts file at ");
            sbF.append(fileLocateHostsFile.getPath());
            internalLogger.warn(sbF.toString(), (Throwable) e2);
            return HostsFileEntries.EMPTY;
        }
    }
}
