123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185 |
- package com.its.api.utils;
- import io.netty.channel.Channel;
- import java.net.*;
- import java.util.ArrayList;
- import java.util.Enumeration;
- public final class NettyUtils {
- 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 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 = "unknown";
- InetSocketAddress inetAddr = (InetSocketAddress)ch.remoteAddress();
- if (inetAddr != null) {
- ip = inetAddr.getAddress().getHostAddress();
- }
- return ip;
- }
- 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 = "unknown";
- 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 final String OS_NAME = System.getProperty("os.name");
- private static boolean isLinuxPlatform = false;
- private static boolean isWindowsPlatform = false;
- static {
- if (OS_NAME != null && OS_NAME.toLowerCase().contains("linux")) {
- isLinuxPlatform = true;
- }
- if (OS_NAME != null && OS_NAME.toLowerCase().contains("windows")) {
- isWindowsPlatform = true;
- }
- }
- public static String getLocalAddress() {
- try {
- // Traversal Network interface to get the first non-loopback and non-private address
- Enumeration<NetworkInterface> enumeration = NetworkInterface.getNetworkInterfaces();
- ArrayList<String> ipv4Result = new ArrayList<String>();
- ArrayList<String> ipv6Result = new ArrayList<String>();
- while (enumeration.hasMoreElements()) {
- final NetworkInterface networkInterface = enumeration.nextElement();
- final Enumeration<InetAddress> en = networkInterface.getInetAddresses();
- while (en.hasMoreElements()) {
- final InetAddress address = en.nextElement();
- if (!address.isLoopbackAddress()) {
- if (address instanceof Inet6Address) {
- ipv6Result.add(normalizeHostAddress(address));
- } else {
- ipv4Result.add(normalizeHostAddress(address));
- }
- }
- }
- }
- // prefer ipv4
- if (!ipv4Result.isEmpty()) {
- for (String ip : ipv4Result) {
- if (ip.startsWith("127.0") || ip.startsWith("192.168")) {
- continue;
- }
- return ip;
- }
- return ipv4Result.get(ipv4Result.size() - 1);
- } else if (!ipv6Result.isEmpty()) {
- return ipv6Result.get(0);
- }
- //If failed to find,fall back to localhost
- final InetAddress localHost = InetAddress.getLocalHost();
- return normalizeHostAddress(localHost);
- } catch (Exception e) {
- }
- return null;
- }
- public static String normalizeHostAddress(final InetAddress localHost) {
- if (localHost instanceof Inet6Address) {
- return "[" + localHost.getHostAddress() + "]";
- } else {
- return localHost.getHostAddress();
- }
- }
- public static SocketAddress string2SocketAddress(final String addr) {
- String[] s = addr.split(":");
- InetSocketAddress isa = new InetSocketAddress(s[0], Integer.parseInt(s[1]));
- return isa;
- }
- public static String socketAddress2String(final 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(final Channel channel) {
- if (null == channel) {
- return "";
- }
- SocketAddress remote = channel.remoteAddress();
- final String addr = remote != null ? remote.toString() : "";
- if (addr.length() > 0) {
- int index = addr.lastIndexOf("/");
- if (index >= 0) {
- return addr.substring(index + 1);
- }
- return addr;
- }
- return "";
- }
- public static String parseSocketAddressAddr(SocketAddress socketAddress) {
- if (socketAddress != null) {
- final String addr = socketAddress.toString();
- if (addr.length() > 0) {
- return addr.startsWith("/") ? addr.substring(1) : addr;
- }
- }
- return "";
- }
- }
|