From ba064b267f3dbb5a6ead7d073ee6207e0f37a654 Mon Sep 17 00:00:00 2001 From: Zane Schepke Date: Wed, 1 Jan 2025 02:38:59 -0500 Subject: [PATCH] fix: try to query ssid if we don't have a valid one yet --- .../autotunnel/AutoTunnelService.kt | 17 +++++++++- .../autotunnel/model/NetworkState.kt | 3 ++ .../service/network/EthernetService.kt | 4 +-- .../service/network/MobileDataService.kt | 4 +-- .../service/network/Status.kt | 3 ++ .../service/network/WifiService.kt | 33 +++++++++++-------- .../autotunnel/advanced/PeerActions.kt | 5 --- .../tunneloptions/config/ConfigScreen.kt | 4 +-- .../config/model}/InterfaceActions.kt | 2 +- .../tunneloptions/config/model/PeerActions.kt | 5 +++ 10 files changed, 53 insertions(+), 27 deletions(-) delete mode 100644 app/src/main/java/com/zaneschepke/wireguardautotunnel/ui/screens/settings/autotunnel/advanced/PeerActions.kt rename app/src/main/java/com/zaneschepke/wireguardautotunnel/ui/screens/{settings/autotunnel/advanced => tunneloptions/config/model}/InterfaceActions.kt (56%) create mode 100644 app/src/main/java/com/zaneschepke/wireguardautotunnel/ui/screens/tunneloptions/config/model/PeerActions.kt 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 443b1dd..f424722 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 @@ -22,6 +22,7 @@ import com.zaneschepke.wireguardautotunnel.service.foreground.autotunnel.model.A import com.zaneschepke.wireguardautotunnel.service.foreground.autotunnel.model.AutoTunnelState import com.zaneschepke.wireguardautotunnel.service.foreground.autotunnel.model.NetworkState import com.zaneschepke.wireguardautotunnel.service.network.NetworkService +import com.zaneschepke.wireguardautotunnel.service.network.WifiService import com.zaneschepke.wireguardautotunnel.service.notification.NotificationAction import com.zaneschepke.wireguardautotunnel.service.notification.NotificationService import com.zaneschepke.wireguardautotunnel.service.notification.WireGuardNotification @@ -29,6 +30,7 @@ import com.zaneschepke.wireguardautotunnel.service.tunnel.TunnelService import com.zaneschepke.wireguardautotunnel.util.Constants import com.zaneschepke.wireguardautotunnel.util.extensions.TunnelConfigs import com.zaneschepke.wireguardautotunnel.util.extensions.cancelWithMessage +import com.zaneschepke.wireguardautotunnel.util.extensions.getCurrentWifiName import com.zaneschepke.wireguardautotunnel.util.extensions.isReachable import com.zaneschepke.wireguardautotunnel.util.extensions.onNotRunning import dagger.hilt.android.AndroidEntryPoint @@ -240,7 +242,19 @@ class AutoTunnelService : LifecycleService() { combineSettings(), combineNetworkEventsJob(), ) { double, networkState -> - AutoTunnelState(tunnelService.get().vpnState.value, networkState, double.first, double.second) + // quick fix for bug where when first setting up auto tunneling we probably want to query for ssid right away + var netState: NetworkState? = 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), + ) + } + } + AutoTunnelState(tunnelService.get().vpnState.value, netState ?: networkState, double.first, double.second) }.collect { state -> Timber.d("Network state: ${state.networkState}") autoTunnelStateFlow.update { @@ -265,6 +279,7 @@ class AutoTunnelService : LifecycleService() { mobileData.available, false, wifi.name, + wifi.capabilities, ) }.distinctUntilChanged().filterNot { it.isWifiConnected && it.wifiName == null } } 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 35bf8d2..68dfa5b 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,8 +1,11 @@ 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 efc4872..ad80d96 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 @@ -57,8 +57,8 @@ constructor( emit(NetworkStatus.Unavailable()) }.map { when (it) { - is NetworkStatus.Available, is NetworkStatus.CapabilitiesChanged -> Status(true, null) - is NetworkStatus.Unavailable -> Status(false, null) + is NetworkStatus.Available, is NetworkStatus.CapabilitiesChanged -> Status(true, null, null) + is NetworkStatus.Unavailable -> Status(false, null, 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 6899a2d..bdf0f48 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 @@ -56,8 +56,8 @@ constructor( emit(NetworkStatus.Unavailable()) }.map { when (it) { - is NetworkStatus.Available, is NetworkStatus.CapabilitiesChanged -> Status(true, null) - is NetworkStatus.Unavailable -> Status(false, null) + is NetworkStatus.Available, is NetworkStatus.CapabilitiesChanged -> Status(true, null, null) + is NetworkStatus.Unavailable -> Status(false, null, null) } } } 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 6a6bf91..224b1f0 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,6 +1,9 @@ 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 c992f17..c31ae85 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 @@ -114,27 +114,32 @@ constructor( if (available || ssid == null || ssid == Constants.UNREADABLE_SSID) { available = false Timber.d("Getting SSID from capabilities") - ssid = getNetworkName(it.networkCapabilities) + ssid = if (settingsRepository.getSettings().isWifiNameByShellEnabled) { + rootShell.get().getCurrentWifiName() + } else { + getNetworkName(it.networkCapabilities, context) + } } - emit(Status(true, ssid)) + emit(Status(true, ssid, it.networkCapabilities)) } - is NetworkStatus.Unavailable -> emit(Status(false, null)) + is NetworkStatus.Unavailable -> emit(Status(false, null, null)) } } - private suspend fun getNetworkName(networkCapabilities: NetworkCapabilities): String? { - if (settingsRepository.getSettings().isWifiNameByShellEnabled) return rootShell.get().getCurrentWifiName() - var ssid = networkCapabilities.getWifiName() - if (Build.VERSION.SDK_INT <= Build.VERSION_CODES.S) { - val wifiManager = - context.applicationContext.getSystemService(Context.WIFI_SERVICE) as WifiManager + companion object { + fun getNetworkName(networkCapabilities: NetworkCapabilities, context: Context): String? { + var ssid = networkCapabilities.getWifiName() + if (Build.VERSION.SDK_INT <= Build.VERSION_CODES.S) { + val wifiManager = + context.applicationContext.getSystemService(Context.WIFI_SERVICE) as WifiManager - @Suppress("DEPRECATION") - val info = wifiManager.connectionInfo - if (info.supplicantState === SupplicantState.COMPLETED) { - ssid = info.ssid + @Suppress("DEPRECATION") + val info = wifiManager.connectionInfo + if (info.supplicantState === SupplicantState.COMPLETED) { + ssid = info.ssid + } } + return ssid?.trim('"') } - return ssid?.trim('"') } } diff --git a/app/src/main/java/com/zaneschepke/wireguardautotunnel/ui/screens/settings/autotunnel/advanced/PeerActions.kt b/app/src/main/java/com/zaneschepke/wireguardautotunnel/ui/screens/settings/autotunnel/advanced/PeerActions.kt deleted file mode 100644 index e163ac3..0000000 --- a/app/src/main/java/com/zaneschepke/wireguardautotunnel/ui/screens/settings/autotunnel/advanced/PeerActions.kt +++ /dev/null @@ -1,5 +0,0 @@ -package com.zaneschepke.wireguardautotunnel.ui.screens.settings.autotunnel.advanced - -enum class PeerActions { - EXCLUDE_LAN, -} diff --git a/app/src/main/java/com/zaneschepke/wireguardautotunnel/ui/screens/tunneloptions/config/ConfigScreen.kt b/app/src/main/java/com/zaneschepke/wireguardautotunnel/ui/screens/tunneloptions/config/ConfigScreen.kt index f9b677d..6fdccaa 100644 --- a/app/src/main/java/com/zaneschepke/wireguardautotunnel/ui/screens/tunneloptions/config/ConfigScreen.kt +++ b/app/src/main/java/com/zaneschepke/wireguardautotunnel/ui/screens/tunneloptions/config/ConfigScreen.kt @@ -66,8 +66,8 @@ import com.zaneschepke.wireguardautotunnel.ui.common.navigation.LocalNavControll import com.zaneschepke.wireguardautotunnel.ui.common.navigation.TopNavBar import com.zaneschepke.wireguardautotunnel.ui.common.prompt.AuthorizationPrompt import com.zaneschepke.wireguardautotunnel.ui.common.snackbar.SnackbarController -import com.zaneschepke.wireguardautotunnel.ui.screens.settings.autotunnel.advanced.InterfaceActions -import com.zaneschepke.wireguardautotunnel.ui.screens.settings.autotunnel.advanced.PeerActions +import com.zaneschepke.wireguardautotunnel.ui.screens.tunneloptions.config.model.InterfaceActions +import com.zaneschepke.wireguardautotunnel.ui.screens.tunneloptions.config.model.PeerActions import com.zaneschepke.wireguardautotunnel.ui.screens.tunneloptions.config.model.InterfaceProxy import com.zaneschepke.wireguardautotunnel.ui.screens.tunneloptions.config.model.PeerProxy import com.zaneschepke.wireguardautotunnel.ui.theme.iconSize diff --git a/app/src/main/java/com/zaneschepke/wireguardautotunnel/ui/screens/settings/autotunnel/advanced/InterfaceActions.kt b/app/src/main/java/com/zaneschepke/wireguardautotunnel/ui/screens/tunneloptions/config/model/InterfaceActions.kt similarity index 56% rename from app/src/main/java/com/zaneschepke/wireguardautotunnel/ui/screens/settings/autotunnel/advanced/InterfaceActions.kt rename to app/src/main/java/com/zaneschepke/wireguardautotunnel/ui/screens/tunneloptions/config/model/InterfaceActions.kt index 1200830..23f5714 100644 --- a/app/src/main/java/com/zaneschepke/wireguardautotunnel/ui/screens/settings/autotunnel/advanced/InterfaceActions.kt +++ b/app/src/main/java/com/zaneschepke/wireguardautotunnel/ui/screens/tunneloptions/config/model/InterfaceActions.kt @@ -1,4 +1,4 @@ -package com.zaneschepke.wireguardautotunnel.ui.screens.settings.autotunnel.advanced +package com.zaneschepke.wireguardautotunnel.ui.screens.tunneloptions.config.model enum class InterfaceActions { TOGGLE_AMNEZIA_VALUES, diff --git a/app/src/main/java/com/zaneschepke/wireguardautotunnel/ui/screens/tunneloptions/config/model/PeerActions.kt b/app/src/main/java/com/zaneschepke/wireguardautotunnel/ui/screens/tunneloptions/config/model/PeerActions.kt new file mode 100644 index 0000000..61a04a6 --- /dev/null +++ b/app/src/main/java/com/zaneschepke/wireguardautotunnel/ui/screens/tunneloptions/config/model/PeerActions.kt @@ -0,0 +1,5 @@ +package com.zaneschepke.wireguardautotunnel.ui.screens.tunneloptions.config.model + +enum class PeerActions { + EXCLUDE_LAN, +}