NettyUtils.java 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. package com.its.api.utils;
  2. import io.netty.channel.Channel;
  3. import java.net.*;
  4. import java.util.ArrayList;
  5. import java.util.Enumeration;
  6. public final class NettyUtils {
  7. public static String getTcpAddress(Channel ch) {
  8. String localIp = "local-unknown";
  9. String remoteIp = "remote-unknown";
  10. int localPort = 0;
  11. int remotePort = 0;
  12. InetSocketAddress localAddr = (InetSocketAddress)ch.localAddress();
  13. if (localAddr != null) {
  14. localIp = localAddr.getAddress().getHostAddress();
  15. localPort = localAddr.getPort();
  16. }
  17. InetSocketAddress remoteAddr = (InetSocketAddress)ch.remoteAddress();
  18. if (remoteAddr != null) {
  19. remoteIp = remoteAddr.getAddress().getHostAddress();
  20. remotePort = remoteAddr.getPort();
  21. }
  22. return "[Local #(" + localIp + ":" + localPort + ") Remote #(" + remoteIp + ":" + remotePort + ")]";
  23. }
  24. public static String getRemoteAddress(Channel ch) {
  25. String ip = getRemoteIpAddress(ch);
  26. int port = getRemotePort(ch);
  27. return "[Remote #(" + ip + ":" + port + ")]";
  28. }
  29. public static String getLocalAddress(Channel ch) {
  30. String ip = getLocalIpAddress(ch);
  31. int port = getLocalPort(ch);
  32. return "[Local #(" + ip + ":" + port + ")]";
  33. }
  34. public static String getRemoteIpAddress(Channel ch) {
  35. String ip = "unknown";
  36. InetSocketAddress inetAddr = (InetSocketAddress)ch.remoteAddress();
  37. if (inetAddr != null) {
  38. ip = inetAddr.getAddress().getHostAddress();
  39. }
  40. return ip;
  41. }
  42. public static int getRemotePort(Channel ch) {
  43. int port = 0;
  44. InetSocketAddress inetAddr = (InetSocketAddress)ch.remoteAddress();
  45. if (inetAddr != null)
  46. port = inetAddr.getPort();
  47. return port;
  48. }
  49. public static String getLocalIpAddress(Channel ch) {
  50. String ip = "unknown";
  51. InetSocketAddress inetAddr = (InetSocketAddress)ch.localAddress();
  52. if (inetAddr != null) {
  53. ip = inetAddr.getAddress().getHostAddress();
  54. }
  55. return ip;
  56. }
  57. public static int getLocalPort(Channel ch) {
  58. int port = 0;
  59. InetSocketAddress inetAddr = (InetSocketAddress)ch.localAddress();
  60. if (inetAddr != null)
  61. port = inetAddr.getPort();
  62. return port;
  63. }
  64. public static final String OS_NAME = System.getProperty("os.name");
  65. private static boolean isLinuxPlatform = false;
  66. private static boolean isWindowsPlatform = false;
  67. static {
  68. if (OS_NAME != null && OS_NAME.toLowerCase().contains("linux")) {
  69. isLinuxPlatform = true;
  70. }
  71. if (OS_NAME != null && OS_NAME.toLowerCase().contains("windows")) {
  72. isWindowsPlatform = true;
  73. }
  74. }
  75. public static String getLocalAddress() {
  76. try {
  77. // Traversal Network interface to get the first non-loopback and non-private address
  78. Enumeration<NetworkInterface> enumeration = NetworkInterface.getNetworkInterfaces();
  79. ArrayList<String> ipv4Result = new ArrayList<String>();
  80. ArrayList<String> ipv6Result = new ArrayList<String>();
  81. while (enumeration.hasMoreElements()) {
  82. final NetworkInterface networkInterface = enumeration.nextElement();
  83. final Enumeration<InetAddress> en = networkInterface.getInetAddresses();
  84. while (en.hasMoreElements()) {
  85. final InetAddress address = en.nextElement();
  86. if (!address.isLoopbackAddress()) {
  87. if (address instanceof Inet6Address) {
  88. ipv6Result.add(normalizeHostAddress(address));
  89. } else {
  90. ipv4Result.add(normalizeHostAddress(address));
  91. }
  92. }
  93. }
  94. }
  95. // prefer ipv4
  96. if (!ipv4Result.isEmpty()) {
  97. for (String ip : ipv4Result) {
  98. if (ip.startsWith("127.0") || ip.startsWith("192.168")) {
  99. continue;
  100. }
  101. return ip;
  102. }
  103. return ipv4Result.get(ipv4Result.size() - 1);
  104. } else if (!ipv6Result.isEmpty()) {
  105. return ipv6Result.get(0);
  106. }
  107. //If failed to find,fall back to localhost
  108. final InetAddress localHost = InetAddress.getLocalHost();
  109. return normalizeHostAddress(localHost);
  110. } catch (Exception e) {
  111. }
  112. return null;
  113. }
  114. public static String normalizeHostAddress(final InetAddress localHost) {
  115. if (localHost instanceof Inet6Address) {
  116. return "[" + localHost.getHostAddress() + "]";
  117. } else {
  118. return localHost.getHostAddress();
  119. }
  120. }
  121. public static SocketAddress string2SocketAddress(final String addr) {
  122. String[] s = addr.split(":");
  123. InetSocketAddress isa = new InetSocketAddress(s[0], Integer.parseInt(s[1]));
  124. return isa;
  125. }
  126. public static String socketAddress2String(final SocketAddress addr) {
  127. StringBuilder sb = new StringBuilder();
  128. InetSocketAddress inetSocketAddress = (InetSocketAddress) addr;
  129. sb.append(inetSocketAddress.getAddress().getHostAddress());
  130. sb.append(":");
  131. sb.append(inetSocketAddress.getPort());
  132. return sb.toString();
  133. }
  134. public static String parseChannelRemoteAddr(final Channel channel) {
  135. if (null == channel) {
  136. return "";
  137. }
  138. SocketAddress remote = channel.remoteAddress();
  139. final String addr = remote != null ? remote.toString() : "";
  140. if (addr.length() > 0) {
  141. int index = addr.lastIndexOf("/");
  142. if (index >= 0) {
  143. return addr.substring(index + 1);
  144. }
  145. return addr;
  146. }
  147. return "";
  148. }
  149. public static String parseSocketAddressAddr(SocketAddress socketAddress) {
  150. if (socketAddress != null) {
  151. final String addr = socketAddress.toString();
  152. if (addr.length() > 0) {
  153. return addr.startsWith("/") ? addr.substring(1) : addr;
  154. }
  155. }
  156. return "";
  157. }
  158. }