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

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