新增 MoreInetAddresses 工具类

feature/net-util
ZhouXY108 2023-09-22 00:37:46 +08:00
parent 383478d2e3
commit ddc08da33e
3 changed files with 125 additions and 0 deletions

View File

@ -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<NetworkInterface> 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<InetAddress> 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<InetAddress> 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");
}
}

View File

@ -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();
}
}

View File

@ -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);
}
}