fix: auto tunnel crash

This commit is contained in:
Zane Schepke 2024-11-07 20:02:35 -05:00
parent dad34b9e24
commit 7d810c7c3d
3 changed files with 29 additions and 19 deletions

View File

@ -82,6 +82,6 @@ class TunnelModule {
@Singleton @Singleton
@Provides @Provides
fun provideServiceManager(@ApplicationContext context: Context): ServiceManager { fun provideServiceManager(@ApplicationContext context: Context): ServiceManager {
return ServiceManager(context) return ServiceManager.getInstance(context)
} }
} }

View File

@ -1,6 +1,5 @@
package com.zaneschepke.wireguardautotunnel.service.foreground package com.zaneschepke.wireguardautotunnel.service.foreground
import android.content.Context
import android.content.Intent import android.content.Intent
import android.net.NetworkCapabilities import android.net.NetworkCapabilities
import android.os.IBinder import android.os.IBinder
@ -162,7 +161,7 @@ class AutoTunnelService : LifecycleService() {
private fun initWakeLock() { private fun initWakeLock() {
wakeLock = wakeLock =
(getSystemService(Context.POWER_SERVICE) as PowerManager).run { (getSystemService(POWER_SERVICE) as PowerManager).run {
val tag = this.javaClass.name val tag = this.javaClass.name
newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "$tag::lock").apply { newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "$tag::lock").apply {
try { try {

View File

@ -91,7 +91,8 @@ constructor(
override suspend fun startTunnel(tunnelConfig: TunnelConfig, background: Boolean): Result<TunnelState> { override suspend fun startTunnel(tunnelConfig: TunnelConfig, background: Boolean): Result<TunnelState> {
return withContext(ioDispatcher) { return withContext(ioDispatcher) {
onBeforeStart(tunnelConfig, background) onBeforeStart(tunnelConfig)
if (background) startBackgroundService()
setState(tunnelConfig, TunnelState.UP).onSuccess { setState(tunnelConfig, TunnelState.UP).onSuccess {
emitTunnelState(it) emitTunnelState(it)
}.onFailure { }.onFailure {
@ -109,6 +110,8 @@ constructor(
}.onFailure { }.onFailure {
Timber.e(it) Timber.e(it)
onStopFailed() onStopFailed()
}.also {
stopBackgroundService()
} }
} }
} }
@ -145,28 +148,36 @@ constructor(
resetBackendStatistics() resetBackendStatistics()
} }
private suspend fun onBeforeStart(tunnelConfig: TunnelConfig, background: Boolean) { private suspend fun shutDownActiveTunnel(config: TunnelConfig) {
if (_vpnState.value.status == TunnelState.UP && with(_vpnState.value) {
tunnelConfig != _vpnState.value.tunnelConfig if (status == TunnelState.UP && tunnelConfig != config) {
) { tunnelConfig?.let { stopTunnel(it) }
vpnState.value.tunnelConfig?.let { stopTunnel(it) } }
} }
if (background) serviceManager.startBackgroundService() }
resetBackendStatistics()
appDataRepository.tunnels.save(tunnelConfig.copy(isActive = true)) private suspend fun startBackgroundService() {
emitVpnStateConfig(tunnelConfig) serviceManager.startBackgroundService()
startStatsJob()
Timber.d("Updating start")
serviceManager.requestTunnelTileUpdate() 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()
}
private suspend fun onBeforeStop(tunnelConfig: TunnelConfig) { private suspend fun onBeforeStop(tunnelConfig: TunnelConfig) {
appDataRepository.tunnels.save(tunnelConfig.copy(isActive = false))
cancelStatsJob() cancelStatsJob()
resetBackendStatistics() resetBackendStatistics()
appDataRepository.tunnels.save(tunnelConfig.copy(isActive = false))
serviceManager.stopBackgroundService()
Timber.d("UPdating stop")
serviceManager.requestTunnelTileUpdate()
} }
private fun emitTunnelState(state: TunnelState) { private fun emitTunnelState(state: TunnelState) {