Added proper WILDCARD addresses. Moved previous IPv4 WILDCARD -> WILDCARD_SAFE

connection_type_change
nathan 2020-09-08 23:54:16 +02:00
parent 89141a11f9
commit 092ad76820
4 changed files with 29 additions and 14 deletions

View File

@ -51,28 +51,35 @@ object IPv4 {
* The [Inet4Address] that represents the IPv4 loopback address '127.0.0.1'
*/
val LOCALHOST: Inet4Address by lazy {
// Create IPv4 loopback address.
// this will ALWAYS work
// Create IPv4 address, this will ALWAYS work
InetAddress.getByAddress("localhost", byteArrayOf(127, 0, 0, 1)) as Inet4Address
}
/**
* The [Inet4Address] that represents the IPv4 wildcard address '0.0.0.0'
*/
val WILDCARD: Inet4Address by lazy {
// Create IPv4 address, this will ALWAYS work
InetAddress.getByAddress("", 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: String by lazy {
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 = "127.0.0.1"
var ip = WILDCARD
runCatching {
Socket().use {
it.connect(InetSocketAddress("1.1.1.1", 80))
ip = it.localAddress.hostAddress
ip = it.localAddress as Inet4Address
}
}.onFailure {
Common.logger.error("Unable to determine outbound traffic local address. Using loopback instead.", it)
@ -81,7 +88,7 @@ object IPv4 {
ip
} else {
// everyone else works correctly
"0.0.0.0"
WILDCARD
}
}
@ -672,8 +679,8 @@ object IPv4 {
*
* @return `String` containing the text-formatted IP address
*/
fun toString(ip: InetAddress): String {
return (ip as Inet4Address).hostAddress
fun toString(ip: Inet4Address): String {
return ip.hostAddress
}

View File

@ -17,6 +17,7 @@
package dorkbox.netUtil
import java.net.Inet4Address
import java.net.Inet6Address
import java.net.InetAddress
import java.net.UnknownHostException
@ -85,11 +86,18 @@ object IPv6 {
* The [Inet6Address] that represents the IPv6 loopback address '::1'
*/
val LOCALHOST: Inet6Address by lazy {
// Create IPv6 loopback address.
// should never fail
// Create IPv6 address, this will ALWAYS work
InetAddress.getByAddress("localhost", byteArrayOf(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1)) as Inet6Address
}
/**
* The [Inet4Address] that represents the IPv4 wildcard address '0.0.0.0'
*/
val WILDCARD: Inet6Address by lazy {
// Create IPv6 address, this will ALWAYS work
InetAddress.getByAddress("", byteArrayOf(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0)) as Inet6Address
}
/**
* Takes a [String] and parses it to see if it is a valid IPV6 address.
*
@ -568,7 +576,7 @@ object IPv6 {
*
* @return `String` containing the text-formatted IP address
*/
fun toString(ip: InetAddress, ipv4Mapped: Boolean = false): String {
fun toString(ip: Inet6Address, ipv4Mapped: Boolean = false): String {
return toAddressString(ip.address, 0, ipv4Mapped)
}

View File

@ -58,7 +58,7 @@ internal object PingResultBuilder {
listOf(
/* Windows */
ResultParser.of("Pinging (.*) with") { result, matcher ->
result.host = IPv4.WILDCARD // note: this is REALLY the host used for default traffic
result.host = IPv4.WILDCARD_SAFE.hostAddress // note: this is REALLY the host used for default traffic
result.ip = matcher.group(1)
result
},

View File

@ -681,7 +681,7 @@ class NetUtilTest {
@Throws(UnknownHostException::class)
fun testIp6AddressToString() {
for ((key, value) in ipv6ToAddressStrings) {
assertEquals(value, IPv6.toString(InetAddress.getByAddress(key)))
assertEquals(value, IP.toString(InetAddress.getByAddress(key)))
}
}
@ -689,7 +689,7 @@ class NetUtilTest {
@Throws(UnknownHostException::class)
fun testIp4AddressToString() {
for ((key, value) in validIpV4Hosts) {
assertEquals(key, IPv4.toString(InetAddress.getByAddress(unhex(value))))
assertEquals(key, IP.toString(InetAddress.getByAddress(unhex(value))))
}
}