| 1 |
/* |
| 2 |
* Copyright 2015-2020 the original author or authors |
| 3 |
* |
| 4 |
* This software is licensed under the Apache License, Version 2.0, |
| 5 |
* the GNU Lesser General Public License version 2 or later ("LGPL") |
| 6 |
* and the WTFPL. |
| 7 |
* You may choose either license to govern your use of this software only |
| 8 |
* upon the condition that you accept all of the terms of either |
| 9 |
* the Apache License 2.0, the LGPL 2.1+ or the WTFPL. |
| 10 |
*/ |
| 11 |
package org.minidns.dnsserverlookup; |
| 12 |
|
| 13 |
import org.minidns.util.PlatformDetection; |
| 14 |
|
| 15 |
import java.io.BufferedReader; |
| 16 |
import java.io.File; |
| 17 |
import java.io.FileInputStream; |
| 18 |
import java.io.IOException; |
| 19 |
import java.io.InputStreamReader; |
| 20 |
import java.nio.charset.StandardCharsets; |
| 21 |
import java.util.ArrayList; |
| 22 |
import java.util.List; |
| 23 |
import java.util.logging.Level; |
| 24 |
import java.util.logging.Logger; |
| 25 |
import java.util.regex.Matcher; |
| 26 |
import java.util.regex.Pattern; |
| 27 |
|
| 28 |
public final class UnixUsingEtcResolvConf extends AbstractDnsServerLookupMechanism { |
| 29 |
|
| 30 |
public static final DnsServerLookupMechanism INSTANCE = new UnixUsingEtcResolvConf(); |
| 31 |
public static final int PRIORITY = 2000; |
| 32 |
|
| 33 |
private static final Logger LOGGER = Logger.getLogger(UnixUsingEtcResolvConf.class.getName()); |
| 34 |
|
| 35 |
private static final String RESOLV_CONF_FILE = "/etc/resolv.conf"; |
| 36 |
private static final Pattern NAMESERVER_PATTERN = Pattern.compile("^nameserver\\s+(.*)$"); |
| 37 |
|
| 38 |
private static List<String> cached; |
| 39 |
private static long lastModified; |
| 40 |
|
| 41 |
private UnixUsingEtcResolvConf() { |
| 42 |
super(UnixUsingEtcResolvConf.class.getSimpleName(), PRIORITY); |
| 43 |
} |
| 44 |
|
| 45 |
@Override |
| 46 |
public List<String> getDnsServerAddresses() { |
| 47 |
File file = new File(RESOLV_CONF_FILE); |
| 48 |
if (!file.exists()) { |
| 49 |
// Not very unixoid systems |
| 50 |
return null; |
| 51 |
} |
| 52 |
|
| 53 |
long currentLastModified = file.lastModified(); |
| 54 |
if (currentLastModified == lastModified && cached != null) { |
| 55 |
return cached; |
| 56 |
} |
| 57 |
|
| 58 |
List<String> servers = new ArrayList<>(); |
| 59 |
BufferedReader reader = null; |
| 60 |
try { |
| 61 |
reader = new BufferedReader(new InputStreamReader(new FileInputStream(file), StandardCharsets.UTF_8)); |
| 62 |
String line; |
| 63 |
while ((line = reader.readLine()) != null) { |
| 64 |
Matcher matcher = NAMESERVER_PATTERN.matcher(line); |
| 65 |
if (matcher.matches()) { |
| 66 |
servers.add(matcher.group(1).trim()); |
| 67 |
} |
| 68 |
} |
| 69 |
} catch (IOException e) { |
| 70 |
LOGGER.log(Level.WARNING, "Could not read from " + RESOLV_CONF_FILE, e); |
| 71 |
return null; |
| 72 |
} finally { |
| 73 |
if (reader != null) try { |
| 74 |
reader.close(); |
| 75 |
} catch (IOException e) { |
| 76 |
LOGGER.log(Level.WARNING, "Could not close reader", e); |
| 77 |
} |
| 78 |
} |
| 79 |
|
| 80 |
if (servers.isEmpty()) { |
| 81 |
LOGGER.fine("Could not find any nameservers in " + RESOLV_CONF_FILE); |
| 82 |
return null; |
| 83 |
} |
| 84 |
|
| 85 |
cached = servers; |
| 86 |
lastModified = currentLastModified; |
| 87 |
|
| 88 |
return cached; |
| 89 |
} |
| 90 |
|
| 91 |
@Override |
| 92 |
public boolean isAvailable() { |
| 93 |
if (PlatformDetection.isAndroid()) { |
| 94 |
// Don't rely on resolv.conf when on Android |
| 95 |
return false; |
| 96 |
} |
| 97 |
|
| 98 |
File file = new File(RESOLV_CONF_FILE); |
| 99 |
|
| 100 |
boolean resolvConfFileExists; |
| 101 |
try { |
| 102 |
resolvConfFileExists = file.exists(); |
| 103 |
} catch (SecurityException securityException) { |
| 104 |
LOGGER.log(Level.FINE, "Access to /etc/resolv.conf not possible", securityException); |
| 105 |
return false; |
| 106 |
} |
| 107 |
return resolvConfFileExists; |
| 108 |
} |
| 109 |
|
| 110 |
} |