From ddc08da33eb7d7e76a7ff4fd380490b039c30a0d Mon Sep 17 00:00:00 2001 From: ZhouXY108 Date: Fri, 22 Sep 2023 00:37:46 +0800 Subject: [PATCH] =?UTF-8?q?=E6=96=B0=E5=A2=9E=20MoreInetAddresses=20?= =?UTF-8?q?=E5=B7=A5=E5=85=B7=E7=B1=BB?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../commons/net/MoreInetAddresses.java | 58 +++++++++++++++++++ .../commons/net/NetworkInterfaceInfo.java | 39 +++++++++++++ .../commons/net/MoreInetAddressesTests.java | 28 +++++++++ 3 files changed, 125 insertions(+) create mode 100644 src/main/java/xyz/zhouxy/plusone/commons/net/MoreInetAddresses.java create mode 100644 src/main/java/xyz/zhouxy/plusone/commons/net/NetworkInterfaceInfo.java create mode 100644 src/test/java/xyz/zhouxy/plusone/commons/net/MoreInetAddressesTests.java diff --git a/src/main/java/xyz/zhouxy/plusone/commons/net/MoreInetAddresses.java b/src/main/java/xyz/zhouxy/plusone/commons/net/MoreInetAddresses.java new file mode 100644 index 0000000..19f6804 --- /dev/null +++ b/src/main/java/xyz/zhouxy/plusone/commons/net/MoreInetAddresses.java @@ -0,0 +1,58 @@ +package xyz.zhouxy.plusone.commons.net; + +import java.net.Inet4Address; +import java.net.Inet6Address; +import java.net.InetAddress; +import java.net.NetworkInterface; +import java.net.SocketException; +import java.util.Enumeration; + +import com.google.common.annotations.Beta; + +@Beta +public class MoreInetAddresses { + + public static NetworkInterfaceInfo getNetworkInterfaceInfo() throws SocketException { + final Enumeration all = NetworkInterface.getNetworkInterfaces(); + NetworkInterface networkInterface; + while (all.hasMoreElements()) { + networkInterface = all.nextElement(); + if (networkInterface.isLoopback() || networkInterface.isVirtual()) { + continue; + } + final byte[] mac = networkInterface.getHardwareAddress(); + if (mac != null) { + return new NetworkInterfaceInfo(networkInterface, mac); + } + } + throw new IllegalStateException("No available network interface found"); + } + + public static Inet4Address getIpv4(NetworkInterface networkInterface) { + final Enumeration ips = networkInterface.getInetAddresses(); + InetAddress ip; + while (ips.hasMoreElements()) { + ip = ips.nextElement(); + if (!ip.isLoopbackAddress() && (ip instanceof Inet4Address)) { + return (Inet4Address) ip; + } + } + throw new IllegalStateException("No available address found"); + } + + public static Inet6Address getIpv6(NetworkInterface networkInterface) { + final Enumeration ips = networkInterface.getInetAddresses(); + InetAddress ip; + while (ips.hasMoreElements()) { + ip = ips.nextElement(); + if (!ip.isLoopbackAddress() && (ip instanceof Inet6Address)) { + return (Inet6Address) ip; + } + } + throw new IllegalStateException("No available address found"); + } + + private MoreInetAddresses() { + throw new UnsupportedOperationException("Utility class"); + } +} diff --git a/src/main/java/xyz/zhouxy/plusone/commons/net/NetworkInterfaceInfo.java b/src/main/java/xyz/zhouxy/plusone/commons/net/NetworkInterfaceInfo.java new file mode 100644 index 0000000..f15b91c --- /dev/null +++ b/src/main/java/xyz/zhouxy/plusone/commons/net/NetworkInterfaceInfo.java @@ -0,0 +1,39 @@ +package xyz.zhouxy.plusone.commons.net; + +import java.net.NetworkInterface; +import java.util.Arrays; +import java.util.Objects; + +public class NetworkInterfaceInfo { + private final NetworkInterface networkInterface; + private final byte[] mac; + + NetworkInterfaceInfo(NetworkInterface networkInterface, byte[] mac) { + this.networkInterface = Objects.requireNonNull(networkInterface); + this.mac = Objects.requireNonNull(mac); + } + + public NetworkInterface getNetworkInterface() { + return networkInterface; + } + + public byte[] getMac() { + return Arrays.copyOf(this.mac, this.mac.length); + } + + public String getMacStr() { + final StringBuilder result = new StringBuilder(); + String s; + for (int i = 0; i < this.mac.length; i++) { + if (i != 0) { + result.append("-"); + } + s = Integer.toHexString(this.mac[i] & 0xFF); + if (s.length() == 1) { + result.append("0"); + } + result.append(s); + } + return result.toString(); + } +} diff --git a/src/test/java/xyz/zhouxy/plusone/commons/net/MoreInetAddressesTests.java b/src/test/java/xyz/zhouxy/plusone/commons/net/MoreInetAddressesTests.java new file mode 100644 index 0000000..c050417 --- /dev/null +++ b/src/test/java/xyz/zhouxy/plusone/commons/net/MoreInetAddressesTests.java @@ -0,0 +1,28 @@ +package xyz.zhouxy.plusone.commons.net; + +import static org.junit.jupiter.api.Assertions.*; + +import java.net.NetworkInterface; +import java.net.SocketException; + +import org.junit.jupiter.api.Test; + +import cn.hutool.core.net.NetUtil; +import lombok.extern.slf4j.Slf4j; + +@Slf4j +class MoreInetAddressesTests { + + @Test + void testGetNetworkInterfaceInfo() throws SocketException { + NetworkInterfaceInfo networkInterfaceInfo = MoreInetAddresses.getNetworkInterfaceInfo(); + String macStr = networkInterfaceInfo.getMacStr(); + log.info("mac: {}", macStr); + assertEquals(NetUtil.getLocalMacAddress(), macStr); + NetworkInterface networkInterface = networkInterfaceInfo.getNetworkInterface(); + String ipv4 = MoreInetAddresses.getIpv4(networkInterface).getHostAddress(); + log.info("ipv4: {}", ipv4); + String ipv6 = MoreInetAddresses.getIpv6(networkInterface).getHostAddress(); + log.info("ipv6: {}", ipv6); + } +}