fix(network): handle gatewayless IPv4 VPN links

Provide compatible gateway metadata for IPv4 VPN links without changing
VPN routing, and keep text and JSON network snapshots consistent.

Add regression tests for gateway selection and network snapshots.
This commit is contained in:
uubooster-team authored and Kurt Himebauch committed 2026-11-05 11:43:30 -05:00
1 parent ef305c93cd
commit e1fe6f99cf
3 files changed
+197 -11

No files matched your search

@@ -9,6 +9,7 @@ import android.net.ConnectivityManager
import android.net.LinkProperties
import android.net.Network
import android.net.NetworkCapabilities
import android.net.RouteInfo
import android.net.wifi.WifiInfo
import android.net.wifi.WifiManager
import android.os.Build
@@ -23,6 +24,7 @@ import java.io.File
import java.io.IOException
import java.net.Inet4Address
import java.net.Inet6Address
import java.net.InetAddress
import org.json.JSONArray
import org.json.JSONObject
@@ -156,7 +158,7 @@ class LinuxNetworkLinkComponent(
try {
file.parentFile?.mkdirs()
val staged = File(file.path + ".staged")
staged.writeText(describe(properties))
staged.writeText(describe(properties, capabilities))
if (!staged.renameTo(file)) throw IOException("Could not replace $file")
} catch (e: IOException) {
Log.w(TAG, "Could not publish the network link", e)
@@ -210,11 +212,7 @@ class LinuxNetworkLinkComponent(
}
})
put("gateways", JSONArray().apply {
properties?.routes.orEmpty().filter { it.isDefaultRoute }.forEach {
it.gateway?.takeUnless { address -> address.isAnyLocalAddress }?.let { address ->
put(address.hostAddress?.substringBefore('%'))
}
}
gateways(properties, capabilities).forEach { put(it.hostAddress?.substringBefore('%')) }
})
put("dns", JSONArray().apply {
properties?.dnsServers.orEmpty().filter { !it.isLinkLocalAddress }.forEach {
@@ -301,7 +299,7 @@ class LinuxNetworkLinkComponent(
}
}
private fun describe(properties: LinkProperties?): String {
internal fun describe(properties: LinkProperties?, capabilities: NetworkCapabilities?): String {
val addresses = properties?.linkAddresses.orEmpty()
.filter { it.address is Inet4Address || it.address is Inet6Address }
val name = properties?.interfaceName?.takeIf { addresses.isNotEmpty() } ?: OFFLINE_NAME
@@ -316,10 +314,7 @@ class LinuxNetworkLinkComponent(
for (address in addresses) {
append("addr ${address.address.hostAddress?.substringBefore('%')} ${address.prefixLength}\n")
}
for (family in listOf(Inet4Address::class.java, Inet6Address::class.java)) {
val gateway = properties?.routes.orEmpty()
.firstOrNull { it.isDefaultRoute && family.isInstance(it.gateway) && !it.gateway!!.isAnyLocalAddress }
?.gateway ?: continue
for (gateway in gateways(properties, capabilities)) {
append("gw ${gateway.hostAddress?.substringBefore('%')}\n")
}
}
@@ -329,6 +324,27 @@ class LinuxNetworkLinkComponent(
seededMac { Settings.Secure.getString(appContext.contentResolver, Settings.Secure.ANDROID_ID) }
companion object {
internal fun gateways(properties: LinkProperties?, capabilities: NetworkCapabilities?): List<InetAddress> =
listOf(Inet4Address::class.java, Inet6Address::class.java).mapNotNull { family ->
properties?.routes.orEmpty()
.firstOrNull { it.isDefaultRoute && family.isInstance(it.gateway) && !it.gateway!!.isAnyLocalAddress }
?.gateway
?: if (family == Inet4Address::class.java &&
capabilities?.hasTransport(NetworkCapabilities.TRANSPORT_VPN) == true &&
capabilities.hasCapability(NetworkCapabilities.NET_CAPABILITY_INTERNET) &&
properties?.interfaceName != null &&
properties.routes.any {
it.destination.address is Inet4Address &&
(Build.VERSION.SDK_INT < Build.VERSION_CODES.TIRAMISU || it.type == RouteInfo.RTN_UNICAST)
}
) {
properties.linkAddresses.firstOrNull {
it.address is Inet4Address && !it.address.isAnyLocalAddress &&
!it.address.isLoopbackAddress && !it.address.isLinkLocalAddress
}?.address
} else null
}
internal fun seededMac(readId: () -> String?): String {
val id = runCatching(readId).getOrNull() ?: "bannerlator"
val bytes = java.security.MessageDigest.getInstance("SHA-1").digest(id.toByteArray())