From 66d74acbfcc8a32a48bc3809fc114ecb82ea954e Mon Sep 17 00:00:00 2001 From: xis Date: Mon, 23 Oct 2023 20:56:22 +0200 Subject: [PATCH] Default network interface used --- .../nextclouddlna/util/ServerInfoProvider.kt | 29 ++++++++++--------- src/main/resources/application.yml | 2 +- 2 files changed, 16 insertions(+), 15 deletions(-) diff --git a/src/main/kotlin/net/schowek/nextclouddlna/util/ServerInfoProvider.kt b/src/main/kotlin/net/schowek/nextclouddlna/util/ServerInfoProvider.kt index 18c155a..7cba39d 100644 --- a/src/main/kotlin/net/schowek/nextclouddlna/util/ServerInfoProvider.kt +++ b/src/main/kotlin/net/schowek/nextclouddlna/util/ServerInfoProvider.kt @@ -19,27 +19,28 @@ class ServerInfoProviderImpl( @param:Value("\${server.port}") override val port: Int, @param:Value("\${server.interface}") private val networkInterface: String ) : ServerInfoProvider { - override val host: String get() = address!!.hostAddress - var address: InetAddress? = null + override val host: String get() = address.hostAddress + val address: InetAddress = guessInetAddress() @PostConstruct fun init() { - address = guessInetAddress() - logger.info("Using server address: {} and port {}", address!!.hostAddress, port) + logger.info("Using server address: {} and port {}", address.hostAddress, port) } private fun guessInetAddress(): InetAddress { - return try { - val iface = NetworkInterface.getByName(networkInterface) - ?: throw RuntimeException("Could not find network interface $networkInterface") - val addresses = iface.inetAddresses - while (addresses.hasMoreElements()) { - val x = addresses.nextElement() - if (x is Inet4Address) { - return x - } + try { + return if (networkInterface.isNotEmpty()) { + logger.debug { "Using network interface $networkInterface" } + val iface = NetworkInterface.getByName(networkInterface) + ?: throw RuntimeException("Could not find network interface $networkInterface") + + iface.inetAddresses.toList().filterIsInstance().first() + } else { + logger.info { "No network interface given, using default local address" } + InetAddress.getLocalHost() + }.also { + logger.debug { "Found local address ${it.hostAddress}" } } - InetAddress.getLocalHost() } catch (e: UnknownHostException) { throw RuntimeException(e) } catch (e: SocketException) { diff --git a/src/main/resources/application.yml b/src/main/resources/application.yml index 978edef..f116b70 100644 --- a/src/main/resources/application.yml +++ b/src/main/resources/application.yml @@ -1,6 +1,6 @@ server: port: ${NEXTCLOUD_DLNA_SERVER_PORT:8080} - interface: ${NEXTCLOUD_DLNA_INTERFACE:eth0} + interface: ${NEXTCLOUD_DLNA_INTERFACE:} friendlyName: ${NEXTCLOUD_DLNA_FRIENDLY_NAME:Nextcloud-DLNA} nextcloud: