Removed WILDCARD_SAFE (it's not really wildcard, and it's not really safe)

connection_type_change
Robinson 2021-04-26 23:46:33 +02:00
parent 0a1ef35489
commit 540d134762
2 changed files with 2 additions and 31 deletions

View File

@ -102,35 +102,6 @@ object IPv4 {
InetAddress.getByAddress(null, byteArrayOf(0, 0, 0, 0)) as Inet4Address
}
/**
* Windows is unable to work with 0.0.0.0 directly, and if you use LOOPBACK, you might not be able to access the server from another
* machine.
*
* What this does is open a connection to 1.1.1.1 and see get the interface this traffic was on, and use that interface IP address
*/
val WILDCARD_SAFE: Inet4Address by lazy {
if (Common.OS_WINDOWS) {
// silly windows can't work with 0.0.0.0, BUT we can't use loopback because we might need to reach this machine from a different host
// what we do is open a connection to 1.1.1.1 and see what interface this happened on, and this is used as the accessible
// interface
var ip = WILDCARD
runCatching {
Socket().use {
it.connect(InetSocketAddress("1.1.1.1", 80))
ip = it.localAddress as Inet4Address
}
}.onFailure {
Common.logger.error("Unable to determine outbound traffic local address. Using loopback instead.", it)
}
ip
} else {
// everyone else works correctly
WILDCARD
}
}
/**
* the length of an address in this particular family.
*/

View File

@ -1,7 +1,7 @@
package dorkbox.netUtil.ping
import dorkbox.netUtil.Common
import dorkbox.netUtil.IPv4
import dorkbox.netUtil.IP
import java.time.Duration
import java.time.temporal.ChronoUnit
import java.util.regex.Matcher
@ -58,7 +58,7 @@ internal object PingResultBuilder {
listOf(
/* Windows */
ResultParser.of("Pinging (.*) with") { result, matcher ->
result.host = IPv4.WILDCARD_SAFE.hostAddress // note: this is REALLY the host used for default traffic
result.host = IP.lanAddress().hostAddress // note: this is REALLY the host used for lan traffic
result.ip = matcher.group(1)
result
},