diff --git a/Flow.Launcher/SettingPages/ViewModels/SettingsPaneProxyViewModel.cs b/Flow.Launcher/SettingPages/ViewModels/SettingsPaneProxyViewModel.cs new file mode 100644 index 000000000..2dd57809d --- /dev/null +++ b/Flow.Launcher/SettingPages/ViewModels/SettingsPaneProxyViewModel.cs @@ -0,0 +1,62 @@ +using System.Net; +using System.Windows; +using CommunityToolkit.Mvvm.Input; +using Flow.Launcher.Core; +using Flow.Launcher.Core.Resource; +using Flow.Launcher.Infrastructure.UserSettings; +using Flow.Launcher.Plugin; + +namespace Flow.Launcher.SettingPages.ViewModels; + +public partial class SettingsPaneProxyViewModel : BaseModel +{ + private readonly Updater _updater; + public Settings Settings { get; } + + public SettingsPaneProxyViewModel(Settings settings, Updater updater) + { + _updater = updater; + Settings = settings; + } + + [RelayCommand] + private void OnTestProxyClicked() + { + var message = TestProxy(); + MessageBox.Show(InternationalizationManager.Instance.GetTranslation(message)); + } + + private string TestProxy() + { + if (string.IsNullOrEmpty(Settings.Proxy.Server)) return "serverCantBeEmpty"; + if (Settings.Proxy.Port <= 0) return "portCantBeEmpty"; + + HttpWebRequest request = (HttpWebRequest)WebRequest.Create(_updater.GitHubRepository); + + if (string.IsNullOrEmpty(Settings.Proxy.UserName) || string.IsNullOrEmpty(Settings.Proxy.Password)) + { + request.Proxy = new WebProxy(Settings.Proxy.Server, Settings.Proxy.Port); + } + else + { + request.Proxy = new WebProxy(Settings.Proxy.Server, Settings.Proxy.Port) + { + Credentials = new NetworkCredential(Settings.Proxy.UserName, Settings.Proxy.Password) + }; + } + + try + { + var response = (HttpWebResponse)request.GetResponse(); + return response.StatusCode switch + { + HttpStatusCode.OK => "proxyIsCorrect", + _ => "proxyConnectFailed" + }; + } + catch + { + return "proxyConnectFailed"; + } + } +} diff --git a/Flow.Launcher/SettingPages/Views/SettingsPaneProxy.xaml b/Flow.Launcher/SettingPages/Views/SettingsPaneProxy.xaml index a6bd91534..efd4ceb89 100644 --- a/Flow.Launcher/SettingPages/Views/SettingsPaneProxy.xaml +++ b/Flow.Launcher/SettingPages/Views/SettingsPaneProxy.xaml @@ -1,181 +1,77 @@ - - - - - - - + - - - - - + + + + + + - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -