diff --git a/collector/server/src/main/java/com/usthe/collector/dispatch/timer/WheelTimerTask.java b/collector/server/src/main/java/com/usthe/collector/dispatch/timer/WheelTimerTask.java index c61fdfe..43d7b02 100644 --- a/collector/server/src/main/java/com/usthe/collector/dispatch/timer/WheelTimerTask.java +++ b/collector/server/src/main/java/com/usthe/collector/dispatch/timer/WheelTimerTask.java @@ -65,8 +65,10 @@ public class WheelTimerTask implements TimerTask { if (value.startsWith("^_^")) { value = value.replaceAll("\\^_\\^", ""); Configmap param = configmap.get(value); - value = (String) param.getValue(); - jsonObject.addProperty(entry.getKey(), value); + if (param != null) { + value = (String) param.getValue(); + jsonObject.addProperty(entry.getKey(), value); + } } } else { jsonObject.add(entry.getKey(), replaceSpecialValue(entry.getValue(), configmap)); @@ -82,8 +84,10 @@ public class WheelTimerTask implements TimerTask { if (value.startsWith("^_^")) { value = value.replaceAll("\\^_\\^", ""); Configmap param = configmap.get(value); - value = (String) param.getValue(); - jsonArray.set(i, new JsonPrimitive(value)); + if (param != null) { + value = (String) param.getValue(); + jsonArray.set(i, new JsonPrimitive(value)); + } } } else { jsonArray.set(i, replaceSpecialValue(element, configmap)); diff --git a/common/src/main/java/com/usthe/common/util/IpDomainUtil.java b/common/src/main/java/com/usthe/common/util/IpDomainUtil.java new file mode 100644 index 0000000..a4c5a58 --- /dev/null +++ b/common/src/main/java/com/usthe/common/util/IpDomainUtil.java @@ -0,0 +1,35 @@ +package com.usthe.common.util; + +import sun.net.util.IPAddressUtil; + +import java.util.regex.Pattern; + +/** + * ipv4 ipv6 domain 工具类 + * @author tomsun28 + * @date 2021/11/17 19:56 + */ +public class IpDomainUtil { + + /** + * 域名校验正则 + */ + private static final Pattern DOMAIN_PATTERN = + Pattern.compile("^([hH][tT]{2}[pP]://|[hH][tT]{2}[pP][sS]://)?(([A-Za-z0-9-~]+).)+([A-Za-z0-9-~\\/])+$"); + + /** + * 校验判断是否是 ip或者domain + * @param ipDomain ip domain string + * @return true-yes false-no + */ + public static boolean validateIpDomain(String ipDomain) { + if (IPAddressUtil.isIPv4LiteralAddress(ipDomain)) { + return true; + } + if (IPAddressUtil.isIPv6LiteralAddress(ipDomain)) { + return true; + } + return DOMAIN_PATTERN.matcher(ipDomain).matches(); + } + +} diff --git a/manager/src/main/java/com/usthe/manager/pojo/entity/Monitor.java b/manager/src/main/java/com/usthe/manager/pojo/entity/Monitor.java index a4ec83e..6fa90e7 100644 --- a/manager/src/main/java/com/usthe/manager/pojo/entity/Monitor.java +++ b/manager/src/main/java/com/usthe/manager/pojo/entity/Monitor.java @@ -1,5 +1,6 @@ package com.usthe.manager.pojo.entity; +import com.usthe.manager.support.valid.HostValid; import io.swagger.annotations.ApiModel; import io.swagger.annotations.ApiModelProperty; import lombok.AllArgsConstructor; @@ -61,6 +62,7 @@ public class Monitor { */ @ApiModelProperty(value = "监控的对端host", example = "192.167.25.11", accessMode = READ_WRITE, position = 4) @Length(max = 100) + @HostValid private String host; /** diff --git a/manager/src/main/java/com/usthe/manager/support/valid/HostParamValidator.java b/manager/src/main/java/com/usthe/manager/support/valid/HostParamValidator.java new file mode 100644 index 0000000..3c10d47 --- /dev/null +++ b/manager/src/main/java/com/usthe/manager/support/valid/HostParamValidator.java @@ -0,0 +1,20 @@ +package com.usthe.manager.support.valid; + +import com.usthe.common.util.IpDomainUtil; + +import javax.validation.ConstraintValidator; +import javax.validation.ConstraintValidatorContext; + +/** + * host注解数据自定义校验器 + * @author tomsun28 + * @date 2021/11/17 19:44 + */ +public class HostParamValidator implements ConstraintValidator { + + @Override + public boolean isValid(String value, ConstraintValidatorContext context) { + // 判断value是否满足ipv4 ipv5 域名 格式 + return IpDomainUtil.validateIpDomain(value); + } +} diff --git a/manager/src/main/java/com/usthe/manager/support/valid/HostValid.java b/manager/src/main/java/com/usthe/manager/support/valid/HostValid.java new file mode 100644 index 0000000..85ec830 --- /dev/null +++ b/manager/src/main/java/com/usthe/manager/support/valid/HostValid.java @@ -0,0 +1,29 @@ +package com.usthe.manager.support.valid; + +import javax.validation.Constraint; +import javax.validation.Payload; +import java.lang.annotation.Documented; +import java.lang.annotation.Retention; +import java.lang.annotation.Target; + +import static java.lang.annotation.ElementType.FIELD; +import static java.lang.annotation.ElementType.PARAMETER; +import static java.lang.annotation.RetentionPolicy.RUNTIME; + +/** + * host注解数据自定义校验器注解 + * @author tomsun28 + * @date 2021/11/17 19:42 + */ +@Target({ FIELD, PARAMETER }) +@Retention(RUNTIME) +@Documented +@Constraint(validatedBy = HostParamValidator.class) +public @interface HostValid { + + String message() default "Host value is invalid,must ipv4, ipv6 or domain"; + + Class[] groups() default {}; + + Class[] payload() default {}; +} diff --git a/manager/src/main/resources/define/app/A-example.yml b/manager/src/main/resources/define/app/A-example.yml index 5b34421..60abaef 100644 --- a/manager/src/main/resources/define/app/A-example.yml +++ b/manager/src/main/resources/define/app/A-example.yml @@ -11,8 +11,6 @@ configmap: type: 1 - key: password type: 2 - - key: param1 - type: 1 # 指标组列表 metrics: # 第一个监控指标组 cpu diff --git a/manager/src/main/resources/define/param/A-example.yml b/manager/src/main/resources/define/param/A-example.yml index 19b750e..0624f23 100644 --- a/manager/src/main/resources/define/param/A-example.yml +++ b/manager/src/main/resources/define/param/A-example.yml @@ -29,5 +29,6 @@ param: - field: ssl name: 启动SSL type: radio + required: false # 当type为radio单选框,checkbox复选框时,option表示可选项值列表 option: Yes,No \ No newline at end of file