Skip to content

Commit 6f25190

Browse files
author
lucaijun
committed
AJ-551: throw error when invalid hostname
1 parent ddadab8 commit 6f25190

File tree

1 file changed

+16
-1
lines changed

1 file changed

+16
-1
lines changed

src/com/xxdb/SimpleDBConnectionPoolConfig.java

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import org.slf4j.Logger;
44
import org.slf4j.LoggerFactory;
5+
import sun.net.util.IPAddressUtil;
56

67
import java.util.Objects;
78

@@ -129,9 +130,11 @@ public void validate() {
129130
hostName = "localhost";
130131
log.warn("HostName not set, use the default value 'localhost'");
131132
}
133+
if (checkHostNameValid(hostName))
134+
throw new RuntimeException(String.format("Invalid hostName: %s", hostName));
132135
if (port <= 0) {
133136
port = 8848;
134-
log.warn("invalid port, use the default value 8848.");
137+
log.warn("Invalid port, use the default value 8848.");
135138
}
136139
userId = getNullIfEmpty(userId);
137140
if (Objects.isNull(userId))
@@ -148,6 +151,18 @@ public void validate() {
148151
private static String getNullIfEmpty(String text) {
149152
return text == null ? null : (text.trim().isEmpty() ? null : text.trim());
150153
}
154+
155+
private static boolean checkHostNameValid(String hostName) {
156+
return hostName.equals("localhost") ||
157+
IPAddressUtil.isIPv4LiteralAddress(hostName) ||
158+
IPAddressUtil.isIPv6LiteralAddress(hostName) ||
159+
isDomain(hostName);
160+
}
161+
162+
private static boolean isDomain(String hostName) {
163+
String regex = "^[a-zA-Z0-9]([a-zA-Z0-9\\-]{0,61}[a-zA-Z0-9])?(\\.[a-zA-Z0-9]([a-zA-Z0-9\\-]{0,61}[a-zA-Z0-9])?)*$";
164+
return hostName.matches(regex);
165+
}
151166
}
152167

153168

0 commit comments

Comments
 (0)