mirror of
https://gitee.com/chinabugotech/hutool.git
synced 2025-08-18 20:38:02 +08:00
fix code
This commit is contained in:
@@ -63,9 +63,9 @@ public class Ipv4Util implements Ipv4Pool {
|
||||
* @since 5.4.4
|
||||
*/
|
||||
public static String getLocalHostName() {
|
||||
if(null == localhostName){
|
||||
synchronized (Ipv4Util.class){
|
||||
if(null == localhostName){
|
||||
if (null == localhostName) {
|
||||
synchronized (Ipv4Util.class) {
|
||||
if (null == localhostName) {
|
||||
localhostName = NetUtil.getAddressName(getLocalhostDirectly());
|
||||
}
|
||||
}
|
||||
@@ -129,17 +129,19 @@ public class Ipv4Util implements Ipv4Pool {
|
||||
* @return 本机网卡IP地址,获取失败返回{@code null}
|
||||
*/
|
||||
public static InetAddress getLocalhostDirectly() {
|
||||
final LinkedHashSet<InetAddress> localAddressList = NetUtil.localAddressList(address -> {
|
||||
final LinkedHashSet<InetAddress> localAddressList = NetUtil.localAddressList(address ->
|
||||
// 需为IPV4地址
|
||||
address instanceof Inet4Address
|
||||
// 非loopback地址,指127.*.*.*的地址
|
||||
return !address.isLoopbackAddress()
|
||||
&& !address.isLoopbackAddress()
|
||||
// 非地区本地地址,指:
|
||||
// 10.0.0.0 ~ 10.255.255.255
|
||||
// 172.16.0.0 ~ 172.31.255.255
|
||||
// 192.168.0.0 ~ 192.168.255.255
|
||||
&& !address.isSiteLocalAddress()
|
||||
// 需为IPV4地址
|
||||
&& address instanceof Inet4Address;
|
||||
});
|
||||
// 非链路本地地址:169.254.0.0/16
|
||||
&& !address.isLinkLocalAddress()
|
||||
);
|
||||
|
||||
if (CollUtil.isNotEmpty(localAddressList)) {
|
||||
// 如果存在多网卡,返回首个地址
|
||||
@@ -148,7 +150,7 @@ public class Ipv4Util implements Ipv4Pool {
|
||||
|
||||
try {
|
||||
final InetAddress localHost = InetAddress.getLocalHost();
|
||||
if(localHost instanceof Inet4Address){
|
||||
if (localHost instanceof Inet4Address) {
|
||||
return localHost;
|
||||
}
|
||||
} catch (final UnknownHostException e) {
|
||||
@@ -560,7 +562,7 @@ public class Ipv4Util implements Ipv4Pool {
|
||||
case 3:
|
||||
return (int) (ip >> 8) & 0xFF;
|
||||
case 4:
|
||||
return (int)ip & 0xFF;
|
||||
return (int) ip & 0xFF;
|
||||
default:
|
||||
throw new IllegalArgumentException("Illegal position of ip Long: " + position);
|
||||
}
|
||||
|
||||
@@ -13,6 +13,7 @@
|
||||
package org.dromara.hutool.core.net;
|
||||
|
||||
import org.dromara.hutool.core.collection.CollUtil;
|
||||
import org.dromara.hutool.core.exceptions.UtilException;
|
||||
import org.dromara.hutool.core.lang.Singleton;
|
||||
|
||||
import java.math.BigInteger;
|
||||
@@ -85,9 +86,9 @@ public class Ipv6Util {
|
||||
* @since 5.4.4
|
||||
*/
|
||||
public static String getLocalHostName() {
|
||||
if(null == localhostName){
|
||||
synchronized (Ipv4Util.class){
|
||||
if(null == localhostName){
|
||||
if (null == localhostName) {
|
||||
synchronized (Ipv4Util.class) {
|
||||
if (null == localhostName) {
|
||||
localhostName = NetUtil.getAddressName(getLocalhostDirectly());
|
||||
}
|
||||
}
|
||||
@@ -148,14 +149,16 @@ public class Ipv6Util {
|
||||
* @return 本机网卡IP地址,获取失败返回{@code null}
|
||||
*/
|
||||
public static InetAddress getLocalhostDirectly() {
|
||||
final LinkedHashSet<InetAddress> localAddressList = NetUtil.localAddressList(address -> {
|
||||
// 非loopback地址
|
||||
return !address.isLoopbackAddress()
|
||||
// 非地区本地地址
|
||||
&& !address.isSiteLocalAddress()
|
||||
final LinkedHashSet<InetAddress> localAddressList = NetUtil.localAddressList(address ->
|
||||
// 需为IPV6地址
|
||||
&& address instanceof Inet6Address;
|
||||
});
|
||||
address instanceof Inet6Address
|
||||
// 非loopback地址,::1
|
||||
&& !address.isLoopbackAddress()
|
||||
// 非地区本地地址,fec0::/10
|
||||
&& !address.isSiteLocalAddress()
|
||||
// 非链路本地地址,fe80::/10
|
||||
&& !address.isLinkLocalAddress()
|
||||
);
|
||||
|
||||
if (CollUtil.isNotEmpty(localAddressList)) {
|
||||
// 如果存在多网卡,返回首个地址
|
||||
@@ -173,4 +176,32 @@ public class Ipv6Util {
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user