fix: try to query ssid if we don't have a valid one yet

This commit is contained in:
Zane Schepke 2025-01-01 02:38:59 -05:00
parent 2c04b9d69c
commit ba064b267f
10 changed files with 53 additions and 27 deletions

View File

@ -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 }
}

View File

@ -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,
)

View File

@ -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)
}
}
}

View File

@ -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)
}
}
}

View File

@ -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?,
)

View File

@ -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('"')
}
}

View File

@ -1,5 +0,0 @@
package com.zaneschepke.wireguardautotunnel.ui.screens.settings.autotunnel.advanced
enum class PeerActions {
EXCLUDE_LAN,
}

View File

@ -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

View File

@ -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,

View File

@ -0,0 +1,5 @@
package com.zaneschepke.wireguardautotunnel.ui.screens.tunneloptions.config.model
enum class PeerActions {
EXCLUDE_LAN,
}