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.5 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. using ReactiveUI;
  2. using ReactiveUI.Fody.Helpers;
  3. using ReactiveUI.Validation.Extensions;
  4. using ReactiveUI.Validation.Helpers;
  5. using Shadowsocks.Controller;
  6. using Shadowsocks.Model;
  7. using Shadowsocks.View;
  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. _config = Program.MainController.GetCurrentConfiguration();
  18. _controller = Program.MainController;
  19. _menuViewController = Program.MenuController;
  20. if (!_config.proxy.useProxy)
  21. NoProxy = true;
  22. else if (_config.proxy.proxyType == 0)
  23. UseSocks5Proxy = true;
  24. else
  25. UseHttpProxy = true;
  26. Address = _config.proxy.proxyServer;
  27. Port = _config.proxy.proxyPort;
  28. Timeout = _config.proxy.proxyTimeout;
  29. Username = _config.proxy.authUser;
  30. Password = _config.proxy.authPwd;
  31. this.WhenAnyValue(x => x.NoProxy, x => !x)
  32. .ToPropertyEx(this, x => x.CanModifyDetails);
  33. AddressRule = this.ValidationRule(
  34. viewModel => viewModel.Address,
  35. address => !string.IsNullOrWhiteSpace(address),
  36. "Address can't be empty or whitespaces.");
  37. PortRule = this.ValidationRule(
  38. viewModel => viewModel.Port,
  39. port => port > 0 && port <= 65535,
  40. port => $"{port} is out of range (0, 65535].");
  41. TimeoutRule = this.ValidationRule(
  42. viewModel => viewModel.Timeout,
  43. timeout => timeout > 0 && timeout <= 10,
  44. timeout => $"{timeout} is out of range (0, 10].");
  45. var authValid = this
  46. .WhenAnyValue(x => x.Username, x => x.Password, (username, password) => new { Username = username, Password = password })
  47. .Select(x => string.IsNullOrWhiteSpace(x.Username) == string.IsNullOrWhiteSpace(x.Password));
  48. AuthRule = this.ValidationRule(authValid, "You must provide both username and password.");
  49. var canSave = this.IsValid();
  50. Save = ReactiveCommand.Create(() =>
  51. {
  52. _controller.SaveProxy(GetForwardProxyConfig());
  53. _menuViewController.CloseForwardProxyWindow();
  54. }, canSave);
  55. Cancel = ReactiveCommand.Create(_menuViewController.CloseForwardProxyWindow);
  56. }
  57. private readonly Configuration _config;
  58. private readonly ShadowsocksController _controller;
  59. private readonly MenuViewController _menuViewController;
  60. public ValidationHelper AddressRule { get; }
  61. public ValidationHelper PortRule { get; }
  62. public ValidationHelper TimeoutRule { get; }
  63. public ValidationHelper AuthRule { get; }
  64. public ReactiveCommand<Unit, Unit> Save { get; }
  65. public ReactiveCommand<Unit, Unit> Cancel { get; }
  66. [ObservableAsProperty]
  67. public bool CanModifyDetails { get; }
  68. [Reactive]
  69. public bool NoProxy { get; set; }
  70. [Reactive]
  71. public bool UseSocks5Proxy { get; set; }
  72. [Reactive]
  73. public bool UseHttpProxy { get; set; }
  74. [Reactive]
  75. public string Address { get; set; }
  76. [Reactive]
  77. public int Port { get; set; }
  78. [Reactive]
  79. public int Timeout { get; set; }
  80. [Reactive]
  81. public string Username { get; set; }
  82. [Reactive]
  83. public string Password { get; set; }
  84. private ForwardProxyConfig GetForwardProxyConfig()
  85. {
  86. var forwardProxyConfig = new ForwardProxyConfig()
  87. {
  88. proxyServer = Address,
  89. proxyPort = Port,
  90. proxyTimeout = Timeout,
  91. authUser = Username,
  92. authPwd = Password
  93. };
  94. if (NoProxy)
  95. forwardProxyConfig.useProxy = false;
  96. else if (UseSocks5Proxy)
  97. {
  98. forwardProxyConfig.useProxy = true;
  99. forwardProxyConfig.proxyType = 0;
  100. }
  101. else
  102. {
  103. forwardProxyConfig.useProxy = true;
  104. forwardProxyConfig.proxyType = 1;
  105. }
  106. return forwardProxyConfig;
  107. }
  108. }
  109. }