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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  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. I18N.TranslateForm(this);
  28. }
  29. private void controller_ConfigChanged(object sender, EventArgs e)
  30. {
  31. LoadCurrentConfiguration();
  32. }
  33. private void LoadCurrentConfiguration()
  34. {
  35. _modifiedProxyConfig = controller.GetConfigurationCopy().proxy;
  36. UseProxyCheckBox.Checked = _modifiedProxyConfig.useProxy;
  37. ProxyServerTextBox.Text = _modifiedProxyConfig.proxyServer;
  38. ProxyPortTextBox.Text = _modifiedProxyConfig.proxyPort.ToString();
  39. ProxyTimeoutTextBox.Text = _modifiedProxyConfig.proxyTimeout.ToString();
  40. ProxyTypeComboBox.SelectedIndex = _modifiedProxyConfig.proxyType;
  41. UseAuthCheckBox.Checked = _modifiedProxyConfig.useAuth;
  42. AuthUserTextBox.Text = _modifiedProxyConfig.authUser;
  43. AuthPwdTextBox.Text = _modifiedProxyConfig.authPwd;
  44. }
  45. private void OKButton_Click(object sender, EventArgs e)
  46. {
  47. _modifiedProxyConfig.useProxy = UseProxyCheckBox.Checked;
  48. if (_modifiedProxyConfig.useProxy)
  49. {
  50. if (!int.TryParse(ProxyPortTextBox.Text, out _modifiedProxyConfig.proxyPort))
  51. {
  52. MessageBox.Show(I18N.GetString("Illegal port number format"));
  53. return;
  54. }
  55. if (!int.TryParse(ProxyTimeoutTextBox.Text, out _modifiedProxyConfig.proxyTimeout))
  56. {
  57. MessageBox.Show(I18N.GetString("Illegal timeout format"));
  58. return;
  59. }
  60. _modifiedProxyConfig.proxyType = ProxyTypeComboBox.SelectedIndex;
  61. try
  62. {
  63. Configuration.CheckServer(_modifiedProxyConfig.proxyServer = ProxyServerTextBox.Text);
  64. Configuration.CheckPort(_modifiedProxyConfig.proxyPort);
  65. Configuration.CheckTimeout(_modifiedProxyConfig.proxyTimeout, ProxyConfig.MaxProxyTimeoutSec);
  66. _modifiedProxyConfig.useAuth = UseAuthCheckBox.Checked;
  67. if (_modifiedProxyConfig.useAuth)
  68. {
  69. Configuration.CheckProxyAuthUser(_modifiedProxyConfig.authUser = AuthUserTextBox.Text);
  70. Configuration.CheckProxyAuthPwd(_modifiedProxyConfig.authPwd = AuthPwdTextBox.Text);
  71. }
  72. }
  73. catch (Exception ex)
  74. {
  75. MessageBox.Show(ex.Message);
  76. return;
  77. }
  78. }
  79. controller.SaveProxy(_modifiedProxyConfig);
  80. this.Close();
  81. }
  82. private void CancelButton_Click(object sender, EventArgs e)
  83. {
  84. this.Close();
  85. }
  86. private void ProxyForm_FormClosed(object sender, FormClosedEventArgs e)
  87. {
  88. controller.ConfigChanged -= controller_ConfigChanged;
  89. }
  90. private void UseProxyCheckBox_CheckedChanged(object sender, EventArgs e)
  91. {
  92. UpdateEnabled();
  93. }
  94. private void UpdateEnabled()
  95. {
  96. if (UseProxyCheckBox.Checked)
  97. {
  98. ProxyServerTextBox.Enabled =
  99. ProxyPortTextBox.Enabled =
  100. ProxyTimeoutTextBox.Enabled =
  101. ProxyTypeComboBox.Enabled = true;
  102. if (ProxyTypeComboBox.SelectedIndex == ProxyConfig.PROXY_HTTP)
  103. {
  104. UseAuthCheckBox.Enabled = true;
  105. if (UseAuthCheckBox.Checked)
  106. {
  107. AuthUserTextBox.Enabled =
  108. AuthPwdTextBox.Enabled = true;
  109. }
  110. else
  111. {
  112. AuthUserTextBox.Enabled =
  113. AuthPwdTextBox.Enabled = false;
  114. }
  115. }
  116. else
  117. {
  118. // TODO support for SOCK5 auth
  119. UseAuthCheckBox.Enabled =
  120. AuthUserTextBox.Enabled =
  121. AuthPwdTextBox.Enabled = false;
  122. }
  123. }
  124. else
  125. {
  126. ProxyServerTextBox.Enabled =
  127. ProxyPortTextBox.Enabled =
  128. ProxyTimeoutTextBox.Enabled =
  129. ProxyTypeComboBox.Enabled =
  130. UseAuthCheckBox.Enabled =
  131. AuthUserTextBox.Enabled =
  132. AuthPwdTextBox.Enabled = false;
  133. }
  134. }
  135. private void ProxyTypeComboBox_SelectedIndexChanged(object sender, EventArgs e)
  136. {
  137. // TODO support for SOCK5 auth
  138. if (ProxyTypeComboBox.SelectedIndex != ProxyConfig.PROXY_HTTP)
  139. {
  140. UseAuthCheckBox.Checked = false;
  141. AuthUserTextBox.Clear();
  142. AuthPwdTextBox.Clear();
  143. }
  144. UpdateEnabled();
  145. }
  146. private void UseAuthCheckBox_CheckedChanged(object sender, EventArgs e)
  147. {
  148. UpdateEnabled();
  149. }
  150. }
  151. }