diff --git a/server/src/main/java/org/eclipse/jifa/server/component/CurrentStaticWorker.java b/server/src/main/java/org/eclipse/jifa/server/component/CurrentStaticWorker.java index 1f7e9989..ce2554ca 100644 --- a/server/src/main/java/org/eclipse/jifa/server/component/CurrentStaticWorker.java +++ b/server/src/main/java/org/eclipse/jifa/server/component/CurrentStaticWorker.java @@ -26,6 +26,8 @@ import java.net.InetAddress; import java.time.Duration; import java.time.Instant; +import java.net.*; +import java.util.Enumeration; @StaticWorker @Component @@ -50,8 +52,7 @@ public CurrentStaticWorker(StaticWorkerRepo staticWorkerRepo, @PostConstruct private void init() throws IOException { - InetAddress localHost = InetAddress.getLocalHost(); - String hostAddress = localHost.getHostAddress(); + String hostAddress = getLocalHostExactAddress(); current = this.staticWorkerRepo.findByHostAddress(hostAddress).orElseGet(() -> { StaticWorkerEntity worker = new StaticWorkerEntity(); worker.setHostAddress(hostAddress); @@ -78,4 +79,21 @@ private void updateStorageSpace() throws IOException { current.setTotalSpace(storageService.getTotalSpace()); current = staticWorkerRepo.save(current); } + + private String getLocalHostExactAddress() throws IOException { + Enumeration allNetworkInterfaces = NetworkInterface.getNetworkInterfaces(); + while (allNetworkInterfaces.hasMoreElements()) { + NetworkInterface networkInterface = allNetworkInterfaces.nextElement(); + if (!networkInterface.isLoopback() && !networkInterface.isVirtual() && networkInterface.isUp()) { + Enumeration addresses = networkInterface.getInetAddresses(); + while (addresses.hasMoreElements()) { + InetAddress inetAddress = addresses.nextElement(); + if (inetAddress instanceof Inet4Address) { + return inetAddress.getHostAddress(); + } + } + } + } + return InetAddress.getLocalHost().getHostAddress(); + } }