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: