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 ifname = properties?.interfaceName ?: return
synchronized(this@DefaultNetworkMonitor) {
when (currentNetwork) {
null -> { }
network -> {
val oldProperties = currentLinkProperties!!
check(ifname == oldProperties.interfaceName)
if (properties.dnsServers == oldProperties.dnsServers) return
}
else -> callbacks.forEach { it.onLost() } // we are using the other default network now
}
val oldProperties = currentLinkProperties
if (currentNetwork != network || ifname != oldProperties?.interfaceName) {
callbacks.forEach { it.onLost() } // we are using the other default network now
currentNetwork = network
} else if (properties.dnsServers == oldProperties.dnsServers) return
currentLinkProperties = properties
callbacks.forEach { it.onAvailable(ifname, properties.dnsServers) }
}

View File

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