Skip to content

Commit

Permalink
monitoring param host with http head will not be error reported (#1155)
Browse files Browse the repository at this point in the history
Co-authored-by: zhongcheng <zhongcheng@winning.com.cn>
  • Loading branch information
littlezhongzer and zhongcheng authored Aug 8, 2023
1 parent 50f7418 commit 25ebf59
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,21 @@
*
*/
public class HostParamValidator implements ConstraintValidator<HostValid, String> {
public static final String HTTP = "http://";
public static final String HTTPS = "https://";
public static final String BLANK = "";
public static final String PATTERN_HTTP = "(?i)http://";
public static final String PATTERN_HTTPS = "(?i)https://";

@Override
public boolean isValid(String value, ConstraintValidatorContext context) {
// 判断value是否满足ipv4 ipv5 域名 格式
if(value != null && value.toLowerCase().contains(HTTP)){
value = value.replaceAll(PATTERN_HTTP, BLANK);
}
if(value != null && value.toLowerCase().contains(HTTPS)){
value = value.replace(PATTERN_HTTPS, BLANK);
}
return IpDomainUtil.validateIpDomain(value);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
@Constraint(validatedBy = HostParamValidator.class)
public @interface HostValid {

String message() default "监控Host必须是ipv4,ipv6或域名";
String message() default "监控Host必须是ipv4,ipv6或域名,<br>EX:127.0.0.1 或者 hertzbeat.com";

Class<?>[] groups() default {};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,12 @@
public class MonitorServiceImpl implements MonitorService {
private static final Long MONITOR_ID_TMP = 1000000000L;

public static final String HTTP = "http://";
public static final String HTTPS = "https://";
public static final String BLANK = "";
public static final String PATTERN_HTTP = "(?i)http://";
public static final String PATTERN_HTTPS = "(?i)https://";

private static final Gson GSON = new Gson();

@Autowired
Expand Down Expand Up @@ -383,6 +389,12 @@ public void validate(MonitorDto monitorDto, Boolean isModify) throws IllegalArgu
break;
case "host":
String hostValue = param.getValue();
if(hostValue.toLowerCase().contains(HTTP)){
hostValue = hostValue.replaceAll(PATTERN_HTTP, BLANK);
}
if(hostValue.toLowerCase().contains(HTTPS)){
hostValue = hostValue.replace(PATTERN_HTTPS, BLANK);
}
if (!IpDomainUtil.validateIpDomain(hostValue)) {
throw new IllegalArgumentException("Params field " + field + " value "
+ hostValue + " is invalid host value.");
Expand Down

0 comments on commit 25ebf59

Please sign in to comment.