|
|
@ -1,6 +1,7 @@
|
|
|
|
package cn.jlw.util;
|
|
|
|
package cn.jlw.util;
|
|
|
|
|
|
|
|
|
|
|
|
import cn.hutool.core.lang.Assert;
|
|
|
|
import cn.hutool.core.lang.Assert;
|
|
|
|
|
|
|
|
import cn.hutool.core.util.ReUtil;
|
|
|
|
import com.ibeetl.admin.core.util.PlatformException;
|
|
|
|
import com.ibeetl.admin.core.util.PlatformException;
|
|
|
|
import com.ibeetl.jlw.entity.IpAddress;
|
|
|
|
import com.ibeetl.jlw.entity.IpAddress;
|
|
|
|
import com.ibeetl.jlw.service.IpAddressService;
|
|
|
|
import com.ibeetl.jlw.service.IpAddressService;
|
|
|
@ -21,7 +22,6 @@ import java.io.*;
|
|
|
|
import java.net.InetAddress;
|
|
|
|
import java.net.InetAddress;
|
|
|
|
import java.net.UnknownHostException;
|
|
|
|
import java.net.UnknownHostException;
|
|
|
|
import java.util.ArrayList;
|
|
|
|
import java.util.ArrayList;
|
|
|
|
import java.util.Arrays;
|
|
|
|
|
|
|
|
import java.util.List;
|
|
|
|
import java.util.List;
|
|
|
|
import java.util.concurrent.Callable;
|
|
|
|
import java.util.concurrent.Callable;
|
|
|
|
import java.util.concurrent.ExecutorService;
|
|
|
|
import java.util.concurrent.ExecutorService;
|
|
|
@ -37,9 +37,11 @@ public class IpUtils {
|
|
|
|
private static TaskCallable taskCallable = new TaskCallable();
|
|
|
|
private static TaskCallable taskCallable = new TaskCallable();
|
|
|
|
|
|
|
|
|
|
|
|
// 分隔符1
|
|
|
|
// 分隔符1
|
|
|
|
public static final String SEPARATOR = ",";
|
|
|
|
public static final String SEPARATOR = "\\,";
|
|
|
|
// 分隔符2
|
|
|
|
// 分隔符2
|
|
|
|
public static final String IP_SPLIT_MARK = "-";
|
|
|
|
public static final String IP_SPLIT_MARK = "\\-";
|
|
|
|
|
|
|
|
// 分隔符3
|
|
|
|
|
|
|
|
public static final String IP_SPLIT_SEPARATOR = "\\.";
|
|
|
|
|
|
|
|
|
|
|
|
private IpUtils() {
|
|
|
|
private IpUtils() {
|
|
|
|
}
|
|
|
|
}
|
|
|
@ -114,23 +116,31 @@ public class IpUtils {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
/**
|
|
|
|
* ID是否在IP段集合内
|
|
|
|
* 验证IP区间是否符合要求
|
|
|
|
|
|
|
|
*
|
|
|
|
|
|
|
|
*
|
|
|
|
|
|
|
|
* @param ipAddressList 支持格式:x.x.x.x-x.x.x.x,x.x.x.x-x.x.x.x。多个IP段逗号隔开
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
public static void verifyIpRange(String ipAddressList) {
|
|
|
|
|
|
|
|
Assert.notBlank(ipAddressList, "待验证的IP区间,不能为空!");
|
|
|
|
|
|
|
|
for (String ipRange : ipAddressList.split(SEPARATOR)) {
|
|
|
|
|
|
|
|
String reg = "^((2(5[0-5]|[0-4]\\d))|[0-1]?\\d{1,2})(\\.((2(5[0-5]|[0-4]\\d))|[0-1]?\\d{1,2})){3}" +
|
|
|
|
|
|
|
|
"-((2(5[0-5]|[0-4]\\d))|[0-1]?\\d{1,2})(\\.((2(5[0-5]|[0-4]\\d))|[0-1]?\\d{1,2})){3}$";
|
|
|
|
|
|
|
|
Assert.isTrue(ReUtil.isMatch(reg, ipRange),
|
|
|
|
|
|
|
|
() -> new PlatformException("ipAddressList 参数格式化错误!支持格式:x.x.x.x-x.x.x.x,x.x.x.x-x.x.x.x"));
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
|
|
* ID是否在IP区间内
|
|
|
|
*
|
|
|
|
*
|
|
|
|
* @param myIpAddress
|
|
|
|
* @param myIpAddress
|
|
|
|
* @param ipAddressList 支持格式:x.x.x.x-x.x.x.x,x.x.x.x-x.x.x.x。多个IP段逗号隔开
|
|
|
|
* @param ipAddressList 支持格式:x.x.x.x-x.x.x.x,x.x.x.x-x.x.x.x。多个IP段逗号隔开
|
|
|
|
* @return
|
|
|
|
* @return
|
|
|
|
*/
|
|
|
|
*/
|
|
|
|
public static Boolean isRangeInner(String myIpAddress, String ipAddressList) throws PlatformException {
|
|
|
|
public static Boolean isRangeInner(String myIpAddress, String ipAddressList) throws PlatformException {
|
|
|
|
try {
|
|
|
|
|
|
|
|
// 单独的验证字符串是否符合要求
|
|
|
|
|
|
|
|
Arrays.stream(ipAddressList.split(SEPARATOR)).forEach(item -> {
|
|
|
|
|
|
|
|
Assert.isTrue(item.split(IP_SPLIT_MARK).length == 2,
|
|
|
|
|
|
|
|
() -> new PlatformException("ipAddressList 参数格式化错误!支持格式:x.x.x.x-x.x.x.x,x.x.x.x-x.x.x.x"));
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
}catch(Exception e) {
|
|
|
|
|
|
|
|
throw new PlatformException("ipAddressList 参数格式化错误!支持格式:x.x.x.x-x.x.x.x,x.x.x.x-x.x.x.x");
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
verifyIpRange(ipAddressList);
|
|
|
|
// 我的IPv4转long类型
|
|
|
|
// 我的IPv4转long类型
|
|
|
|
long myIpLong = ipv4ToLong(myIpAddress);
|
|
|
|
long myIpLong = ipv4ToLong(myIpAddress);
|
|
|
|
|
|
|
|
|
|
|
|