From 2c04b9d69cc727d6dde2ca78ed512313199af6ed Mon Sep 17 00:00:00 2001 From: Zane Schepke Date: Wed, 1 Jan 2025 01:32:24 -0500 Subject: [PATCH] fix: active tunnel bug --- .../autotunnel/AutoTunnelService.kt | 2 +- .../service/network/WifiService.kt | 3 ++- .../service/tunnel/WireGuardTunnel.kt | 26 +++++++------------ .../wireguardautotunnel/ui/MainActivity.kt | 2 +- .../tunneloptions/config/ConfigScreen.kt | 2 -- .../TunnelAutoTunnelScreen.kt | 1 - 6 files changed, 14 insertions(+), 22 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 263ffdf..443b1dd 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 @@ -266,7 +266,7 @@ class AutoTunnelService : LifecycleService() { false, wifi.name, ) - }.distinctUntilChanged().filterNot { it.isWifiConnected && it.wifiName == null }.debounce(500L) + }.distinctUntilChanged().filterNot { it.isWifiConnected && it.wifiName == null } } private fun combineSettings(): Flow> { 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 d29218d..c992f17 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 @@ -12,6 +12,7 @@ import android.os.Build import com.wireguard.android.util.RootShell import com.zaneschepke.wireguardautotunnel.data.repository.SettingsRepository import com.zaneschepke.wireguardautotunnel.module.AppShell +import com.zaneschepke.wireguardautotunnel.util.Constants import com.zaneschepke.wireguardautotunnel.util.extensions.getCurrentWifiName import dagger.hilt.android.qualifiers.ApplicationContext import kotlinx.coroutines.channels.awaitClose @@ -110,7 +111,7 @@ constructor( available = true } is NetworkStatus.CapabilitiesChanged -> mutex.withLock { - if (available) { + if (available || ssid == null || ssid == Constants.UNREADABLE_SSID) { available = false Timber.d("Getting SSID from capabilities") ssid = getNetworkName(it.networkCapabilities) diff --git a/app/src/main/java/com/zaneschepke/wireguardautotunnel/service/tunnel/WireGuardTunnel.kt b/app/src/main/java/com/zaneschepke/wireguardautotunnel/service/tunnel/WireGuardTunnel.kt index 191f7e0..c783e4f 100644 --- a/app/src/main/java/com/zaneschepke/wireguardautotunnel/service/tunnel/WireGuardTunnel.kt +++ b/app/src/main/java/com/zaneschepke/wireguardautotunnel/service/tunnel/WireGuardTunnel.kt @@ -108,7 +108,7 @@ constructor( } private fun isTunnelAlreadyRunning(tunnelConfig: TunnelConfig): Boolean { - val isRunning = tunnelConfig == _vpnState.value.tunnelConfig && _vpnState.value.status.isUp() + val isRunning = tunnelConfig.id == _vpnState.value.tunnelConfig?.id && _vpnState.value.status.isUp() if (isRunning) Timber.w("Tunnel already running") return isRunning } @@ -116,16 +116,12 @@ constructor( override suspend fun startTunnel(tunnelConfig: TunnelConfig?, background: Boolean) { withContext(ioDispatcher) { if (tunnelConfig == null || isTunnelAlreadyRunning(tunnelConfig)) return@withContext + onBeforeStart(background) updateTunnelConfig(tunnelConfig) // need to update this here withServiceActive { - onBeforeStart(background) - tunnelControlMutex.withLock { - setState(tunnelConfig, TunnelState.UP).onSuccess { - updateTunnelState(it, tunnelConfig) - onTunnelStart(tunnelConfig, background) - } - }.onFailure { - Timber.e(it) + setState(tunnelConfig, TunnelState.UP).onSuccess { + updateTunnelState(it, tunnelConfig) + onTunnelStart(tunnelConfig, background) } } } @@ -136,13 +132,11 @@ constructor( if (_vpnState.value.status.isDown()) return@withContext with(_vpnState.value) { if (tunnelConfig == null) return@withContext - tunnelControlMutex.withLock { - setState(tunnelConfig, TunnelState.DOWN).onSuccess { - onTunnelStop(tunnelConfig) - updateTunnelState(it, null) - }.onFailure { - Timber.e(it) - } + setState(tunnelConfig, TunnelState.DOWN).onSuccess { + onTunnelStop(tunnelConfig) + updateTunnelState(it, null) + }.onFailure { + Timber.e(it) } } } diff --git a/app/src/main/java/com/zaneschepke/wireguardautotunnel/ui/MainActivity.kt b/app/src/main/java/com/zaneschepke/wireguardautotunnel/ui/MainActivity.kt index b93f813..bc3c7ab 100644 --- a/app/src/main/java/com/zaneschepke/wireguardautotunnel/ui/MainActivity.kt +++ b/app/src/main/java/com/zaneschepke/wireguardautotunnel/ui/MainActivity.kt @@ -242,7 +242,7 @@ class MainActivity : AppCompatActivity() { composable { val args = it.toRoute() val config = appUiState.tunnels.first { it.id == args.id } - SplitTunnelScreen(config,viewModel) + SplitTunnelScreen(config, viewModel) } composable { val args = it.toRoute() 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 8448f09..f9b677d 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 @@ -59,7 +59,6 @@ import androidx.compose.ui.unit.dp import androidx.lifecycle.compose.collectAsStateWithLifecycle import com.zaneschepke.wireguardautotunnel.R import com.zaneschepke.wireguardautotunnel.data.domain.TunnelConfig -import com.zaneschepke.wireguardautotunnel.ui.AppUiState import com.zaneschepke.wireguardautotunnel.ui.AppViewModel import com.zaneschepke.wireguardautotunnel.ui.common.config.ConfigurationTextBox import com.zaneschepke.wireguardautotunnel.ui.common.label.GroupLabel @@ -72,7 +71,6 @@ import com.zaneschepke.wireguardautotunnel.ui.screens.settings.autotunnel.advanc 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 -import com.zaneschepke.wireguardautotunnel.util.Constants import com.zaneschepke.wireguardautotunnel.util.extensions.scaledHeight import com.zaneschepke.wireguardautotunnel.util.extensions.scaledWidth import org.amnezia.awg.crypto.KeyPair diff --git a/app/src/main/java/com/zaneschepke/wireguardautotunnel/ui/screens/tunneloptions/tunnelautotunnel/TunnelAutoTunnelScreen.kt b/app/src/main/java/com/zaneschepke/wireguardautotunnel/ui/screens/tunneloptions/tunnelautotunnel/TunnelAutoTunnelScreen.kt index 7464304..02c7c8c 100644 --- a/app/src/main/java/com/zaneschepke/wireguardautotunnel/ui/screens/tunneloptions/tunnelautotunnel/TunnelAutoTunnelScreen.kt +++ b/app/src/main/java/com/zaneschepke/wireguardautotunnel/ui/screens/tunneloptions/tunnelautotunnel/TunnelAutoTunnelScreen.kt @@ -50,7 +50,6 @@ import com.zaneschepke.wireguardautotunnel.util.extensions.scaledWidth @Composable fun TunnelAutoTunnelScreen(tunnelConfig: TunnelConfig, settings: Settings, tunnelAutoTunnelViewModel: TunnelAutoTunnelViewModel = hiltViewModel()) { - var currentText by remember { mutableStateOf("") } LaunchedEffect(tunnelConfig.tunnelNetworks) {