Refine onAvailable handling

Do not assume interfaceName unchanging.
This commit is contained in:
Mygod
2019-08-02 13:41:09 +08:00
parent 3327ba300e
commit f8766fde59
2 changed files with 8 additions and 20 deletions

View File

@@ -24,16 +24,11 @@ object DefaultNetworkMonitor : UpstreamMonitor() {
val properties = app.connectivity.getLinkProperties(network) val properties = app.connectivity.getLinkProperties(network)
val ifname = properties?.interfaceName ?: return val ifname = properties?.interfaceName ?: return
synchronized(this@DefaultNetworkMonitor) { synchronized(this@DefaultNetworkMonitor) {
when (currentNetwork) { val oldProperties = currentLinkProperties
null -> { } if (currentNetwork != network || ifname != oldProperties?.interfaceName) {
network -> { callbacks.forEach { it.onLost() } // we are using the other default network now
val oldProperties = currentLinkProperties!! currentNetwork = network
check(ifname == oldProperties.interfaceName) } else if (properties.dnsServers == oldProperties.dnsServers) return
if (properties.dnsServers == oldProperties.dnsServers) return
}
else -> callbacks.forEach { it.onLost() } // we are using the other default network now
}
currentNetwork = network
currentLinkProperties = properties currentLinkProperties = properties
callbacks.forEach { it.onAvailable(ifname, properties.dnsServers) } callbacks.forEach { it.onAvailable(ifname, properties.dnsServers) }
} }

View File

@@ -25,18 +25,11 @@ object VpnMonitor : UpstreamMonitor() {
val properties = app.connectivity.getLinkProperties(network) val properties = app.connectivity.getLinkProperties(network)
val ifname = properties?.interfaceName ?: return val ifname = properties?.interfaceName ?: return
synchronized(this@VpnMonitor) { synchronized(this@VpnMonitor) {
val old = currentNetwork
val oldProperties = available.put(network, properties) val oldProperties = available.put(network, properties)
if (old != network) { if (currentNetwork != network || ifname != oldProperties?.interfaceName) {
if (old != null) { if (currentNetwork != null) callbacks.forEach { it.onLost() }
DebugHelper.log(TAG, "Assuming old VPN interface ${available[old]} is dying")
callbacks.forEach { it.onLost() }
}
currentNetwork = network currentNetwork = network
} else { } else if (properties.dnsServers == oldProperties.dnsServers) return
check(ifname == oldProperties!!.interfaceName)
if (properties.dnsServers == oldProperties.dnsServers) return
}
callbacks.forEach { it.onAvailable(ifname, properties.dnsServers) } callbacks.forEach { it.onAvailable(ifname, properties.dnsServers) }
} }
} }