From 01d42085c80251b32cb67a8b8c59ffbe510fe8fe Mon Sep 17 00:00:00 2001 From: HuangXin Date: Wed, 30 Sep 2020 12:22:02 +0800 Subject: [PATCH] =?UTF-8?q?OCT=20REM:=201.=20=E5=A2=9E=E5=8A=A00.0.0.0,::,?= =?UTF-8?q?=E8=A1=A8=E7=A4=BA=E6=89=80=E6=9C=89IP=E7=9A=84=E5=8A=9F?= =?UTF-8?q?=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/com/dispose/common/Helper.java | 21 ++++-- .../java/com/dispose/test/dev/debug/demo.java | 67 +++++++++++++++---- 2 files changed, 70 insertions(+), 18 deletions(-) diff --git a/src/main/java/com/dispose/common/Helper.java b/src/main/java/com/dispose/common/Helper.java index d054fff4..f92a11de 100644 --- a/src/main/java/com/dispose/common/Helper.java +++ b/src/main/java/com/dispose/common/Helper.java @@ -101,8 +101,8 @@ public class Helper { return Arrays.stream(ip.split("\\.")) - .map(Integer::parseInt).map(String::valueOf) - .collect(Collectors.joining(".")) + mask; + .map(Integer::parseInt).map(String::valueOf) + .collect(Collectors.joining(".")) + mask; } } @@ -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; } } diff --git a/src/test/java/com/dispose/test/dev/debug/demo.java b/src/test/java/com/dispose/test/dev/debug/demo.java index ca2d7f18..a1ad9d1e 100644 --- a/src/test/java/com/dispose/test/dev/debug/demo.java +++ b/src/test/java/com/dispose/test/dev/debug/demo.java @@ -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() { }