Skip to content

Commit

Permalink
fix: restart tunnel on boot with aovpn
Browse files Browse the repository at this point in the history
closes #292

also fixed bug where pausing auto tunnel did not pause pinger
  • Loading branch information
zaneschepke committed Sep 19, 2024
1 parent a9d7648 commit 911ed14
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 8 deletions.
3 changes: 1 addition & 2 deletions app/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -228,8 +228,7 @@ val incrementVersionCode by tasks.registering {
}

tasks.whenTaskAdded {
if (name.startsWith("assemble")) {
if (name.contains("debug")) return@whenTaskAdded
if (name.startsWith("assemble") && !name.lowercase().contains("debug")) {
dependsOn(incrementVersionCode)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import com.zaneschepke.wireguardautotunnel.data.repository.AppDataRepository
import com.zaneschepke.wireguardautotunnel.module.ApplicationScope
import com.zaneschepke.wireguardautotunnel.service.foreground.ServiceManager
import com.zaneschepke.wireguardautotunnel.service.tunnel.TunnelService
import com.zaneschepke.wireguardautotunnel.service.tunnel.TunnelState
import com.zaneschepke.wireguardautotunnel.util.extensions.startTunnelBackground
import dagger.hilt.android.AndroidEntryPoint
import kotlinx.coroutines.CoroutineScope
Expand All @@ -33,7 +34,9 @@ class BootReceiver : BroadcastReceiver() {
with(appDataRepository.settings.getSettings()) {
if (isRestoreOnBootEnabled) {
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)
}
if (isAutoTunnelEnabled) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -253,7 +253,8 @@ class AutoTunnelService : LifecycleService() {
runCatching {
do {
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) {
val config = TunnelConfig.configFromWgQuick(vpnState.tunnelConfig.wgQuick)
val results = if (vpnState.tunnelConfig.pingIp != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,18 @@ import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import androidx.navigation.NavHostController
import com.zaneschepke.wireguardautotunnel.data.repository.AppDataRepository
import com.zaneschepke.wireguardautotunnel.module.IoDispatcher
import com.zaneschepke.wireguardautotunnel.service.tunnel.TunnelService
import com.zaneschepke.wireguardautotunnel.util.Constants
import com.zaneschepke.wireguardautotunnel.util.extensions.TunnelConfigs
import dagger.hilt.android.lifecycle.HiltViewModel
import kotlinx.coroutines.CoroutineDispatcher
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.SharingStarted
import kotlinx.coroutines.flow.combine
import kotlinx.coroutines.flow.stateIn
import kotlinx.coroutines.launch
import kotlinx.coroutines.plus
import xyz.teamgravity.pin_lock_compose.PinManager
import javax.inject.Inject

Expand All @@ -21,8 +24,9 @@ class AppViewModel
@Inject
constructor(
private val appDataRepository: AppDataRepository,
private val tunnelService: TunnelService,
tunnelService: TunnelService,
val navHostController: NavHostController,
@IoDispatcher private val ioDispatcher: CoroutineDispatcher,
) : ViewModel() {

private val _appUiState = MutableStateFlow(AppUiState())
Expand All @@ -42,20 +46,20 @@ constructor(
)
}
.stateIn(
viewModelScope,
viewModelScope + ioDispatcher,
SharingStarted.WhileSubscribed(Constants.SUBSCRIPTION_TIMEOUT),
_appUiState.value,
)

fun setTunnels(tunnels: TunnelConfigs) = viewModelScope.launch {
fun setTunnels(tunnels: TunnelConfigs) = viewModelScope.launch(ioDispatcher) {
_appUiState.emit(
_appUiState.value.copy(
tunnels = tunnels,
),
)
}

fun onPinLockDisabled() = viewModelScope.launch {
fun onPinLockDisabled() = viewModelScope.launch(ioDispatcher) {
PinManager.clearPin()
appDataRepository.appState.setPinLockEnabled(false)
}
Expand Down

0 comments on commit 911ed14

Please sign in to comment.