2
2
3
3
import org .slf4j .Logger ;
4
4
import org .slf4j .LoggerFactory ;
5
+ import sun .net .util .IPAddressUtil ;
5
6
6
7
import java .util .Objects ;
7
8
@@ -129,9 +130,11 @@ public void validate() {
129
130
hostName = "localhost" ;
130
131
log .warn ("HostName not set, use the default value 'localhost'" );
131
132
}
133
+ if (checkHostNameValid (hostName ))
134
+ throw new RuntimeException (String .format ("Invalid hostName: %s" , hostName ));
132
135
if (port <= 0 ) {
133
136
port = 8848 ;
134
- log .warn ("invalid port, use the default value 8848." );
137
+ log .warn ("Invalid port, use the default value 8848." );
135
138
}
136
139
userId = getNullIfEmpty (userId );
137
140
if (Objects .isNull (userId ))
@@ -148,6 +151,18 @@ public void validate() {
148
151
private static String getNullIfEmpty (String text ) {
149
152
return text == null ? null : (text .trim ().isEmpty () ? null : text .trim ());
150
153
}
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
+ }
151
166
}
152
167
153
168
0 commit comments