1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package io.netty.resolver.dns;
17
18 import java.io.BufferedReader;
19 import java.io.FileReader;
20 import java.io.IOException;
21 import java.net.InetSocketAddress;
22 import java.util.ArrayList;
23 import java.util.Collections;
24 import java.util.List;
25
26
27
28
29 final class ResolvConf {
30 private final List<InetSocketAddress> nameservers;
31
32
33
34
35
36
37
38
39 static ResolvConf fromReader(BufferedReader reader) throws IOException {
40 return new ResolvConf(reader);
41 }
42
43
44
45
46
47 static ResolvConf fromFile(String file) throws IOException {
48 FileReader fileReader = new FileReader(file);
49 try {
50 BufferedReader reader = new BufferedReader(new FileReader(file));
51 return fromReader(reader);
52 } finally {
53 fileReader.close();
54 }
55 }
56
57
58
59
60
61 static ResolvConf system() {
62 ResolvConf resolvConv = ResolvConfLazy.machineResolvConf;
63 if (resolvConv != null) {
64 return resolvConv;
65 }
66 throw new IllegalStateException("/etc/resolv.conf could not be read");
67 }
68
69 private ResolvConf(BufferedReader reader) throws IOException {
70 List<InetSocketAddress> nameservers = new ArrayList<InetSocketAddress>();
71 String ln;
72 while ((ln = reader.readLine()) != null) {
73 ln = ln.trim();
74 if (ln.isEmpty()) {
75 continue;
76 }
77
78 if (ln.startsWith("nameserver")) {
79 ln = ln.substring("nameserver".length()).trim();
80 nameservers.add(new InetSocketAddress(ln, 53));
81 }
82 }
83 this.nameservers = Collections.unmodifiableList(nameservers);
84 }
85
86 List<InetSocketAddress> getNameservers() {
87 return nameservers;
88 }
89
90 private static final class ResolvConfLazy {
91 static final ResolvConf machineResolvConf;
92
93 static {
94 ResolvConf resolvConf;
95 try {
96 resolvConf = ResolvConf.fromFile("/etc/resolv.conf");
97 } catch (IOException e) {
98 resolvConf = null;
99 } catch (SecurityException ignore) {
100 resolvConf = null;
101 }
102 machineResolvConf = resolvConf;
103 }
104 }
105 }