1
This commit is contained in:
@@ -1,7 +1,5 @@
|
|||||||
package cn.van333.wxsend.enums;
|
package cn.van333.wxsend.enums;
|
||||||
|
|
||||||
import cn.hutool.core.util.ObjectUtil;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @author Leo
|
* @author Leo
|
||||||
* @version 1.0
|
* @version 1.0
|
||||||
|
|||||||
@@ -2,13 +2,12 @@ package cn.van333.wxsend.util.ip;
|
|||||||
|
|
||||||
import cn.van333.wxsend.business.service.LogService;
|
import cn.van333.wxsend.business.service.LogService;
|
||||||
import cn.van333.wxsend.util.str.StringUtils;
|
import cn.van333.wxsend.util.str.StringUtils;
|
||||||
import com.alibaba.fastjson2.JSON;
|
|
||||||
import org.slf4j.Logger;
|
import org.slf4j.Logger;
|
||||||
import org.slf4j.LoggerFactory;
|
import org.slf4j.LoggerFactory;
|
||||||
|
|
||||||
|
import javax.servlet.http.HttpServletRequest;
|
||||||
import java.net.InetAddress;
|
import java.net.InetAddress;
|
||||||
import java.net.UnknownHostException;
|
import java.net.UnknownHostException;
|
||||||
import javax.servlet.http.HttpServletRequest;
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -16,25 +15,22 @@ import javax.servlet.http.HttpServletRequest;
|
|||||||
*
|
*
|
||||||
* @author ruoyi
|
* @author ruoyi
|
||||||
*/
|
*/
|
||||||
public class IpUtils
|
public class IpUtils {
|
||||||
{
|
|
||||||
|
|
||||||
private static final Logger logger = LoggerFactory.getLogger(LogService.class);
|
|
||||||
|
|
||||||
public final static String REGX_0_255 = "(25[0-5]|2[0-4]\\d|1\\d{2}|[1-9]\\d|\\d)";
|
public final static String REGX_0_255 = "(25[0-5]|2[0-4]\\d|1\\d{2}|[1-9]\\d|\\d)";
|
||||||
// 匹配 ip
|
// 匹配 ip
|
||||||
public final static String REGX_IP = "((" + REGX_0_255 + "\\.){3}" + REGX_0_255 + ")";
|
public final static String REGX_IP = "((" + REGX_0_255 + "\\.){3}" + REGX_0_255 + ")";
|
||||||
public final static String REGX_IP_WILDCARD = "(((\\*\\.){3}\\*)|(" + REGX_0_255 + "(\\.\\*){3})|(" + REGX_0_255 + "\\." + REGX_0_255 + ")(\\.\\*){2}" + "|((" + REGX_0_255 + "\\.){3}\\*))";
|
|
||||||
// 匹配网段
|
// 匹配网段
|
||||||
public final static String REGX_IP_SEG = "(" + REGX_IP + "\\-" + REGX_IP + ")";
|
public final static String REGX_IP_SEG = "(" + REGX_IP + "\\-" + REGX_IP + ")";
|
||||||
|
public final static String REGX_IP_WILDCARD = "(((\\*\\.){3}\\*)|(" + REGX_0_255 + "(\\.\\*){3})|(" + REGX_0_255 + "\\." + REGX_0_255 + ")(\\.\\*){2}" + "|((" + REGX_0_255 + "\\.){3}\\*))";
|
||||||
|
private static final Logger logger = LoggerFactory.getLogger(LogService.class);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 获取客户端IP
|
* 获取客户端IP
|
||||||
*
|
*
|
||||||
* @return IP地址
|
* @return IP地址
|
||||||
*/
|
*/
|
||||||
public static String getIpAddr()
|
public static String getIpAddr() {
|
||||||
{
|
|
||||||
return getIpAddr(ServletUtils.getRequest());
|
return getIpAddr(ServletUtils.getRequest());
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -44,40 +40,33 @@ public class IpUtils
|
|||||||
* @param request 请求对象
|
* @param request 请求对象
|
||||||
* @return IP地址
|
* @return IP地址
|
||||||
*/
|
*/
|
||||||
public static String getIpAddr(HttpServletRequest request)
|
public static String getIpAddr(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");
|
||||||
logger.info(ip);
|
logger.info(ip);
|
||||||
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");
|
||||||
logger.info(ip);
|
logger.info(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("X-Forwarded-For");
|
||||||
logger.info(ip);
|
logger.info(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("WL-Proxy-Client-IP");
|
||||||
logger.info(ip);
|
logger.info(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.getHeader("X-Real-IP");
|
||||||
logger.info(ip);
|
logger.info(ip);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip))
|
if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
|
||||||
{
|
|
||||||
ip = request.getRemoteAddr();
|
ip = request.getRemoteAddr();
|
||||||
logger.info(ip);
|
logger.info(ip);
|
||||||
logger.info(ip);
|
logger.info(ip);
|
||||||
@@ -93,8 +82,7 @@ public class IpUtils
|
|||||||
* @param ip IP地址
|
* @param ip IP地址
|
||||||
* @return 结果
|
* @return 结果
|
||||||
*/
|
*/
|
||||||
public static boolean internalIp(String ip)
|
public static boolean internalIp(String ip) {
|
||||||
{
|
|
||||||
byte[] addr = textToNumericFormatV4(ip);
|
byte[] addr = textToNumericFormatV4(ip);
|
||||||
return internalIp(addr) || "127.0.0.1".equals(ip);
|
return internalIp(addr) || "127.0.0.1".equals(ip);
|
||||||
}
|
}
|
||||||
@@ -105,10 +93,8 @@ public class IpUtils
|
|||||||
* @param addr byte地址
|
* @param addr byte地址
|
||||||
* @return 结果
|
* @return 结果
|
||||||
*/
|
*/
|
||||||
private static boolean internalIp(byte[] addr)
|
private static boolean internalIp(byte[] addr) {
|
||||||
{
|
if (StringUtils.isNull(addr) || addr.length < 2) {
|
||||||
if (StringUtils.isNull(addr) || addr.length < 2)
|
|
||||||
{
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
final byte b0 = addr[0];
|
final byte b0 = addr[0];
|
||||||
@@ -122,18 +108,15 @@ public class IpUtils
|
|||||||
// 192.168.x.x/16
|
// 192.168.x.x/16
|
||||||
final byte SECTION_5 = (byte) 0xC0;
|
final byte SECTION_5 = (byte) 0xC0;
|
||||||
final byte SECTION_6 = (byte) 0xA8;
|
final byte SECTION_6 = (byte) 0xA8;
|
||||||
switch (b0)
|
switch (b0) {
|
||||||
{
|
|
||||||
case SECTION_1:
|
case SECTION_1:
|
||||||
return true;
|
return true;
|
||||||
case SECTION_2:
|
case SECTION_2:
|
||||||
if (b1 >= SECTION_3 && b1 <= SECTION_4)
|
if (b1 >= SECTION_3 && b1 <= SECTION_4) {
|
||||||
{
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
case SECTION_5:
|
case SECTION_5:
|
||||||
switch (b1)
|
switch (b1) {
|
||||||
{
|
|
||||||
case SECTION_6:
|
case SECTION_6:
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
@@ -148,25 +131,20 @@ public class IpUtils
|
|||||||
* @param text IPv4地址
|
* @param text IPv4地址
|
||||||
* @return byte 字节
|
* @return byte 字节
|
||||||
*/
|
*/
|
||||||
public static byte[] textToNumericFormatV4(String text)
|
public static byte[] textToNumericFormatV4(String text) {
|
||||||
{
|
if (text.length() == 0) {
|
||||||
if (text.length() == 0)
|
|
||||||
{
|
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
byte[] bytes = new byte[4];
|
byte[] bytes = new byte[4];
|
||||||
String[] elements = text.split("\\.", -1);
|
String[] elements = text.split("\\.", -1);
|
||||||
try
|
try {
|
||||||
{
|
|
||||||
long l;
|
long l;
|
||||||
int i;
|
int i;
|
||||||
switch (elements.length)
|
switch (elements.length) {
|
||||||
{
|
|
||||||
case 1:
|
case 1:
|
||||||
l = Long.parseLong(elements[0]);
|
l = Long.parseLong(elements[0]);
|
||||||
if ((l < 0L) || (l > 4294967295L))
|
if ((l < 0L) || (l > 4294967295L)) {
|
||||||
{
|
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
bytes[0] = (byte) (int) (l >> 24 & 0xFF);
|
bytes[0] = (byte) (int) (l >> 24 & 0xFF);
|
||||||
@@ -176,14 +154,12 @@ public class IpUtils
|
|||||||
break;
|
break;
|
||||||
case 2:
|
case 2:
|
||||||
l = Integer.parseInt(elements[0]);
|
l = Integer.parseInt(elements[0]);
|
||||||
if ((l < 0L) || (l > 255L))
|
if ((l < 0L) || (l > 255L)) {
|
||||||
{
|
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
bytes[0] = (byte) (int) (l & 0xFF);
|
bytes[0] = (byte) (int) (l & 0xFF);
|
||||||
l = Integer.parseInt(elements[1]);
|
l = Integer.parseInt(elements[1]);
|
||||||
if ((l < 0L) || (l > 16777215L))
|
if ((l < 0L) || (l > 16777215L)) {
|
||||||
{
|
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
bytes[1] = (byte) (int) (l >> 16 & 0xFF);
|
bytes[1] = (byte) (int) (l >> 16 & 0xFF);
|
||||||
@@ -191,29 +167,24 @@ public class IpUtils
|
|||||||
bytes[3] = (byte) (int) (l & 0xFF);
|
bytes[3] = (byte) (int) (l & 0xFF);
|
||||||
break;
|
break;
|
||||||
case 3:
|
case 3:
|
||||||
for (i = 0; i < 2; ++i)
|
for (i = 0; i < 2; ++i) {
|
||||||
{
|
|
||||||
l = Integer.parseInt(elements[i]);
|
l = Integer.parseInt(elements[i]);
|
||||||
if ((l < 0L) || (l > 255L))
|
if ((l < 0L) || (l > 255L)) {
|
||||||
{
|
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
bytes[i] = (byte) (int) (l & 0xFF);
|
bytes[i] = (byte) (int) (l & 0xFF);
|
||||||
}
|
}
|
||||||
l = Integer.parseInt(elements[2]);
|
l = Integer.parseInt(elements[2]);
|
||||||
if ((l < 0L) || (l > 65535L))
|
if ((l < 0L) || (l > 65535L)) {
|
||||||
{
|
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
bytes[2] = (byte) (int) (l >> 8 & 0xFF);
|
bytes[2] = (byte) (int) (l >> 8 & 0xFF);
|
||||||
bytes[3] = (byte) (int) (l & 0xFF);
|
bytes[3] = (byte) (int) (l & 0xFF);
|
||||||
break;
|
break;
|
||||||
case 4:
|
case 4:
|
||||||
for (i = 0; i < 4; ++i)
|
for (i = 0; i < 4; ++i) {
|
||||||
{
|
|
||||||
l = Integer.parseInt(elements[i]);
|
l = Integer.parseInt(elements[i]);
|
||||||
if ((l < 0L) || (l > 255L))
|
if ((l < 0L) || (l > 255L)) {
|
||||||
{
|
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
bytes[i] = (byte) (int) (l & 0xFF);
|
bytes[i] = (byte) (int) (l & 0xFF);
|
||||||
@@ -222,9 +193,7 @@ public class IpUtils
|
|||||||
default:
|
default:
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
}
|
} catch (NumberFormatException e) {
|
||||||
catch (NumberFormatException e)
|
|
||||||
{
|
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
return bytes;
|
return bytes;
|
||||||
@@ -235,14 +204,10 @@ public class IpUtils
|
|||||||
*
|
*
|
||||||
* @return 本地IP地址
|
* @return 本地IP地址
|
||||||
*/
|
*/
|
||||||
public static String getHostIp()
|
public static String getHostIp() {
|
||||||
{
|
try {
|
||||||
try
|
|
||||||
{
|
|
||||||
return InetAddress.getLocalHost().getHostAddress();
|
return InetAddress.getLocalHost().getHostAddress();
|
||||||
}
|
} catch (UnknownHostException e) {
|
||||||
catch (UnknownHostException e)
|
|
||||||
{
|
|
||||||
}
|
}
|
||||||
return "127.0.0.1";
|
return "127.0.0.1";
|
||||||
}
|
}
|
||||||
@@ -252,14 +217,10 @@ public class IpUtils
|
|||||||
*
|
*
|
||||||
* @return 本地主机名
|
* @return 本地主机名
|
||||||
*/
|
*/
|
||||||
public static String getHostName()
|
public static String getHostName() {
|
||||||
{
|
try {
|
||||||
try
|
|
||||||
{
|
|
||||||
return InetAddress.getLocalHost().getHostName();
|
return InetAddress.getLocalHost().getHostName();
|
||||||
}
|
} catch (UnknownHostException e) {
|
||||||
catch (UnknownHostException e)
|
|
||||||
{
|
|
||||||
}
|
}
|
||||||
return "未知";
|
return "未知";
|
||||||
}
|
}
|
||||||
@@ -270,18 +231,12 @@ public class IpUtils
|
|||||||
* @param ip 获得的IP地址
|
* @param ip 获得的IP地址
|
||||||
* @return 第一个非unknown IP地址
|
* @return 第一个非unknown IP地址
|
||||||
*/
|
*/
|
||||||
public static String getMultistageReverseProxyIp(String ip)
|
public static String getMultistageReverseProxyIp(String ip) {
|
||||||
{
|
|
||||||
logger.info(ip);
|
|
||||||
|
|
||||||
// 多级反向代理检测
|
// 多级反向代理检测
|
||||||
if (ip != null && ip.indexOf(",") > 0)
|
if (ip != null && ip.indexOf(",") > 0) {
|
||||||
{
|
|
||||||
final String[] ips = ip.trim().split(",");
|
final String[] ips = ip.trim().split(",");
|
||||||
for (String subIp : ips)
|
for (String subIp : ips) {
|
||||||
{
|
if (false == isUnknown(subIp)) {
|
||||||
if (false == isUnknown(subIp))
|
|
||||||
{
|
|
||||||
ip = subIp;
|
ip = subIp;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
@@ -296,39 +251,33 @@ public class IpUtils
|
|||||||
* @param checkString 被检测的字符串
|
* @param checkString 被检测的字符串
|
||||||
* @return 是否未知
|
* @return 是否未知
|
||||||
*/
|
*/
|
||||||
public static boolean isUnknown(String checkString)
|
public static boolean isUnknown(String checkString) {
|
||||||
{
|
|
||||||
return StringUtils.isBlank(checkString) || "unknown".equalsIgnoreCase(checkString);
|
return StringUtils.isBlank(checkString) || "unknown".equalsIgnoreCase(checkString);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 是否为IP
|
* 是否为IP
|
||||||
*/
|
*/
|
||||||
public static boolean isIP(String ip)
|
public static boolean isIP(String ip) {
|
||||||
{
|
|
||||||
return StringUtils.isNotBlank(ip) && ip.matches(REGX_IP);
|
return StringUtils.isNotBlank(ip) && ip.matches(REGX_IP);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 是否为IP,或 *为间隔的通配符地址
|
* 是否为IP,或 *为间隔的通配符地址
|
||||||
*/
|
*/
|
||||||
public static boolean isIpWildCard(String ip)
|
public static boolean isIpWildCard(String ip) {
|
||||||
{
|
|
||||||
return StringUtils.isNotBlank(ip) && ip.matches(REGX_IP_WILDCARD);
|
return StringUtils.isNotBlank(ip) && ip.matches(REGX_IP_WILDCARD);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 检测参数是否在ip通配符里
|
* 检测参数是否在ip通配符里
|
||||||
*/
|
*/
|
||||||
public static boolean ipIsInWildCardNoCheck(String ipWildCard, String ip)
|
public static boolean ipIsInWildCardNoCheck(String ipWildCard, String ip) {
|
||||||
{
|
|
||||||
String[] s1 = ipWildCard.split("\\.");
|
String[] s1 = ipWildCard.split("\\.");
|
||||||
String[] s2 = ip.split("\\.");
|
String[] s2 = ip.split("\\.");
|
||||||
boolean isMatchedSeg = true;
|
boolean isMatchedSeg = true;
|
||||||
for (int i = 0; i < s1.length && !s1[i].equals("*"); i++)
|
for (int i = 0; i < s1.length && !s1[i].equals("*"); i++) {
|
||||||
{
|
if (!s1[i].equals(s2[i])) {
|
||||||
if (!s1[i].equals(s2[i]))
|
|
||||||
{
|
|
||||||
isMatchedSeg = false;
|
isMatchedSeg = false;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
@@ -339,29 +288,25 @@ public class IpUtils
|
|||||||
/**
|
/**
|
||||||
* 是否为特定格式如:“10.10.10.1-10.10.10.99”的ip段字符串
|
* 是否为特定格式如:“10.10.10.1-10.10.10.99”的ip段字符串
|
||||||
*/
|
*/
|
||||||
public static boolean isIPSegment(String ipSeg)
|
public static boolean isIPSegment(String ipSeg) {
|
||||||
{
|
|
||||||
return StringUtils.isNotBlank(ipSeg) && ipSeg.matches(REGX_IP_SEG);
|
return StringUtils.isNotBlank(ipSeg) && ipSeg.matches(REGX_IP_SEG);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 判断ip是否在指定网段中
|
* 判断ip是否在指定网段中
|
||||||
*/
|
*/
|
||||||
public static boolean ipIsInNetNoCheck(String iparea, String ip)
|
public static boolean ipIsInNetNoCheck(String iparea, String ip) {
|
||||||
{
|
|
||||||
int idx = iparea.indexOf('-');
|
int idx = iparea.indexOf('-');
|
||||||
String[] sips = iparea.substring(0, idx).split("\\.");
|
String[] sips = iparea.substring(0, idx).split("\\.");
|
||||||
String[] sipe = iparea.substring(idx + 1).split("\\.");
|
String[] sipe = iparea.substring(idx + 1).split("\\.");
|
||||||
String[] sipt = ip.split("\\.");
|
String[] sipt = ip.split("\\.");
|
||||||
long ips = 0L, ipe = 0L, ipt = 0L;
|
long ips = 0L, ipe = 0L, ipt = 0L;
|
||||||
for (int i = 0; i < 4; ++i)
|
for (int i = 0; i < 4; ++i) {
|
||||||
{
|
|
||||||
ips = ips << 8 | Integer.parseInt(sips[i]);
|
ips = ips << 8 | Integer.parseInt(sips[i]);
|
||||||
ipe = ipe << 8 | Integer.parseInt(sipe[i]);
|
ipe = ipe << 8 | Integer.parseInt(sipe[i]);
|
||||||
ipt = ipt << 8 | Integer.parseInt(sipt[i]);
|
ipt = ipt << 8 | Integer.parseInt(sipt[i]);
|
||||||
}
|
}
|
||||||
if (ips > ipe)
|
if (ips > ipe) {
|
||||||
{
|
|
||||||
long t = ips;
|
long t = ips;
|
||||||
ips = ipe;
|
ips = ipe;
|
||||||
ipe = t;
|
ipe = t;
|
||||||
@@ -376,25 +321,17 @@ public class IpUtils
|
|||||||
* @param ip 校验IP地址
|
* @param ip 校验IP地址
|
||||||
* @return boolean 结果
|
* @return boolean 结果
|
||||||
*/
|
*/
|
||||||
public static boolean isMatchedIp(String filter, String ip)
|
public static boolean isMatchedIp(String filter, String ip) {
|
||||||
{
|
if (StringUtils.isEmpty(filter) || StringUtils.isEmpty(ip)) {
|
||||||
if (StringUtils.isEmpty(filter) || StringUtils.isEmpty(ip))
|
|
||||||
{
|
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
String[] ips = filter.split(";");
|
String[] ips = filter.split(";");
|
||||||
for (String iStr : ips)
|
for (String iStr : ips) {
|
||||||
{
|
if (isIP(iStr) && iStr.equals(ip)) {
|
||||||
if (isIP(iStr) && iStr.equals(ip))
|
|
||||||
{
|
|
||||||
return true;
|
return true;
|
||||||
}
|
} else if (isIpWildCard(iStr) && ipIsInWildCardNoCheck(iStr, ip)) {
|
||||||
else if (isIpWildCard(iStr) && ipIsInWildCardNoCheck(iStr, ip))
|
|
||||||
{
|
|
||||||
return true;
|
return true;
|
||||||
}
|
} else if (isIPSegment(iStr) && ipIsInNetNoCheck(iStr, ip)) {
|
||||||
else if (isIPSegment(iStr) && ipIsInNetNoCheck(iStr, ip))
|
|
||||||
{
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user