fix: active tunnel bug

This commit is contained in:
Zane Schepke 2025-01-01 01:32:24 -05:00
parent 48adaae0a0
commit 2c04b9d69c
6 changed files with 14 additions and 22 deletions

View File

@ -266,7 +266,7 @@ class AutoTunnelService : LifecycleService() {
false, false,
wifi.name, wifi.name,
) )
}.distinctUntilChanged().filterNot { it.isWifiConnected && it.wifiName == null }.debounce(500L) }.distinctUntilChanged().filterNot { it.isWifiConnected && it.wifiName == null }
} }
private fun combineSettings(): Flow<Pair<Settings, TunnelConfigs>> { private fun combineSettings(): Flow<Pair<Settings, TunnelConfigs>> {

View File

@ -12,6 +12,7 @@ import android.os.Build
import com.wireguard.android.util.RootShell import com.wireguard.android.util.RootShell
import com.zaneschepke.wireguardautotunnel.data.repository.SettingsRepository import com.zaneschepke.wireguardautotunnel.data.repository.SettingsRepository
import com.zaneschepke.wireguardautotunnel.module.AppShell import com.zaneschepke.wireguardautotunnel.module.AppShell
import com.zaneschepke.wireguardautotunnel.util.Constants
import com.zaneschepke.wireguardautotunnel.util.extensions.getCurrentWifiName import com.zaneschepke.wireguardautotunnel.util.extensions.getCurrentWifiName
import dagger.hilt.android.qualifiers.ApplicationContext import dagger.hilt.android.qualifiers.ApplicationContext
import kotlinx.coroutines.channels.awaitClose import kotlinx.coroutines.channels.awaitClose
@ -110,7 +111,7 @@ constructor(
available = true available = true
} }
is NetworkStatus.CapabilitiesChanged -> mutex.withLock { is NetworkStatus.CapabilitiesChanged -> mutex.withLock {
if (available) { if (available || ssid == null || ssid == Constants.UNREADABLE_SSID) {
available = false available = false
Timber.d("Getting SSID from capabilities") Timber.d("Getting SSID from capabilities")
ssid = getNetworkName(it.networkCapabilities) ssid = getNetworkName(it.networkCapabilities)

View File

@ -108,7 +108,7 @@ constructor(
} }
private fun isTunnelAlreadyRunning(tunnelConfig: TunnelConfig): Boolean { 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") if (isRunning) Timber.w("Tunnel already running")
return isRunning return isRunning
} }
@ -116,17 +116,13 @@ constructor(
override suspend fun startTunnel(tunnelConfig: TunnelConfig?, background: Boolean) { override suspend fun startTunnel(tunnelConfig: TunnelConfig?, background: Boolean) {
withContext(ioDispatcher) { withContext(ioDispatcher) {
if (tunnelConfig == null || isTunnelAlreadyRunning(tunnelConfig)) return@withContext if (tunnelConfig == null || isTunnelAlreadyRunning(tunnelConfig)) return@withContext
onBeforeStart(background)
updateTunnelConfig(tunnelConfig) // need to update this here updateTunnelConfig(tunnelConfig) // need to update this here
withServiceActive { withServiceActive {
onBeforeStart(background)
tunnelControlMutex.withLock {
setState(tunnelConfig, TunnelState.UP).onSuccess { setState(tunnelConfig, TunnelState.UP).onSuccess {
updateTunnelState(it, tunnelConfig) updateTunnelState(it, tunnelConfig)
onTunnelStart(tunnelConfig, background) onTunnelStart(tunnelConfig, background)
} }
}.onFailure {
Timber.e(it)
}
} }
} }
} }
@ -136,7 +132,6 @@ constructor(
if (_vpnState.value.status.isDown()) return@withContext if (_vpnState.value.status.isDown()) return@withContext
with(_vpnState.value) { with(_vpnState.value) {
if (tunnelConfig == null) return@withContext if (tunnelConfig == null) return@withContext
tunnelControlMutex.withLock {
setState(tunnelConfig, TunnelState.DOWN).onSuccess { setState(tunnelConfig, TunnelState.DOWN).onSuccess {
onTunnelStop(tunnelConfig) onTunnelStop(tunnelConfig)
updateTunnelState(it, null) updateTunnelState(it, null)
@ -146,7 +141,6 @@ constructor(
} }
} }
} }
}
private suspend fun toggleTunnel(tunnelConfig: TunnelConfig) { private suspend fun toggleTunnel(tunnelConfig: TunnelConfig) {
withContext(ioDispatcher) { withContext(ioDispatcher) {

View File

@ -242,7 +242,7 @@ class MainActivity : AppCompatActivity() {
composable<Route.SplitTunnel> { composable<Route.SplitTunnel> {
val args = it.toRoute<Route.SplitTunnel>() val args = it.toRoute<Route.SplitTunnel>()
val config = appUiState.tunnels.first { it.id == args.id } val config = appUiState.tunnels.first { it.id == args.id }
SplitTunnelScreen(config,viewModel) SplitTunnelScreen(config, viewModel)
} }
composable<Route.TunnelAutoTunnel> { composable<Route.TunnelAutoTunnel> {
val args = it.toRoute<Route.TunnelOptions>() val args = it.toRoute<Route.TunnelOptions>()

View File

@ -59,7 +59,6 @@ import androidx.compose.ui.unit.dp
import androidx.lifecycle.compose.collectAsStateWithLifecycle import androidx.lifecycle.compose.collectAsStateWithLifecycle
import com.zaneschepke.wireguardautotunnel.R import com.zaneschepke.wireguardautotunnel.R
import com.zaneschepke.wireguardautotunnel.data.domain.TunnelConfig import com.zaneschepke.wireguardautotunnel.data.domain.TunnelConfig
import com.zaneschepke.wireguardautotunnel.ui.AppUiState
import com.zaneschepke.wireguardautotunnel.ui.AppViewModel import com.zaneschepke.wireguardautotunnel.ui.AppViewModel
import com.zaneschepke.wireguardautotunnel.ui.common.config.ConfigurationTextBox import com.zaneschepke.wireguardautotunnel.ui.common.config.ConfigurationTextBox
import com.zaneschepke.wireguardautotunnel.ui.common.label.GroupLabel 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.InterfaceProxy
import com.zaneschepke.wireguardautotunnel.ui.screens.tunneloptions.config.model.PeerProxy import com.zaneschepke.wireguardautotunnel.ui.screens.tunneloptions.config.model.PeerProxy
import com.zaneschepke.wireguardautotunnel.ui.theme.iconSize 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.scaledHeight
import com.zaneschepke.wireguardautotunnel.util.extensions.scaledWidth import com.zaneschepke.wireguardautotunnel.util.extensions.scaledWidth
import org.amnezia.awg.crypto.KeyPair import org.amnezia.awg.crypto.KeyPair

View File

@ -50,7 +50,6 @@ import com.zaneschepke.wireguardautotunnel.util.extensions.scaledWidth
@Composable @Composable
fun TunnelAutoTunnelScreen(tunnelConfig: TunnelConfig, settings: Settings, tunnelAutoTunnelViewModel: TunnelAutoTunnelViewModel = hiltViewModel()) { fun TunnelAutoTunnelScreen(tunnelConfig: TunnelConfig, settings: Settings, tunnelAutoTunnelViewModel: TunnelAutoTunnelViewModel = hiltViewModel()) {
var currentText by remember { mutableStateOf("") } var currentText by remember { mutableStateOf("") }
LaunchedEffect(tunnelConfig.tunnelNetworks) { LaunchedEffect(tunnelConfig.tunnelNetworks) {