You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

ForwardProxyViewModel.cs 3.7 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. using ReactiveUI;
  2. using ReactiveUI.Fody.Helpers;
  3. using ReactiveUI.Validation.Extensions;
  4. using ReactiveUI.Validation.Helpers;
  5. using Shadowsocks.Net.Settings;
  6. using Shadowsocks.WPF.Models;
  7. using Splat;
  8. using System.Linq;
  9. using System.Reactive;
  10. using System.Reactive.Linq;
  11. namespace Shadowsocks.WPF.ViewModels
  12. {
  13. public class ForwardProxyViewModel : ReactiveValidationObject
  14. {
  15. public ForwardProxyViewModel()
  16. {
  17. _forwardProxySettings = Locator.Current.GetService<Settings>().Net.ForwardProxy;
  18. NoProxy = _forwardProxySettings.NoProxy;
  19. UseSocks5Proxy = _forwardProxySettings.UseSocks5Proxy;
  20. UseHttpProxy = _forwardProxySettings.UseHttpProxy;
  21. Address = _forwardProxySettings.Address;
  22. Port = _forwardProxySettings.Port;
  23. Timeout = 5;
  24. Username = _forwardProxySettings.Username;
  25. Password = _forwardProxySettings.Password;
  26. this.WhenAnyValue(x => x.NoProxy, x => !x)
  27. .ToPropertyEx(this, x => x.CanModifyDetails);
  28. AddressRule = this.ValidationRule(
  29. viewModel => viewModel.Address,
  30. address => !string.IsNullOrWhiteSpace(address),
  31. "Address can't be empty or whitespaces.");
  32. PortRule = this.ValidationRule(
  33. viewModel => viewModel.Port,
  34. port => port > 0 && port <= 65535,
  35. port => $"{port} is out of range (0, 65535].");
  36. TimeoutRule = this.ValidationRule(
  37. viewModel => viewModel.Timeout,
  38. timeout => timeout > 0 && timeout <= 10,
  39. timeout => $"{timeout} is out of range (0, 10].");
  40. var authValid = this
  41. .WhenAnyValue(x => x.Username, x => x.Password, (username, password) => new { Username = username, Password = password })
  42. .Select(x => string.IsNullOrWhiteSpace(x.Username) == string.IsNullOrWhiteSpace(x.Password));
  43. AuthRule = this.ValidationRule(authValid, "You must provide both username and password.");
  44. var canSave = this.IsValid();
  45. Save = ReactiveCommand.Create(() =>
  46. {
  47. // TODO: save settings
  48. }, canSave);
  49. //Cancel = ReactiveCommand.Create(_menuViewController.CloseForwardProxyWindow);
  50. }
  51. private ForwardProxySettings _forwardProxySettings;
  52. public ValidationHelper AddressRule { get; }
  53. public ValidationHelper PortRule { get; }
  54. public ValidationHelper TimeoutRule { get; }
  55. public ValidationHelper AuthRule { get; }
  56. public ReactiveCommand<Unit, Unit> Save { get; }
  57. public ReactiveCommand<Unit, Unit> Cancel { get; }
  58. [ObservableAsProperty]
  59. public bool CanModifyDetails { get; }
  60. [Reactive]
  61. public bool NoProxy { get; set; }
  62. [Reactive]
  63. public bool UseSocks5Proxy { get; set; }
  64. [Reactive]
  65. public bool UseHttpProxy { get; set; }
  66. [Reactive]
  67. public string Address { get; set; }
  68. [Reactive]
  69. public int Port { get; set; }
  70. [Reactive]
  71. public int Timeout { get; set; }
  72. [Reactive]
  73. public string Username { get; set; }
  74. [Reactive]
  75. public string Password { get; set; }
  76. private ForwardProxySettings GetForwardProxySettings()
  77. => new ForwardProxySettings()
  78. {
  79. NoProxy = NoProxy,
  80. UseSocks5Proxy = UseSocks5Proxy,
  81. UseHttpProxy = UseHttpProxy,
  82. Address = Address,
  83. Port = Port,
  84. Username = Username,
  85. Password = Password,
  86. };
  87. }
  88. }