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

View File

@ -91,7 +91,8 @@ constructor(
override suspend fun startTunnel(tunnelConfig: TunnelConfig, background: Boolean): Result<TunnelState> {
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()
appDataRepository.tunnels.save(tunnelConfig.copy(isActive = true))
emitVpnStateConfig(tunnelConfig)
startStatsJob()
Timber.d("Updating start")
}
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()
}
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) {