fix: restart tunnel on boot with aovpn

closes #292

also fixed bug where pausing auto tunnel did not pause pinger
This commit is contained in:
Zane Schepke 2024-09-19 19:09:33 -04:00
parent a9d7648425
commit 911ed140e0
4 changed files with 15 additions and 8 deletions

View File

@ -228,8 +228,7 @@ val incrementVersionCode by tasks.registering {
} }
tasks.whenTaskAdded { tasks.whenTaskAdded {
if (name.startsWith("assemble")) { if (name.startsWith("assemble") && !name.lowercase().contains("debug")) {
if (name.contains("debug")) return@whenTaskAdded
dependsOn(incrementVersionCode) dependsOn(incrementVersionCode)
} }
} }

View File

@ -7,6 +7,7 @@ import com.zaneschepke.wireguardautotunnel.data.repository.AppDataRepository
import com.zaneschepke.wireguardautotunnel.module.ApplicationScope import com.zaneschepke.wireguardautotunnel.module.ApplicationScope
import com.zaneschepke.wireguardautotunnel.service.foreground.ServiceManager import com.zaneschepke.wireguardautotunnel.service.foreground.ServiceManager
import com.zaneschepke.wireguardautotunnel.service.tunnel.TunnelService import com.zaneschepke.wireguardautotunnel.service.tunnel.TunnelService
import com.zaneschepke.wireguardautotunnel.service.tunnel.TunnelState
import com.zaneschepke.wireguardautotunnel.util.extensions.startTunnelBackground import com.zaneschepke.wireguardautotunnel.util.extensions.startTunnelBackground
import dagger.hilt.android.AndroidEntryPoint import dagger.hilt.android.AndroidEntryPoint
import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.CoroutineScope
@ -33,7 +34,9 @@ class BootReceiver : BroadcastReceiver() {
with(appDataRepository.settings.getSettings()) { with(appDataRepository.settings.getSettings()) {
if (isRestoreOnBootEnabled) { if (isRestoreOnBootEnabled) {
val activeTunnels = appDataRepository.tunnels.getActive() val activeTunnels = appDataRepository.tunnels.getActive()
if (activeTunnels.isNotEmpty()) { val tunState = tunnelService.get().vpnState.value.status
if (activeTunnels.isNotEmpty() && tunState != TunnelState.UP) {
Timber.i("Starting previously active tunnel")
context.startTunnelBackground(activeTunnels.first().id) context.startTunnelBackground(activeTunnels.first().id)
} }
if (isAutoTunnelEnabled) { if (isAutoTunnelEnabled) {

View File

@ -253,7 +253,8 @@ class AutoTunnelService : LifecycleService() {
runCatching { runCatching {
do { do {
val vpnState = tunnelService.get().vpnState.value val vpnState = tunnelService.get().vpnState.value
if (vpnState.status == TunnelState.UP) { val settings = appDataRepository.settings.getSettings()
if (vpnState.status == TunnelState.UP && !settings.isAutoTunnelPaused) {
if (vpnState.tunnelConfig != null) { if (vpnState.tunnelConfig != null) {
val config = TunnelConfig.configFromWgQuick(vpnState.tunnelConfig.wgQuick) val config = TunnelConfig.configFromWgQuick(vpnState.tunnelConfig.wgQuick)
val results = if (vpnState.tunnelConfig.pingIp != null) { val results = if (vpnState.tunnelConfig.pingIp != null) {

View File

@ -4,15 +4,18 @@ import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope import androidx.lifecycle.viewModelScope
import androidx.navigation.NavHostController import androidx.navigation.NavHostController
import com.zaneschepke.wireguardautotunnel.data.repository.AppDataRepository import com.zaneschepke.wireguardautotunnel.data.repository.AppDataRepository
import com.zaneschepke.wireguardautotunnel.module.IoDispatcher
import com.zaneschepke.wireguardautotunnel.service.tunnel.TunnelService import com.zaneschepke.wireguardautotunnel.service.tunnel.TunnelService
import com.zaneschepke.wireguardautotunnel.util.Constants import com.zaneschepke.wireguardautotunnel.util.Constants
import com.zaneschepke.wireguardautotunnel.util.extensions.TunnelConfigs import com.zaneschepke.wireguardautotunnel.util.extensions.TunnelConfigs
import dagger.hilt.android.lifecycle.HiltViewModel import dagger.hilt.android.lifecycle.HiltViewModel
import kotlinx.coroutines.CoroutineDispatcher
import kotlinx.coroutines.flow.MutableStateFlow import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.SharingStarted import kotlinx.coroutines.flow.SharingStarted
import kotlinx.coroutines.flow.combine import kotlinx.coroutines.flow.combine
import kotlinx.coroutines.flow.stateIn import kotlinx.coroutines.flow.stateIn
import kotlinx.coroutines.launch import kotlinx.coroutines.launch
import kotlinx.coroutines.plus
import xyz.teamgravity.pin_lock_compose.PinManager import xyz.teamgravity.pin_lock_compose.PinManager
import javax.inject.Inject import javax.inject.Inject
@ -21,8 +24,9 @@ class AppViewModel
@Inject @Inject
constructor( constructor(
private val appDataRepository: AppDataRepository, private val appDataRepository: AppDataRepository,
private val tunnelService: TunnelService, tunnelService: TunnelService,
val navHostController: NavHostController, val navHostController: NavHostController,
@IoDispatcher private val ioDispatcher: CoroutineDispatcher,
) : ViewModel() { ) : ViewModel() {
private val _appUiState = MutableStateFlow(AppUiState()) private val _appUiState = MutableStateFlow(AppUiState())
@ -42,12 +46,12 @@ constructor(
) )
} }
.stateIn( .stateIn(
viewModelScope, viewModelScope + ioDispatcher,
SharingStarted.WhileSubscribed(Constants.SUBSCRIPTION_TIMEOUT), SharingStarted.WhileSubscribed(Constants.SUBSCRIPTION_TIMEOUT),
_appUiState.value, _appUiState.value,
) )
fun setTunnels(tunnels: TunnelConfigs) = viewModelScope.launch { fun setTunnels(tunnels: TunnelConfigs) = viewModelScope.launch(ioDispatcher) {
_appUiState.emit( _appUiState.emit(
_appUiState.value.copy( _appUiState.value.copy(
tunnels = tunnels, tunnels = tunnels,
@ -55,7 +59,7 @@ constructor(
) )
} }
fun onPinLockDisabled() = viewModelScope.launch { fun onPinLockDisabled() = viewModelScope.launch(ioDispatcher) {
PinManager.clearPin() PinManager.clearPin()
appDataRepository.appState.setPinLockEnabled(false) appDataRepository.appState.setPinLockEnabled(false)
} }