forked from plusone/plusone-commons
Compare commits
1 Commits
dev
...
feature/ne
Author | SHA1 | Date |
---|---|---|
ZhouXY108 | ddc08da33e |
|
@ -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");
|
||||
}
|
||||
}
|
|
@ -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();
|
||||
}
|
||||
}
|
|
@ -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);
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue