From a98a47f54d78e8a5d4fd2edb96714acc8b27abd9 Mon Sep 17 00:00:00 2001 From: Zane Schepke Date: Wed, 5 Jul 2023 23:21:55 -0400 Subject: [PATCH] fix: prevent crash from bad file config upload and bug preventing vpn from starting via watcher service after reboot Add catch for file configs that are malformed and display message to the user Pass full tunnel config to watcher service after detecting reboot to allow watcher service to start tunnel properly. --- app/build.gradle.kts | 2 +- .../wireguardautotunnel/BootReceiver.kt | 2 +- .../service/foreground/ServiceTracker.kt | 16 ++++++++++++---- .../WireGuardConnectivityWatcherService.kt | 2 ++ .../ui/screens/main/MainViewModel.kt | 17 ++++++++++++----- app/src/main/res/values/strings.xml | 1 + 6 files changed, 29 insertions(+), 11 deletions(-) diff --git a/app/build.gradle.kts b/app/build.gradle.kts index 07a00f8f..c53e4122 100644 --- a/app/build.gradle.kts +++ b/app/build.gradle.kts @@ -17,7 +17,7 @@ android { val versionMajor = 2 val versionMinor = 0 - val versionPatch = 1 + val versionPatch = 2 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 cdfeb195..78c146b5 100644 --- a/app/src/main/java/com/zaneschepke/wireguardautotunnel/BootReceiver.kt +++ b/app/src/main/java/com/zaneschepke/wireguardautotunnel/BootReceiver.kt @@ -36,7 +36,7 @@ class BootReceiver : BroadcastReceiver() { Action.START, context, WireGuardConnectivityWatcherService::class.java, mapOf(context.resources.getString(R.string.tunnel_extras_key) to - defaultTunnel.id.toString()) + defaultTunnel.toString()) ) } } diff --git a/app/src/main/java/com/zaneschepke/wireguardautotunnel/service/foreground/ServiceTracker.kt b/app/src/main/java/com/zaneschepke/wireguardautotunnel/service/foreground/ServiceTracker.kt index 9309a8ad..9c885ecd 100644 --- a/app/src/main/java/com/zaneschepke/wireguardautotunnel/service/foreground/ServiceTracker.kt +++ b/app/src/main/java/com/zaneschepke/wireguardautotunnel/service/foreground/ServiceTracker.kt @@ -6,8 +6,8 @@ import android.app.Service import android.content.Context import android.content.Context.ACTIVITY_SERVICE import android.content.Intent -import android.content.SharedPreferences -import com.zaneschepke.wireguardautotunnel.R +import com.google.firebase.crashlytics.ktx.crashlytics +import com.google.firebase.ktx.Firebase object ServiceTracker { @Suppress("DEPRECATION") @@ -31,7 +31,11 @@ object ServiceTracker { } } intent.component?.javaClass - application.startService(intent) + try { + application.startService(intent) + } catch (e : Exception) { + e.message?.let { Firebase.crashlytics.log(it) } + } } fun actionOnService(action: Action, context: Context, cls : Class, extras : Map? = null) { @@ -43,6 +47,10 @@ object ServiceTracker { } } intent.component?.javaClass - context.startService(intent) + try { + context.startService(intent) + } catch (e : Exception) { + e.message?.let { Firebase.crashlytics.log(it) } + } } } \ No newline at end of file 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 dcf6497d..3463189b 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 @@ -161,6 +161,7 @@ class WireGuardConnectivityWatcherService : ForegroundService() { if(!isWifiConnected && vpnService.getState() == Tunnel.State.UP) stopVPN() Timber.d("Lost mobile data connection") } + else -> {} } } } @@ -200,6 +201,7 @@ class WireGuardConnectivityWatcherService : ForegroundService() { startVPN() } } + else -> {} } } } diff --git a/app/src/main/java/com/zaneschepke/wireguardautotunnel/ui/screens/main/MainViewModel.kt b/app/src/main/java/com/zaneschepke/wireguardautotunnel/ui/screens/main/MainViewModel.kt index 58676704..91ea6cfc 100644 --- a/app/src/main/java/com/zaneschepke/wireguardautotunnel/ui/screens/main/MainViewModel.kt +++ b/app/src/main/java/com/zaneschepke/wireguardautotunnel/ui/screens/main/MainViewModel.kt @@ -7,6 +7,7 @@ import android.net.Uri import android.provider.OpenableColumns import androidx.lifecycle.ViewModel import androidx.lifecycle.viewModelScope +import com.wireguard.config.BadConfigException import com.wireguard.config.Config import com.zaneschepke.wireguardautotunnel.R import com.zaneschepke.wireguardautotunnel.repository.Repository @@ -122,12 +123,18 @@ class MainViewModel @Inject constructor(private val application : Application, val stream = application.applicationContext.contentResolver.openInputStream(uri) stream ?: return val bufferReader = stream.bufferedReader(charset = Charsets.UTF_8) - val config = Config.parse(bufferReader) - val tunnelName = getNameFromFileName(fileName) - viewModelScope.launch { - tunnelRepo.save(TunnelConfig(name = tunnelName, wgQuick = config.toWgQuickString())) + try { + val config = Config.parse(bufferReader) + val tunnelName = getNameFromFileName(fileName) + viewModelScope.launch { + tunnelRepo.save(TunnelConfig(name = tunnelName, wgQuick = config.toWgQuickString())) + } + stream.close() + } catch(_: BadConfigException) { + viewModelScope.launch { + showSnackBarMessage(application.applicationContext.getString(R.string.bad_config)) + } } - stream.close() } @SuppressLint("Range") diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 80624fd0..1879d0b3 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -57,4 +57,5 @@ No thanks Turn on Map + Bad config. Please try again. \ No newline at end of file