From 53c09267df4cb44556d5a61df829767299bb10b0 Mon Sep 17 00:00:00 2001 From: Zane Schepke Date: Wed, 1 Jan 2025 19:14:56 -0500 Subject: [PATCH] fix: ethernet monitoring and auto tunnel override --- .../autotunnel/AutoTunnelService.kt | 31 ++++++++++--------- .../autotunnel/model/NetworkState.kt | 3 -- .../service/network/EthernetService.kt | 7 +++-- .../service/network/MobileDataService.kt | 8 +++-- .../service/network/NetworkService.kt | 1 + .../service/network/Status.kt | 3 -- .../service/network/WifiService.kt | 7 +++-- 7 files changed, 34 insertions(+), 26 deletions(-) diff --git a/app/src/main/java/com/zaneschepke/wireguardautotunnel/service/foreground/autotunnel/AutoTunnelService.kt b/app/src/main/java/com/zaneschepke/wireguardautotunnel/service/foreground/autotunnel/AutoTunnelService.kt index 0a015bb..bc83c92 100644 --- a/app/src/main/java/com/zaneschepke/wireguardautotunnel/service/foreground/autotunnel/AutoTunnelService.kt +++ b/app/src/main/java/com/zaneschepke/wireguardautotunnel/service/foreground/autotunnel/AutoTunnelService.kt @@ -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 { 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() } diff --git a/app/src/main/java/com/zaneschepke/wireguardautotunnel/service/foreground/autotunnel/model/NetworkState.kt b/app/src/main/java/com/zaneschepke/wireguardautotunnel/service/foreground/autotunnel/model/NetworkState.kt index 68dfa5b..35bf8d2 100644 --- a/app/src/main/java/com/zaneschepke/wireguardautotunnel/service/foreground/autotunnel/model/NetworkState.kt +++ b/app/src/main/java/com/zaneschepke/wireguardautotunnel/service/foreground/autotunnel/model/NetworkState.kt @@ -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, ) diff --git a/app/src/main/java/com/zaneschepke/wireguardautotunnel/service/network/EthernetService.kt b/app/src/main/java/com/zaneschepke/wireguardautotunnel/service/network/EthernetService.kt index ad80d96..43d5570 100644 --- a/app/src/main/java/com/zaneschepke/wireguardautotunnel/service/network/EthernetService.kt +++ b/app/src/main/java/com/zaneschepke/wireguardautotunnel/service/network/EthernetService.kt @@ -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) } } } diff --git a/app/src/main/java/com/zaneschepke/wireguardautotunnel/service/network/MobileDataService.kt b/app/src/main/java/com/zaneschepke/wireguardautotunnel/service/network/MobileDataService.kt index bdf0f48..58ff278 100644 --- a/app/src/main/java/com/zaneschepke/wireguardautotunnel/service/network/MobileDataService.kt +++ b/app/src/main/java/com/zaneschepke/wireguardautotunnel/service/network/MobileDataService.kt @@ -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) } } } diff --git a/app/src/main/java/com/zaneschepke/wireguardautotunnel/service/network/NetworkService.kt b/app/src/main/java/com/zaneschepke/wireguardautotunnel/service/network/NetworkService.kt index 6c46076..d4ec68f 100644 --- a/app/src/main/java/com/zaneschepke/wireguardautotunnel/service/network/NetworkService.kt +++ b/app/src/main/java/com/zaneschepke/wireguardautotunnel/service/network/NetworkService.kt @@ -7,6 +7,7 @@ import kotlinx.coroutines.flow.map interface NetworkService { val status: Flow + var capabilities: NetworkCapabilities? } inline fun Flow.map( diff --git a/app/src/main/java/com/zaneschepke/wireguardautotunnel/service/network/Status.kt b/app/src/main/java/com/zaneschepke/wireguardautotunnel/service/network/Status.kt index 224b1f0..6a6bf91 100644 --- a/app/src/main/java/com/zaneschepke/wireguardautotunnel/service/network/Status.kt +++ b/app/src/main/java/com/zaneschepke/wireguardautotunnel/service/network/Status.kt @@ -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?, ) diff --git a/app/src/main/java/com/zaneschepke/wireguardautotunnel/service/network/WifiService.kt b/app/src/main/java/com/zaneschepke/wireguardautotunnel/service/network/WifiService.kt index c31ae85..f51b748 100644 --- a/app/src/main/java/com/zaneschepke/wireguardautotunnel/service/network/WifiService.kt +++ b/app/src/main/java/com/zaneschepke/wireguardautotunnel/service/network/WifiService.kt @@ -34,6 +34,8 @@ constructor( @AppShell private val rootShell: Provider, ) : 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)) } }