This commit is contained in:
Looly 2023-04-15 10:51:19 +08:00
parent 8a2dfafea9
commit 241aea60e4
2 changed files with 80 additions and 47 deletions

View File

@ -63,9 +63,9 @@ public class Ipv4Util implements Ipv4Pool {
* @since 5.4.4 * @since 5.4.4
*/ */
public static String getLocalHostName() { public static String getLocalHostName() {
if(null == localhostName){ if (null == localhostName) {
synchronized (Ipv4Util.class){ synchronized (Ipv4Util.class) {
if(null == localhostName){ if (null == localhostName) {
localhostName = NetUtil.getAddressName(getLocalhostDirectly()); localhostName = NetUtil.getAddressName(getLocalhostDirectly());
} }
} }
@ -129,17 +129,19 @@ public class Ipv4Util implements Ipv4Pool {
* @return 本机网卡IP地址获取失败返回{@code null} * @return 本机网卡IP地址获取失败返回{@code null}
*/ */
public static InetAddress getLocalhostDirectly() { public static InetAddress getLocalhostDirectly() {
final LinkedHashSet<InetAddress> localAddressList = NetUtil.localAddressList(address -> { final LinkedHashSet<InetAddress> localAddressList = NetUtil.localAddressList(address ->
// 需为IPV4地址
address instanceof Inet4Address
// 非loopback地址指127.*.*.*的地址 // 非loopback地址指127.*.*.*的地址
return !address.isLoopbackAddress() && !address.isLoopbackAddress()
// 非地区本地地址 // 非地区本地地址
// 10.0.0.0 ~ 10.255.255.255 // 10.0.0.0 ~ 10.255.255.255
// 172.16.0.0 ~ 172.31.255.255 // 172.16.0.0 ~ 172.31.255.255
// 192.168.0.0 ~ 192.168.255.255 // 192.168.0.0 ~ 192.168.255.255
&& !address.isSiteLocalAddress() && !address.isSiteLocalAddress()
// 需为IPV4地址 // 非链路本地地址169.254.0.0/16
&& address instanceof Inet4Address; && !address.isLinkLocalAddress()
}); );
if (CollUtil.isNotEmpty(localAddressList)) { if (CollUtil.isNotEmpty(localAddressList)) {
// 如果存在多网卡返回首个地址 // 如果存在多网卡返回首个地址
@ -148,7 +150,7 @@ public class Ipv4Util implements Ipv4Pool {
try { try {
final InetAddress localHost = InetAddress.getLocalHost(); final InetAddress localHost = InetAddress.getLocalHost();
if(localHost instanceof Inet4Address){ if (localHost instanceof Inet4Address) {
return localHost; return localHost;
} }
} catch (final UnknownHostException e) { } catch (final UnknownHostException e) {
@ -560,7 +562,7 @@ public class Ipv4Util implements Ipv4Pool {
case 3: case 3:
return (int) (ip >> 8) & 0xFF; return (int) (ip >> 8) & 0xFF;
case 4: case 4:
return (int)ip & 0xFF; return (int) ip & 0xFF;
default: default:
throw new IllegalArgumentException("Illegal position of ip Long: " + position); throw new IllegalArgumentException("Illegal position of ip Long: " + position);
} }

View File

@ -13,6 +13,7 @@
package org.dromara.hutool.core.net; package org.dromara.hutool.core.net;
import org.dromara.hutool.core.collection.CollUtil; import org.dromara.hutool.core.collection.CollUtil;
import org.dromara.hutool.core.exceptions.UtilException;
import org.dromara.hutool.core.lang.Singleton; import org.dromara.hutool.core.lang.Singleton;
import java.math.BigInteger; import java.math.BigInteger;
@ -85,9 +86,9 @@ public class Ipv6Util {
* @since 5.4.4 * @since 5.4.4
*/ */
public static String getLocalHostName() { public static String getLocalHostName() {
if(null == localhostName){ if (null == localhostName) {
synchronized (Ipv4Util.class){ synchronized (Ipv4Util.class) {
if(null == localhostName){ if (null == localhostName) {
localhostName = NetUtil.getAddressName(getLocalhostDirectly()); localhostName = NetUtil.getAddressName(getLocalhostDirectly());
} }
} }
@ -148,14 +149,16 @@ public class Ipv6Util {
* @return 本机网卡IP地址获取失败返回{@code null} * @return 本机网卡IP地址获取失败返回{@code null}
*/ */
public static InetAddress getLocalhostDirectly() { public static InetAddress getLocalhostDirectly() {
final LinkedHashSet<InetAddress> localAddressList = NetUtil.localAddressList(address -> { final LinkedHashSet<InetAddress> localAddressList = NetUtil.localAddressList(address ->
// 非loopback地址
return !address.isLoopbackAddress()
// 非地区本地地址
&& !address.isSiteLocalAddress()
// 需为IPV6地址 // 需为IPV6地址
&& address instanceof Inet6Address; address instanceof Inet6Address
}); // 非loopback地址::1
&& !address.isLoopbackAddress()
// 非地区本地地址fec0::/10
&& !address.isSiteLocalAddress()
// 非链路本地地址fe80::/10
&& !address.isLinkLocalAddress()
);
if (CollUtil.isNotEmpty(localAddressList)) { if (CollUtil.isNotEmpty(localAddressList)) {
// 如果存在多网卡返回首个地址 // 如果存在多网卡返回首个地址
@ -173,4 +176,32 @@ public class Ipv6Util {
return null; return null;
} }
/**
* 规范IPv6地址转换scope名称为scope id
* <pre>
* fe80:0:0:0:894:aeec:f37d:23e1%en0
* |
* fe80:0:0:0:894:aeec:f37d:23e1%5
* </pre>
* <p>
* 地址后的%5 叫做 scope id.
* <p>
* 方法来自于Dubbo
*
* @param address IPv6地址
* @return 规范之后的IPv6地址使用scope id
*/
public static InetAddress normalizeV6Address(final Inet6Address address) {
final String addr = address.getHostAddress();
final int index = addr.lastIndexOf('%');
if (index > 0) {
try {
return InetAddress.getByName(addr.substring(0, index) + '%' + address.getScopeId());
} catch (final UnknownHostException e) {
throw new UtilException(e);
}
}
return address;
}
} }