|
26 | 26 | import java.io.OutputStream;
|
27 | 27 | import java.io.OutputStreamWriter;
|
28 | 28 | import java.io.Writer;
|
29 |
| -import java.net.Inet6Address; |
30 |
| -import java.net.InetAddress; |
31 |
| -import java.net.UnknownHostException; |
| 29 | +import java.net.*; |
32 | 30 | import java.nio.charset.StandardCharsets;
|
33 | 31 | import java.nio.file.Files;
|
34 | 32 | import java.nio.file.Path;
|
35 | 33 | import java.nio.file.Paths;
|
36 |
| -import java.util.HashSet; |
37 |
| -import java.util.Properties; |
38 |
| -import java.util.Set; |
| 34 | +import java.util.*; |
39 | 35 | import javax.management.*;
|
40 | 36 | import jakarta.servlet.ServletConfig;
|
41 | 37 | import jakarta.servlet.ServletException;
|
@@ -218,32 +214,34 @@ public void init(ServletConfig config) throws ServletException {
|
218 | 214 | * Register all known IP-addresses for localhost.
|
219 | 215 | */
|
220 | 216 | void registerLocalHostAddresses() {
|
221 |
| - // The external IP address of the server |
222 |
| - try { |
223 |
| - localhostAddresses.add(InetAddress.getLocalHost().getHostAddress()); |
224 |
| - } catch (UnknownHostException ex) { |
225 |
| - LOG.warn("Unable to get HostAddress for localhost: {}", ex.getMessage()); |
226 |
| - } |
227 | 217 |
|
228 |
| - // The configured Localhost addresses |
229 | 218 | try {
|
230 |
| - for (InetAddress address : InetAddress.getAllByName("localhost")) { |
231 |
| - localhostAddresses.add(address.getHostAddress()); |
| 219 | + for (final NetworkInterface networkInterface : NetworkInterface.networkInterfaces().toList()) { |
| 220 | + if (networkInterface.isLoopback()) { |
| 221 | + final Enumeration<InetAddress> inetAddresses = networkInterface.getInetAddresses(); |
| 222 | + while (inetAddresses.hasMoreElements()) { |
| 223 | + final String hostAddress = inetAddresses.nextElement().getHostAddress(); |
| 224 | + if (hostAddress != null) { |
| 225 | + if (hostAddress.contains("%")) { |
| 226 | + final int position = hostAddress.indexOf("%"); |
| 227 | + localhostAddresses.add(hostAddress.substring(0, position)); |
| 228 | + } else { |
| 229 | + localhostAddresses.add(hostAddress); |
| 230 | + } |
232 | 231 | }
|
233 |
| - } catch (UnknownHostException ex) { |
234 |
| - LOG.warn("Unable to retrieve ipaddresses (v4) for localhost: {}", ex.getMessage()); |
235 | 232 | }
|
236 | 233 |
|
237 |
| - try { |
238 |
| - for (InetAddress address : Inet6Address.getAllByName("localhost") ){ |
239 |
| - localhostAddresses.add(address.getHostAddress()); |
240 | 234 | }
|
241 |
| - } catch (UnknownHostException ex) { |
242 |
| - LOG.warn("Unable to retrieve ipaddresses (v6) for localhost: {}", ex.getMessage()); |
| 235 | + } |
| 236 | + |
| 237 | + } catch (final SocketException e) { |
| 238 | + LOG.error("Unable to determine localhost loopback addresses: {} ", e.getMessage(), e); |
243 | 239 | }
|
244 | 240 |
|
245 | 241 | if (localhostAddresses.isEmpty()) {
|
246 |
| - LOG.error("Unable to determine addresses for localhost, jmx servlet might be disfunctional."); |
| 242 | + LOG.error("Unable to determine addresses for localhost, jmx servlet might be dysfunctional."); |
| 243 | + } else { |
| 244 | + LOG.info("Detected loopback addresses: {}", localhostAddresses); |
247 | 245 | }
|
248 | 246 | }
|
249 | 247 |
|
|
0 commit comments