|
83 | 83 | import java.util.HashMap; |
84 | 84 | import java.util.Iterator; |
85 | 85 | import java.util.List; |
| 86 | +import java.util.Locale; |
86 | 87 | import java.util.Map; |
87 | 88 | import java.util.concurrent.TimeUnit; |
88 | 89 |
|
@@ -102,8 +103,8 @@ public class DnsNameResolver extends InetNameResolver { |
102 | 103 |
|
103 | 104 | private static final InternalLogger logger = InternalLoggerFactory.getInstance(DnsNameResolver.class); |
104 | 105 | private static final String LOCALHOST = "localhost"; |
| 106 | + private static final String DOT_LOCALHOST = '.' + LOCALHOST; |
105 | 107 | private static final String WINDOWS_HOST_NAME; |
106 | | - private static final InetAddress LOCALHOST_ADDRESS; |
107 | 108 | private static final DnsRecord[] EMPTY_ADDITIONALS = new DnsRecord[0]; |
108 | 109 | private static final DnsRecordType[] IPV4_ONLY_RESOLVED_RECORD_TYPES = |
109 | 110 | {DnsRecordType.A}; |
@@ -136,18 +137,14 @@ public boolean isSharable() { |
136 | 137 | static { |
137 | 138 | if (NetUtil.isIpV4StackPreferred() || !anyInterfaceSupportsIpV6()) { |
138 | 139 | DEFAULT_RESOLVE_ADDRESS_TYPES = ResolvedAddressTypes.IPV4_ONLY; |
139 | | - LOCALHOST_ADDRESS = NetUtil.LOCALHOST4; |
140 | 140 | } else { |
141 | 141 | if (NetUtil.isIpV6AddressesPreferred()) { |
142 | 142 | DEFAULT_RESOLVE_ADDRESS_TYPES = ResolvedAddressTypes.IPV6_PREFERRED; |
143 | | - LOCALHOST_ADDRESS = NetUtil.LOCALHOST6; |
144 | 143 | } else { |
145 | 144 | DEFAULT_RESOLVE_ADDRESS_TYPES = ResolvedAddressTypes.IPV4_PREFERRED; |
146 | | - LOCALHOST_ADDRESS = NetUtil.LOCALHOST4; |
147 | 145 | } |
148 | 146 | } |
149 | 147 | logger.debug("Default ResolvedAddressTypes: {}", DEFAULT_RESOLVE_ADDRESS_TYPES); |
150 | | - logger.debug("Localhost address: {}", LOCALHOST_ADDRESS); |
151 | 148 |
|
152 | 149 | String hostName; |
153 | 150 | try { |
@@ -711,7 +708,7 @@ private InetAddress resolveHostsFileEntry(String hostname) { |
711 | 708 | return null; |
712 | 709 | } |
713 | 710 | InetAddress address = hostsFileEntriesResolver.address(hostname, resolvedAddressTypes); |
714 | | - return address == null && isLocalWindowsHost(hostname) ? LOCALHOST_ADDRESS : address; |
| 711 | + return address == null && isLocalHostAddress(hostname)? getLocalHostAddress() : address; |
715 | 712 | } |
716 | 713 |
|
717 | 714 | private List<InetAddress> resolveHostsFileEntries(String hostname) { |
@@ -724,23 +721,54 @@ private List<InetAddress> resolveHostsFileEntries(String hostname) { |
724 | 721 | .addresses(hostname, resolvedAddressTypes); |
725 | 722 | } else { |
726 | 723 | InetAddress address = hostsFileEntriesResolver.address(hostname, resolvedAddressTypes); |
727 | | - addresses = address != null ? Collections.singletonList(address) : null; |
| 724 | + addresses = address != null? Collections.singletonList(address) : null; |
728 | 725 | } |
729 | | - return addresses == null && isLocalWindowsHost(hostname) ? |
730 | | - Collections.singletonList(LOCALHOST_ADDRESS) : addresses; |
| 726 | + return addresses == null && isLocalHostAddress(hostname)? |
| 727 | + Collections.singletonList(getLocalHostAddress()) : addresses; |
731 | 728 | } |
732 | 729 |
|
733 | 730 | /** |
734 | | - * Checks whether the given hostname is the localhost/host (computer) name on Windows OS. |
735 | | - * Windows OS removed the localhost/host (computer) name information from the hosts file in the later versions |
736 | | - * and such hostname cannot be resolved from hosts file. |
737 | | - * See https://github.com/netty/netty/issues/5386 |
738 | | - * See https://github.com/netty/netty/issues/11142 |
| 731 | + * Checks whether the given hostname refers to the current computer. This is the case for: |
| 732 | + * <ul> |
| 733 | + * <li>localhost.</li> |
| 734 | + * <li>any domain within .localhost.</li> |
| 735 | + * <li>the hostname of the local computer on Windows</li> |
| 736 | + * </ul> |
| 737 | + * <p> |
| 738 | + * According to RFC 6761 Section 6.3, localhost and subdomains of localhost should be resolved to the loopback |
| 739 | + * address by name resolution libraries without querying DNS servers. The hostname of the local machine can usually |
| 740 | + * be resolved from the hosts file, but on Windows, this is no longer possible. |
| 741 | + * |
| 742 | + * @param hostname the hostname that's being looked up |
| 743 | + * @return true if the hostname should point to the loopback adress. False otherwise. |
| 744 | + * @see <a href="https://github.com/netty/netty/issues/5386">Issue 5386</a> |
| 745 | + * @see <a href="https://github.com/netty/netty/issues/11142">Issue 11142</a> |
| 746 | + * @see <a href="https://github.com/netty/netty/issues/16744">Issue 16744</a> |
| 747 | + * @see <a href="https://www.rfc-editor.org/rfc/rfc6761.html#section-6.3">RFC 6761</a> |
739 | 748 | */ |
740 | | - private static boolean isLocalWindowsHost(String hostname) { |
741 | | - return PlatformDependent.isWindows() && |
742 | | - (LOCALHOST.equalsIgnoreCase(hostname) || |
743 | | - (WINDOWS_HOST_NAME != null && WINDOWS_HOST_NAME.equalsIgnoreCase(hostname))); |
| 749 | + private static boolean isLocalHostAddress(String hostname) { |
| 750 | + if (PlatformDependent.isWindows() && WINDOWS_HOST_NAME != null && |
| 751 | + WINDOWS_HOST_NAME.equalsIgnoreCase(hostname)) { |
| 752 | + return true; |
| 753 | + } |
| 754 | + |
| 755 | + if (hostname.endsWith(".")) { |
| 756 | + hostname = hostname.substring(0, hostname.length() - 1); |
| 757 | + } |
| 758 | + return hostname.equalsIgnoreCase(LOCALHOST) || hostname.toLowerCase(Locale.US).endsWith(DOT_LOCALHOST); |
| 759 | + } |
| 760 | + |
| 761 | + private InetAddress getLocalHostAddress() { |
| 762 | + switch (resolvedAddressTypes) { |
| 763 | + case IPV4_ONLY: |
| 764 | + case IPV4_PREFERRED: |
| 765 | + return NetUtil.LOCALHOST4; |
| 766 | + case IPV6_ONLY: |
| 767 | + case IPV6_PREFERRED: |
| 768 | + return NetUtil.LOCALHOST6; |
| 769 | + default: |
| 770 | + throw new IllegalStateException("Unknown ResolvedAddressTypes " + resolvedAddressTypes); |
| 771 | + } |
744 | 772 | } |
745 | 773 |
|
746 | 774 | /** |
|