1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package io.netty5.resolver.dns;
17
18 import io.netty5.util.NetUtil;
19 import io.netty5.util.internal.PlatformDependent;
20 import io.netty5.util.internal.SocketUtils;
21 import io.netty5.util.internal.logging.InternalLogger;
22 import io.netty5.util.internal.logging.InternalLoggerFactory;
23
24 import java.net.Inet6Address;
25 import java.net.InetSocketAddress;
26 import java.util.ArrayList;
27 import java.util.Collections;
28 import java.util.List;
29
30 import static io.netty5.resolver.dns.DnsServerAddresses.sequential;
31
32
33
34
35
36
37
38 public final class DefaultDnsServerAddressStreamProvider implements DnsServerAddressStreamProvider {
39 private static final InternalLogger logger =
40 InternalLoggerFactory.getInstance(DefaultDnsServerAddressStreamProvider.class);
41 public static final DefaultDnsServerAddressStreamProvider INSTANCE = new DefaultDnsServerAddressStreamProvider();
42
43 private static final List<InetSocketAddress> DEFAULT_NAME_SERVER_LIST;
44 private static final DnsServerAddresses DEFAULT_NAME_SERVERS;
45 static final int DNS_PORT = 53;
46
47 static {
48 final List<InetSocketAddress> defaultNameServers = new ArrayList<>(2);
49 if (!PlatformDependent.isAndroid()) {
50
51
52 DirContextUtils.addNameServers(defaultNameServers, DNS_PORT);
53 }
54
55 if (!defaultNameServers.isEmpty()) {
56 if (logger.isDebugEnabled()) {
57 logger.debug(
58 "Default DNS servers: {} (sun.net.dns.ResolverConfiguration)", defaultNameServers);
59 }
60 } else {
61
62
63
64 if (NetUtil.isIpV6AddressesPreferred() ||
65 NetUtil.LOCALHOST instanceof Inet6Address && !NetUtil.isIpV4StackPreferred()) {
66 Collections.addAll(
67 defaultNameServers,
68 SocketUtils.socketAddress("2001:4860:4860::8888", DNS_PORT),
69 SocketUtils.socketAddress("2001:4860:4860::8844", DNS_PORT));
70 } else {
71 Collections.addAll(
72 defaultNameServers,
73 SocketUtils.socketAddress("8.8.8.8", DNS_PORT),
74 SocketUtils.socketAddress("8.8.4.4", DNS_PORT));
75 }
76
77 if (logger.isWarnEnabled()) {
78 logger.warn(
79 "Default DNS servers: {} (Google Public DNS as a fallback)", defaultNameServers);
80 }
81 }
82
83 DEFAULT_NAME_SERVER_LIST = Collections.unmodifiableList(defaultNameServers);
84 DEFAULT_NAME_SERVERS = sequential(DEFAULT_NAME_SERVER_LIST);
85 }
86
87 private DefaultDnsServerAddressStreamProvider() {
88 }
89
90 @Override
91 public DnsServerAddressStream nameServerAddressStream(String hostname) {
92 return DEFAULT_NAME_SERVERS.stream();
93 }
94
95
96
97
98
99
100 public static List<InetSocketAddress> defaultAddressList() {
101 return DEFAULT_NAME_SERVER_LIST;
102 }
103
104
105
106
107
108
109
110
111
112
113
114
115 public static DnsServerAddresses defaultAddresses() {
116 return DEFAULT_NAME_SERVERS;
117 }
118 }