Skip to content

Commit

Permalink
fix: auto tunnel crash
Browse files Browse the repository at this point in the history
  • Loading branch information
zaneschepke committed Nov 8, 2024
1 parent dad34b9 commit 7d810c7
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,6 @@ class TunnelModule {
@Singleton
@Provides
fun provideServiceManager(@ApplicationContext context: Context): ServiceManager {
return ServiceManager(context)
return ServiceManager.getInstance(context)
}
}
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -109,6 +110,8 @@ constructor(
}.onFailure {
Timber.e(it)
onStopFailed()
}.also {
stopBackgroundService()
}
}
}
Expand Down Expand Up @@ -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) {
Expand Down

0 comments on commit 7d810c7

Please sign in to comment.