-
Notifications
You must be signed in to change notification settings - Fork 352
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'main' into patch-actions
- Loading branch information
Showing
87 changed files
with
2,530 additions
and
1,214 deletions.
There are no files selected for viewing
Validating CODEOWNERS rules …
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
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
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
Oops, something went wrong.