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 package be.mygod.vpnhotspot.net
import android.annotation.TargetApi import android.annotation.TargetApi
import android.net.LinkProperties
import android.os.Build import android.os.Build
import androidx.annotation.RequiresApi import androidx.annotation.RequiresApi
import be.mygod.vpnhotspot.App.Companion.app 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 subrouting: Subrouting? = null
var dns: List<InetAddress> = emptyList() 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 val subrouting = subrouting
when { when {
subrouting != null -> check(subrouting.upstream == ifname) { "${subrouting.upstream} != $ifname" } 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 null
} }
} }
this.dns = dns dns = properties.dnsServers
updateDnsRoute() updateDnsRoute()
} }

View File

@@ -28,9 +28,9 @@ object DefaultNetworkMonitor : UpstreamMonitor() {
if (currentNetwork != network || ifname != oldProperties?.interfaceName) { if (currentNetwork != network || ifname != oldProperties?.interfaceName) {
callbacks.forEach { it.onLost() } // we are using the other default network now callbacks.forEach { it.onLost() } // we are using the other default network now
currentNetwork = network currentNetwork = network
} else if (properties.dnsServers == oldProperties.dnsServers) return }
currentLinkProperties = properties 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")) Timber.w(RuntimeException("interfaceName changed: $oldProperties -> $properties"))
callbacks.forEach { callbacks.forEach {
it.onLost() it.onLost()
it.onAvailable(ifname, properties.dnsServers) it.onAvailable(ifname, properties)
} }
} }
properties.dnsServers != oldProperties.dnsServers -> callbacks.forEach { else -> callbacks.forEach { it.onAvailable(ifname, properties) }
it.onAvailable(ifname, properties.dnsServers)
}
} }
} }
} }
@@ -75,7 +73,7 @@ object DefaultNetworkMonitor : UpstreamMonitor() {
if (registered) { if (registered) {
val currentLinkProperties = currentLinkProperties val currentLinkProperties = currentLinkProperties
if (currentLinkProperties != null) { if (currentLinkProperties != null) {
callback.onAvailable(currentLinkProperties.interfaceName!!, currentLinkProperties.dnsServers) callback.onAvailable(currentLinkProperties.interfaceName!!, currentLinkProperties)
} }
} else { } else {
if (Build.VERSION.SDK_INT in 24..27) @TargetApi(24) { 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 if (present == old) return
currentIface = if (present) iface else null currentIface = if (present) iface else null
if (present) { if (present) {
val dns = currentDns val lp = currentLinkProperties ?: return@synchronized
callbacks.forEach { it.onAvailable(iface, dns) } callbacks.forEach { it.onAvailable(iface, lp) }
} else callbacks.forEach { it.onLost() } } else callbacks.forEach { it.onLost() }
} }
@@ -24,7 +24,7 @@ class InterfaceMonitor(val iface: String) : UpstreamMonitor() {
if (!registered) { if (!registered) {
IpLinkMonitor.registerCallback(this, iface, this::setPresent) IpLinkMonitor.registerCallback(this, iface, this::setPresent)
registered = true registered = true
} else if (currentIface != null) callback.onAvailable(iface, currentDns) } else if (currentIface != null) callback.onAvailable(iface, currentLinkProperties ?: return)
} }
override fun destroyLocked() { override fun destroyLocked() {

View File

@@ -51,9 +51,9 @@ abstract class UpstreamMonitor {
interface Callback { interface Callback {
/** /**
* Called if some interface is available. This might be called on different ifname without having called onLost. * 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. * Called if no interface is available.
*/ */

View File

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