From 5b835076f0dcba01d3281b932572cc9a008443a7 Mon Sep 17 00:00:00 2001 From: Minjae Gwon Date: Thu, 7 Dec 2023 22:50:14 +0900 Subject: [PATCH] fix: ensure that hooks return non-loopback address --- utils/src/main/scala/Hooks.scala | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/utils/src/main/scala/Hooks.scala b/utils/src/main/scala/Hooks.scala index f4e4020..2700643 100644 --- a/utils/src/main/scala/Hooks.scala +++ b/utils/src/main/scala/Hooks.scala @@ -1,12 +1,22 @@ package kr.ac.postech.paranode.utils -import java.net.InetAddress +import java.net.Inet4Address +import java.net.NetworkInterface import java.net.ServerSocket +import scala.jdk.CollectionConverters._ import scala.reflect.io.Directory import scala.util.Using object Hooks { - def useLocalHostAddress: String = InetAddress.getLocalHost.getHostAddress + def useLocalHostAddress: String = + NetworkInterface.getNetworkInterfaces.asScala + .flatMap(_.getInetAddresses.asScala.toList) + .find(address => + address.isInstanceOf[Inet4Address] && !address.isLoopbackAddress + ) + .get + .getHostAddress + def useAvailablePort: Int = Using(new ServerSocket(0))(_.getLocalPort).get def useTemporaryDirectory: Directory = Directory.makeTemp("paranode") }