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.

ConfigForm.cs 10 kB

12 years ago
10 years ago
12 years ago
10 years ago
12 years ago
10 years ago
12 years ago
10 years ago
10 years ago
12 years ago
12 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
12 years ago
12 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
12 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Drawing;
  5. using System.Text;
  6. using System.Windows.Forms;
  7. using System.Diagnostics;
  8. using Microsoft.Win32;
  9. using Shadowsocks.Controller;
  10. using Shadowsocks.Model;
  11. using Shadowsocks.Properties;
  12. namespace Shadowsocks.View
  13. {
  14. public partial class ConfigForm : Form
  15. {
  16. private ShadowsocksController controller;
  17. // this is a copy of configuration that we are working on
  18. private Configuration _modifiedConfiguration;
  19. private int _oldSelectedIndex = -1;
  20. private ContextMenuStrip ServersListBoxContextMenu = null;
  21. public ConfigForm(ShadowsocksController controller)
  22. {
  23. this.Font = System.Drawing.SystemFonts.MessageBoxFont;
  24. InitializeComponent();
  25. // a dirty hack
  26. this.ServersListBox.Dock = System.Windows.Forms.DockStyle.Fill;
  27. this.PerformLayout();
  28. UpdateTexts();
  29. this.Icon = Icon.FromHandle(Resources.ssw128.GetHicon());
  30. this.controller = controller;
  31. controller.ConfigChanged += controller_ConfigChanged;
  32. LoadCurrentConfiguration();
  33. }
  34. private void ServersListBoxContextMenu_Opening(object sender, CancelEventArgs e)
  35. {
  36. //clear the menu and add custom items
  37. string contextItemText = I18N.GetString("Select");
  38. ServersListBoxContextMenu.Items.Clear();
  39. ToolStripItem item = new ToolStripMenuItem();
  40. item.Name = string.Format("{0}", ServersListBox.SelectedIndex.ToString());
  41. item.Text = string.Format("{0} - {1}", contextItemText, ServersListBox.SelectedItem.ToString());
  42. ServersListBoxContextMenu.Items.Add(item);
  43. ServersListBoxContextMenu.ItemClicked += new ToolStripItemClickedEventHandler(ServersListBoxContextMenu_ItemClicked);
  44. }
  45. void ServersListBoxContextMenu_ItemClicked(object sender, ToolStripItemClickedEventArgs e)
  46. {
  47. ToolStripItem item = e.ClickedItem;
  48. controller.SelectServerIndex(int.Parse(item.Name));
  49. }
  50. private void ServersListBox_MouseDown(object sender, MouseEventArgs e)
  51. {
  52. if (e.Button == MouseButtons.Right)
  53. {
  54. //select the item under the mouse pointer
  55. ServersListBox.SelectedIndex = ServersListBox.IndexFromPoint(e.Location);
  56. if (ServersListBox.SelectedIndex != -1)
  57. {
  58. ServersListBoxContextMenu.Show();
  59. }
  60. }
  61. }
  62. private void UpdateTexts()
  63. {
  64. AddButton.Text = I18N.GetString("&Add");
  65. DeleteButton.Text = I18N.GetString("&Delete");
  66. IPLabel.Text = I18N.GetString("Server IP");
  67. ServerPortLabel.Text = I18N.GetString("Server Port");
  68. PasswordLabel.Text = I18N.GetString("Password");
  69. EncryptionLabel.Text = I18N.GetString("Encryption");
  70. ProxyPortLabel.Text = I18N.GetString("Proxy Port");
  71. RemarksLabel.Text = I18N.GetString("Remarks");
  72. ServerGroupBox.Text = I18N.GetString("Server");
  73. OKButton.Text = I18N.GetString("OK");
  74. MyCancelButton.Text = I18N.GetString("Cancel");
  75. URIButton.Text = I18N.GetString("URI Parse");
  76. this.Text = I18N.GetString("Edit Servers");
  77. }
  78. private void controller_ConfigChanged(object sender, EventArgs e)
  79. {
  80. LoadCurrentConfiguration();
  81. }
  82. private void ShowWindow()
  83. {
  84. this.Opacity = 1;
  85. this.Show();
  86. IPTextBox.Focus();
  87. }
  88. private bool SaveOldSelectedServer()
  89. {
  90. try
  91. {
  92. if (_oldSelectedIndex == -1 || _oldSelectedIndex >= _modifiedConfiguration.configs.Count)
  93. {
  94. return true;
  95. }
  96. Server server = new Server
  97. {
  98. server = IPTextBox.Text,
  99. server_port = int.Parse(ServerPortTextBox.Text),
  100. password = PasswordTextBox.Text,
  101. local_port = int.Parse(ProxyPortTextBox.Text),
  102. method = EncryptionSelect.Text,
  103. remarks = RemarksTextBox.Text
  104. };
  105. Configuration.CheckServer(server);
  106. _modifiedConfiguration.configs[_oldSelectedIndex] = server;
  107. return true;
  108. }
  109. catch (FormatException)
  110. {
  111. MessageBox.Show(I18N.GetString("Illegal port number format"));
  112. }
  113. catch (Exception ex)
  114. {
  115. MessageBox.Show(ex.Message);
  116. }
  117. return false;
  118. }
  119. private void LoadSelectedServer()
  120. {
  121. if (ServersListBox.SelectedIndex >= 0 && ServersListBox.SelectedIndex < _modifiedConfiguration.configs.Count)
  122. {
  123. Server server = _modifiedConfiguration.configs[ServersListBox.SelectedIndex];
  124. IPTextBox.Text = server.server;
  125. ServerPortTextBox.Text = server.server_port.ToString();
  126. PasswordTextBox.Text = server.password;
  127. ProxyPortTextBox.Text = server.local_port.ToString();
  128. EncryptionSelect.Text = server.method ?? "aes-256-cfb";
  129. RemarksTextBox.Text = server.remarks;
  130. ServerGroupBox.Visible = true;
  131. //IPTextBox.Focus();
  132. }
  133. else
  134. {
  135. ServerGroupBox.Visible = false;
  136. }
  137. }
  138. private void LoadConfiguration(Configuration configuration)
  139. {
  140. ServersListBox.Items.Clear();
  141. foreach (Server server in _modifiedConfiguration.configs)
  142. {
  143. ServersListBox.Items.Add(server.FriendlyName());
  144. }
  145. }
  146. private void LoadCurrentConfiguration()
  147. {
  148. _modifiedConfiguration = controller.GetConfiguration();
  149. LoadConfiguration(_modifiedConfiguration);
  150. _oldSelectedIndex = _modifiedConfiguration.index;
  151. ServersListBox.SelectedIndex = _modifiedConfiguration.index;
  152. LoadSelectedServer();
  153. }
  154. private void ConfigForm_Load(object sender, EventArgs e)
  155. {
  156. ServersListBoxContextMenu = new ContextMenuStrip();
  157. ServersListBoxContextMenu.Opening += new CancelEventHandler(ServersListBoxContextMenu_Opening);
  158. ServersListBox.ContextMenuStrip = ServersListBoxContextMenu;
  159. }
  160. private void ServersListBox_SelectedIndexChanged(object sender, EventArgs e)
  161. {
  162. if (_oldSelectedIndex == ServersListBox.SelectedIndex)
  163. {
  164. // we are moving back to oldSelectedIndex or doing a force move
  165. return;
  166. }
  167. if (!SaveOldSelectedServer())
  168. {
  169. // why this won't cause stack overflow?
  170. ServersListBox.SelectedIndex = _oldSelectedIndex;
  171. return;
  172. }
  173. LoadSelectedServer();
  174. _oldSelectedIndex = ServersListBox.SelectedIndex;
  175. }
  176. private void AddButton_Click(object sender, EventArgs e)
  177. {
  178. if (!SaveOldSelectedServer())
  179. {
  180. return;
  181. }
  182. Server server = Configuration.GetDefaultServer();
  183. _modifiedConfiguration.configs.Add(server);
  184. LoadConfiguration(_modifiedConfiguration);
  185. ServersListBox.SelectedIndex = _modifiedConfiguration.configs.Count - 1;
  186. _oldSelectedIndex = ServersListBox.SelectedIndex;
  187. }
  188. private void DeleteButton_Click(object sender, EventArgs e)
  189. {
  190. _oldSelectedIndex = ServersListBox.SelectedIndex;
  191. if (_oldSelectedIndex >= 0 && _oldSelectedIndex < _modifiedConfiguration.configs.Count)
  192. {
  193. _modifiedConfiguration.configs.RemoveAt(_oldSelectedIndex);
  194. }
  195. if (_oldSelectedIndex >= _modifiedConfiguration.configs.Count)
  196. {
  197. // can be -1
  198. _oldSelectedIndex = _modifiedConfiguration.configs.Count - 1;
  199. }
  200. ServersListBox.SelectedIndex = _oldSelectedIndex;
  201. LoadConfiguration(_modifiedConfiguration);
  202. ServersListBox.SelectedIndex = _oldSelectedIndex;
  203. LoadSelectedServer();
  204. }
  205. private void OKButton_Click(object sender, EventArgs e)
  206. {
  207. if (!SaveOldSelectedServer())
  208. {
  209. return;
  210. }
  211. if (_modifiedConfiguration.configs.Count == 0)
  212. {
  213. MessageBox.Show(I18N.GetString("Please add at least one server"));
  214. return;
  215. }
  216. controller.SaveServers(_modifiedConfiguration.configs);
  217. //this.Close();
  218. }
  219. private void CancelButton_Click(object sender, EventArgs e)
  220. {
  221. this.Close();
  222. }
  223. private void ConfigForm_Shown(object sender, EventArgs e)
  224. {
  225. IPTextBox.Focus();
  226. }
  227. private void ConfigForm_FormClosed(object sender, FormClosedEventArgs e)
  228. {
  229. controller.ConfigChanged -= controller_ConfigChanged;
  230. }
  231. private void URIButton_Click(object sender, EventArgs e)
  232. {
  233. URIParseForm uriParseForm = new URIParseForm();
  234. uriParseForm.ShowDialog(this);
  235. if(uriParseForm.Parsed)
  236. {
  237. if (!SaveOldSelectedServer())
  238. {
  239. return;
  240. }
  241. _modifiedConfiguration.configs.Add(uriParseForm.server);
  242. LoadConfiguration(_modifiedConfiguration);
  243. ServersListBox.SelectedIndex = _modifiedConfiguration.configs.Count - 1;
  244. _oldSelectedIndex = ServersListBox.SelectedIndex;
  245. }
  246. }
  247. }
  248. }