IpUtils.java 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. package com.sooka.common.utils;
  2. import java.net.InetAddress;
  3. import java.net.UnknownHostException;
  4. import javax.servlet.http.HttpServletRequest;
  5. /**
  6. * 获取IP方法
  7. *
  8. * @author lei_wang
  9. */
  10. public class IpUtils
  11. {
  12. public static String getIpAddr(HttpServletRequest request)
  13. {
  14. if (request == null)
  15. {
  16. return "unknown";
  17. }
  18. String ip = request.getHeader("x-forwarded-for");
  19. if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip))
  20. {
  21. ip = request.getHeader("Proxy-Client-IP");
  22. }
  23. if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip))
  24. {
  25. ip = request.getHeader("X-Forwarded-For");
  26. }
  27. if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip))
  28. {
  29. ip = request.getHeader("WL-Proxy-Client-IP");
  30. }
  31. if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip))
  32. {
  33. ip = request.getHeader("X-Real-IP");
  34. }
  35. if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip))
  36. {
  37. ip = request.getRemoteAddr();
  38. }
  39. return "0:0:0:0:0:0:0:1".equals(ip) ? "127.0.0.1" : ip;
  40. }
  41. public static boolean internalIp(String ip)
  42. {
  43. byte[] addr = textToNumericFormatV4(ip);
  44. return internalIp(addr) || "127.0.0.1".equals(ip);
  45. }
  46. private static boolean internalIp(byte[] addr)
  47. {
  48. if (StringUtils.isNull(addr) || addr.length < 2)
  49. {
  50. return true;
  51. }
  52. final byte b0 = addr[0];
  53. final byte b1 = addr[1];
  54. // 10.x.x.x/8
  55. final byte SECTION_1 = 0x0A;
  56. // 172.16.x.x/12
  57. final byte SECTION_2 = (byte) 0xAC;
  58. final byte SECTION_3 = (byte) 0x10;
  59. final byte SECTION_4 = (byte) 0x1F;
  60. // 192.168.x.x/16
  61. final byte SECTION_5 = (byte) 0xC0;
  62. final byte SECTION_6 = (byte) 0xA8;
  63. switch (b0)
  64. {
  65. case SECTION_1:
  66. return true;
  67. case SECTION_2:
  68. if (b1 >= SECTION_3 && b1 <= SECTION_4)
  69. {
  70. return true;
  71. }
  72. case SECTION_5:
  73. switch (b1)
  74. {
  75. case SECTION_6:
  76. return true;
  77. }
  78. default:
  79. return false;
  80. }
  81. }
  82. /**
  83. * 将IPv4地址转换成字节
  84. *
  85. * @param text IPv4地址
  86. * @return byte 字节
  87. */
  88. public static byte[] textToNumericFormatV4(String text)
  89. {
  90. if (text.length() == 0)
  91. {
  92. return null;
  93. }
  94. byte[] bytes = new byte[4];
  95. String[] elements = text.split("\\.", -1);
  96. try
  97. {
  98. long l;
  99. int i;
  100. switch (elements.length)
  101. {
  102. case 1:
  103. l = Long.parseLong(elements[0]);
  104. if ((l < 0L) || (l > 4294967295L)) {
  105. return null;
  106. }
  107. bytes[0] = (byte) (int) (l >> 24 & 0xFF);
  108. bytes[1] = (byte) (int) ((l & 0xFFFFFF) >> 16 & 0xFF);
  109. bytes[2] = (byte) (int) ((l & 0xFFFF) >> 8 & 0xFF);
  110. bytes[3] = (byte) (int) (l & 0xFF);
  111. break;
  112. case 2:
  113. l = Integer.parseInt(elements[0]);
  114. if ((l < 0L) || (l > 255L)) {
  115. return null;
  116. }
  117. bytes[0] = (byte) (int) (l & 0xFF);
  118. l = Integer.parseInt(elements[1]);
  119. if ((l < 0L) || (l > 16777215L)) {
  120. return null;
  121. }
  122. bytes[1] = (byte) (int) (l >> 16 & 0xFF);
  123. bytes[2] = (byte) (int) ((l & 0xFFFF) >> 8 & 0xFF);
  124. bytes[3] = (byte) (int) (l & 0xFF);
  125. break;
  126. case 3:
  127. for (i = 0; i < 2; ++i)
  128. {
  129. l = Integer.parseInt(elements[i]);
  130. if ((l < 0L) || (l > 255L)) {
  131. return null;
  132. }
  133. bytes[i] = (byte) (int) (l & 0xFF);
  134. }
  135. l = Integer.parseInt(elements[2]);
  136. if ((l < 0L) || (l > 65535L)) {
  137. return null;
  138. }
  139. bytes[2] = (byte) (int) (l >> 8 & 0xFF);
  140. bytes[3] = (byte) (int) (l & 0xFF);
  141. break;
  142. case 4:
  143. for (i = 0; i < 4; ++i)
  144. {
  145. l = Integer.parseInt(elements[i]);
  146. if ((l < 0L) || (l > 255L)) {
  147. return null;
  148. }
  149. bytes[i] = (byte) (int) (l & 0xFF);
  150. }
  151. break;
  152. default:
  153. return null;
  154. }
  155. }
  156. catch (NumberFormatException e)
  157. {
  158. return null;
  159. }
  160. return bytes;
  161. }
  162. public static String getHostIp()
  163. {
  164. try
  165. {
  166. return InetAddress.getLocalHost().getHostAddress();
  167. }
  168. catch (UnknownHostException e)
  169. {
  170. }
  171. return "127.0.0.1";
  172. }
  173. public static String getHostName()
  174. {
  175. try
  176. {
  177. return InetAddress.getLocalHost().getHostName();
  178. }
  179. catch (UnknownHostException e)
  180. {
  181. }
  182. return "未知";
  183. }
  184. }