-
Notifications
You must be signed in to change notification settings - Fork 361
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'android-tokio-shutdown-timeout'
- Loading branch information
Showing
28 changed files
with
613 additions
and
323 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
105 changes: 57 additions & 48 deletions
105
android/lib/talpid/src/main/kotlin/net/mullvad/talpid/ConnectivityListener.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,70 +1,79 @@ | ||
package net.mullvad.talpid | ||
|
||
import android.content.Context | ||
import android.net.ConnectivityManager | ||
import android.net.ConnectivityManager.NetworkCallback | ||
import android.net.LinkProperties | ||
import android.net.Network | ||
import android.net.NetworkCapabilities | ||
import android.net.NetworkRequest | ||
import kotlin.properties.Delegates.observable | ||
import java.net.InetAddress | ||
import kotlinx.coroutines.CoroutineScope | ||
import kotlinx.coroutines.flow.Flow | ||
import kotlinx.coroutines.flow.SharingStarted | ||
import kotlinx.coroutines.flow.StateFlow | ||
import kotlinx.coroutines.flow.distinctUntilChanged | ||
import kotlinx.coroutines.flow.filterIsInstance | ||
import kotlinx.coroutines.flow.map | ||
import kotlinx.coroutines.flow.onEach | ||
import kotlinx.coroutines.flow.scan | ||
import kotlinx.coroutines.flow.stateIn | ||
import net.mullvad.talpid.util.NetworkEvent | ||
import net.mullvad.talpid.util.defaultNetworkFlow | ||
import net.mullvad.talpid.util.networkFlow | ||
|
||
class ConnectivityListener { | ||
private val availableNetworks = HashSet<Network>() | ||
class ConnectivityListener(val connectivityManager: ConnectivityManager) { | ||
private lateinit var _isConnected: StateFlow<Boolean> | ||
// Used by JNI | ||
val isConnected | ||
get() = _isConnected.value | ||
|
||
private val callback = | ||
object : NetworkCallback() { | ||
override fun onAvailable(network: Network) { | ||
availableNetworks.add(network) | ||
isConnected = true | ||
} | ||
private lateinit var _currentDnsServers: StateFlow<List<InetAddress>> | ||
// Used by JNI | ||
val currentDnsServers | ||
get() = ArrayList(_currentDnsServers.value) | ||
|
||
override fun onLost(network: Network) { | ||
availableNetworks.remove(network) | ||
isConnected = availableNetworks.isNotEmpty() | ||
} | ||
} | ||
fun register(scope: CoroutineScope) { | ||
_currentDnsServers = | ||
dnsServerChanges().stateIn(scope, SharingStarted.Eagerly, currentDnsServers()) | ||
|
||
private lateinit var connectivityManager: ConnectivityManager | ||
_isConnected = | ||
hasInternetCapability() | ||
.onEach { notifyConnectivityChange(it) } | ||
.stateIn(scope, SharingStarted.Eagerly, false) | ||
} | ||
|
||
// Used by JNI | ||
var isConnected by | ||
observable(false) { _, oldValue, newValue -> | ||
if (newValue != oldValue) { | ||
if (senderAddress != 0L) { | ||
notifyConnectivityChange(newValue, senderAddress) | ||
} | ||
} | ||
} | ||
private fun dnsServerChanges(): Flow<List<InetAddress>> = | ||
connectivityManager | ||
.defaultNetworkFlow() | ||
.filterIsInstance<NetworkEvent.LinkPropertiesChanged>() | ||
.map { it.linkProperties.dnsServersWithoutFallback() } | ||
|
||
private fun currentDnsServers(): List<InetAddress> = | ||
connectivityManager | ||
.getLinkProperties(connectivityManager.activeNetwork) | ||
?.dnsServersWithoutFallback() ?: emptyList() | ||
|
||
var senderAddress = 0L | ||
private fun LinkProperties.dnsServersWithoutFallback(): List<InetAddress> = | ||
dnsServers.filter { it.hostAddress != TalpidVpnService.FALLBACK_DUMMY_DNS_SERVER } | ||
|
||
fun register(context: Context) { | ||
private fun hasInternetCapability(): Flow<Boolean> { | ||
val request = | ||
NetworkRequest.Builder() | ||
.addCapability(NetworkCapabilities.NET_CAPABILITY_INTERNET) | ||
.addCapability(NetworkCapabilities.NET_CAPABILITY_NOT_VPN) | ||
.build() | ||
|
||
connectivityManager = | ||
context.getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager | ||
|
||
connectivityManager.registerNetworkCallback(request, callback) | ||
} | ||
|
||
fun unregister() { | ||
connectivityManager.unregisterNetworkCallback(callback) | ||
} | ||
|
||
// DROID-1401 | ||
// This function has never been used and should most likely be merged into unregister(), | ||
// along with ensuring that the lifecycle of it is correct. | ||
@Suppress("UnusedPrivateMember") | ||
private fun finalize() { | ||
destroySender(senderAddress) | ||
senderAddress = 0L | ||
return connectivityManager | ||
.networkFlow(request) | ||
.scan(setOf<Network>()) { networks, event -> | ||
when (event) { | ||
is NetworkEvent.Available -> networks + event.network | ||
is NetworkEvent.Lost -> networks - event.network | ||
else -> networks | ||
} | ||
} | ||
.map { it.isNotEmpty() } | ||
.distinctUntilChanged() | ||
} | ||
|
||
private external fun notifyConnectivityChange(isConnected: Boolean, senderAddress: Long) | ||
|
||
private external fun destroySender(senderAddress: Long) | ||
private external fun notifyConnectivityChange(isConnected: Boolean) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
132 changes: 132 additions & 0 deletions
132
android/lib/talpid/src/main/kotlin/net/mullvad/talpid/util/ConnectivityManagerUtil.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,132 @@ | ||
package net.mullvad.talpid.util | ||
|
||
import android.net.ConnectivityManager | ||
import android.net.ConnectivityManager.NetworkCallback | ||
import android.net.LinkProperties | ||
import android.net.Network | ||
import android.net.NetworkCapabilities | ||
import android.net.NetworkRequest | ||
import kotlinx.coroutines.channels.awaitClose | ||
import kotlinx.coroutines.channels.trySendBlocking | ||
import kotlinx.coroutines.flow.Flow | ||
import kotlinx.coroutines.flow.callbackFlow | ||
|
||
fun ConnectivityManager.defaultNetworkFlow(): Flow<NetworkEvent> = | ||
callbackFlow<NetworkEvent> { | ||
val callback = | ||
object : NetworkCallback() { | ||
override fun onLinkPropertiesChanged( | ||
network: Network, | ||
linkProperties: LinkProperties, | ||
) { | ||
super.onLinkPropertiesChanged(network, linkProperties) | ||
trySendBlocking(NetworkEvent.LinkPropertiesChanged(network, linkProperties)) | ||
} | ||
|
||
override fun onAvailable(network: Network) { | ||
super.onAvailable(network) | ||
trySendBlocking(NetworkEvent.Available(network)) | ||
} | ||
|
||
override fun onCapabilitiesChanged( | ||
network: Network, | ||
networkCapabilities: NetworkCapabilities, | ||
) { | ||
super.onCapabilitiesChanged(network, networkCapabilities) | ||
trySendBlocking(NetworkEvent.CapabilitiesChanged(network, networkCapabilities)) | ||
} | ||
|
||
override fun onBlockedStatusChanged(network: Network, blocked: Boolean) { | ||
super.onBlockedStatusChanged(network, blocked) | ||
trySendBlocking(NetworkEvent.BlockedStatusChanged(network, blocked)) | ||
} | ||
|
||
override fun onLosing(network: Network, maxMsToLive: Int) { | ||
super.onLosing(network, maxMsToLive) | ||
trySendBlocking(NetworkEvent.Losing(network, maxMsToLive)) | ||
} | ||
|
||
override fun onLost(network: Network) { | ||
super.onLost(network) | ||
trySendBlocking(NetworkEvent.Lost(network)) | ||
} | ||
|
||
override fun onUnavailable() { | ||
super.onUnavailable() | ||
trySendBlocking(NetworkEvent.Unavailable) | ||
} | ||
} | ||
registerDefaultNetworkCallback(callback) | ||
|
||
awaitClose { unregisterNetworkCallback(callback) } | ||
} | ||
|
||
fun ConnectivityManager.networkFlow(networkRequest: NetworkRequest): Flow<NetworkEvent> = | ||
callbackFlow<NetworkEvent> { | ||
val callback = | ||
object : NetworkCallback() { | ||
override fun onLinkPropertiesChanged( | ||
network: Network, | ||
linkProperties: LinkProperties, | ||
) { | ||
super.onLinkPropertiesChanged(network, linkProperties) | ||
trySendBlocking(NetworkEvent.LinkPropertiesChanged(network, linkProperties)) | ||
} | ||
|
||
override fun onAvailable(network: Network) { | ||
super.onAvailable(network) | ||
trySendBlocking(NetworkEvent.Available(network)) | ||
} | ||
|
||
override fun onCapabilitiesChanged( | ||
network: Network, | ||
networkCapabilities: NetworkCapabilities, | ||
) { | ||
super.onCapabilitiesChanged(network, networkCapabilities) | ||
trySendBlocking(NetworkEvent.CapabilitiesChanged(network, networkCapabilities)) | ||
} | ||
|
||
override fun onBlockedStatusChanged(network: Network, blocked: Boolean) { | ||
super.onBlockedStatusChanged(network, blocked) | ||
trySendBlocking(NetworkEvent.BlockedStatusChanged(network, blocked)) | ||
} | ||
|
||
override fun onLosing(network: Network, maxMsToLive: Int) { | ||
super.onLosing(network, maxMsToLive) | ||
trySendBlocking(NetworkEvent.Losing(network, maxMsToLive)) | ||
} | ||
|
||
override fun onLost(network: Network) { | ||
super.onLost(network) | ||
trySendBlocking(NetworkEvent.Lost(network)) | ||
} | ||
|
||
override fun onUnavailable() { | ||
super.onUnavailable() | ||
trySendBlocking(NetworkEvent.Unavailable) | ||
} | ||
} | ||
registerNetworkCallback(networkRequest, callback) | ||
|
||
awaitClose { unregisterNetworkCallback(callback) } | ||
} | ||
|
||
sealed interface NetworkEvent { | ||
data class Available(val network: Network) : NetworkEvent | ||
|
||
data object Unavailable : NetworkEvent | ||
|
||
data class LinkPropertiesChanged(val network: Network, val linkProperties: LinkProperties) : | ||
NetworkEvent | ||
|
||
data class CapabilitiesChanged( | ||
val network: Network, | ||
val networkCapabilities: NetworkCapabilities, | ||
) : NetworkEvent | ||
|
||
data class BlockedStatusChanged(val network: Network, val blocked: Boolean) : NetworkEvent | ||
|
||
data class Losing(val network: Network, val maxMsToLive: Int) : NetworkEvent | ||
|
||
data class Lost(val network: Network) : NetworkEvent | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.