|
78 | 78 | import java.util.HashMap; |
79 | 79 | import java.util.Iterator; |
80 | 80 | import java.util.List; |
| 81 | +import java.util.Locale; |
81 | 82 | import java.util.Map; |
82 | 83 | import java.util.concurrent.TimeUnit; |
83 | 84 |
|
@@ -97,8 +98,8 @@ public class DnsNameResolver extends InetNameResolver { |
97 | 98 |
|
98 | 99 | private static final InternalLogger logger = InternalLoggerFactory.getInstance(DnsNameResolver.class); |
99 | 100 | private static final String LOCALHOST = "localhost"; |
| 101 | + private static final String DOT_LOCALHOST = '.' + LOCALHOST; |
100 | 102 | private static final String WINDOWS_HOST_NAME; |
101 | | - private static final InetAddress LOCALHOST_ADDRESS; |
102 | 103 | private static final DnsRecord[] EMPTY_ADDITIONALS = new DnsRecord[0]; |
103 | 104 | private static final DnsRecordType[] IPV4_ONLY_RESOLVED_RECORD_TYPES = |
104 | 105 | {DnsRecordType.A}; |
@@ -131,18 +132,14 @@ public boolean isSharable() { |
131 | 132 | static { |
132 | 133 | if (NetUtil.isIpV4StackPreferred() || !anyInterfaceSupportsIpV6()) { |
133 | 134 | DEFAULT_RESOLVE_ADDRESS_TYPES = ResolvedAddressTypes.IPV4_ONLY; |
134 | | - LOCALHOST_ADDRESS = NetUtil.LOCALHOST4; |
135 | 135 | } else { |
136 | 136 | if (NetUtil.isIpV6AddressesPreferred()) { |
137 | 137 | DEFAULT_RESOLVE_ADDRESS_TYPES = ResolvedAddressTypes.IPV6_PREFERRED; |
138 | | - LOCALHOST_ADDRESS = NetUtil.LOCALHOST6; |
139 | 138 | } else { |
140 | 139 | DEFAULT_RESOLVE_ADDRESS_TYPES = ResolvedAddressTypes.IPV4_PREFERRED; |
141 | | - LOCALHOST_ADDRESS = NetUtil.LOCALHOST4; |
142 | 140 | } |
143 | 141 | } |
144 | 142 | logger.debug("Default ResolvedAddressTypes: {}", DEFAULT_RESOLVE_ADDRESS_TYPES); |
145 | | - logger.debug("Localhost address: {}", LOCALHOST_ADDRESS); |
146 | 143 |
|
147 | 144 | String hostName; |
148 | 145 | try { |
@@ -688,7 +685,7 @@ private InetAddress resolveHostsFileEntry(String hostname) { |
688 | 685 | return null; |
689 | 686 | } |
690 | 687 | InetAddress address = hostsFileEntriesResolver.address(hostname, resolvedAddressTypes); |
691 | | - return address == null && isLocalWindowsHost(hostname) ? LOCALHOST_ADDRESS : address; |
| 688 | + return address == null && isLocalHostAddress(hostname)? getLocalHostAddress() : address; |
692 | 689 | } |
693 | 690 |
|
694 | 691 | private List<InetAddress> resolveHostsFileEntries(String hostname) { |
@@ -701,23 +698,54 @@ private List<InetAddress> resolveHostsFileEntries(String hostname) { |
701 | 698 | .addresses(hostname, resolvedAddressTypes); |
702 | 699 | } else { |
703 | 700 | InetAddress address = hostsFileEntriesResolver.address(hostname, resolvedAddressTypes); |
704 | | - addresses = address != null ? Collections.singletonList(address) : null; |
| 701 | + addresses = address != null? Collections.singletonList(address) : null; |
705 | 702 | } |
706 | | - return addresses == null && isLocalWindowsHost(hostname) ? |
707 | | - Collections.singletonList(LOCALHOST_ADDRESS) : addresses; |
| 703 | + return addresses == null && isLocalHostAddress(hostname)? |
| 704 | + Collections.singletonList(getLocalHostAddress()) : addresses; |
708 | 705 | } |
709 | 706 |
|
710 | 707 | /** |
711 | | - * Checks whether the given hostname is the localhost/host (computer) name on Windows OS. |
712 | | - * Windows OS removed the localhost/host (computer) name information from the hosts file in the later versions |
713 | | - * and such hostname cannot be resolved from hosts file. |
714 | | - * See https://github.com/netty/netty/issues/5386 |
715 | | - * See https://github.com/netty/netty/issues/11142 |
| 708 | + * Checks whether the given hostname refers to the current computer. This is the case for: |
| 709 | + * <ul> |
| 710 | + * <li>localhost.</li> |
| 711 | + * <li>any domain within .localhost.</li> |
| 712 | + * <li>the hostname of the local computer on Windows</li> |
| 713 | + * </ul> |
| 714 | + * <p> |
| 715 | + * According to RFC 6761 Section 6.3, localhost and subdomains of localhost should be resolved to the loopback |
| 716 | + * address by name resolution libraries without querying DNS servers. The hostname of the local machine can usually |
| 717 | + * be resolved from the hosts file, but on Windows, this is no longer possible. |
| 718 | + * |
| 719 | + * @param hostname the hostname that's being looked up |
| 720 | + * @return true if the hostname should point to the loopback adress. False otherwise. |
| 721 | + * @see <a href="https://github.com/netty/netty/issues/5386">Issue 5386</a> |
| 722 | + * @see <a href="https://github.com/netty/netty/issues/11142">Issue 11142</a> |
| 723 | + * @see <a href="https://github.com/netty/netty/issues/16744">Issue 16744</a> |
| 724 | + * @see <a href="https://www.rfc-editor.org/rfc/rfc6761.html#section-6.3">RFC 6761</a> |
716 | 725 | */ |
717 | | - private static boolean isLocalWindowsHost(String hostname) { |
718 | | - return PlatformDependent.isWindows() && |
719 | | - (LOCALHOST.equalsIgnoreCase(hostname) || |
720 | | - (WINDOWS_HOST_NAME != null && WINDOWS_HOST_NAME.equalsIgnoreCase(hostname))); |
| 726 | + private static boolean isLocalHostAddress(String hostname) { |
| 727 | + if (PlatformDependent.isWindows() && WINDOWS_HOST_NAME != null && |
| 728 | + WINDOWS_HOST_NAME.equalsIgnoreCase(hostname)) { |
| 729 | + return true; |
| 730 | + } |
| 731 | + |
| 732 | + if (hostname.endsWith(".")) { |
| 733 | + hostname = hostname.substring(0, hostname.length() - 1); |
| 734 | + } |
| 735 | + return hostname.equalsIgnoreCase(LOCALHOST) || hostname.toLowerCase(Locale.US).endsWith(DOT_LOCALHOST); |
| 736 | + } |
| 737 | + |
| 738 | + private InetAddress getLocalHostAddress() { |
| 739 | + switch (resolvedAddressTypes) { |
| 740 | + case IPV4_ONLY: |
| 741 | + case IPV4_PREFERRED: |
| 742 | + return NetUtil.LOCALHOST4; |
| 743 | + case IPV6_ONLY: |
| 744 | + case IPV6_PREFERRED: |
| 745 | + return NetUtil.LOCALHOST6; |
| 746 | + default: |
| 747 | + throw new IllegalStateException("Unknown ResolvedAddressTypes " + resolvedAddressTypes); |
| 748 | + } |
721 | 749 | } |
722 | 750 |
|
723 | 751 | /** |
|