增加参数范围校验

This commit is contained in:
waner
2025-07-24 18:02:14 +08:00
parent 1f2709326f
commit 7e4bc7f87b
3 changed files with 133 additions and 25 deletions
@@ -0,0 +1,70 @@
package com.sunyard.util;
import java.util.regex.Pattern;
public class AddressUtil {
// 处理带端口号的地址
public static boolean isValidAddressWithPort(String address) {
if (address == null) {
return false;
}
// 分离 IP:port 或 [IPv6]:port
String[] parts;
if (address.startsWith("[") && address.contains("]:")) {
parts = address.split("\\]:", 2);
parts[0] = parts[0] + "]"; // 补全 IPv6 的括号
} else {
parts = address.split(":", 2);
}
if (parts.length != 2) {
return false;
}
String ipPart = parts[0];
String portPart = parts[1];
return isValidIP(ipPart) &&
isValidPort(portPart);
}
// IPv4 正则(允许 0-255)
private static final Pattern IPV4_PATTERN = Pattern.compile(
"^((25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\.){3}(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$"
);
// IPv6 正则(包含缩写形式)
private static final Pattern IPV6_PATTERN = Pattern.compile(
"^(?:[0-9a-fA-F]{1,4}:){7}[0-9a-fA-F]{1,4}$"
);
// 简化域名正则(允许字母、数字、连字符和点)
private static final Pattern DOMAIN_PATTERN = Pattern.compile(
"^([a-zA-Z0-9-]{1,63}\\.)+[a-zA-Z]{2,63}$"
);
public static boolean isValidIP(String ip) {
// 检查 IPv4 或 IPv6
return IPV4_PATTERN.matcher(ip).matches() ||
IPV6_PATTERN.matcher(ip).matches();
}
public static boolean isValidDomain(String domain) {
return DOMAIN_PATTERN.matcher(domain).matches();
}
public static boolean isValidPort(int port) {
return port >= 0 && port <= 65535;
}
// 字符串形式的端口校验
public static boolean isValidPort(String portStr) {
try {
int port = Integer.parseInt(portStr);
return port >= 0 && port <= 65535;
} catch (NumberFormatException e) {
return false;
}
}
}
+27 -2
View File
@@ -146,6 +146,15 @@ public class SydApi4j implements SydApi {
Assert.notBlank(ip1, "主机IP不能为空");
Assert.notBlank(ip2, "备机IP不能为空");
Assert.notNull(port, "端口不能为空");
if (!AddressUtil.isValidIP(ip1) || !AddressUtil.isValidIP(ip2)) {
throw new IllegalArgumentException("主备机ip格式有误");
}
if (!AddressUtil.isValidPort(port)) {
throw new IllegalArgumentException("端口范围有误");
}
if (timeout < 0 || timeout > 60000) {
throw new IllegalArgumentException("超时时间范围不正确");
}
SydApi4j.ha_ip1 = ip1;
SydApi4j.ha_ip2 = ip2;
SydApi4j.ha_port = port;
@@ -1358,6 +1367,9 @@ public class SydApi4j implements SydApi {
if (null == addressList || addressList.isEmpty()) {
throw new IllegalArgumentException("签名服务器地址不能为空");
}
if (addressList.size() > 50) {
throw new IllegalArgumentException("签名服务器地址数量不大于50");
}
if (null == cert || cert.length() == 0) {
throw new IllegalArgumentException("证书不能为空");
}
@@ -1365,7 +1377,7 @@ public class SydApi4j implements SydApi {
throw new IllegalArgumentException("snmp配置不能为空");
}
List<TaskContext<String>> taskContexts =
addressList.stream().map(p -> createTaskContext(p, timeout, cert)).collect(Collectors.toList());
addressList.stream().distinct().map(p -> createTaskContext(p, timeout, cert)).collect(Collectors.toList());
AsyncTaskExecutor executor = new AsyncTaskExecutor();
List<TaskOutcome<String>> taskOutcomes = executor.execBatchTasks(taskContexts);
executor.shutdown();
@@ -1429,6 +1441,9 @@ public class SydApi4j implements SydApi {
private String importCertToServer(String address, int timeout, String cert) {
//address 格式为 ip:port,拆分成ip和port,需要进行格式判断
if (!AddressUtil.isValidAddressWithPort(address)) {
throw new IllegalArgumentException("地址格式有误:" + address);
}
String[] addressParts = address.split(":");
String ip = addressParts[0];
int port = Integer.parseInt(addressParts[1]);
@@ -2674,7 +2689,17 @@ public class SydApi4j implements SydApi {
@Override
synchronized public SydApi connect(String ip, int port, String pwd, int timeout) {
Assert.notBlank(ip, "IP不能为空");
Assert.notNull(port, "端口不能为空");
if (!AddressUtil.isValidIP(ip)) {
throw new IllegalArgumentException("ip格式有误");
}
if (!AddressUtil.isValidPort(port)) {
throw new IllegalArgumentException("端口范围有误");
}
if (timeout < 0 || timeout > 60000) {
throw new IllegalArgumentException("超时时间范围不正确");
}
// if (null != ha_ip2) {
// throw new SydApiException("双机热备模式不用调用此方法", -1);
// }