From 59b4a464b0cbc090067059e064e6d46f345be63b Mon Sep 17 00:00:00 2001 From: Mygod Date: Mon, 20 Jan 2020 09:19:10 +0800 Subject: [PATCH] Notify all LinkProperties for onAvailable --- .../main/java/be/mygod/vpnhotspot/net/Routing.kt | 5 +++-- .../net/monitor/DefaultNetworkMonitor.kt | 12 +++++------- .../vpnhotspot/net/monitor/InterfaceMonitor.kt | 6 +++--- .../vpnhotspot/net/monitor/UpstreamMonitor.kt | 4 ++-- .../be/mygod/vpnhotspot/net/monitor/VpnMonitor.kt | 14 ++++++-------- 5 files changed, 19 insertions(+), 22 deletions(-) diff --git a/mobile/src/main/java/be/mygod/vpnhotspot/net/Routing.kt b/mobile/src/main/java/be/mygod/vpnhotspot/net/Routing.kt index f18b93a6..456a23c9 100644 --- a/mobile/src/main/java/be/mygod/vpnhotspot/net/Routing.kt +++ b/mobile/src/main/java/be/mygod/vpnhotspot/net/Routing.kt @@ -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 = emptyList() - override fun onAvailable(ifname: String, dns: List) = 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() } diff --git a/mobile/src/main/java/be/mygod/vpnhotspot/net/monitor/DefaultNetworkMonitor.kt b/mobile/src/main/java/be/mygod/vpnhotspot/net/monitor/DefaultNetworkMonitor.kt index 94559f25..08097b90 100644 --- a/mobile/src/main/java/be/mygod/vpnhotspot/net/monitor/DefaultNetworkMonitor.kt +++ b/mobile/src/main/java/be/mygod/vpnhotspot/net/monitor/DefaultNetworkMonitor.kt @@ -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) { diff --git a/mobile/src/main/java/be/mygod/vpnhotspot/net/monitor/InterfaceMonitor.kt b/mobile/src/main/java/be/mygod/vpnhotspot/net/monitor/InterfaceMonitor.kt index 5377536c..25a5ab17 100644 --- a/mobile/src/main/java/be/mygod/vpnhotspot/net/monitor/InterfaceMonitor.kt +++ b/mobile/src/main/java/be/mygod/vpnhotspot/net/monitor/InterfaceMonitor.kt @@ -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() { diff --git a/mobile/src/main/java/be/mygod/vpnhotspot/net/monitor/UpstreamMonitor.kt b/mobile/src/main/java/be/mygod/vpnhotspot/net/monitor/UpstreamMonitor.kt index 1be4fe3c..754ba73f 100644 --- a/mobile/src/main/java/be/mygod/vpnhotspot/net/monitor/UpstreamMonitor.kt +++ b/mobile/src/main/java/be/mygod/vpnhotspot/net/monitor/UpstreamMonitor.kt @@ -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) + fun onAvailable(ifname: String, properties: LinkProperties) /** * Called if no interface is available. */ diff --git a/mobile/src/main/java/be/mygod/vpnhotspot/net/monitor/VpnMonitor.kt b/mobile/src/main/java/be/mygod/vpnhotspot/net/monitor/VpnMonitor.kt index dcefa5ad..d13beb99 100644 --- a/mobile/src/main/java/be/mygod/vpnhotspot/net/monitor/VpnMonitor.kt +++ b/mobile/src/main/java/be/mygod/vpnhotspot/net/monitor/VpnMonitor.kt @@ -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)