| 1 | // Copyright (C) Microsoft Corporation. All rights reserved. |
| 2 | |
| 3 | using CommunityToolkit.Mvvm.ComponentModel; |
| 4 | using CommunityToolkit.Mvvm.Input; |
| 5 | using Microsoft.UI.Xaml; |
| 6 | using Microsoft.UI.Xaml.Controls; |
| 7 | using System.Windows.Input; |
| 8 | using WslSettings.Contracts.Services; |
| 9 | |
| 10 | namespace WslSettings.ViewModels.Settings; |
| 11 | |
| 12 | public partial class NetworkingViewModel : WslConfigSettingViewModel |
| 13 | { |
| 14 | private IWslConfigSetting? _networkingMode; |
| 15 | private IWslConfigSetting? _hyperVFirewall; |
| 16 | private IWslConfigSetting? _ignoredPorts; |
| 17 | private IWslConfigSetting? _localhostForwarding; |
| 18 | private IWslConfigSetting? _hostAddressLoopback; |
| 19 | private IWslConfigSetting? _autoProxy; |
| 20 | private IWslConfigSetting? _initialAutoProxyTimeout; |
| 21 | private IWslConfigSetting? _dNSProxy; |
| 22 | private IWslConfigSetting? _dNSTunneling; |
| 23 | private IWslConfigSetting? _bestEffortDNS; |
| 24 | private string? _defaultIgnoredPorts; |
| 25 | private int _defaultInitialAutoProxyTimeout; |
| 26 | private List<ComboBoxItem> _networkingModeItems; |
| 27 | private bool _ignoredPorts_ResetEnabled; |
| 28 | private bool _initialAutoProxyTimeout_ResetEnabled; |
| 29 | |
| 30 | public NetworkingViewModel() |
| 31 | { |
| 32 | InitializeConfigSettings(); |
| 33 | |
| 34 | InitialAutoProxyTimeout_ResetEnabled = !Equals(_defaultInitialAutoProxyTimeout, _initialAutoProxyTimeout!.Int32Value); |
| 35 | |
| 36 | _networkingModeItems = new List<ComboBoxItem>(); |
| 37 | foreach (var networkModeItem in Enum.GetNames(typeof(NetworkingConfiguration)).ToList()) |
| 38 | { |
| 39 | _networkingModeItems.Add(new ComboBoxItem() { Name = networkModeItem, Content = networkModeItem }); |
| 40 | } |
| 41 | |
| 42 | _networkingModeItems[(int)NetworkingConfiguration.Bridged].Visibility = NetworkingModeSelected == (int)NetworkingConfiguration.Bridged ? |
| 43 | Visibility.Visible : Visibility.Collapsed; |
| 44 | } |
| 45 | |
| 46 | protected override void InitializeConfigSettings() |
| 47 | { |
| 48 | var wslConfigService = App.GetService<IWslConfigService>(); |
| 49 | _networkingMode = wslConfigService.GetWslConfigSetting(WslConfigEntry.NetworkingMode); |
| 50 | _hyperVFirewall = wslConfigService.GetWslConfigSetting(WslConfigEntry.FirewallEnabled); |
| 51 | _ignoredPorts = wslConfigService.GetWslConfigSetting(WslConfigEntry.IgnoredPorts); |
| 52 | _localhostForwarding = wslConfigService.GetWslConfigSetting(WslConfigEntry.LocalhostForwardingEnabled); |
| 53 | _hostAddressLoopback = wslConfigService.GetWslConfigSetting(WslConfigEntry.HostAddressLoopbackEnabled); |
| 54 | _autoProxy = wslConfigService.GetWslConfigSetting(WslConfigEntry.AutoProxyEnabled); |
| 55 | _initialAutoProxyTimeout = wslConfigService.GetWslConfigSetting(WslConfigEntry.InitialAutoProxyTimeout); |
| 56 | _dNSProxy = wslConfigService.GetWslConfigSetting(WslConfigEntry.DNSProxyEnabled); |
| 57 | _dNSTunneling = wslConfigService.GetWslConfigSetting(WslConfigEntry.DNSTunnelingEnabled); |
| 58 | _bestEffortDNS = wslConfigService.GetWslConfigSetting(WslConfigEntry.BestEffortDNSParsingEnabled); |
| 59 | |
| 60 | string defaultIgnoredPorts = wslConfigService.GetWslConfigSetting(WslConfigEntry.IgnoredPorts, true).StringValue; |
| 61 | _defaultIgnoredPorts = defaultIgnoredPorts == null ? String.Empty : defaultIgnoredPorts; |
| 62 | _defaultInitialAutoProxyTimeout = wslConfigService.GetWslConfigSetting(WslConfigEntry.InitialAutoProxyTimeout, true).Int32Value; |
| 63 | } |
| 64 | |
| 65 | public List<ComboBoxItem> NetworkingModes |
| 66 | { |
| 67 | get { return _networkingModeItems; } |
| 68 | } |
| 69 | |
| 70 | public int NetworkingModeSelected |
| 71 | { |
| 72 | get { return (int)_networkingMode!.NetworkingConfigurationValue; } |
| 73 | set { Set(ref _networkingMode!, value); } |
| 74 | } |
| 75 | |
| 76 | public bool IsOnHyperVFirewall |
| 77 | { |
| 78 | get { return _hyperVFirewall!.BoolValue; } |
| 79 | set { Set(ref _hyperVFirewall!, value); } |
| 80 | } |
| 81 | |
| 82 | public string IgnoredPorts |
| 83 | { |
| 84 | get |
| 85 | { |
| 86 | IgnoredPorts_ResetEnabled = !Equals(_defaultIgnoredPorts, _ignoredPorts!.StringValue); |
| 87 | return _ignoredPorts!.StringValue; |
| 88 | } |
| 89 | set |
| 90 | { |
| 91 | if (ValidateInput(value, Constants.CommaSeparatedWholeNumbersOrEmptyRegex)) |
| 92 | { |
| 93 | Set(ref _ignoredPorts!, value); |
| 94 | } |
| 95 | } |
| 96 | } |
| 97 | |
| 98 | public bool IgnoredPorts_ResetEnabled |
| 99 | { |
| 100 | get => _ignoredPorts_ResetEnabled; |
| 101 | set => SetProperty(ref _ignoredPorts_ResetEnabled, value); |
| 102 | } |
| 103 | |
| 104 | private void IgnoredPorts_ResetExecuted(string? param) |
| 105 | { |
| 106 | IgnoredPorts = _defaultIgnoredPorts!; |
| 107 | } |
| 108 | |
| 109 | public ICommand IgnoredPorts_ResetCommand => new RelayCommand<string>(IgnoredPorts_ResetExecuted); |
| 110 | |
| 111 | public bool IsOnLocalhostForwarding |
| 112 | { |
| 113 | get { return _localhostForwarding!.BoolValue; } |
| 114 | set { Set(ref _localhostForwarding!, value); } |
| 115 | } |
| 116 | |
| 117 | public bool IsOnHostAddressLoopback |
| 118 | { |
| 119 | get { return _hostAddressLoopback!.BoolValue; } |
| 120 | set { Set(ref _hostAddressLoopback!, value); } |
| 121 | } |
| 122 | |
| 123 | public bool IsOnAutoProxy |
| 124 | { |
| 125 | get { return _autoProxy!.BoolValue; } |
| 126 | set { Set(ref _autoProxy!, value); } |
| 127 | } |
| 128 | |
| 129 | public string InitialAutoProxyTimeout |
| 130 | { |
| 131 | get |
| 132 | { |
| 133 | return _initialAutoProxyTimeout!.Int32Value.ToString(); |
| 134 | } |
| 135 | set |
| 136 | { |
| 137 | if (ValidateInput(value, Constants.IntegerRegex)) |
| 138 | { |
| 139 | if (Int32.TryParse(value, out int parsedValue)) |
| 140 | { |
| 141 | Set(ref _initialAutoProxyTimeout!, parsedValue); |
| 142 | } |
| 143 | else |
| 144 | { |
| 145 | OnPropertyChanged(); |
| 146 | } |
| 147 | } |
| 148 | } |
| 149 | } |
| 150 | |
| 151 | public void SetInitialAutoProxyTimeout_ResetEnabled(string? value) |
| 152 | { |
| 153 | if (Int32.TryParse(value, out Int32 parseResult)) |
| 154 | { |
| 155 | InitialAutoProxyTimeout_ResetEnabled = !Equals(_defaultInitialAutoProxyTimeout, parseResult); |
| 156 | } |
| 157 | else |
| 158 | { |
| 159 | InitialAutoProxyTimeout_ResetEnabled = true; |
| 160 | } |
| 161 | } |
| 162 | |
| 163 | public bool InitialAutoProxyTimeout_ResetEnabled |
| 164 | { |
| 165 | get => _initialAutoProxyTimeout_ResetEnabled; |
| 166 | set => SetProperty(ref _initialAutoProxyTimeout_ResetEnabled, value); |
| 167 | } |
| 168 | |
| 169 | private void InitialAutoProxyTimeout_ResetExecuted(string? param) |
| 170 | { |
| 171 | InitialAutoProxyTimeout = _defaultInitialAutoProxyTimeout.ToString(); |
| 172 | } |
| 173 | |
| 174 | public ICommand InitialAutoProxyTimeout_ResetCommand => new RelayCommand<string>(InitialAutoProxyTimeout_ResetExecuted); |
| 175 | |
| 176 | public bool IsOnDNSProxy |
| 177 | { |
| 178 | get { return _dNSProxy!.BoolValue; } |
| 179 | set { Set(ref _dNSProxy!, value); } |
| 180 | } |
| 181 | |
| 182 | public bool IsOnDNSTunneling |
| 183 | { |
| 184 | get { return _dNSTunneling!.BoolValue; } |
| 185 | set { Set(ref _dNSTunneling!, value); } |
| 186 | } |
| 187 | |
| 188 | public bool IsOnBestEffortDNS |
| 189 | { |
| 190 | get { return _bestEffortDNS!.BoolValue; } |
| 191 | set { Set(ref _bestEffortDNS!, value); } |
| 192 | } |
| 193 | } |