From 2cb387892afd416778776784b85ed47b5ea532a3 Mon Sep 17 00:00:00 2001 From: huangyonghui Date: Fri, 19 Apr 2019 11:52:15 +0800 Subject: [PATCH 1/3] to support registering instance with host name --- .../main/java/com/alibaba/nacos/naming/core/Instance.java | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/naming/src/main/java/com/alibaba/nacos/naming/core/Instance.java b/naming/src/main/java/com/alibaba/nacos/naming/core/Instance.java index 6f9679c4ded..0cec5d23cff 100644 --- a/naming/src/main/java/com/alibaba/nacos/naming/core/Instance.java +++ b/naming/src/main/java/com/alibaba/nacos/naming/core/Instance.java @@ -297,9 +297,11 @@ public String generateInstanceId() { public boolean validate() { - Matcher matcher = IP_PATTERN.matcher(getIp() + ":" + getPort()); - if (!matcher.matches()) { - return false; + if (getIp().contains(UtilsAndCommons.IP_PORT_SPLITER)) { + Matcher matcher = IP_PATTERN.matcher(getIp() + ":" + getPort()); + if (!matcher.matches()) { + return false; + } } if (getWeight() > MAX_WEIGHT_VALUE || getWeight() < MIN_WEIGHT_VALUE) { From e6f82a940e6e20d507779fad97e8d343b95736a7 Mon Sep 17 00:00:00 2001 From: Albert-Hugo Date: Sat, 20 Apr 2019 10:35:01 +0800 Subject: [PATCH 2/3] remove useless validate code --- .../main/java/com/alibaba/nacos/naming/core/Instance.java | 7 ------- 1 file changed, 7 deletions(-) diff --git a/naming/src/main/java/com/alibaba/nacos/naming/core/Instance.java b/naming/src/main/java/com/alibaba/nacos/naming/core/Instance.java index 0cec5d23cff..1941959fc0d 100644 --- a/naming/src/main/java/com/alibaba/nacos/naming/core/Instance.java +++ b/naming/src/main/java/com/alibaba/nacos/naming/core/Instance.java @@ -297,13 +297,6 @@ public String generateInstanceId() { public boolean validate() { - if (getIp().contains(UtilsAndCommons.IP_PORT_SPLITER)) { - Matcher matcher = IP_PATTERN.matcher(getIp() + ":" + getPort()); - if (!matcher.matches()) { - return false; - } - } - if (getWeight() > MAX_WEIGHT_VALUE || getWeight() < MIN_WEIGHT_VALUE) { return false; } From b67956f0b45eef257b2aad0f2100441575223a2b Mon Sep 17 00:00:00 2001 From: huangyonghui Date: Tue, 23 Apr 2019 10:28:36 +0800 Subject: [PATCH 3/3] add only digit and dot char for ip validation --- .../com/alibaba/nacos/naming/core/Instance.java | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/naming/src/main/java/com/alibaba/nacos/naming/core/Instance.java b/naming/src/main/java/com/alibaba/nacos/naming/core/Instance.java index 1941959fc0d..c87294fdf86 100644 --- a/naming/src/main/java/com/alibaba/nacos/naming/core/Instance.java +++ b/naming/src/main/java/com/alibaba/nacos/naming/core/Instance.java @@ -51,6 +51,9 @@ public class Instance extends com.alibaba.nacos.api.naming.pojo.Instance impleme public static final Pattern IP_PATTERN = Pattern.compile("(\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}):?(\\d{1,5})?"); + public static final Pattern ONLY_DIGIT_AND_DOT + = Pattern.compile("(\\d|\\.)+"); + public static final String SPLITER = "_"; public Instance() { @@ -296,6 +299,12 @@ public String generateInstanceId() { } public boolean validate() { + if (onlyContainsDigitAndDot()) { + Matcher matcher = IP_PATTERN.matcher(getIp() + ":" + getPort()); + if (!matcher.matches()) { + return false; + } + } if (getWeight() > MAX_WEIGHT_VALUE || getWeight() < MIN_WEIGHT_VALUE) { return false; @@ -304,6 +313,11 @@ public boolean validate() { return true; } + private boolean onlyContainsDigitAndDot() { + Matcher matcher = ONLY_DIGIT_AND_DOT.matcher(getIp()); + return matcher.matches(); + } + @Override public int compareTo(Object o) { if (!(o instanceof Instance)) {