1
1
package com .xxdb ;
2
2
3
+ import org .apache .commons .validator .routines .InetAddressValidator ;
3
4
import org .slf4j .Logger ;
4
5
import org .slf4j .LoggerFactory ;
5
6
import sun .net .util .IPAddressUtil ;
@@ -130,7 +131,7 @@ public void validate() {
130
131
hostName = "localhost" ;
131
132
log .warn ("HostName not set, use the default value 'localhost'" );
132
133
}
133
- if (checkHostNameValid (hostName ))
134
+ if (! checkHostNameValid (hostName ))
134
135
throw new RuntimeException (String .format ("Invalid hostName: %s" , hostName ));
135
136
if (port <= 0 ) {
136
137
port = 8848 ;
@@ -154,15 +155,37 @@ private static String getNullIfEmpty(String text) {
154
155
155
156
private static boolean checkHostNameValid (String hostName ) {
156
157
return hostName .equals ("localhost" ) ||
157
- IPAddressUtil . isIPv4LiteralAddress (hostName ) ||
158
+ isIPV4 (hostName ) ||
158
159
IPAddressUtil .isIPv6LiteralAddress (hostName ) ||
159
160
isDomain (hostName );
160
161
}
161
162
162
163
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
+ String regex = "^( [a-z0-9]+(- [a-z0-9]+)* \\ .)+ [a-z]{2,} $" ;
164
165
return hostName .matches (regex );
165
166
}
167
+
168
+ private static boolean isIPV4 (String hostName ) {
169
+ String regex = "^(\\ d{1,3})\\ .(\\ d{1,3})\\ .(\\ d{1,3})\\ .(\\ d{1,3})$" ;
170
+ if (hostName == null || hostName .trim ().isEmpty ()) {
171
+ return false ;
172
+ }
173
+ if (!hostName .matches (regex )) {
174
+ return false ;
175
+ }
176
+ String [] parts = hostName .split ("\\ ." );
177
+ try {
178
+ for (String segment : parts ) {
179
+ if (Integer .parseInt (segment ) > 255 ||
180
+ (segment .length () > 1 && segment .startsWith ("0" ))) {
181
+ return false ;
182
+ }
183
+ }
184
+ } catch (NumberFormatException e ) {
185
+ return false ;
186
+ }
187
+ return true ;
188
+ }
166
189
}
167
190
168
191
0 commit comments