REM:
1. 增加0.0.0.0,::,表示所有IP的功能
This commit is contained in:
HuangXin 2020-09-30 12:22:02 +08:00
parent d7f1d79518
commit 01d42085c8
2 changed files with 70 additions and 18 deletions

View File

@ -125,14 +125,26 @@ public class Helper {
* @return the boolean
*/
public static boolean ipInRange(String rangeIp, String ipAddr) {
IPAddress addr;
final int numIp = 2;
final String cidrSplit = "/";
final String ipV4All = "0.0.0.0";
final String ipV6All = "::";
// 范围为空字符串或者与IP相同时返回真
if (rangeIp.length() == 0 || rangeIp.equals(ipAddr)) {
return true;
}
try {
addr = new IPAddressString(ipAddr).toAddress();
if (addr.isIPv4() && ipV4All.equals(rangeIp)) {
return true;
} else if (addr.isIPv6() && ipV6All.equals(rangeIp)) {
return true;
}
} catch (Exception ignored) {}
// 处理CIDR格式
if (rangeIp.contains(cidrSplit)) {
IPAddressString network = new IPAddressString(rangeIp);
@ -150,12 +162,11 @@ public class Helper {
try {
IPAddress lower = new IPAddressString(ipList[0]).toAddress();
IPAddress upper = new IPAddressString(ipList[1]).toAddress();
IPAddress addr = new IPAddressString(ipAddr).toAddress();
addr = new IPAddressString(ipAddr).toAddress();
IPAddressSeqRange range = lower.toSequentialRange(upper);
return range.contains(addr);
} catch (AddressStringException ignored) {
} catch (AddressStringException ignored) {}
}
return false;
}
}

View File

@ -15,6 +15,7 @@ import com.security.arithmetic.CryptoHelper;
import inet.ipaddr.IPAddress;
import inet.ipaddr.IPAddressString;
import lombok.extern.slf4j.Slf4j;
import org.junit.Assert;
import org.junit.Test;
import javax.crypto.BadPaddingException;
@ -64,19 +65,20 @@ public class demo {
// //id = new ObjectMapper().readValue(json4, IDArrayReq.class);
// }
//
// /**
// * A 2 ip range test.
// *
// * @throws AddressStringException the address string exception
// */
// @Test
// public void a2_ipRangeTest() throws AddressStringException {
// Assert.assertTrue(IPAddrType.ipInRange("192.168.0.1-192.168.0.100", "192.168.0.30"));
// Assert.assertTrue(IPAddrType.ipInRange("192.168.0.30", "192.168.0.30"));
// Assert.assertTrue(IPAddrType.ipInRange("192.168.0.40-192.168.0.40", "192.168.0.40"));
// Assert.assertTrue(IPAddrType.ipInRange("0.0.0.0-255.255.255.255", "192.168.0.30"));
// Assert.assertTrue(IPAddrType.ipInRange("", "192.168.0.30"));
// }
/**
* A 2 ip range test.
*/
@Test
public void a2_ipRangeTest() {
Assert.assertTrue(Helper.ipInRange("192.168.0.1-192.168.0.100", "192.168.0.30"));
Assert.assertTrue(Helper.ipInRange("192.168.0.30", "192.168.0.30"));
Assert.assertTrue(Helper.ipInRange("192.168.0.40-192.168.0.40", "192.168.0.40"));
Assert.assertTrue(Helper.ipInRange("0.0.0.0-255.255.255.255", "192.168.0.30"));
Assert.assertTrue(Helper.ipInRange("", "192.168.0.30"));
Assert.assertTrue(Helper.ipInRange("0.0.0.0", "192.168.0.30"));
Assert.assertTrue(Helper.ipInRange("::", "::abcd"));
}
/**
* Date time debug.
@ -194,6 +196,9 @@ public class demo {
// }
}
/**
* Ip rang.
*/
@Test
public void ipRang() {
String[] src = new String[]{"112.13.86.0-112.13.86.255", "112.13.82.0-112.13.82.255", "117.148.175.0-117.148" +
@ -444,6 +449,9 @@ public class demo {
}
}
/**
* Ip rang 2.
*/
public void ipRang2() {
String ip = "117.149.253.197";
String n = "0.0.0.0";
@ -453,6 +461,9 @@ public class demo {
ip);
}
/**
* Haohan start error.
*/
public void haohanStartError() {
String errMsg = "[{ip=192.168.50.2, 任务名称=三方接口任务467}, {ip=192.168.50.2, 任务名称=三方接口任务468}, {ip=192.168" +
".50.2, 任务名称=三方接口任务470}, {ip=192.168.50.2, 任务名称=三方接口任务469}]";
@ -469,6 +480,9 @@ public class demo {
}
}
/**
* Ipaddr variety.
*/
public void ipaddrVariety() {
String[] ipAddrs = new String[]{
"ffff::8fff:ffff:0:ffff",
@ -487,6 +501,9 @@ public class demo {
}
}
/**
* Group array.
*/
public void groupArray() {
Integer[] org = new Integer[]{1, 2, 3, 4, 5};
Integer[] diff = new Integer[]{1, 3, 5, 6, 7, 8, 9};
@ -501,6 +518,9 @@ public class demo {
log.info("Result: {}", groupMap);
}
/**
* Ip segment.
*/
public void ipSegment() {
String str = "192.168.1.1/23, 192.168.1.2-192.168.1.80, ::1-::255, 192.168.1, 01.012.012.01, " +
"123.123.123-";
@ -538,6 +558,15 @@ public class demo {
}
}
/**
* Aes.
*
* @throws IllegalBlockSizeException the illegal block size exception
* @throws InvalidKeyException the invalid key exception
* @throws BadPaddingException the bad padding exception
* @throws NoSuchAlgorithmException the no such algorithm exception
* @throws NoSuchPaddingException the no such padding exception
*/
public void aes() throws IllegalBlockSizeException, InvalidKeyException, BadPaddingException,
NoSuchAlgorithmException, NoSuchPaddingException {
String plainText = "{\"disposeTime\":60,\"flowDirection\":0,\"mulDisposeIp\":[\"1.1.1.1\"],\"type\":0}";
@ -550,6 +579,15 @@ public class demo {
log.info("AES256: {}", CryptoHelper.base64Encryption(encode));
}
/**
* Aesdec.
*
* @throws IllegalBlockSizeException the illegal block size exception
* @throws InvalidKeyException the invalid key exception
* @throws BadPaddingException the bad padding exception
* @throws NoSuchAlgorithmException the no such algorithm exception
* @throws NoSuchPaddingException the no such padding exception
*/
public void aesdec() throws IllegalBlockSizeException, InvalidKeyException, BadPaddingException,
NoSuchAlgorithmException, NoSuchPaddingException {
String plainText = "6PgUrZa8zZUzDAxAFpV307JfUDVI1gFNo3ZFT7LKtVzRXc3UbwFh6+5i6" +
@ -563,6 +601,9 @@ public class demo {
log.info("AES256: {}", new String(encode));
}
/**
* Code debug.
*/
@Test
public void codeDebug() {
}