Skip to content

Commit e96da7f

Browse files
committed
[bugfix] Made local loop detection more reliable
1 parent 471656b commit e96da7f

File tree

1 file changed

+21
-23
lines changed

1 file changed

+21
-23
lines changed

exist-core/src/main/java/org/exist/management/client/JMXServlet.java

Lines changed: 21 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -26,16 +26,12 @@
2626
import java.io.OutputStream;
2727
import java.io.OutputStreamWriter;
2828
import java.io.Writer;
29-
import java.net.Inet6Address;
30-
import java.net.InetAddress;
31-
import java.net.UnknownHostException;
29+
import java.net.*;
3230
import java.nio.charset.StandardCharsets;
3331
import java.nio.file.Files;
3432
import java.nio.file.Path;
3533
import java.nio.file.Paths;
36-
import java.util.HashSet;
37-
import java.util.Properties;
38-
import java.util.Set;
34+
import java.util.*;
3935
import javax.management.*;
4036
import jakarta.servlet.ServletConfig;
4137
import jakarta.servlet.ServletException;
@@ -218,32 +214,34 @@ public void init(ServletConfig config) throws ServletException {
218214
* Register all known IP-addresses for localhost.
219215
*/
220216
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-
}
227217

228-
// The configured Localhost addresses
229218
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+
}
232231
}
233-
} catch (UnknownHostException ex) {
234-
LOG.warn("Unable to retrieve ipaddresses (v4) for localhost: {}", ex.getMessage());
235232
}
236233

237-
try {
238-
for (InetAddress address : Inet6Address.getAllByName("localhost") ){
239-
localhostAddresses.add(address.getHostAddress());
240234
}
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);
243239
}
244240

245241
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);
247245
}
248246
}
249247

0 commit comments

Comments
 (0)