From 89212fe191b5e3ade314be6500eb3651b7670d61 Mon Sep 17 00:00:00 2001 From: Zane Schepke Date: Fri, 7 Jul 2023 23:11:15 -0400 Subject: [PATCH] fix: fix bug causing inconsistent behavior after making trusted ssid changes, fix bug causing vpn not to disconnect on mobile data This fixes a bug causing inconsistent connects and disconnects to vpn on wifi capability changes by properly associated a child job to a parent job using a SupervisorJob instead of a GlobalScope job. This fixes a bug causing VPN to stay connected on mobile data even when the setting is disabled by adding an additional check for this situation to disable the tunnel. --- app/build.gradle.kts | 2 +- .../wireguardautotunnel/BootReceiver.kt | 5 ++-- .../WireGuardConnectivityWatcherService.kt | 30 +++++++++++-------- .../foreground/WireGuardTunnelService.kt | 6 ++-- 4 files changed, 23 insertions(+), 20 deletions(-) diff --git a/app/build.gradle.kts b/app/build.gradle.kts index c53e412..9169f10 100644 --- a/app/build.gradle.kts +++ b/app/build.gradle.kts @@ -17,7 +17,7 @@ android { val versionMajor = 2 val versionMinor = 0 - val versionPatch = 2 + val versionPatch = 3 val versionBuild = 0 defaultConfig { diff --git a/app/src/main/java/com/zaneschepke/wireguardautotunnel/BootReceiver.kt b/app/src/main/java/com/zaneschepke/wireguardautotunnel/BootReceiver.kt index 78c146b..e1d35f8 100644 --- a/app/src/main/java/com/zaneschepke/wireguardautotunnel/BootReceiver.kt +++ b/app/src/main/java/com/zaneschepke/wireguardautotunnel/BootReceiver.kt @@ -10,8 +10,9 @@ import com.zaneschepke.wireguardautotunnel.service.foreground.WireGuardConnectiv import com.zaneschepke.wireguardautotunnel.service.tunnel.model.Settings import com.zaneschepke.wireguardautotunnel.service.tunnel.model.TunnelConfig import dagger.hilt.android.AndroidEntryPoint +import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.DelicateCoroutinesApi -import kotlinx.coroutines.GlobalScope +import kotlinx.coroutines.SupervisorJob import kotlinx.coroutines.cancel import kotlinx.coroutines.launch import javax.inject.Inject @@ -25,7 +26,7 @@ class BootReceiver : BroadcastReceiver() { @OptIn(DelicateCoroutinesApi::class) override fun onReceive(context: Context, intent: Intent) { if (intent.action == Intent.ACTION_BOOT_COMPLETED) { - GlobalScope.launch { + CoroutineScope(SupervisorJob()).launch { try { val settings = settingsRepo.getAll() if (!settings.isNullOrEmpty()) { diff --git a/app/src/main/java/com/zaneschepke/wireguardautotunnel/service/foreground/WireGuardConnectivityWatcherService.kt b/app/src/main/java/com/zaneschepke/wireguardautotunnel/service/foreground/WireGuardConnectivityWatcherService.kt index 3463189..9845fbf 100644 --- a/app/src/main/java/com/zaneschepke/wireguardautotunnel/service/foreground/WireGuardConnectivityWatcherService.kt +++ b/app/src/main/java/com/zaneschepke/wireguardautotunnel/service/foreground/WireGuardConnectivityWatcherService.kt @@ -19,9 +19,10 @@ import com.zaneschepke.wireguardautotunnel.service.notification.NotificationServ import com.zaneschepke.wireguardautotunnel.service.tunnel.VpnService import com.zaneschepke.wireguardautotunnel.service.tunnel.model.Settings import dagger.hilt.android.AndroidEntryPoint +import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.DelicateCoroutinesApi -import kotlinx.coroutines.GlobalScope import kotlinx.coroutines.Job +import kotlinx.coroutines.SupervisorJob import kotlinx.coroutines.launch import timber.log.Timber import javax.inject.Inject @@ -99,9 +100,7 @@ class WireGuardConnectivityWatcherService : ForegroundService() { //try to start task again if killed override fun onTaskRemoved(rootIntent: Intent) { Timber.d("Task Removed called") - val restartServiceIntent = Intent(applicationContext, this::class.java).also { - it.setPackage(packageName) - }; + val restartServiceIntent = Intent(rootIntent) val restartServicePendingIntent: PendingIntent = PendingIntent.getService(this, 1, restartServiceIntent, PendingIntent.FLAG_ONE_SHOT or PendingIntent.FLAG_IMMUTABLE); applicationContext.getSystemService(Context.ALARM_SERVICE); @@ -126,16 +125,16 @@ class WireGuardConnectivityWatcherService : ForegroundService() { @OptIn(DelicateCoroutinesApi::class) private fun startWatcherJob() { - watcherJob = GlobalScope.launch { + watcherJob = CoroutineScope(SupervisorJob()).launch { val settings = settingsRepo.getAll(); if(!settings.isNullOrEmpty()) { setting = settings[0] } - GlobalScope.launch { + CoroutineScope(watcherJob).launch { watchForWifiConnectivityChanges() } if(setting.isTunnelOnMobileDataEnabled) { - GlobalScope.launch { + CoroutineScope(watcherJob).launch { watchForMobileDataConnectivityChanges() } } @@ -161,7 +160,6 @@ class WireGuardConnectivityWatcherService : ForegroundService() { if(!isWifiConnected && vpnService.getState() == Tunnel.State.UP) stopVPN() Timber.d("Lost mobile data connection") } - else -> {} } } } @@ -195,13 +193,19 @@ class WireGuardConnectivityWatcherService : ForegroundService() { is NetworkStatus.Unavailable -> { isWifiConnected = false Timber.d("Lost Wi-Fi connection") - if(setting.isTunnelOnMobileDataEnabled && vpnService.getState() == Tunnel.State.DOWN - && isMobileDataConnected){ - Timber.d("Wifi not available so starting vpn for mobile data") - startVPN() + if(!connecting || !disconnecting) { + if(setting.isTunnelOnMobileDataEnabled && vpnService.getState() == Tunnel.State.DOWN + && isMobileDataConnected){ + Timber.d("Wifi not available so starting vpn for mobile data") + startVPN() + } + if(!setting.isTunnelOnMobileDataEnabled && vpnService.getState() == Tunnel.State.UP) { + Timber.d("Lost WiFi connection, disabling vpn") + stopVPN() + } } + } - else -> {} } } } diff --git a/app/src/main/java/com/zaneschepke/wireguardautotunnel/service/foreground/WireGuardTunnelService.kt b/app/src/main/java/com/zaneschepke/wireguardautotunnel/service/foreground/WireGuardTunnelService.kt index bd690da..816bf3a 100644 --- a/app/src/main/java/com/zaneschepke/wireguardautotunnel/service/foreground/WireGuardTunnelService.kt +++ b/app/src/main/java/com/zaneschepke/wireguardautotunnel/service/foreground/WireGuardTunnelService.kt @@ -8,10 +8,9 @@ import com.zaneschepke.wireguardautotunnel.service.tunnel.VpnService import com.zaneschepke.wireguardautotunnel.service.tunnel.model.TunnelConfig import dagger.hilt.android.AndroidEntryPoint import kotlinx.coroutines.CoroutineScope -import kotlinx.coroutines.DelicateCoroutinesApi import kotlinx.coroutines.Dispatchers -import kotlinx.coroutines.GlobalScope import kotlinx.coroutines.Job +import kotlinx.coroutines.SupervisorJob import kotlinx.coroutines.launch import timber.log.Timber import javax.inject.Inject @@ -29,12 +28,11 @@ class WireGuardTunnelService : ForegroundService() { private lateinit var job : Job - @OptIn(DelicateCoroutinesApi::class) override fun startService(extras : Bundle?) { super.startService(extras) val tunnelConfigString = extras?.getString(getString(R.string.tunnel_extras_key)) cancelJob() - job = GlobalScope.launch { + job = CoroutineScope(SupervisorJob()).launch { if(tunnelConfigString != null) { try { val tunnelConfig = TunnelConfig.from(tunnelConfigString)