mirror of
https://github.com/austinried/subtracks.git
synced 2026-11-16 01:00:17 +01:00
net: distinguish unreachable VPN routes from exclusions
Only RTN_THROW continues routing onto the physical LAN. RTN_UNICAST and RTN_UNREACHABLE do not, including for .local names and IPv6-only unreachable routes. Regression tests failed against the previous predicate and now pass.
This commit is contained in:
@@ -37,7 +37,7 @@ class LocalNetworkAccess(
|
||||
vpnRoutes.mayFallThroughToLan(linkAddresses)
|
||||
} else {
|
||||
resolved.any { candidate ->
|
||||
linkAddresses.any { it.contains(candidate) } && !vpnRoutes.routesThroughVpn(candidate)
|
||||
linkAddresses.any { it.contains(candidate) } && vpnRoutes.fallsThroughToLan(candidate)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -65,17 +65,17 @@ internal fun isLocalHostname(host: String): Boolean {
|
||||
private fun LinkAddress.contains(address: InetAddress): Boolean = IpPrefix(this.address, prefixLength).contains(address)
|
||||
|
||||
@RequiresApi(37)
|
||||
private fun List<RouteInfo>.routesThroughVpn(address: InetAddress): Boolean =
|
||||
private fun List<RouteInfo>.fallsThroughToLan(address: InetAddress): Boolean =
|
||||
filter { it.matches(address) }
|
||||
.maxByOrNull { it.destination.prefixLength }
|
||||
?.type == RouteInfo.RTN_UNICAST
|
||||
?.let { it.type == RouteInfo.RTN_THROW } ?: true
|
||||
|
||||
@RequiresApi(37)
|
||||
private fun List<RouteInfo>.mayFallThroughToLan(linkAddresses: List<LinkAddress>): Boolean =
|
||||
any { route ->
|
||||
route.type != RouteInfo.RTN_UNICAST &&
|
||||
route.type == RouteInfo.RTN_THROW &&
|
||||
linkAddresses.any { link -> route.matches(link.address) || link.contains(route.destination.address) }
|
||||
} || linkAddresses.any { !routesThroughVpn(it.address) }
|
||||
} || linkAddresses.any { fallsThroughToLan(it.address) }
|
||||
|
||||
private val NetworkCapabilities.isPhysicalLan: Boolean
|
||||
get() =
|
||||
|
||||
@@ -128,6 +128,28 @@ class LocalNetworkAccessRobolectricTest {
|
||||
assertTrue(checker().requiresPermission("http://music.local"))
|
||||
}
|
||||
|
||||
@Test
|
||||
fun skipsAnIpv6AddressThatIsUnreachableThroughTheVpn() =
|
||||
runTest {
|
||||
activateUnreachableIpv6Vpn()
|
||||
assertFalse(checker().requiresPermission("http://[fd00::5]"))
|
||||
}
|
||||
|
||||
@Test
|
||||
fun skipsDotLocalWhenIpv6IsUnreachableThroughTheVpn() =
|
||||
runTest {
|
||||
activateUnreachableIpv6Vpn()
|
||||
assertFalse(checker().requiresPermission("http://music.local"))
|
||||
}
|
||||
|
||||
private fun activateUnreachableIpv6Vpn() {
|
||||
addWifiNetwork("fd00::2", 64)
|
||||
activateVpn(
|
||||
route("0.0.0.0", 0),
|
||||
route("::", 0, RouteInfo.RTN_UNREACHABLE),
|
||||
)
|
||||
}
|
||||
|
||||
private fun activateWifi() = activate(ConnectivityManager.TYPE_WIFI, NetworkCapabilities.TRANSPORT_WIFI)
|
||||
|
||||
private fun activateVpn(vararg routes: RouteInfo) =
|
||||
@@ -150,10 +172,13 @@ class LocalNetworkAccessRobolectricTest {
|
||||
configure(manager().activeNetwork!!, transport, address, prefix, routes)
|
||||
}
|
||||
|
||||
private fun addWifiNetwork() {
|
||||
private fun addWifiNetwork(
|
||||
address: String = "10.0.0.2",
|
||||
prefix: Int = 24,
|
||||
) {
|
||||
val network = ShadowNetwork.newInstance(7)
|
||||
shadowOf(manager()).addNetwork(network, info(ConnectivityManager.TYPE_WIFI))
|
||||
configure(network, NetworkCapabilities.TRANSPORT_WIFI, "10.0.0.2", 24)
|
||||
configure(network, NetworkCapabilities.TRANSPORT_WIFI, address, prefix)
|
||||
}
|
||||
|
||||
private fun configure(
|
||||
|
||||
Reference in New Issue
Block a user