From 95dc49e86527ec9c705e5d7b3c58377a70807888 Mon Sep 17 00:00:00 2001 From: Jamie <5356206+Stoom@users.noreply.github.com> Date: Sun, 11 Aug 2024 09:03:43 -0500 Subject: [PATCH 1/2] feat: Auto connect on insecure wifi --- src/ProtonVPN.App/App.config | 3 +++ .../Diagnostic/UserSettingsLog.cs | 1 + src/ProtonVPN.App/Core/AppSettings.cs | 6 +++++ src/ProtonVPN.App/Core/AutoConnect.cs | 27 +++++++++++++++++++ .../Properties/Settings.Designer.cs | 12 +++++++++ .../Properties/Settings.settings | 3 +++ .../v1_27_1/AppSettingsMigration.cs | 7 +++++ .../Settings/SettingsModalView.xaml | 16 +++++++++++ .../Settings/SettingsModalViewModel.cs | 6 +++++ src/ProtonVPN.Core/ProtonVPN.Core.csproj | 1 + src/ProtonVPN.Core/Settings/IAppSettings.cs | 1 + .../Automation/Config.xaml | 1 + .../Properties/Resources.Designer.cs | 20 +++++++++++++- .../Properties/Resources.en-US.resx | 8 ++++++ .../Windows/SettingsWindow.cs | 7 +++++ 15 files changed, 118 insertions(+), 1 deletion(-) diff --git a/src/ProtonVPN.App/App.config b/src/ProtonVPN.App/App.config index 8a3ae4b87..6b4df2f54 100644 --- a/src/ProtonVPN.App/App.config +++ b/src/ProtonVPN.App/App.config @@ -370,6 +370,9 @@ True + + True + True diff --git a/src/ProtonVPN.App/BugReporting/Diagnostic/UserSettingsLog.cs b/src/ProtonVPN.App/BugReporting/Diagnostic/UserSettingsLog.cs index 321f373bc..7e7adb956 100644 --- a/src/ProtonVPN.App/BugReporting/Diagnostic/UserSettingsLog.cs +++ b/src/ProtonVPN.App/BugReporting/Diagnostic/UserSettingsLog.cs @@ -71,6 +71,7 @@ private IEnumerable> GetProperties() yield return new(nameof(IAppSettings.AppFirstRun), _appSettings.AppFirstRun); yield return new(nameof(IAppSettings.ShowNotifications), _appSettings.ShowNotifications); yield return new(nameof(IAppSettings.ConnectOnAppStart), _appSettings.ConnectOnAppStart); + yield return new(nameof(IAppSettings.ConnectOnInsecureWifi), _appSettings.ConnectOnInsecureWifi); yield return new(nameof(IAppSettings.QuickConnect), _appSettings.QuickConnect); yield return new(nameof(IAppSettings.StartOnBoot), _appSettings.StartOnBoot); yield return new(nameof(IAppSettings.StartMinimized), _appSettings.StartMinimized); diff --git a/src/ProtonVPN.App/Core/AppSettings.cs b/src/ProtonVPN.App/Core/AppSettings.cs index 0ac2ead78..c1f05d25b 100644 --- a/src/ProtonVPN.App/Core/AppSettings.cs +++ b/src/ProtonVPN.App/Core/AppSettings.cs @@ -498,6 +498,12 @@ public bool ConnectOnAppStart set => Set(value); } + public bool ConnectOnInsecureWifi + { + get => Get(); + set => Set(value); + } + [Obsolete( "Use this only for checking if the user enabled/disabled the feature." + "Use IsSmartReconnectEnabled() for checking if Smart Reconnect is/should be enabled.")] diff --git a/src/ProtonVPN.App/Core/AutoConnect.cs b/src/ProtonVPN.App/Core/AutoConnect.cs index 464f376c0..1866e3097 100644 --- a/src/ProtonVPN.App/Core/AutoConnect.cs +++ b/src/ProtonVPN.App/Core/AutoConnect.cs @@ -26,24 +26,30 @@ using ProtonVPN.Core.Service.Vpn; using ProtonVPN.Core.Settings; using ProtonVPN.Core.Vpn; +using ProtonVPN.Core.Network; namespace ProtonVPN.Core { internal class AutoConnect : IVpnStateAware { private readonly IAppSettings _appSettings; + private readonly INetworkClient _networkClient; private readonly IVpnManager _vpnManager; private readonly ILogger _logger; private VpnStatus _vpnStatus; public AutoConnect( IAppSettings appSettings, + INetworkClient networkClient, IVpnManager vpnManager, ILogger logger) { _appSettings = appSettings; + _networkClient = networkClient; _vpnManager = vpnManager; _logger = logger; + + _networkClient.WifiChangeDetected += OnWifiChangeDetected; } public async Task LoadAsync(bool autoLogin) @@ -75,5 +81,26 @@ public Task OnVpnStateChanged(VpnStateChangedEventArgs e) return Task.CompletedTask; } + + private void OnWifiChangeDetected(object sender, WifiChangeEventArgs e) + { + if (e.Secure || !_appSettings.ConnectOnInsecureWifi) + { + return; + } + + Task.Factory.StartNew(async () => + { + try + { + _logger.Info("Automatically connecting on insecure wifi"); + await _vpnManager.QuickConnectAsync(); + } + catch (OperationCanceledException ex) + { + _logger.Error("An error occurred when connecting automatically on insecure wifi.", ex); + } + }); + } } } \ No newline at end of file diff --git a/src/ProtonVPN.App/Properties/Settings.Designer.cs b/src/ProtonVPN.App/Properties/Settings.Designer.cs index 28fb95f79..326397c13 100644 --- a/src/ProtonVPN.App/Properties/Settings.Designer.cs +++ b/src/ProtonVPN.App/Properties/Settings.Designer.cs @@ -1458,6 +1458,18 @@ public bool ConnectOnAppStart { } } + [global::System.Configuration.UserScopedSettingAttribute()] + [global::System.Diagnostics.DebuggerNonUserCodeAttribute()] + [global::System.Configuration.DefaultSettingValueAttribute("True")] + public bool ConnectOnInsecureWifi { + get { + return ((bool)(this["ConnectOnInsecureWifi"])); + } + set { + this["ConnectOnInsecureWifi"] = value; + } + } + [global::System.Configuration.UserScopedSettingAttribute()] [global::System.Diagnostics.DebuggerNonUserCodeAttribute()] [global::System.Configuration.DefaultSettingValueAttribute("True")] diff --git a/src/ProtonVPN.App/Properties/Settings.settings b/src/ProtonVPN.App/Properties/Settings.settings index 276ed6166..bf2cbe1b3 100644 --- a/src/ProtonVPN.App/Properties/Settings.settings +++ b/src/ProtonVPN.App/Properties/Settings.settings @@ -362,6 +362,9 @@ True + + True + True diff --git a/src/ProtonVPN.App/Settings/Migrations/v1_27_1/AppSettingsMigration.cs b/src/ProtonVPN.App/Settings/Migrations/v1_27_1/AppSettingsMigration.cs index 72793e1d8..47959d22a 100644 --- a/src/ProtonVPN.App/Settings/Migrations/v1_27_1/AppSettingsMigration.cs +++ b/src/ProtonVPN.App/Settings/Migrations/v1_27_1/AppSettingsMigration.cs @@ -28,6 +28,7 @@ namespace ProtonVPN.Settings.Migrations.v1_27_1 internal class AppSettingsMigration : BaseAppSettingsMigration { private const string UserAutoConnectKey = "UserAutoConnect"; + private const string UserAutoConnectOnInsecureWifiKey = "ConnectOnInsecureWifi"; private const string StartOnStartupKey = "StartOnStartup"; private readonly InitialAppSettingsMigration _initialAppSettingsMigration; @@ -53,6 +54,12 @@ private void MigrateAutoConnect() bool autoConnect = autoConnectSettings.Any(setting => !setting.Value.IsNullOrEmpty()); Settings.Set(nameof(IAppSettings.ConnectOnAppStart), _initialAppSettingsMigration.IsCleanInstall || autoConnect); } + PerUser[] autoConnectOnInsecureSettings = Settings.Get[]>(UserAutoConnectOnInsecureWifiKey); + if (autoConnectOnInsecureSettings != null) + { + bool autoConnect = autoConnectOnInsecureSettings.Any(setting => !setting.Value.IsNullOrEmpty()); + Settings.Set(nameof(IAppSettings.ConnectOnInsecureWifi), _initialAppSettingsMigration.IsCleanInstall || autoConnect); + } } private void MigrateStartOnStartup() diff --git a/src/ProtonVPN.App/Settings/SettingsModalView.xaml b/src/ProtonVPN.App/Settings/SettingsModalView.xaml index e8147b6f4..34efb104a 100644 --- a/src/ProtonVPN.App/Settings/SettingsModalView.xaml +++ b/src/ProtonVPN.App/Settings/SettingsModalView.xaml @@ -210,6 +210,22 @@ along with ProtonVPN. If not, see . + + + + + + + + - + + + + + + + + + + +