change auto tunnel to not watch vpn state
This commit is contained in:
parent
a5e60c3fbe
commit
2df1bb07ab
|
@ -2,83 +2,75 @@ package com.zaneschepke.wireguardautotunnel.service.foreground
|
||||||
|
|
||||||
import com.zaneschepke.wireguardautotunnel.data.domain.Settings
|
import com.zaneschepke.wireguardautotunnel.data.domain.Settings
|
||||||
import com.zaneschepke.wireguardautotunnel.data.domain.TunnelConfig
|
import com.zaneschepke.wireguardautotunnel.data.domain.TunnelConfig
|
||||||
import com.zaneschepke.wireguardautotunnel.service.tunnel.TunnelState
|
|
||||||
|
|
||||||
data class WatcherState(
|
data class WatcherState(
|
||||||
val isWifiConnected: Boolean = false,
|
val isWifiConnected: Boolean = false,
|
||||||
val config: TunnelConfig? = null,
|
val config: TunnelConfig? = null,
|
||||||
val vpnStatus: TunnelState = TunnelState.DOWN,
|
//val vpnStatus: TunnelState = TunnelState.DOWN,
|
||||||
val isEthernetConnected: Boolean = false,
|
val isEthernetConnected: Boolean = false,
|
||||||
val isMobileDataConnected: Boolean = false,
|
val isMobileDataConnected: Boolean = false,
|
||||||
val currentNetworkSSID: String = "",
|
val currentNetworkSSID: String = "",
|
||||||
val settings: Settings = Settings()
|
val settings: Settings = Settings()
|
||||||
) {
|
) {
|
||||||
|
|
||||||
private fun isVpnConnected() = vpnStatus == TunnelState.UP
|
//private fun isVpnConnected() = vpnStatus == TunnelState.UP
|
||||||
fun isEthernetConditionMet(): Boolean {
|
fun isEthernetConditionMet(): Boolean {
|
||||||
return (isEthernetConnected &&
|
return (isEthernetConnected &&
|
||||||
settings.isTunnelOnEthernetEnabled &&
|
settings.isTunnelOnEthernetEnabled)
|
||||||
!isVpnConnected())
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fun isMobileDataConditionMet(): Boolean {
|
fun isMobileDataConditionMet(): Boolean {
|
||||||
return (!isEthernetConnected &&
|
return (!isEthernetConnected &&
|
||||||
settings.isTunnelOnMobileDataEnabled &&
|
settings.isTunnelOnMobileDataEnabled &&
|
||||||
!isWifiConnected &&
|
!isWifiConnected &&
|
||||||
isMobileDataConnected &&
|
isMobileDataConnected)
|
||||||
!isVpnConnected())
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fun isTunnelNotMobileDataPreferredConditionMet(): Boolean {
|
fun isTunnelOnMobileDataPreferredConditionMet(): Boolean {
|
||||||
return (!isEthernetConnected &&
|
return (!isEthernetConnected &&
|
||||||
settings.isTunnelOnMobileDataEnabled &&
|
settings.isTunnelOnMobileDataEnabled &&
|
||||||
!isWifiConnected &&
|
!isWifiConnected &&
|
||||||
isMobileDataConnected &&
|
isMobileDataConnected &&
|
||||||
config?.isMobileDataTunnel == false && isVpnConnected())
|
config?.isMobileDataTunnel == false)
|
||||||
}
|
}
|
||||||
|
|
||||||
fun isTunnelOffOnMobileDataConditionMet(): Boolean {
|
fun isTunnelOffOnMobileDataConditionMet(): Boolean {
|
||||||
return (!isEthernetConnected &&
|
return (!isEthernetConnected &&
|
||||||
!settings.isTunnelOnMobileDataEnabled &&
|
!settings.isTunnelOnMobileDataEnabled &&
|
||||||
isMobileDataConnected &&
|
isMobileDataConnected &&
|
||||||
!isWifiConnected &&
|
!isWifiConnected)
|
||||||
isVpnConnected())
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fun isUntrustedWifiConditionMet(): Boolean {
|
fun isUntrustedWifiConditionMet(): Boolean {
|
||||||
return (!isEthernetConnected &&
|
return (!isEthernetConnected &&
|
||||||
isWifiConnected &&
|
isWifiConnected &&
|
||||||
!settings.trustedNetworkSSIDs.contains(currentNetworkSSID) &&
|
!settings.trustedNetworkSSIDs.contains(currentNetworkSSID) &&
|
||||||
settings.isTunnelOnWifiEnabled
|
settings.isTunnelOnWifiEnabled)
|
||||||
&& !isVpnConnected())
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fun isTunnelNotWifiNamePreferredMet(ssid: String): Boolean {
|
fun isTunnelNotWifiNamePreferredMet(ssid: String): Boolean {
|
||||||
return (!isEthernetConnected &&
|
return (!isEthernetConnected &&
|
||||||
isWifiConnected &&
|
isWifiConnected &&
|
||||||
!settings.trustedNetworkSSIDs.contains(currentNetworkSSID) &&
|
!settings.trustedNetworkSSIDs.contains(currentNetworkSSID) &&
|
||||||
settings.isTunnelOnWifiEnabled && config?.tunnelNetworks?.contains(ssid) == false && isVpnConnected())
|
settings.isTunnelOnWifiEnabled && config?.tunnelNetworks?.contains(ssid) == false)
|
||||||
}
|
}
|
||||||
|
|
||||||
fun isTrustedWifiConditionMet(): Boolean {
|
fun isTrustedWifiConditionMet(): Boolean {
|
||||||
return (!isEthernetConnected &&
|
return (!isEthernetConnected &&
|
||||||
(isWifiConnected &&
|
(isWifiConnected &&
|
||||||
settings.trustedNetworkSSIDs.contains(currentNetworkSSID)) &&
|
settings.trustedNetworkSSIDs.contains(currentNetworkSSID)))
|
||||||
(isVpnConnected()))
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fun isTunnelOffOnWifiConditionMet(): Boolean {
|
fun isTunnelOffOnWifiConditionMet(): Boolean {
|
||||||
return (!isEthernetConnected &&
|
return (!isEthernetConnected &&
|
||||||
(isWifiConnected &&
|
(isWifiConnected &&
|
||||||
!settings.isTunnelOnWifiEnabled &&
|
!settings.isTunnelOnWifiEnabled))
|
||||||
(isVpnConnected())))
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fun isTunnelOffOnNoConnectivityMet(): Boolean {
|
fun isTunnelOffOnNoConnectivityMet(): Boolean {
|
||||||
return (!isEthernetConnected &&
|
return (!isEthernetConnected &&
|
||||||
!isWifiConnected &&
|
!isWifiConnected &&
|
||||||
!isMobileDataConnected &&
|
!isMobileDataConnected)
|
||||||
(isVpnConnected()))
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -25,7 +25,6 @@ import kotlinx.coroutines.delay
|
||||||
import kotlinx.coroutines.flow.MutableStateFlow
|
import kotlinx.coroutines.flow.MutableStateFlow
|
||||||
import kotlinx.coroutines.flow.collectLatest
|
import kotlinx.coroutines.flow.collectLatest
|
||||||
import kotlinx.coroutines.launch
|
import kotlinx.coroutines.launch
|
||||||
import kotlinx.coroutines.withContext
|
|
||||||
import timber.log.Timber
|
import timber.log.Timber
|
||||||
import java.net.InetAddress
|
import java.net.InetAddress
|
||||||
import javax.inject.Inject
|
import javax.inject.Inject
|
||||||
|
@ -237,7 +236,7 @@ class WireGuardConnectivityWatcherService : ForegroundService() {
|
||||||
Timber.i("Restarting VPN for ping failure")
|
Timber.i("Restarting VPN for ping failure")
|
||||||
serviceManager.stopVpnServiceForeground(this)
|
serviceManager.stopVpnServiceForeground(this)
|
||||||
delay(Constants.VPN_RESTART_DELAY)
|
delay(Constants.VPN_RESTART_DELAY)
|
||||||
serviceManager.startVpnServiceForeground(this)
|
serviceManager.startVpnServiceForeground(this, it.id)
|
||||||
delay(Constants.PING_COOLDOWN)
|
delay(Constants.PING_COOLDOWN)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -268,7 +267,6 @@ class WireGuardConnectivityWatcherService : ForegroundService() {
|
||||||
vpnService.vpnState.collect {
|
vpnService.vpnState.collect {
|
||||||
networkEventsFlow.value =
|
networkEventsFlow.value =
|
||||||
networkEventsFlow.value.copy(
|
networkEventsFlow.value.copy(
|
||||||
vpnStatus = it.status,
|
|
||||||
config = it.tunnelConfig,
|
config = it.tunnelConfig,
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
@ -353,6 +351,10 @@ class WireGuardConnectivityWatcherService : ForegroundService() {
|
||||||
return appDataRepository.tunnels.findByTunnelNetworksName(ssid).firstOrNull()
|
return appDataRepository.tunnels.findByTunnelNetworksName(ssid).firstOrNull()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private fun isTunnelDown() : Boolean {
|
||||||
|
return vpnService.vpnState.value.status == TunnelState.DOWN
|
||||||
|
}
|
||||||
|
|
||||||
private suspend fun manageVpn() {
|
private suspend fun manageVpn() {
|
||||||
networkEventsFlow.collectLatest { watcherState ->
|
networkEventsFlow.collectLatest { watcherState ->
|
||||||
val autoTunnel = "Auto-tunnel watcher"
|
val autoTunnel = "Auto-tunnel watcher"
|
||||||
|
@ -362,18 +364,18 @@ class WireGuardConnectivityWatcherService : ForegroundService() {
|
||||||
when {
|
when {
|
||||||
watcherState.isEthernetConditionMet() -> {
|
watcherState.isEthernetConditionMet() -> {
|
||||||
Timber.i("$autoTunnel - tunnel on on ethernet condition met")
|
Timber.i("$autoTunnel - tunnel on on ethernet condition met")
|
||||||
serviceManager.startVpnServiceForeground(this)
|
if(isTunnelDown()) serviceManager.startVpnServiceForeground(this)
|
||||||
}
|
}
|
||||||
|
|
||||||
watcherState.isMobileDataConditionMet() -> {
|
watcherState.isMobileDataConditionMet() -> {
|
||||||
Timber.i("$autoTunnel - tunnel on on mobile data condition met")
|
Timber.i("$autoTunnel - tunnel on on mobile data condition met")
|
||||||
serviceManager.startVpnServiceForeground(this, getMobileDataTunnel()?.id)
|
if(isTunnelDown()) serviceManager.startVpnServiceForeground(this, getMobileDataTunnel()?.id)
|
||||||
}
|
}
|
||||||
|
|
||||||
watcherState.isTunnelNotMobileDataPreferredConditionMet() -> {
|
watcherState.isTunnelOnMobileDataPreferredConditionMet() -> {
|
||||||
getMobileDataTunnel()?.let {
|
getMobileDataTunnel()?.let {
|
||||||
Timber.i("$autoTunnel - tunnel connected on mobile data is not preferred condition met, switching to preferred")
|
Timber.i("$autoTunnel - tunnel connected on mobile data is not preferred condition met, switching to preferred")
|
||||||
serviceManager.startVpnServiceForeground(
|
if(isTunnelDown()) serviceManager.startVpnServiceForeground(
|
||||||
this,
|
this,
|
||||||
getMobileDataTunnel()?.id,
|
getMobileDataTunnel()?.id,
|
||||||
)
|
)
|
||||||
|
@ -382,25 +384,25 @@ class WireGuardConnectivityWatcherService : ForegroundService() {
|
||||||
|
|
||||||
watcherState.isTunnelOffOnMobileDataConditionMet() -> {
|
watcherState.isTunnelOffOnMobileDataConditionMet() -> {
|
||||||
Timber.i("$autoTunnel - tunnel off on mobile data met, turning vpn off")
|
Timber.i("$autoTunnel - tunnel off on mobile data met, turning vpn off")
|
||||||
serviceManager.stopVpnServiceForeground(this)
|
if(!isTunnelDown()) serviceManager.stopVpnServiceForeground(this)
|
||||||
}
|
}
|
||||||
|
|
||||||
watcherState.isTunnelNotWifiNamePreferredMet(watcherState.currentNetworkSSID) -> {
|
watcherState.isTunnelNotWifiNamePreferredMet(watcherState.currentNetworkSSID) -> {
|
||||||
Timber.i("$autoTunnel - tunnel on ssid not associated with current tunnel condition met")
|
Timber.i("$autoTunnel - tunnel on ssid not associated with current tunnel condition met")
|
||||||
getSsidTunnel(watcherState.currentNetworkSSID)?.let {
|
getSsidTunnel(watcherState.currentNetworkSSID)?.let {
|
||||||
Timber.i("Found tunnel associated with this SSID, bringing tunnel up")
|
Timber.i("Found tunnel associated with this SSID, bringing tunnel up")
|
||||||
serviceManager.startVpnServiceForeground(this, it.id)
|
if(isTunnelDown()) serviceManager.startVpnServiceForeground(this, it.id)
|
||||||
} ?: suspend {
|
} ?: suspend {
|
||||||
Timber.i("No tunnel associated with this SSID, using defaults")
|
Timber.i("No tunnel associated with this SSID, using defaults")
|
||||||
if (appDataRepository.getPrimaryOrFirstTunnel()?.name != vpnService.name) {
|
if (appDataRepository.getPrimaryOrFirstTunnel()?.name != vpnService.name) {
|
||||||
serviceManager.startVpnServiceForeground(this)
|
if(isTunnelDown()) serviceManager.startVpnServiceForeground(this)
|
||||||
}
|
}
|
||||||
}.invoke()
|
}.invoke()
|
||||||
}
|
}
|
||||||
|
|
||||||
watcherState.isUntrustedWifiConditionMet() -> {
|
watcherState.isUntrustedWifiConditionMet() -> {
|
||||||
Timber.i("$autoTunnel - tunnel on untrusted wifi condition met")
|
Timber.i("$autoTunnel - tunnel on untrusted wifi condition met")
|
||||||
serviceManager.startVpnServiceForeground(
|
if(isTunnelDown()) serviceManager.startVpnServiceForeground(
|
||||||
this,
|
this,
|
||||||
getSsidTunnel(watcherState.currentNetworkSSID)?.id,
|
getSsidTunnel(watcherState.currentNetworkSSID)?.id,
|
||||||
)
|
)
|
||||||
|
@ -408,17 +410,17 @@ class WireGuardConnectivityWatcherService : ForegroundService() {
|
||||||
|
|
||||||
watcherState.isTrustedWifiConditionMet() -> {
|
watcherState.isTrustedWifiConditionMet() -> {
|
||||||
Timber.i("$autoTunnel - tunnel off on trusted wifi condition met, turning vpn off")
|
Timber.i("$autoTunnel - tunnel off on trusted wifi condition met, turning vpn off")
|
||||||
serviceManager.stopVpnServiceForeground(this)
|
if(!isTunnelDown()) serviceManager.stopVpnServiceForeground(this)
|
||||||
}
|
}
|
||||||
|
|
||||||
watcherState.isTunnelOffOnWifiConditionMet() -> {
|
watcherState.isTunnelOffOnWifiConditionMet() -> {
|
||||||
Timber.i("$autoTunnel - tunnel off on wifi condition met, turning vpn off")
|
Timber.i("$autoTunnel - tunnel off on wifi condition met, turning vpn off")
|
||||||
serviceManager.stopVpnServiceForeground(this)
|
if(!isTunnelDown()) serviceManager.stopVpnServiceForeground(this)
|
||||||
}
|
}
|
||||||
|
|
||||||
watcherState.isTunnelOffOnNoConnectivityMet() -> {
|
watcherState.isTunnelOffOnNoConnectivityMet() -> {
|
||||||
Timber.i("$autoTunnel - tunnel off on no connectivity met, turning vpn off")
|
Timber.i("$autoTunnel - tunnel off on no connectivity met, turning vpn off")
|
||||||
serviceManager.stopVpnServiceForeground(this)
|
if(!isTunnelDown()) serviceManager.stopVpnServiceForeground(this)
|
||||||
}
|
}
|
||||||
|
|
||||||
else -> {
|
else -> {
|
||||||
|
|
Loading…
Reference in New Issue