Notify all LinkProperties for onAvailable

This commit is contained in:
Mygod
2020-01-20 09:19:10 +08:00
parent 006e4e47c0
commit 59b4a464b0
5 changed files with 19 additions and 22 deletions

View File

@@ -1,6 +1,7 @@
package be.mygod.vpnhotspot.net
import android.annotation.TargetApi
import android.net.LinkProperties
import android.os.Build
import androidx.annotation.RequiresApi
import be.mygod.vpnhotspot.App.Companion.app
@@ -163,7 +164,7 @@ class Routing(private val caller: Any, private val downstream: String) : IpNeigh
var subrouting: Subrouting? = null
var dns: List<InetAddress> = emptyList()
override fun onAvailable(ifname: String, dns: List<InetAddress>) = synchronized(this@Routing) {
override fun onAvailable(ifname: String, properties: LinkProperties) = synchronized(this@Routing) {
val subrouting = subrouting
when {
subrouting != null -> check(subrouting.upstream == ifname) { "${subrouting.upstream} != $ifname" }
@@ -176,7 +177,7 @@ class Routing(private val caller: Any, private val downstream: String) : IpNeigh
null
}
}
this.dns = dns
dns = properties.dnsServers
updateDnsRoute()
}

View File

@@ -28,9 +28,9 @@ object DefaultNetworkMonitor : UpstreamMonitor() {
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) }
callbacks.forEach { it.onAvailable(ifname, properties) }
}
}
@@ -53,12 +53,10 @@ object DefaultNetworkMonitor : UpstreamMonitor() {
Timber.w(RuntimeException("interfaceName changed: $oldProperties -> $properties"))
callbacks.forEach {
it.onLost()
it.onAvailable(ifname, properties.dnsServers)
it.onAvailable(ifname, properties)
}
}
properties.dnsServers != oldProperties.dnsServers -> callbacks.forEach {
it.onAvailable(ifname, properties.dnsServers)
}
else -> callbacks.forEach { it.onAvailable(ifname, properties) }
}
}
}
@@ -75,7 +73,7 @@ object DefaultNetworkMonitor : UpstreamMonitor() {
if (registered) {
val currentLinkProperties = currentLinkProperties
if (currentLinkProperties != null) {
callback.onAvailable(currentLinkProperties.interfaceName!!, currentLinkProperties.dnsServers)
callback.onAvailable(currentLinkProperties.interfaceName!!, currentLinkProperties)
}
} else {
if (Build.VERSION.SDK_INT in 24..27) @TargetApi(24) {

View File

@@ -8,8 +8,8 @@ class InterfaceMonitor(val iface: String) : UpstreamMonitor() {
if (present == old) return
currentIface = if (present) iface else null
if (present) {
val dns = currentDns
callbacks.forEach { it.onAvailable(iface, dns) }
val lp = currentLinkProperties ?: return@synchronized
callbacks.forEach { it.onAvailable(iface, lp) }
} else callbacks.forEach { it.onLost() }
}
@@ -24,7 +24,7 @@ class InterfaceMonitor(val iface: String) : UpstreamMonitor() {
if (!registered) {
IpLinkMonitor.registerCallback(this, iface, this::setPresent)
registered = true
} else if (currentIface != null) callback.onAvailable(iface, currentDns)
} else if (currentIface != null) callback.onAvailable(iface, currentLinkProperties ?: return)
}
override fun destroyLocked() {

View File

@@ -51,9 +51,9 @@ abstract class UpstreamMonitor {
interface Callback {
/**
* Called if some interface is available. This might be called on different ifname without having called onLost.
* This might also be called on the same ifname but with updated DNS list.
* This might also be called on the same ifname but with updated link properties.
*/
fun onAvailable(ifname: String, dns: List<InetAddress>)
fun onAvailable(ifname: String, properties: LinkProperties)
/**
* Called if no interface is available.
*/

View File

@@ -29,8 +29,8 @@ object VpnMonitor : UpstreamMonitor() {
if (currentNetwork != network || ifname != oldProperties?.interfaceName) {
if (currentNetwork != null) callbacks.forEach { it.onLost() }
currentNetwork = network
} else if (properties.dnsServers == oldProperties.dnsServers) return
callbacks.forEach { it.onAvailable(ifname, properties.dnsServers) }
}
callbacks.forEach { it.onAvailable(ifname, properties) }
}
}
@@ -52,12 +52,10 @@ object VpnMonitor : UpstreamMonitor() {
Timber.w("interfaceName changed: $oldProperties -> $properties")
callbacks.forEach {
it.onLost()
it.onAvailable(ifname, properties.dnsServers)
it.onAvailable(ifname, properties)
}
}
properties.dnsServers != oldProperties.dnsServers -> callbacks.forEach {
it.onAvailable(ifname, properties.dnsServers)
}
else -> callbacks.forEach { it.onAvailable(ifname, properties) }
}
}
}
@@ -69,7 +67,7 @@ object VpnMonitor : UpstreamMonitor() {
val next = available.entries.first()
currentNetwork = next.key
DebugHelper.log(TAG, "Switching to ${next.value.interfaceName} as VPN interface")
callbacks.forEach { it.onAvailable(next.value.interfaceName!!, next.value.dnsServers) }
callbacks.forEach { it.onAvailable(next.value.interfaceName!!, next.value) }
} else currentNetwork = null
}
}
@@ -78,7 +76,7 @@ object VpnMonitor : UpstreamMonitor() {
if (registered) {
val currentLinkProperties = currentLinkProperties
if (currentLinkProperties != null) {
callback.onAvailable(currentLinkProperties.interfaceName!!, currentLinkProperties.dnsServers)
callback.onAvailable(currentLinkProperties.interfaceName!!, currentLinkProperties)
}
} else {
app.connectivity.registerNetworkCallback(request, networkCallback)