forked from MiniDNS/minidns
-
Notifications
You must be signed in to change notification settings - Fork 0
/
UnixUsingEtcResolvConf.java
73 lines (63 loc) · 2.3 KB
/
UnixUsingEtcResolvConf.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
/*
* Copyright 2015 the original author or authors
*
* This software is licensed under the Apache License, Version 2.0,
* the GNU Lesser General Public License version 2 or later ("LGPL")
* and the WTFPL.
* You may choose either license to govern your use of this software only
* upon the condition that you accept all of the terms of either
* the Apache License 2.0, the LGPL 2.1+ or the WTFPL.
*/
package de.measite.minidns.dnsserverlookup;
import de.measite.minidns.util.PlatformDetection;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
public class UnixUsingEtcResolvConf extends AbstractDNSServerLookupMechanism {
public static final DNSServerLookupMechanism INSTANCE = new UnixUsingEtcResolvConf();
public static final int PRIORITY = 2000;
private static final String RESOLV_CONF_FILE = "/etc/resolv.conf";
private static final String NAMESERVER_PREFIX = "nameserver ";
protected UnixUsingEtcResolvConf() {
super(UnixUsingEtcResolvConf.class.getSimpleName(), PRIORITY);
}
@Override
public String[] getDnsServerAddresses() {
if (PlatformDetection.isAndroid()) {
// Don't rely on resolv.conf when on Android
return null;
}
File file = new File(RESOLV_CONF_FILE);
if (!file.exists()) {
// Not very unixoid systems
return null;
}
List<String> servers = new ArrayList<>();
BufferedReader reader = null;
try {
reader = new BufferedReader(new InputStreamReader(new FileInputStream(file)));
String line;
while ((line = reader.readLine()) != null) {
if (line.startsWith(NAMESERVER_PREFIX)) {
servers.add(line.substring(NAMESERVER_PREFIX.length()));
}
}
} catch (IOException e) {
return null;
} finally {
if (reader != null) try {
reader.close();
} catch (IOException ignored) {
// continue
}
}
if (servers.isEmpty()) {
return null;
}
return servers.toArray(new String[servers.size()]);
}
}