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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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 _modifiedProxyConfig;
  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. ProxyNotificationLabel.Text = I18N.GetString("If server has a plugin, proxy will not be used");
  33. OKButton.Text = I18N.GetString("OK");
  34. MyCancelButton.Text = I18N.GetString("Cancel");
  35. this.Text = I18N.GetString("Edit Proxy");
  36. }
  37. private void controller_ConfigChanged(object sender, EventArgs e)
  38. {
  39. LoadCurrentConfiguration();
  40. }
  41. private void LoadCurrentConfiguration()
  42. {
  43. _modifiedProxyConfig = controller.GetConfigurationCopy().proxy;
  44. UseProxyCheckBox.Checked = _modifiedProxyConfig.useProxy;
  45. ProxyServerTextBox.Text = _modifiedProxyConfig.proxyServer;
  46. ProxyPortTextBox.Text = _modifiedProxyConfig.proxyPort.ToString();
  47. ProxyTimeoutTextBox.Text = _modifiedProxyConfig.proxyTimeout.ToString();
  48. ProxyTypeComboBox.SelectedIndex = _modifiedProxyConfig.proxyType;
  49. }
  50. private void OKButton_Click(object sender, EventArgs e)
  51. {
  52. if (_modifiedProxyConfig.useProxy=UseProxyCheckBox.Checked)
  53. {
  54. if (!int.TryParse(ProxyPortTextBox.Text, out _modifiedProxyConfig.proxyPort))
  55. {
  56. MessageBox.Show(I18N.GetString("Illegal port number format"));
  57. return;
  58. }
  59. if (!int.TryParse(ProxyTimeoutTextBox.Text, out _modifiedProxyConfig.proxyTimeout))
  60. {
  61. MessageBox.Show(I18N.GetString("Illegal timeout format"));
  62. return;
  63. }
  64. _modifiedProxyConfig.proxyType = ProxyTypeComboBox.SelectedIndex;
  65. try
  66. {
  67. Configuration.CheckServer(_modifiedProxyConfig.proxyServer = ProxyServerTextBox.Text);
  68. Configuration.CheckPort(_modifiedProxyConfig.proxyPort);
  69. Configuration.CheckTimeout(_modifiedProxyConfig.proxyTimeout, ProxyConfig.MaxProxyTimeoutSec);
  70. }
  71. catch (Exception ex)
  72. {
  73. MessageBox.Show(ex.Message);
  74. return;
  75. }
  76. }
  77. controller.SaveProxy(_modifiedProxyConfig);
  78. this.Close();
  79. }
  80. private void CancelButton_Click(object sender, EventArgs e)
  81. {
  82. this.Close();
  83. }
  84. private void ProxyForm_FormClosed(object sender, FormClosedEventArgs e)
  85. {
  86. controller.ConfigChanged -= controller_ConfigChanged;
  87. }
  88. private void UseProxyCheckBox_CheckedChanged(object sender, EventArgs e)
  89. {
  90. UpdateEnabled();
  91. }
  92. private void UpdateEnabled()
  93. {
  94. if (UseProxyCheckBox.Checked)
  95. {
  96. ProxyServerTextBox.Enabled =
  97. ProxyPortTextBox.Enabled =
  98. ProxyTimeoutTextBox.Enabled =
  99. ProxyTypeComboBox.Enabled = true;
  100. }
  101. else
  102. {
  103. ProxyServerTextBox.Enabled =
  104. ProxyPortTextBox.Enabled =
  105. ProxyTimeoutTextBox.Enabled =
  106. ProxyTypeComboBox.Enabled = false;
  107. }
  108. }
  109. }
  110. }