fix: ethernet monitoring and auto tunnel override
This commit is contained in:
parent
f772dc0f8a
commit
53c09267df
|
@ -173,19 +173,12 @@ class AutoTunnelService : LifecycleService() {
|
|||
combineSettings(),
|
||||
combineNetworkEventsJob(),
|
||||
) { double, networkState ->
|
||||
// quick fix for bug where when first setting up auto tunneling we probably want to query for ssid right away
|
||||
var netState: NetworkState? = null
|
||||
var wifiName: String? = null
|
||||
if (networkState.wifiName == Constants.UNREADABLE_SSID && double.first.isTunnelOnWifiEnabled) {
|
||||
if (double.first.isWifiNameByShellEnabled) {
|
||||
netState = networkState.copy(wifiName = rootShell.get().getCurrentWifiName())
|
||||
} else if (networkState.capabilities != null) {
|
||||
netState = networkState.copy(
|
||||
wifiName =
|
||||
WifiService.getNetworkName(networkState.capabilities, this@AutoTunnelService),
|
||||
)
|
||||
}
|
||||
wifiName = getWifiName(double.first)
|
||||
}
|
||||
AutoTunnelState(tunnelService.get().vpnState.value, netState ?: networkState, double.first, double.second)
|
||||
val netState = wifiName?.let { networkState.copy(wifiName = it) } ?: networkState
|
||||
AutoTunnelState(tunnelService.get().vpnState.value, netState, double.first, double.second)
|
||||
}.collect { state ->
|
||||
Timber.d("Network state: ${state.networkState}")
|
||||
autoTunnelStateFlow.update {
|
||||
|
@ -194,18 +187,28 @@ class AutoTunnelService : LifecycleService() {
|
|||
}
|
||||
}
|
||||
|
||||
private fun getWifiName(setting: Settings): String? {
|
||||
return if (setting.isWifiNameByShellEnabled) {
|
||||
rootShell.get().getCurrentWifiName()
|
||||
} else if (wifiService.capabilities != null) {
|
||||
WifiService.getNetworkName(wifiService.capabilities!!, this@AutoTunnelService)
|
||||
} else {
|
||||
null
|
||||
}
|
||||
}
|
||||
|
||||
@OptIn(FlowPreview::class)
|
||||
private fun combineNetworkEventsJob(): Flow<NetworkState> {
|
||||
return combine(
|
||||
wifiService.status,
|
||||
mobileDataService.status,
|
||||
) { wifi, mobileData ->
|
||||
ethernetService.status,
|
||||
) { wifi, mobileData, ethernet ->
|
||||
NetworkState(
|
||||
wifi.available,
|
||||
mobileData.available,
|
||||
false,
|
||||
ethernet.available,
|
||||
wifi.name,
|
||||
wifi.capabilities,
|
||||
)
|
||||
}.distinctUntilChanged()
|
||||
}
|
||||
|
|
|
@ -1,11 +1,8 @@
|
|||
package com.zaneschepke.wireguardautotunnel.service.foreground.autotunnel.model
|
||||
|
||||
import android.net.NetworkCapabilities
|
||||
|
||||
data class NetworkState(
|
||||
val isWifiConnected: Boolean = false,
|
||||
val isMobileDataConnected: Boolean = false,
|
||||
val isEthernetConnected: Boolean = false,
|
||||
val wifiName: String? = null,
|
||||
val capabilities: NetworkCapabilities? = null,
|
||||
)
|
||||
|
|
|
@ -20,6 +20,8 @@ constructor(
|
|||
@ApplicationContext context: Context,
|
||||
) : NetworkService {
|
||||
|
||||
override var capabilities: NetworkCapabilities? = null
|
||||
|
||||
private val connectivityManager =
|
||||
context.getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager
|
||||
|
||||
|
@ -32,6 +34,7 @@ constructor(
|
|||
trySend(NetworkStatus.Unavailable())
|
||||
}
|
||||
override fun onCapabilitiesChanged(network: Network, networkCapabilities: NetworkCapabilities) {
|
||||
capabilities = networkCapabilities
|
||||
trySend(
|
||||
NetworkStatus.CapabilitiesChanged(
|
||||
network,
|
||||
|
@ -57,8 +60,8 @@ constructor(
|
|||
emit(NetworkStatus.Unavailable())
|
||||
}.map {
|
||||
when (it) {
|
||||
is NetworkStatus.Available, is NetworkStatus.CapabilitiesChanged -> Status(true, null, null)
|
||||
is NetworkStatus.Unavailable -> Status(false, null, null)
|
||||
is NetworkStatus.Available, is NetworkStatus.CapabilitiesChanged -> Status(true, null)
|
||||
is NetworkStatus.Unavailable -> Status(false, null)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -19,6 +19,9 @@ class MobileDataService
|
|||
constructor(
|
||||
@ApplicationContext context: Context,
|
||||
) : NetworkService {
|
||||
|
||||
override var capabilities: NetworkCapabilities? = null
|
||||
|
||||
private val connectivityManager =
|
||||
context.getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager
|
||||
|
||||
|
@ -31,6 +34,7 @@ constructor(
|
|||
trySend(NetworkStatus.Unavailable())
|
||||
}
|
||||
override fun onCapabilitiesChanged(network: Network, networkCapabilities: NetworkCapabilities) {
|
||||
capabilities = networkCapabilities
|
||||
trySend(
|
||||
NetworkStatus.CapabilitiesChanged(
|
||||
network,
|
||||
|
@ -56,8 +60,8 @@ constructor(
|
|||
emit(NetworkStatus.Unavailable())
|
||||
}.map {
|
||||
when (it) {
|
||||
is NetworkStatus.Available, is NetworkStatus.CapabilitiesChanged -> Status(true, null, null)
|
||||
is NetworkStatus.Unavailable -> Status(false, null, null)
|
||||
is NetworkStatus.Available, is NetworkStatus.CapabilitiesChanged -> Status(true, null)
|
||||
is NetworkStatus.Unavailable -> Status(false, null)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -7,6 +7,7 @@ import kotlinx.coroutines.flow.map
|
|||
|
||||
interface NetworkService {
|
||||
val status: Flow<Status>
|
||||
var capabilities: NetworkCapabilities?
|
||||
}
|
||||
|
||||
inline fun <Result> Flow<NetworkStatus>.map(
|
||||
|
|
|
@ -1,9 +1,6 @@
|
|||
package com.zaneschepke.wireguardautotunnel.service.network
|
||||
|
||||
import android.net.NetworkCapabilities
|
||||
|
||||
data class Status(
|
||||
val available: Boolean,
|
||||
val name: String?,
|
||||
val capabilities: NetworkCapabilities?,
|
||||
)
|
||||
|
|
|
@ -34,6 +34,8 @@ constructor(
|
|||
@AppShell private val rootShell: Provider<RootShell>,
|
||||
) : NetworkService {
|
||||
|
||||
override var capabilities: NetworkCapabilities? = null
|
||||
|
||||
val mutex = Mutex()
|
||||
|
||||
private var ssid: String? = null
|
||||
|
@ -80,6 +82,7 @@ constructor(
|
|||
}
|
||||
|
||||
override fun onCapabilitiesChanged(network: Network, networkCapabilities: NetworkCapabilities) {
|
||||
capabilities = networkCapabilities
|
||||
trySend(
|
||||
NetworkStatus.CapabilitiesChanged(
|
||||
network,
|
||||
|
@ -120,9 +123,9 @@ constructor(
|
|||
getNetworkName(it.networkCapabilities, context)
|
||||
}
|
||||
}
|
||||
emit(Status(true, ssid, it.networkCapabilities))
|
||||
emit(Status(true, ssid))
|
||||
}
|
||||
is NetworkStatus.Unavailable -> emit(Status(false, null, null))
|
||||
is NetworkStatus.Unavailable -> emit(Status(false, null))
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue