REM:
1. 修正由于IPv6地址不规范导致下发处置任务失败
This commit is contained in:
HuangXin 2021-01-28 20:04:06 +08:00
parent ca70018b0c
commit f01434f22e
2 changed files with 40 additions and 20 deletions

View File

@ -16,7 +16,10 @@ import java.nio.charset.StandardCharsets;
import java.sql.Timestamp; import java.sql.Timestamp;
import java.time.LocalDateTime; import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter; import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.Arrays; import java.util.Arrays;
import java.util.List;
import java.util.Objects;
import java.util.Optional; import java.util.Optional;
import java.util.stream.Collectors; import java.util.stream.Collectors;
@ -130,19 +133,33 @@ public class Helper {
* @return the string * @return the string
*/ */
public static String ipAddrToFullFormat(String ipAddr) { public static String ipAddrToFullFormat(String ipAddr) {
String[] ipArrays = ipAddr.split("-"); List<String> formatString = new ArrayList<>();
for (String v : ipAddr.split(DisposeConfigValue.SPLIT_CHAR)) {
if (v.contains("/")) {
String[] ret = v.split("/");
IPAddressString addrString = new IPAddressString(ret[0]);
IPAddress addr = addrString.getAddress();
formatString.add(addr.toNormalizedString().toUpperCase() + "/" + ret[1]);
} else if (v.contains("-")) {
String[] ret = v.split("-");
for (int i = 0; i < ret.length; i++) {
IPAddressString addrString = new IPAddressString(ret[i]);
IPAddress addr = addrString.getAddress();
ret[i] = addr.toNormalizedString().toUpperCase();
}
formatString.add(ret[0] + "-" + ret[1]);
} else {
IPAddressString addrString = new IPAddressString(v);
IPAddress addr = addrString.getAddress();
formatString.add(addr.toNormalizedString().toUpperCase());
}
for(int i = 0; i < ipArrays.length; i++) {
IPAddressString addrString = new IPAddressString(ipArrays[i]);
IPAddress addr = addrString.getAddress();
ipArrays[i] = addr.toFullString().toUpperCase();
} }
if(ipArrays.length == 1) { return formatString.stream().map(Objects::toString).collect(Collectors.joining(","));
return ipArrays[0];
} else {
return ipArrays[0] + "-" + ipArrays[1];
}
} }
/** /**
@ -220,7 +237,8 @@ public class Helper {
public static String inputStream2String(InputStream inputStream) { public static String inputStream2String(InputStream inputStream) {
StringBuilder sb = new StringBuilder(); StringBuilder sb = new StringBuilder();
try (BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream, StandardCharsets.UTF_8))) { try (BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream,
StandardCharsets.UTF_8))) {
String line; String line;
while ((line = reader.readLine()) != null) { while ((line = reader.readLine()) != null) {
sb.append(line); sb.append(line);

View File

@ -523,20 +523,22 @@ public class demo {
/** /**
* Ip segment. * Ip segment.
*/ */
public void ipSegment() { @Test
String str = "192.168.1.1/23, 192.168.1.2-192.168.1.80, ::1-::255, 192.168.1, 01.012.012.01, " + public void ipSegmentTest() {
"123.123.123-"; String str = "192.168.1.1/23, 192.168.1.2-192.168.1.80, ::1-::255, 01.012.012.01, ::1/24, ::02:23";
String fIpAddr = ""; String fIpAddr = "";
for (String s : str.split(DisposeConfigValue.SPLIT_CHAR)) { for (String s : str.split(DisposeConfigValue.SPLIT_CHAR)) {
log.info("process string: {}", s); // log.info("process string: {}", s);
//
// String normalString = s.replace(" ", "");
//
// if (!Pattern.matches(ConstValue.ipAddrSegmentReg(), normalString)) {
// log.error("Step1: {} not match", normalString);
// }
String normalString = s.replace(" ", ""); log.info("Format: {} --> {}", s, Helper.ipAddrToFullFormat(s));
if (!Pattern.matches(ConstValue.ipAddrSegmentReg(), normalString)) {
log.error("Step1: {} not match", normalString);
}
// if (normalString.indexOf("-") == normalString.lastIndexOf("-")) { // if (normalString.indexOf("-") == normalString.lastIndexOf("-")) {
// if(normalString.contains("-")) { // if(normalString.contains("-")) {