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

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