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