guessing the default host address when no interface name provided
This commit is contained in:
parent
4e64f4add4
commit
66cfe2a6ad
1 changed files with 16 additions and 7 deletions
|
@ -20,14 +20,13 @@ class ServerInfoProviderImpl(
|
|||
@param:Value("\${server.interface}") private val networkInterface: String
|
||||
) : ServerInfoProvider {
|
||||
override val host: String get() = address.hostAddress
|
||||
val address: InetAddress = guessInetAddress()
|
||||
private val address: InetAddress = getInetAddress()
|
||||
|
||||
@PostConstruct
|
||||
fun init() {
|
||||
logger.info("Using server address: {} and port {}", address.hostAddress, port)
|
||||
init {
|
||||
logger.info("Using server address: ${address.hostAddress} and port $port")
|
||||
}
|
||||
|
||||
private fun guessInetAddress(): InetAddress {
|
||||
private fun getInetAddress(): InetAddress {
|
||||
try {
|
||||
return if (networkInterface.isNotEmpty()) {
|
||||
logger.debug { "Using network interface $networkInterface" }
|
||||
|
@ -36,8 +35,8 @@ class ServerInfoProviderImpl(
|
|||
|
||||
iface.inetAddresses.toList().filterIsInstance<Inet4Address>().first()
|
||||
} else {
|
||||
logger.info { "No network interface given, using default local address" }
|
||||
InetAddress.getLocalHost()
|
||||
logger.info { "No network interface name given, trying to use default local address" }
|
||||
guessLocalAddress()
|
||||
}.also {
|
||||
logger.debug { "Found local address ${it.hostAddress}" }
|
||||
}
|
||||
|
@ -48,5 +47,15 @@ class ServerInfoProviderImpl(
|
|||
}
|
||||
}
|
||||
|
||||
private fun guessLocalAddress() = try {
|
||||
DatagramSocket().use { s ->
|
||||
s.connect(InetAddress.getByAddress(byteArrayOf(1, 1, 1, 1)), 80)
|
||||
s.localAddress
|
||||
}
|
||||
} catch (e: Exception) {
|
||||
logger.warn { e.message }
|
||||
InetAddress.getLocalHost()
|
||||
}
|
||||
|
||||
companion object : KLogging()
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue