From b95d9dff5c02b234efdd1b93403b3353c12ee6b2 Mon Sep 17 00:00:00 2001 From: RenFei Date: Thu, 16 Nov 2023 09:38:22 +0800 Subject: [PATCH] Fixed wrong value returned by CIDRToIPv6 function see: https://github.com/ip2location/ip2location-java/commit/6ff27d6972c3a7a8a9cbff08472ec377959c5157 --- README.md | 2 +- README_zh.md | 2 +- pom.xml | 10 +++- .../java/net/renfei/ip2location/IPTools.java | 46 ++++++++----------- 4 files changed, 31 insertions(+), 29 deletions(-) diff --git a/README.md b/README.md index 61bbbf9..57429dc 100644 --- a/README.md +++ b/README.md @@ -30,7 +30,7 @@ Or download the zip file in the warehouse and decompress it by yourself. net.renfei ip2location - 1.2.0 + 1.2.1 ``` diff --git a/README_zh.md b/README_zh.md index 67b1ec6..ca489ee 100644 --- a/README_zh.md +++ b/README_zh.md @@ -30,7 +30,7 @@ net.renfei ip2location - 1.2.0 + 1.2.1 ``` diff --git a/pom.xml b/pom.xml index 284b30e..d4ef1ba 100644 --- a/pom.xml +++ b/pom.xml @@ -6,7 +6,7 @@ net.renfei ip2location - 1.2.0 + 1.2.1 ip2location http://www.renfei.net IP2Location Java Component enables applications to get info from IP address such as the visitor’s country, region, city, latitude, longitude, ZIP code, ISP name, domain name, time zone, connection speed, IDD code, area code, weather station code, weather station name, MCC, MNC, mobile brand name, elevation and usage type. @@ -57,6 +57,14 @@ 8 + + + org.apache.commons + commons-lang3 + 3.13.0 + + + diff --git a/src/main/java/net/renfei/ip2location/IPTools.java b/src/main/java/net/renfei/ip2location/IPTools.java index 953e46c..7481c03 100644 --- a/src/main/java/net/renfei/ip2location/IPTools.java +++ b/src/main/java/net/renfei/ip2location/IPTools.java @@ -1,5 +1,7 @@ package net.renfei.ip2location; +import org.apache.commons.lang3.StringUtils; + import java.math.BigInteger; import java.net.InetAddress; import java.net.Inet4Address; @@ -388,8 +390,7 @@ public List IPv6ToCIDR(String IPFrom, String IPTo) throws UnknownHostExc } while (ipFromBin.compareTo(ipToBin) < 0); - for (Map.Entry - entry : networks.entrySet()) { + for (Map.Entry entry : networks.entrySet()) { result.add(CompressIPv6(BinaryToIP(entry.getKey())) + "/" + entry.getValue()); } @@ -464,33 +465,26 @@ public String[] CIDRToIPv6(String CIDR) throws UnknownHostException { ip = arr[0]; prefix = Integer.parseInt(arr[1]); - String hexStartAddress = ExpandIPv6(ip).replaceAll(":", ""); - String hexEndAddress = hexStartAddress; + String[] parts = ExpandIPv6(ip).split("\\:"); - int bits = 128 - prefix; - int x; - String y; - int pos = 31; - List values; - char[] tmp; - while (bits > 0) { - values = Arrays.asList(4, bits); - x = Integer.parseInt(String.valueOf(hexEndAddress.charAt(pos)), 16); - y = String.format("%x", (x | (int) (Math.pow(2, Collections.min(values)) - 1))); // single hex char - - // replace char - tmp = hexEndAddress.toCharArray(); - tmp[pos] = y.charAt(0); - hexEndAddress = String.valueOf(tmp); - - bits -= 4; - pos -= 1; + String bitStart = StringUtils.repeat('1', prefix) + StringUtils.repeat('0', 128 - prefix); + String bitEnd = StringUtils.repeat('0', prefix) + StringUtils.repeat('1', 128 - prefix); + + int chunkSize = 16; + + String[] floors = bitStart.split("(?<=\\G.{" + chunkSize + "})"); + String[] ceilings = bitEnd.split("(?<=\\G.{" + chunkSize + "})"); + + List startIP = new ArrayList(8); + List endIP = new ArrayList(8); + + for (int x = 0; x < 8; x++) { + startIP.add(Integer.toHexString(Integer.parseInt(parts[x], 16) & Integer.parseInt(floors[x], 2))); + endIP.add(Integer.toHexString(Integer.parseInt(parts[x], 16) | Integer.parseInt(ceilings[x], 2))); } - hexStartAddress = hexStartAddress.replaceAll("(.{4})", "$1:"); - hexStartAddress = hexStartAddress.substring(0, hexStartAddress.length() - 1); - hexEndAddress = hexEndAddress.replaceAll("(.{4})", "$1:"); - hexEndAddress = hexEndAddress.substring(0, hexEndAddress.length() - 1); + String hexStartAddress = ExpandIPv6(StringUtils.join(startIP, ":")); + String hexEndAddress = ExpandIPv6(StringUtils.join(endIP, ":")); return new String[]{hexStartAddress, hexEndAddress}; }