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 4.1 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. using ReactiveUI;
  2. using ReactiveUI.Fody.Helpers;
  3. using ReactiveUI.Validation.Extensions;
  4. using ReactiveUI.Validation.Helpers;
  5. using Shadowsocks.WPF.Models;
  6. using System.Linq;
  7. using System.Reactive;
  8. using System.Reactive.Linq;
  9. namespace Shadowsocks.WPF.ViewModels
  10. {
  11. public class ForwardProxyViewModel : ReactiveValidationObject
  12. {
  13. public ForwardProxyViewModel()
  14. {
  15. if (!_config.proxy.useProxy)
  16. NoProxy = true;
  17. else if (_config.proxy.proxyType == 0)
  18. UseSocks5Proxy = true;
  19. else
  20. UseHttpProxy = true;
  21. Address = _config.proxy.proxyServer;
  22. Port = _config.proxy.proxyPort;
  23. Timeout = _config.proxy.proxyTimeout;
  24. Username = _config.proxy.authUser;
  25. Password = _config.proxy.authPwd;
  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. _controller.SaveProxy(GetForwardProxyConfig());
  48. _menuViewController.CloseForwardProxyWindow();
  49. }, canSave);
  50. Cancel = ReactiveCommand.Create(_menuViewController.CloseForwardProxyWindow);
  51. }
  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 ForwardProxyConfig GetForwardProxyConfig()
  77. {
  78. var forwardProxyConfig = new ForwardProxyConfig()
  79. {
  80. proxyServer = Address,
  81. proxyPort = Port,
  82. proxyTimeout = Timeout,
  83. authUser = Username,
  84. authPwd = Password
  85. };
  86. if (NoProxy)
  87. forwardProxyConfig.useProxy = false;
  88. else if (UseSocks5Proxy)
  89. {
  90. forwardProxyConfig.useProxy = true;
  91. forwardProxyConfig.proxyType = 0;
  92. }
  93. else
  94. {
  95. forwardProxyConfig.useProxy = true;
  96. forwardProxyConfig.proxyType = 1;
  97. }
  98. return forwardProxyConfig;
  99. }
  100. }
  101. }