|
76 | 76 | import java.util.Map; |
77 | 77 | import java.util.List; |
78 | 78 | import java.util.ArrayList; |
| 79 | +import java.util.Comparator; |
| 80 | +import java.util.function.Function; |
79 | 81 |
|
80 | 82 | public class Utils { |
81 | 83 | private static final Log log = LogFactory.getLog(Utils.class); |
@@ -612,16 +614,9 @@ public static List<InetAddress> getLocalHostLANAddresses() throws SocketExceptio |
612 | 614 | if (!inetAddr.isLinkLocalAddress()) |
613 | 615 | { |
614 | 616 | if (inetAddr instanceof Inet6Address) { |
615 | | - Inet6Address inet6Addr = (Inet6Address) inetAddr; |
616 | | - if ((inet6Addr.getAddress()[0] ^ 0xfc) > 1) |
617 | | - { |
618 | | - // we ignore the site-local attribute for IPv6 because |
619 | | - // it has been deprecated, see https://www.ietf.org/rfc/rfc3879.txt |
620 | | - // instead we verify that this is not a unique local address, |
621 | | - // this check is unfortunately not in the standard library (yet) |
622 | | - // https://en.wikipedia.org/wiki/Unique_local_address |
623 | | - addresses.add(inetAddr); |
624 | | - } |
| 617 | + // we ignore the site-local attribute for IPv6 because |
| 618 | + // it has been deprecated, see https://www.ietf.org/rfc/rfc3879.txt |
| 619 | + addresses.add(inetAddr); |
625 | 620 | } else if (inetAddr instanceof Inet4Address && inetAddr.isSiteLocalAddress()) { |
626 | 621 | // check site-local |
627 | 622 | addresses.add(inetAddr); |
@@ -683,7 +678,11 @@ public static List<InetAddress> getLocalHostLANAddresses() throws SocketExceptio |
683 | 678 | * @return Returns String. |
684 | 679 | */ |
685 | 680 | public static String getIpAddress() throws SocketException { |
686 | | - return getLocalHostLANAddresses().stream().findFirst().map(InetAddress::getHostAddress).orElse("127.0.0.1"); |
| 681 | + //prefer ipv4 for backwards compatibility, we used to only consider ipv4 addresses |
| 682 | + Function<InetAddress,Integer> preferIpv4 = (i) -> i instanceof Inet4Address ? 1 : 0; |
| 683 | + return getLocalHostLANAddresses().stream() |
| 684 | + .min(Comparator.comparing(preferIpv4)) |
| 685 | + .map(InetAddress::getHostAddress).orElse("127.0.0.1"); |
687 | 686 | } |
688 | 687 |
|
689 | 688 | /** |
|
0 commit comments