From 7d810c7c3d381617946ccbf955cea6f1f5d8ba09 Mon Sep 17 00:00:00 2001 From: Zane Schepke Date: Thu, 7 Nov 2024 20:02:35 -0500 Subject: [PATCH] fix: auto tunnel crash --- .../module/TunnelModule.kt | 2 +- .../service/foreground/AutoTunnelService.kt | 3 +- .../service/tunnel/WireGuardTunnel.kt | 39 ++++++++++++------- 3 files changed, 27 insertions(+), 17 deletions(-) diff --git a/app/src/main/java/com/zaneschepke/wireguardautotunnel/module/TunnelModule.kt b/app/src/main/java/com/zaneschepke/wireguardautotunnel/module/TunnelModule.kt index e8bf218f..dc19286d 100644 --- a/app/src/main/java/com/zaneschepke/wireguardautotunnel/module/TunnelModule.kt +++ b/app/src/main/java/com/zaneschepke/wireguardautotunnel/module/TunnelModule.kt @@ -82,6 +82,6 @@ class TunnelModule { @Singleton @Provides fun provideServiceManager(@ApplicationContext context: Context): ServiceManager { - return ServiceManager(context) + return ServiceManager.getInstance(context) } } diff --git a/app/src/main/java/com/zaneschepke/wireguardautotunnel/service/foreground/AutoTunnelService.kt b/app/src/main/java/com/zaneschepke/wireguardautotunnel/service/foreground/AutoTunnelService.kt index 133382b1..994ce539 100644 --- a/app/src/main/java/com/zaneschepke/wireguardautotunnel/service/foreground/AutoTunnelService.kt +++ b/app/src/main/java/com/zaneschepke/wireguardautotunnel/service/foreground/AutoTunnelService.kt @@ -1,6 +1,5 @@ package com.zaneschepke.wireguardautotunnel.service.foreground -import android.content.Context import android.content.Intent import android.net.NetworkCapabilities import android.os.IBinder @@ -162,7 +161,7 @@ class AutoTunnelService : LifecycleService() { private fun initWakeLock() { wakeLock = - (getSystemService(Context.POWER_SERVICE) as PowerManager).run { + (getSystemService(POWER_SERVICE) as PowerManager).run { val tag = this.javaClass.name newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "$tag::lock").apply { try { 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 16948ac5..a500bb34 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 @@ -91,7 +91,8 @@ constructor( override suspend fun startTunnel(tunnelConfig: TunnelConfig, background: Boolean): Result { return withContext(ioDispatcher) { - onBeforeStart(tunnelConfig, background) + onBeforeStart(tunnelConfig) + if (background) startBackgroundService() setState(tunnelConfig, TunnelState.UP).onSuccess { emitTunnelState(it) }.onFailure { @@ -109,6 +110,8 @@ constructor( }.onFailure { Timber.e(it) onStopFailed() + }.also { + stopBackgroundService() } } } @@ -145,28 +148,36 @@ constructor( resetBackendStatistics() } - private suspend fun onBeforeStart(tunnelConfig: TunnelConfig, background: Boolean) { - if (_vpnState.value.status == TunnelState.UP && - tunnelConfig != _vpnState.value.tunnelConfig - ) { - vpnState.value.tunnelConfig?.let { stopTunnel(it) } + private suspend fun shutDownActiveTunnel(config: TunnelConfig) { + with(_vpnState.value) { + if (status == TunnelState.UP && tunnelConfig != config) { + tunnelConfig?.let { stopTunnel(it) } + } } - if (background) serviceManager.startBackgroundService() - resetBackendStatistics() + } + + private suspend fun startBackgroundService() { + serviceManager.startBackgroundService() + serviceManager.requestTunnelTileUpdate() + } + + private fun stopBackgroundService() { + serviceManager.stopBackgroundService() + serviceManager.requestTunnelTileUpdate() + } + + private suspend fun onBeforeStart(tunnelConfig: TunnelConfig) { + shutDownActiveTunnel(tunnelConfig) appDataRepository.tunnels.save(tunnelConfig.copy(isActive = true)) emitVpnStateConfig(tunnelConfig) + resetBackendStatistics() startStatsJob() - Timber.d("Updating start") - serviceManager.requestTunnelTileUpdate() } private suspend fun onBeforeStop(tunnelConfig: TunnelConfig) { + appDataRepository.tunnels.save(tunnelConfig.copy(isActive = false)) cancelStatsJob() resetBackendStatistics() - appDataRepository.tunnels.save(tunnelConfig.copy(isActive = false)) - serviceManager.stopBackgroundService() - Timber.d("UPdating stop") - serviceManager.requestTunnelTileUpdate() } private fun emitTunnelState(state: TunnelState) {