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.NetUtil;
19 import io.netty.util.internal.PlatformDependent;
20 import io.netty.util.internal.SocketUtils;
21 import io.netty.util.internal.SystemPropertyUtil;
22 import io.netty.util.internal.logging.InternalLogger;
23 import io.netty.util.internal.logging.InternalLoggerFactory;
24
25 import java.lang.reflect.Method;
26 import java.net.Inet6Address;
27 import java.net.InetSocketAddress;
28 import java.util.ArrayList;
29 import java.util.Collections;
30 import java.util.List;
31
32 import static io.netty.resolver.dns.DnsServerAddresses.sequential;
33
34
35
36
37
38
39
40 public final class DefaultDnsServerAddressStreamProvider implements DnsServerAddressStreamProvider {
41 private static final InternalLogger logger =
42 InternalLoggerFactory.getInstance(DefaultDnsServerAddressStreamProvider.class);
43 private static final String DEFAULT_FALLBACK_SERVER_PROPERTY = "io.netty.resolver.dns.defaultNameServerFallback";
44 public static final DefaultDnsServerAddressStreamProvider INSTANCE = new DefaultDnsServerAddressStreamProvider();
45
46 private static final List<InetSocketAddress> DEFAULT_NAME_SERVER_LIST;
47 private static final DnsServerAddresses DEFAULT_NAME_SERVERS;
48 static final int DNS_PORT = 53;
49
50 static {
51 final List<InetSocketAddress> defaultNameServers = new ArrayList<InetSocketAddress>(2);
52 if (!PlatformDependent.isAndroid()) {
53
54
55 if (!PlatformDependent.isWindows()) {
56
57 try {
58 defaultNameServers.addAll(ResolvConf.system().getNameservers());
59 } catch (IllegalStateException e) {
60 String fallbackMessage = "Failed to get name servers from /etc/resolv.conf; will fall back to JNDI";
61 if (logger.isDebugEnabled()) {
62
63 logger.info(fallbackMessage, e);
64 } else {
65 logger.info(fallbackMessage);
66 }
67 DirContextUtils.addNameServers(defaultNameServers, DNS_PORT);
68 }
69 } else {
70 DirContextUtils.addNameServers(defaultNameServers, DNS_PORT);
71 }
72 }
73
74
75
76 if (PlatformDependent.javaVersion() < 9 && defaultNameServers.isEmpty()) {
77 try {
78 Class<?> configClass = Class.forName("sun.net.dns.ResolverConfiguration");
79 Method open = configClass.getMethod("open");
80 Method nameservers = configClass.getMethod("nameservers");
81 Object instance = open.invoke(null);
82
83 @SuppressWarnings("unchecked")
84 final List<String> list = (List<String>) nameservers.invoke(instance);
85 for (String a: list) {
86 if (a != null) {
87 defaultNameServers.add(new InetSocketAddress(SocketUtils.addressByName(a), DNS_PORT));
88 }
89 }
90 } catch (Exception ignore) {
91
92
93 }
94 }
95
96 if (!defaultNameServers.isEmpty()) {
97 if (logger.isDebugEnabled()) {
98 logger.debug(
99 "Default DNS servers: {} (sun.net.dns.ResolverConfiguration)", defaultNameServers);
100 }
101 } else {
102 String defaultNameserverString = SystemPropertyUtil.get(DEFAULT_FALLBACK_SERVER_PROPERTY, null);
103 if (defaultNameserverString != null) {
104 for (String server : defaultNameserverString.split(",")) {
105 String dns = server.trim();
106 if (!NetUtil.isValidIpV4Address(dns) && !NetUtil.isValidIpV6Address(dns)) {
107 throw new ExceptionInInitializerError(DEFAULT_FALLBACK_SERVER_PROPERTY + " doesn't" +
108 " contain a valid list of NameServers: " + defaultNameserverString);
109 }
110 defaultNameServers.add(SocketUtils.socketAddress(server.trim(), DNS_PORT));
111 }
112 if (defaultNameServers.isEmpty()) {
113 throw new ExceptionInInitializerError(DEFAULT_FALLBACK_SERVER_PROPERTY + " doesn't" +
114 " contain a valid list of NameServers: " + defaultNameserverString);
115 }
116
117 if (logger.isWarnEnabled()) {
118 logger.warn(
119 "Default DNS servers: {} (Configured by {} system property)",
120 defaultNameServers, DEFAULT_FALLBACK_SERVER_PROPERTY);
121 }
122 } else {
123
124
125
126 if (NetUtil.isIpV6AddressesPreferred() ||
127 (NetUtil.LOCALHOST instanceof Inet6Address && !NetUtil.isIpV4StackPreferred())) {
128 Collections.addAll(
129 defaultNameServers,
130 SocketUtils.socketAddress("2001:4860:4860::8888", DNS_PORT),
131 SocketUtils.socketAddress("2001:4860:4860::8844", DNS_PORT));
132 } else {
133 Collections.addAll(
134 defaultNameServers,
135 SocketUtils.socketAddress("8.8.8.8", DNS_PORT),
136 SocketUtils.socketAddress("8.8.4.4", DNS_PORT));
137 }
138
139 if (logger.isWarnEnabled()) {
140 logger.warn(
141 "Default DNS servers: {} (Google Public DNS as a fallback)", defaultNameServers);
142 }
143 }
144 }
145
146 DEFAULT_NAME_SERVER_LIST = Collections.unmodifiableList(defaultNameServers);
147 DEFAULT_NAME_SERVERS = sequential(DEFAULT_NAME_SERVER_LIST);
148 }
149
150 private DefaultDnsServerAddressStreamProvider() {
151 }
152
153 @Override
154 public DnsServerAddressStream nameServerAddressStream(String hostname) {
155 return DEFAULT_NAME_SERVERS.stream();
156 }
157
158
159
160
161
162
163 public static List<InetSocketAddress> defaultAddressList() {
164 return DEFAULT_NAME_SERVER_LIST;
165 }
166
167
168
169
170
171
172
173
174
175
176
177
178 public static DnsServerAddresses defaultAddresses() {
179 return DEFAULT_NAME_SERVERS;
180 }
181 }