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.

ProxyForm.cs 4.6 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. using System;
  2. using System.Drawing;
  3. using System.Windows.Forms;
  4. using Shadowsocks.Controller;
  5. using Shadowsocks.Model;
  6. using Shadowsocks.Properties;
  7. namespace Shadowsocks.View
  8. {
  9. public partial class ProxyForm : Form
  10. {
  11. private ShadowsocksController controller;
  12. // this is a copy of configuration that we are working on
  13. private ProxyConfig _modifiedConfiguration;
  14. public ProxyForm(ShadowsocksController controller)
  15. {
  16. this.Font = System.Drawing.SystemFonts.MessageBoxFont;
  17. InitializeComponent();
  18. UpdateTexts();
  19. this.Icon = Icon.FromHandle(Resources.ssw128.GetHicon());
  20. this.controller = controller;
  21. controller.ConfigChanged += controller_ConfigChanged;
  22. UpdateEnabled();
  23. LoadCurrentConfiguration();
  24. }
  25. private void UpdateTexts()
  26. {
  27. UseProxyCheckBox.Text = I18N.GetString("Use Proxy");
  28. ProxyTypeLabel.Text = I18N.GetString("Proxy Type");
  29. ProxyAddrLabel.Text = I18N.GetString("Proxy Addr");
  30. ProxyPortLabel.Text = I18N.GetString("Proxy Port");
  31. ProxyTimeoutLabel.Text = I18N.GetString("Timeout(Sec)");
  32. OKButton.Text = I18N.GetString("OK");
  33. MyCancelButton.Text = I18N.GetString("Cancel");
  34. this.Text = I18N.GetString("Edit Proxy");
  35. }
  36. private void controller_ConfigChanged(object sender, EventArgs e)
  37. {
  38. LoadCurrentConfiguration();
  39. }
  40. private void LoadCurrentConfiguration()
  41. {
  42. _modifiedConfiguration = controller.GetConfigurationCopy().proxy;
  43. UseProxyCheckBox.Checked = _modifiedConfiguration.useProxy;
  44. ProxyServerTextBox.Text = _modifiedConfiguration.proxyServer;
  45. ProxyPortTextBox.Text = _modifiedConfiguration.proxyPort.ToString();
  46. ProxyTimeoutTextBox.Text = _modifiedConfiguration.proxyTimeout.ToString();
  47. ProxyTypeComboBox.SelectedIndex = _modifiedConfiguration.proxyType;
  48. }
  49. private void OKButton_Click(object sender, EventArgs e)
  50. {
  51. if (UseProxyCheckBox.Checked)
  52. {
  53. int port;
  54. int timeout;
  55. if (!int.TryParse(ProxyPortTextBox.Text, out port))
  56. {
  57. MessageBox.Show(I18N.GetString("Illegal port number format"));
  58. return;
  59. }
  60. if (!int.TryParse(ProxyTimeoutTextBox.Text, out timeout))
  61. {
  62. MessageBox.Show(I18N.GetString("Illegal timeout format"));
  63. return;
  64. }
  65. var type = ProxyTypeComboBox.SelectedIndex;
  66. var proxy = ProxyServerTextBox.Text;
  67. try
  68. {
  69. Configuration.CheckServer(proxy);
  70. Configuration.CheckPort(port);
  71. Configuration.CheckTimeout(timeout, ProxyConfig.MaxProxyTimeoutSec);
  72. }
  73. catch (Exception ex)
  74. {
  75. MessageBox.Show(ex.Message);
  76. return;
  77. }
  78. controller.EnableProxy(type, proxy, port, timeout);
  79. }
  80. else
  81. {
  82. controller.DisableProxy();
  83. }
  84. this.Close();
  85. }
  86. private void CancelButton_Click(object sender, EventArgs e)
  87. {
  88. this.Close();
  89. }
  90. private void ProxyForm_FormClosed(object sender, FormClosedEventArgs e)
  91. {
  92. controller.ConfigChanged -= controller_ConfigChanged;
  93. }
  94. private void UseProxyCheckBox_CheckedChanged(object sender, EventArgs e)
  95. {
  96. UpdateEnabled();
  97. }
  98. private void UpdateEnabled()
  99. {
  100. if (UseProxyCheckBox.Checked)
  101. {
  102. ProxyServerTextBox.Enabled = true;
  103. ProxyPortTextBox.Enabled = true;
  104. ProxyTimeoutTextBox.Enabled = true;
  105. ProxyTypeComboBox.Enabled = true;
  106. }
  107. else
  108. {
  109. ProxyServerTextBox.Enabled = false;
  110. ProxyPortTextBox.Enabled = false;
  111. ProxyTimeoutTextBox.Enabled = false;
  112. ProxyTypeComboBox.Enabled = false;
  113. }
  114. }
  115. }
  116. }