init commit

This commit is contained in:
thinkgem
2018-01-01 22:20:21 +08:00
parent 698e162225
commit f273b9e146

View File

@@ -1,297 +1,293 @@
package com.jeesite.common.network; package com.jeesite.common.network;
import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletRequest;
import com.jeesite.common.lang.ObjectUtils; import com.jeesite.common.lang.ObjectUtils;
import com.jeesite.common.lang.StringUtils; import com.jeesite.common.lang.StringUtils;
public class IpUtils { public class IpUtils {
/** /**
* 获取客户端IP地址 * 获取客户端IP地址
* @param request * @param request
* @return * @return
*/ */
public static String getRemoteAddr(HttpServletRequest request) { public static String getRemoteAddr(HttpServletRequest request) {
if (request == null) { if (request == null) {
return "unknown"; return "unknown";
} }
String ip = request.getHeader("x-forwarded-for"); String ip = request.getHeader("X-Forwarded-For");
if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) { if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
ip = request.getHeader("Proxy-Client-IP"); ip = request.getHeader("Proxy-Client-IP");
} }
if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) { if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
ip = request.getHeader("X-Forwarded-For"); ip = request.getHeader("WL-Proxy-Client-IP");
} }
if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) { if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
ip = request.getHeader("WL-Proxy-Client-IP"); ip = request.getHeader("X-Real-IP");
} }
if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) { if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
ip = request.getHeader("X-Real-IP"); ip = request.getRemoteAddr();
} }
return StringUtils.split(ObjectUtils.toString(ip), ",")[0];
if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) { }
ip = request.getRemoteAddr();
} /**
return StringUtils.split(ObjectUtils.toString(ip), ",")[0]; * 是否是本地地址
} * @param ip
* @return
/** */
* 是否是本地地址 public static boolean isLocalAddr(String ip){
* @param ip return StringUtils.inString(ip, "127.0.0.1", "0:0:0:0:0:0:0:1");
* @return }
*/
public static boolean isLocalAddr(String ip){ /**
return StringUtils.inString(ip, "127.0.0.1", "0:0:0:0:0:0:0:1"); * 判断IP地址为内网IP还是公网IP
} *
* tcp/ip协议中专门保留了三个IP地址区域作为私有地址其地址范围如下
/** * 10.0.0.0/810.0.0.010.255.255.255
* 判断IP地址为内网IP还是公网IP * 172.16.0.0/12172.16.0.0172.31.255.255
* * 192.168.0.0/16192.168.0.0192.168.255.255
* tcp/ip协议中专门保留了三个IP地址区域作为私有地址其地址范围如下 *
* 10.0.0.0/810.0.0.010.255.255.255 * @param ip
* 172.16.0.0/12172.16.0.0172.31.255.255 * @return
* 192.168.0.0/16192.168.0.0192.168.255.255 */
* public static boolean isInternalAddr(String ip) {
* @param ip
* @return if (isLocalAddr(ip)){
*/ return true;
public static boolean isInternalAddr(String ip) { }
if (isLocalAddr(ip)){ byte[] addr = textToNumericFormatV4(ip);
return true;
} final byte b0 = addr[0];
final byte b1 = addr[1];
byte[] addr = textToNumericFormatV4(ip); //10.x.x.x/8
final byte SECTION_1 = 0x0A;
final byte b0 = addr[0]; //172.16.x.x/12
final byte b1 = addr[1]; final byte SECTION_2 = (byte) 0xAC;
//10.x.x.x/8 final byte SECTION_3 = (byte) 0x10;
final byte SECTION_1 = 0x0A; final byte SECTION_4 = (byte) 0x1F;
//172.16.x.x/12 //192.168.x.x/16
final byte SECTION_2 = (byte) 0xAC; final byte SECTION_5 = (byte) 0xC0;
final byte SECTION_3 = (byte) 0x10; final byte SECTION_6 = (byte) 0xA8;
final byte SECTION_4 = (byte) 0x1F; switch (b0) {
//192.168.x.x/16 case SECTION_1:
final byte SECTION_5 = (byte) 0xC0; return true;
final byte SECTION_6 = (byte) 0xA8; case SECTION_2:
switch (b0) { if (b1 >= SECTION_3 && b1 <= SECTION_4) {
case SECTION_1: return true;
return true; }
case SECTION_2: case SECTION_5:
if (b1 >= SECTION_3 && b1 <= SECTION_4) { switch (b1) {
return true; case SECTION_6:
} return true;
case SECTION_5: }
switch (b1) { default:
case SECTION_6: return false;
return true; }
} }
default:
return false; public static byte[] textToNumericFormatV4(String paramString) {
} if (paramString.length() == 0) {
} return null;
}
public static byte[] textToNumericFormatV4(String paramString) { byte[] arrayOfByte = new byte[4];
if (paramString.length() == 0) { String[] arrayOfString = paramString.split("\\.", -1);
return null; try {
} long l;
byte[] arrayOfByte = new byte[4]; int i;
String[] arrayOfString = paramString.split("\\.", -1); switch (arrayOfString.length) {
try { case 1:
long l; l = Long.parseLong(arrayOfString[0]);
int i; if ((l < 0L) || (l > 4294967295L)) {
switch (arrayOfString.length) { return null;
case 1: }
l = Long.parseLong(arrayOfString[0]); arrayOfByte[0] = ((byte) (int) (l >> 24 & 0xFF));
if ((l < 0L) || (l > 4294967295L)) { arrayOfByte[1] = ((byte) (int) ((l & 0xFFFFFF) >> 16 & 0xFF));
return null; arrayOfByte[2] = ((byte) (int) ((l & 0xFFFF) >> 8 & 0xFF));
} arrayOfByte[3] = ((byte) (int) (l & 0xFF));
arrayOfByte[0] = ((byte) (int) (l >> 24 & 0xFF)); break;
arrayOfByte[1] = ((byte) (int) ((l & 0xFFFFFF) >> 16 & 0xFF)); case 2:
arrayOfByte[2] = ((byte) (int) ((l & 0xFFFF) >> 8 & 0xFF)); l = Integer.parseInt(arrayOfString[0]);
arrayOfByte[3] = ((byte) (int) (l & 0xFF)); if ((l < 0L) || (l > 255L)) {
break; return null;
case 2: }
l = Integer.parseInt(arrayOfString[0]); arrayOfByte[0] = ((byte) (int) (l & 0xFF));
if ((l < 0L) || (l > 255L)) { l = Integer.parseInt(arrayOfString[1]);
return null; if ((l < 0L) || (l > 16777215L)) {
} return null;
arrayOfByte[0] = ((byte) (int) (l & 0xFF)); }
l = Integer.parseInt(arrayOfString[1]); arrayOfByte[1] = ((byte) (int) (l >> 16 & 0xFF));
if ((l < 0L) || (l > 16777215L)) { arrayOfByte[2] = ((byte) (int) ((l & 0xFFFF) >> 8 & 0xFF));
return null; arrayOfByte[3] = ((byte) (int) (l & 0xFF));
} break;
arrayOfByte[1] = ((byte) (int) (l >> 16 & 0xFF)); case 3:
arrayOfByte[2] = ((byte) (int) ((l & 0xFFFF) >> 8 & 0xFF)); for (i = 0; i < 2; i++) {
arrayOfByte[3] = ((byte) (int) (l & 0xFF)); l = Integer.parseInt(arrayOfString[i]);
break; if ((l < 0L) || (l > 255L)) {
case 3: return null;
for (i = 0; i < 2; i++) { }
l = Integer.parseInt(arrayOfString[i]); arrayOfByte[i] = ((byte) (int) (l & 0xFF));
if ((l < 0L) || (l > 255L)) { }
return null; l = Integer.parseInt(arrayOfString[2]);
} if ((l < 0L) || (l > 65535L)) {
arrayOfByte[i] = ((byte) (int) (l & 0xFF)); return null;
} }
l = Integer.parseInt(arrayOfString[2]); arrayOfByte[2] = ((byte) (int) (l >> 8 & 0xFF));
if ((l < 0L) || (l > 65535L)) { arrayOfByte[3] = ((byte) (int) (l & 0xFF));
return null; break;
} case 4:
arrayOfByte[2] = ((byte) (int) (l >> 8 & 0xFF)); for (i = 0; i < 4; i++) {
arrayOfByte[3] = ((byte) (int) (l & 0xFF)); l = Integer.parseInt(arrayOfString[i]);
break; if ((l < 0L) || (l > 255L)) {
case 4: return null;
for (i = 0; i < 4; i++) { }
l = Integer.parseInt(arrayOfString[i]); arrayOfByte[i] = ((byte) (int) (l & 0xFF));
if ((l < 0L) || (l > 255L)) { }
return null; break;
} default:
arrayOfByte[i] = ((byte) (int) (l & 0xFF)); return null;
} }
break; } catch (NumberFormatException localNumberFormatException) {
default: return null;
return null; }
} return arrayOfByte;
} catch (NumberFormatException localNumberFormatException) { }
return null;
} public static byte[] textToNumericFormatV6(String paramString) {
return arrayOfByte; if (paramString.length() < 2) {
} return null;
}
public static byte[] textToNumericFormatV6(String paramString) { char[] arrayOfChar = paramString.toCharArray();
if (paramString.length() < 2) { byte[] arrayOfByte1 = new byte[16];
return null;
} int m = arrayOfChar.length;
char[] arrayOfChar = paramString.toCharArray(); int n = paramString.indexOf("%");
byte[] arrayOfByte1 = new byte[16]; if (n == m - 1) {
return null;
int m = arrayOfChar.length; }
int n = paramString.indexOf("%"); if (n != -1) {
if (n == m - 1) { m = n;
return null; }
} int i = -1;
if (n != -1) { int i1 = 0;
m = n; int i2 = 0;
} if ((arrayOfChar[i1] == ':') && (arrayOfChar[(++i1)] != ':')) {
int i = -1; return null;
int i1 = 0; }
int i2 = 0; int i3 = i1;
if ((arrayOfChar[i1] == ':') && (arrayOfChar[(++i1)] != ':')) { int j = 0;
return null; int k = 0;
} int i4;
int i3 = i1; while (i1 < m) {
int j = 0; char c = arrayOfChar[(i1++)];
int k = 0; i4 = Character.digit(c, 16);
int i4; if (i4 != -1) {
while (i1 < m) { k <<= 4;
char c = arrayOfChar[(i1++)]; k |= i4;
i4 = Character.digit(c, 16); if (k > 65535) {
if (i4 != -1) { return null;
k <<= 4; }
k |= i4; j = 1;
if (k > 65535) { } else if (c == ':') {
return null; i3 = i1;
} if (j == 0) {
j = 1; if (i != -1) {
} else if (c == ':') { return null;
i3 = i1; }
if (j == 0) { i = i2;
if (i != -1) { } else {
return null; if (i1 == m) {
} return null;
i = i2; }
} else { if (i2 + 2 > 16) {
if (i1 == m) { return null;
return null; }
} arrayOfByte1[(i2++)] = ((byte) (k >> 8 & 0xFF));
if (i2 + 2 > 16) { arrayOfByte1[(i2++)] = ((byte) (k & 0xFF));
return null; j = 0;
} k = 0;
arrayOfByte1[(i2++)] = ((byte) (k >> 8 & 0xFF)); }
arrayOfByte1[(i2++)] = ((byte) (k & 0xFF)); } else if ((c == '.') && (i2 + 4 <= 16)) {
j = 0; String str = paramString.substring(i3, m);
k = 0;
} int i5 = 0;
} else if ((c == '.') && (i2 + 4 <= 16)) { int i6 = 0;
String str = paramString.substring(i3, m); while ((i6 = str.indexOf('.', i6)) != -1) {
i5++;
int i5 = 0; i6++;
int i6 = 0; }
while ((i6 = str.indexOf('.', i6)) != -1) { if (i5 != 3) {
i5++; return null;
i6++; }
} byte[] arrayOfByte3 = textToNumericFormatV4(str);
if (i5 != 3) { if (arrayOfByte3 == null) {
return null; return null;
} }
byte[] arrayOfByte3 = textToNumericFormatV4(str); for (int i7 = 0; i7 < 4; i7++) {
if (arrayOfByte3 == null) { arrayOfByte1[(i2++)] = arrayOfByte3[i7];
return null; }
} j = 0;
for (int i7 = 0; i7 < 4; i7++) { } else {
arrayOfByte1[(i2++)] = arrayOfByte3[i7]; return null;
} }
j = 0; }
} else { if (j != 0) {
return null; if (i2 + 2 > 16) {
} return null;
} }
if (j != 0) { arrayOfByte1[(i2++)] = ((byte) (k >> 8 & 0xFF));
if (i2 + 2 > 16) { arrayOfByte1[(i2++)] = ((byte) (k & 0xFF));
return null; }
} if (i != -1) {
arrayOfByte1[(i2++)] = ((byte) (k >> 8 & 0xFF)); i4 = i2 - i;
arrayOfByte1[(i2++)] = ((byte) (k & 0xFF)); if (i2 == 16) {
} return null;
if (i != -1) { }
i4 = i2 - i; for (i1 = 1; i1 <= i4; i1++) {
if (i2 == 16) { arrayOfByte1[(16 - i1)] = arrayOfByte1[(i + i4 - i1)];
return null; arrayOfByte1[(i + i4 - i1)] = 0;
} }
for (i1 = 1; i1 <= i4; i1++) { i2 = 16;
arrayOfByte1[(16 - i1)] = arrayOfByte1[(i + i4 - i1)]; }
arrayOfByte1[(i + i4 - i1)] = 0; if (i2 != 16) {
} return null;
i2 = 16; }
} byte[] arrayOfByte2 = convertFromIPv4MappedAddress(arrayOfByte1);
if (i2 != 16) { if (arrayOfByte2 != null) {
return null; return arrayOfByte2;
} }
byte[] arrayOfByte2 = convertFromIPv4MappedAddress(arrayOfByte1); return arrayOfByte1;
if (arrayOfByte2 != null) { }
return arrayOfByte2;
} public static boolean isIPv4LiteralAddress(String paramString) {
return arrayOfByte1; return textToNumericFormatV4(paramString) != null;
} }
public static boolean isIPv4LiteralAddress(String paramString) { public static boolean isIPv6LiteralAddress(String paramString) {
return textToNumericFormatV4(paramString) != null; return textToNumericFormatV6(paramString) != null;
} }
public static boolean isIPv6LiteralAddress(String paramString) { public static byte[] convertFromIPv4MappedAddress(byte[] paramArrayOfByte) {
return textToNumericFormatV6(paramString) != null; if (isIPv4MappedAddress(paramArrayOfByte)) {
} byte[] arrayOfByte = new byte[4];
System.arraycopy(paramArrayOfByte, 12, arrayOfByte, 0, 4);
public static byte[] convertFromIPv4MappedAddress(byte[] paramArrayOfByte) { return arrayOfByte;
if (isIPv4MappedAddress(paramArrayOfByte)) { }
byte[] arrayOfByte = new byte[4]; return null;
System.arraycopy(paramArrayOfByte, 12, arrayOfByte, 0, 4); }
return arrayOfByte;
} private static boolean isIPv4MappedAddress(byte[] paramArrayOfByte) {
return null; if (paramArrayOfByte.length < 16) {
} return false;
}
private static boolean isIPv4MappedAddress(byte[] paramArrayOfByte) { if ((paramArrayOfByte[0] == 0) && (paramArrayOfByte[1] == 0) && (paramArrayOfByte[2] == 0) && (paramArrayOfByte[3] == 0)
if (paramArrayOfByte.length < 16) { && (paramArrayOfByte[4] == 0) && (paramArrayOfByte[5] == 0) && (paramArrayOfByte[6] == 0) && (paramArrayOfByte[7] == 0)
return false; && (paramArrayOfByte[8] == 0) && (paramArrayOfByte[9] == 0) && (paramArrayOfByte[10] == -1) && (paramArrayOfByte[11] == -1)) {
} return true;
if ((paramArrayOfByte[0] == 0) && (paramArrayOfByte[1] == 0) && (paramArrayOfByte[2] == 0) && (paramArrayOfByte[3] == 0) }
&& (paramArrayOfByte[4] == 0) && (paramArrayOfByte[5] == 0) && (paramArrayOfByte[6] == 0) && (paramArrayOfByte[7] == 0) return false;
&& (paramArrayOfByte[8] == 0) && (paramArrayOfByte[9] == 0) && (paramArrayOfByte[10] == -1) && (paramArrayOfByte[11] == -1)) { }
return true;
} }
return false;
}
}