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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  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. var type = ProxyTypeComboBox.SelectedIndex;
  52. var proxy = ProxyServerTextBox.Text;
  53. var port = 0;
  54. var timeout = 3;
  55. if (UseProxyCheckBox.Checked)
  56. {
  57. try
  58. {
  59. port = int.Parse(ProxyPortTextBox.Text);
  60. }
  61. catch (FormatException)
  62. {
  63. MessageBox.Show(I18N.GetString("Illegal port number format"));
  64. ProxyPortTextBox.Clear();
  65. return;
  66. }
  67. try
  68. {
  69. timeout = int.Parse(ProxyTimeoutTextBox.Text);
  70. }
  71. catch (FormatException)
  72. {
  73. MessageBox.Show(I18N.GetString("Illegal timeout format"));
  74. ProxyTimeoutTextBox.Clear();
  75. return;
  76. }
  77. try
  78. {
  79. Configuration.CheckServer(proxy);
  80. Configuration.CheckPort(port);
  81. Configuration.CheckTimeout(timeout, ProxyConfig.MaxProxyTimeoutSec);
  82. }
  83. catch (Exception ex)
  84. {
  85. MessageBox.Show(ex.Message);
  86. return;
  87. }
  88. controller.EnableProxy(type, proxy, port);
  89. }
  90. else
  91. {
  92. controller.DisableProxy();
  93. }
  94. _modifiedConfiguration.useProxy = UseProxyCheckBox.Checked;
  95. _modifiedConfiguration.proxyType = type;
  96. _modifiedConfiguration.proxyServer = proxy;
  97. _modifiedConfiguration.proxyPort = port;
  98. _modifiedConfiguration.proxyTimeout = timeout;
  99. controller.SaveProxyConfig(_modifiedConfiguration);
  100. this.Close();
  101. }
  102. private void CancelButton_Click(object sender, EventArgs e)
  103. {
  104. this.Close();
  105. }
  106. private void ProxyForm_FormClosed(object sender, FormClosedEventArgs e)
  107. {
  108. controller.ConfigChanged -= controller_ConfigChanged;
  109. }
  110. private void UseProxyCheckBox_CheckedChanged(object sender, EventArgs e)
  111. {
  112. UpdateEnabled();
  113. }
  114. private void UpdateEnabled()
  115. {
  116. if (UseProxyCheckBox.Checked)
  117. {
  118. ProxyServerTextBox.Enabled = true;
  119. ProxyPortTextBox.Enabled = true;
  120. ProxyTimeoutTextBox.Enabled = true;
  121. ProxyTypeComboBox.Enabled = true;
  122. }
  123. else
  124. {
  125. ProxyServerTextBox.Clear();
  126. ProxyPortTextBox.Clear();
  127. ProxyTimeoutTextBox.Clear();
  128. ProxyServerTextBox.Enabled = false;
  129. ProxyPortTextBox.Enabled = false;
  130. ProxyTimeoutTextBox.Enabled = false;
  131. ProxyTypeComboBox.Enabled = false;
  132. }
  133. }
  134. }
  135. }