package com.its.common.cluster.utils; import io.netty.channel.Channel; import io.netty.channel.EventLoopGroup; import io.netty.channel.epoll.Epoll; import io.netty.channel.epoll.EpollEventLoopGroup; import io.netty.channel.epoll.EpollServerSocketChannel; import io.netty.channel.epoll.EpollSocketChannel; import io.netty.channel.nio.NioEventLoopGroup; import io.netty.channel.socket.ServerSocketChannel; import io.netty.channel.socket.SocketChannel; import io.netty.channel.socket.nio.NioServerSocketChannel; import io.netty.channel.socket.nio.NioSocketChannel; import io.netty.util.concurrent.DefaultThreadFactory; import java.net.*; import java.util.ArrayList; import java.util.Enumeration; public class ClusterUtils { public static final String OS_NAME = System.getProperty("os.name"); private static boolean isLinuxPlatform = false; private static boolean isWindowsPlatform = false; private ClusterUtils() { } public static String getAddress(Channel ch) { String localIp = "local-unknown"; String remoteIp = "remote-unknown"; int localPort = 0; int remotePort = 0; InetSocketAddress localAddr = (InetSocketAddress)ch.localAddress(); if (localAddr != null) { localIp = localAddr.getAddress().getHostAddress(); localPort = localAddr.getPort(); } InetSocketAddress remoteAddr = (InetSocketAddress)ch.remoteAddress(); if (remoteAddr != null) { remoteIp = remoteAddr.getAddress().getHostAddress(); remotePort = remoteAddr.getPort(); } return "[Local #(" + localIp + ":" + localPort + ") Remote #(" + remoteIp + ":" + remotePort + ")]"; } public static String getRemoteAddress(Channel ch) { String ip = getRemoteIpAddress(ch); int port = getRemotePort(ch); return "[Remote #(" + ip + ":" + port + ")]"; } public static String getLocalAddress(Channel ch) { String ip = getLocalIpAddress(ch); int port = getLocalPort(ch); return "[Local #(" + ip + ":" + port + ")]"; } public static String getRemoteIpAddress(Channel ch) { String ip = "255.255.255.255"; InetSocketAddress inetAddr = (InetSocketAddress)ch.remoteAddress(); if (inetAddr != null) { ip = inetAddr.getAddress().getHostAddress(); } return ip; } public static long getRemoteIpAddressToLong(Channel ch) { String[] ipAddressInArray = getRemoteIpAddress(ch).split("\\."); long result = 0L; for(int i = 0; i < ipAddressInArray.length; ++i) { int power = 3 - i; int ip = Integer.parseInt(ipAddressInArray[i]); result += (long)((double)ip * Math.pow((double)256.0F, (double)power)); } return result; } public static int getRemotePort(Channel ch) { int port = 0; InetSocketAddress inetAddr = (InetSocketAddress)ch.remoteAddress(); if (inetAddr != null) { port = inetAddr.getPort(); } return port; } public static String getLocalIpAddress(Channel ch) { String ip = "127.0.0.1"; InetSocketAddress inetAddr = (InetSocketAddress)ch.localAddress(); if (inetAddr != null) { ip = inetAddr.getAddress().getHostAddress(); } return ip; } public static int getLocalPort(Channel ch) { int port = 0; InetSocketAddress inetAddr = (InetSocketAddress)ch.localAddress(); if (inetAddr != null) { port = inetAddr.getPort(); } return port; } public static boolean isEpollAvailable() { return Epoll.isAvailable(); } public static EventLoopGroup newEventLoopGroup(int nThreads, String threadPoolName) { if (isEpollAvailable()) { return threadPoolName.isEmpty() ? new EpollEventLoopGroup(nThreads) : new EpollEventLoopGroup(nThreads, new DefaultThreadFactory("epo" + threadPoolName)); } else { return threadPoolName.isEmpty() ? new NioEventLoopGroup(nThreads) : new NioEventLoopGroup(nThreads, new DefaultThreadFactory("nio" + threadPoolName)); } } public static Class getSocketChannel() { return isEpollAvailable() ? EpollSocketChannel.class : NioSocketChannel.class; } public static Class getServerSocketChannel() { return isEpollAvailable() ? EpollServerSocketChannel.class : NioServerSocketChannel.class; } public static String getTcpAddress(Channel ch) { String localIp = "local-unknown"; String remoteIp = "remote-unknown"; int localPort = 0; int remotePort = 0; InetSocketAddress localAddr = (InetSocketAddress)ch.localAddress(); if (localAddr != null) { localIp = localAddr.getAddress().getHostAddress(); localPort = localAddr.getPort(); } InetSocketAddress remoteAddr = (InetSocketAddress)ch.remoteAddress(); if (remoteAddr != null) { remoteIp = remoteAddr.getAddress().getHostAddress(); remotePort = remoteAddr.getPort(); } return "[Local #(" + localIp + ":" + localPort + ") Remote #(" + remoteIp + ":" + remotePort + ")]"; } public static String getLocalAddress() { Enumeration enumeration = null; try { enumeration = NetworkInterface.getNetworkInterfaces(); } catch (SocketException var7) { return null; } ArrayList ipv4Result = new ArrayList<>(); ArrayList ipv6Result = new ArrayList<>(); while(enumeration.hasMoreElements()) { NetworkInterface networkInterface = (NetworkInterface)enumeration.nextElement(); Enumeration en = networkInterface.getInetAddresses(); while(en.hasMoreElements()) { InetAddress address = (InetAddress)en.nextElement(); if (!address.isLoopbackAddress()) { if (address instanceof Inet6Address) { ipv6Result.add(normalizeHostAddress(address)); } else { ipv4Result.add(normalizeHostAddress(address)); } } } } if (!ipv4Result.isEmpty()) { for(String ip : ipv4Result) { if (!ip.startsWith("127.0") && !ip.startsWith("192.168")) { return ip; } } return (String)ipv4Result.get(ipv4Result.size() - 1); } else if (!ipv6Result.isEmpty()) { return (String)ipv6Result.get(0); } else { InetAddress localHost; try { localHost = InetAddress.getLocalHost(); } catch (UnknownHostException var6) { return null; } return normalizeHostAddress(localHost); } } public static String normalizeHostAddress(InetAddress localHost) { return localHost instanceof Inet6Address ? "[" + localHost.getHostAddress() + "]" : localHost.getHostAddress(); } public static SocketAddress string2SocketAddress(String addr) { String[] s = addr.split(":"); InetSocketAddress isa = new InetSocketAddress(s[0], Integer.parseInt(s[1])); return isa; } public static String socketAddress2String(SocketAddress addr) { StringBuilder sb = new StringBuilder(); InetSocketAddress inetSocketAddress = (InetSocketAddress)addr; sb.append(inetSocketAddress.getAddress().getHostAddress()); sb.append(":"); sb.append(inetSocketAddress.getPort()); return sb.toString(); } public static String parseChannelRemoteAddr(Channel channel) { if (null == channel) { return ""; } else { SocketAddress remote = channel.remoteAddress(); String addr = remote != null ? remote.toString() : ""; if (addr.length() > 0) { int index = addr.lastIndexOf("/"); return index >= 0 ? addr.substring(index + 1) : addr; } else { return ""; } } } public static String parseSocketAddressAddr(SocketAddress socketAddress) { if (socketAddress != null) { String addr = socketAddress.toString(); if (addr.length() > 0) { return addr.startsWith("/") ? addr.substring(1) : addr; } } return ""; } static { if (OS_NAME != null && OS_NAME.toLowerCase().contains("linux")) { isLinuxPlatform = true; } if (OS_NAME != null && OS_NAME.toLowerCase().contains("windows")) { isWindowsPlatform = true; } } }