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

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