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

13 years ago
11 years ago
13 years ago
11 years ago
13 years ago
11 years ago
13 years ago
11 years ago
11 years ago
13 years ago
13 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
13 years ago
13 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
13 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359
  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 _lastSelectedIndex = -1;
  20. public ConfigForm(ShadowsocksController controller)
  21. {
  22. this.Font = System.Drawing.SystemFonts.MessageBoxFont;
  23. InitializeComponent();
  24. // a dirty hack
  25. this.ServersListBox.Dock = System.Windows.Forms.DockStyle.Fill;
  26. this.PerformLayout();
  27. UpdateTexts();
  28. this.Icon = Icon.FromHandle(Resources.ssw128.GetHicon());
  29. this.controller = controller;
  30. controller.ConfigChanged += controller_ConfigChanged;
  31. LoadCurrentConfiguration();
  32. }
  33. private void UpdateTexts()
  34. {
  35. AddButton.Text = I18N.GetString("&Add");
  36. DeleteButton.Text = I18N.GetString("&Delete");
  37. DuplicateButton.Text = I18N.GetString("Dupli&cate");
  38. IPLabel.Text = I18N.GetString("Server Addr");
  39. ServerPortLabel.Text = I18N.GetString("Server Port");
  40. PasswordLabel.Text = I18N.GetString("Password");
  41. EncryptionLabel.Text = I18N.GetString("Encryption");
  42. ProxyPortLabel.Text = I18N.GetString("Proxy Port");
  43. RemarksLabel.Text = I18N.GetString("Remarks");
  44. TimeoutLabel.Text = I18N.GetString("Timeout(Sec)");
  45. OneTimeAuth.Text = I18N.GetString("Onetime Authentication");
  46. ServerGroupBox.Text = I18N.GetString("Server");
  47. OKButton.Text = I18N.GetString("OK");
  48. MyCancelButton.Text = I18N.GetString("Cancel");
  49. MoveUpButton.Text = I18N.GetString("Move &Up");
  50. MoveDownButton.Text = I18N.GetString("Move D&own");
  51. this.Text = I18N.GetString("Edit Servers");
  52. }
  53. private void controller_ConfigChanged(object sender, EventArgs e)
  54. {
  55. LoadCurrentConfiguration();
  56. }
  57. private void ShowWindow()
  58. {
  59. this.Opacity = 1;
  60. this.Show();
  61. IPTextBox.Focus();
  62. }
  63. private bool SaveOldSelectedServer()
  64. {
  65. try
  66. {
  67. if (_lastSelectedIndex == -1 || _lastSelectedIndex >= _modifiedConfiguration.configs.Count)
  68. {
  69. return true;
  70. }
  71. Server server = new Server();
  72. if (Uri.CheckHostName(server.server = IPTextBox.Text.Trim()) == UriHostNameType.Unknown)
  73. {
  74. MessageBox.Show(I18N.GetString("Invalid server address"));
  75. IPTextBox.Focus();
  76. return false;
  77. }
  78. if (!int.TryParse(ServerPortTextBox.Text, out server.server_port))
  79. {
  80. MessageBox.Show(I18N.GetString("Illegal port number format"));
  81. ServerPortTextBox.Focus();
  82. return false;
  83. }
  84. server.password = PasswordTextBox.Text;
  85. server.method = EncryptionSelect.Text;
  86. server.remarks = RemarksTextBox.Text;
  87. if (!int.TryParse(TimeoutTextBox.Text, out server.timeout))
  88. {
  89. MessageBox.Show(I18N.GetString("Illegal timeout format"));
  90. TimeoutTextBox.Focus();
  91. return false;
  92. }
  93. server.auth = OneTimeAuth.Checked;
  94. int localPort = int.Parse(ProxyPortTextBox.Text);
  95. Configuration.CheckServer(server);
  96. Configuration.CheckLocalPort(localPort);
  97. _modifiedConfiguration.configs[_lastSelectedIndex] = server;
  98. _modifiedConfiguration.localPort = localPort;
  99. return true;
  100. }
  101. catch (Exception ex)
  102. {
  103. MessageBox.Show(ex.Message);
  104. }
  105. return false;
  106. }
  107. private void LoadSelectedServer()
  108. {
  109. if (ServersListBox.SelectedIndex >= 0 && ServersListBox.SelectedIndex < _modifiedConfiguration.configs.Count)
  110. {
  111. Server server = _modifiedConfiguration.configs[ServersListBox.SelectedIndex];
  112. IPTextBox.Text = server.server;
  113. ServerPortTextBox.Text = server.server_port.ToString();
  114. PasswordTextBox.Text = server.password;
  115. ProxyPortTextBox.Text = _modifiedConfiguration.localPort.ToString();
  116. EncryptionSelect.Text = server.method ?? "aes-256-cfb";
  117. RemarksTextBox.Text = server.remarks;
  118. TimeoutTextBox.Text = server.timeout.ToString();
  119. OneTimeAuth.Checked = server.auth;
  120. }
  121. }
  122. private void LoadConfiguration(Configuration configuration)
  123. {
  124. ServersListBox.Items.Clear();
  125. foreach (Server server in _modifiedConfiguration.configs)
  126. {
  127. ServersListBox.Items.Add(server.FriendlyName());
  128. }
  129. }
  130. private void LoadCurrentConfiguration()
  131. {
  132. _modifiedConfiguration = controller.GetConfigurationCopy();
  133. LoadConfiguration(_modifiedConfiguration);
  134. _lastSelectedIndex = _modifiedConfiguration.index;
  135. if (_lastSelectedIndex < 0 || _lastSelectedIndex >= ServersListBox.Items.Count)
  136. {
  137. _lastSelectedIndex = 0;
  138. }
  139. ServersListBox.SelectedIndex = _lastSelectedIndex;
  140. UpdateMoveUpAndDownButton();
  141. LoadSelectedServer();
  142. }
  143. private void ConfigForm_Load(object sender, EventArgs e)
  144. {
  145. }
  146. private void ConfigForm_KeyDown(object sender, KeyEventArgs e)
  147. {
  148. // Sometimes the users may hit enter key by mistake, and the form will close without saving entries.
  149. if (e.KeyCode == Keys.Enter)
  150. {
  151. Server server = controller.GetCurrentServer();
  152. if (!SaveOldSelectedServer())
  153. {
  154. return;
  155. }
  156. if (_modifiedConfiguration.configs.Count == 0)
  157. {
  158. MessageBox.Show(I18N.GetString("Please add at least one server"));
  159. return;
  160. }
  161. controller.SaveServers(_modifiedConfiguration.configs, _modifiedConfiguration.localPort);
  162. controller.SelectServerIndex(_modifiedConfiguration.configs.IndexOf(server));
  163. }
  164. }
  165. private void ServersListBox_SelectedIndexChanged(object sender, EventArgs e)
  166. {
  167. if (!ServersListBox.CanSelect)
  168. {
  169. return;
  170. }
  171. if (_lastSelectedIndex == ServersListBox.SelectedIndex)
  172. {
  173. // we are moving back to oldSelectedIndex or doing a force move
  174. return;
  175. }
  176. if (!SaveOldSelectedServer())
  177. {
  178. // why this won't cause stack overflow?
  179. ServersListBox.SelectedIndex = _lastSelectedIndex;
  180. return;
  181. }
  182. if (_lastSelectedIndex >= 0)
  183. {
  184. ServersListBox.Items[_lastSelectedIndex] = _modifiedConfiguration.configs[_lastSelectedIndex].FriendlyName();
  185. }
  186. UpdateMoveUpAndDownButton();
  187. LoadSelectedServer();
  188. _lastSelectedIndex = ServersListBox.SelectedIndex;
  189. }
  190. private void AddButton_Click(object sender, EventArgs e)
  191. {
  192. if (!SaveOldSelectedServer())
  193. {
  194. return;
  195. }
  196. Server server = Configuration.GetDefaultServer();
  197. _modifiedConfiguration.configs.Add(server);
  198. LoadConfiguration(_modifiedConfiguration);
  199. ServersListBox.SelectedIndex = _modifiedConfiguration.configs.Count - 1;
  200. _lastSelectedIndex = ServersListBox.SelectedIndex;
  201. }
  202. private void DuplicateButton_Click( object sender, EventArgs e )
  203. {
  204. if (!SaveOldSelectedServer())
  205. {
  206. return;
  207. }
  208. Server currServer = _modifiedConfiguration.configs[_lastSelectedIndex];
  209. var currIndex = _modifiedConfiguration.configs.IndexOf( currServer );
  210. _modifiedConfiguration.configs.Insert(currIndex + 1, currServer);
  211. LoadConfiguration(_modifiedConfiguration);
  212. ServersListBox.SelectedIndex = currIndex + 1;
  213. _lastSelectedIndex = ServersListBox.SelectedIndex;
  214. }
  215. private void DeleteButton_Click(object sender, EventArgs e)
  216. {
  217. _lastSelectedIndex = ServersListBox.SelectedIndex;
  218. if (_lastSelectedIndex >= 0 && _lastSelectedIndex < _modifiedConfiguration.configs.Count)
  219. {
  220. _modifiedConfiguration.configs.RemoveAt(_lastSelectedIndex);
  221. }
  222. if (_lastSelectedIndex >= _modifiedConfiguration.configs.Count)
  223. {
  224. // can be -1
  225. _lastSelectedIndex = _modifiedConfiguration.configs.Count - 1;
  226. }
  227. ServersListBox.SelectedIndex = _lastSelectedIndex;
  228. LoadConfiguration(_modifiedConfiguration);
  229. ServersListBox.SelectedIndex = _lastSelectedIndex;
  230. LoadSelectedServer();
  231. }
  232. private void OKButton_Click(object sender, EventArgs e)
  233. {
  234. if (!SaveOldSelectedServer())
  235. {
  236. return;
  237. }
  238. if (_modifiedConfiguration.configs.Count == 0)
  239. {
  240. MessageBox.Show(I18N.GetString("Please add at least one server"));
  241. return;
  242. }
  243. controller.SaveServers(_modifiedConfiguration.configs, _modifiedConfiguration.localPort);
  244. // SelectedIndex remains valid
  245. // We handled this in event handlers, e.g. Add/DeleteButton, SelectedIndexChanged
  246. // and move operations
  247. controller.SelectServerIndex(ServersListBox.SelectedIndex);
  248. this.Close();
  249. }
  250. private void CancelButton_Click(object sender, EventArgs e)
  251. {
  252. this.Close();
  253. }
  254. private void ConfigForm_Shown(object sender, EventArgs e)
  255. {
  256. IPTextBox.Focus();
  257. }
  258. private void ConfigForm_FormClosed(object sender, FormClosedEventArgs e)
  259. {
  260. controller.ConfigChanged -= controller_ConfigChanged;
  261. }
  262. private void MoveConfigItem(int step)
  263. {
  264. int index = ServersListBox.SelectedIndex;
  265. Server server = _modifiedConfiguration.configs[index];
  266. object item = ServersListBox.Items[index];
  267. _modifiedConfiguration.configs.Remove(server);
  268. _modifiedConfiguration.configs.Insert(index + step, server);
  269. _modifiedConfiguration.index += step;
  270. ServersListBox.BeginUpdate();
  271. ServersListBox.Enabled = false;
  272. _lastSelectedIndex = index + step;
  273. ServersListBox.Items.Remove(item);
  274. ServersListBox.Items.Insert(index + step, item);
  275. ServersListBox.Enabled = true;
  276. ServersListBox.SelectedIndex = index + step;
  277. ServersListBox.EndUpdate();
  278. UpdateMoveUpAndDownButton();
  279. }
  280. private void UpdateMoveUpAndDownButton()
  281. {
  282. if (ServersListBox.SelectedIndex == 0)
  283. {
  284. MoveUpButton.Enabled = false;
  285. }
  286. else
  287. {
  288. MoveUpButton.Enabled = true;
  289. }
  290. if (ServersListBox.SelectedIndex == ServersListBox.Items.Count - 1)
  291. {
  292. MoveDownButton.Enabled = false;
  293. }
  294. else
  295. {
  296. MoveDownButton.Enabled = true;
  297. }
  298. }
  299. private void MoveUpButton_Click(object sender, EventArgs e)
  300. {
  301. if (!SaveOldSelectedServer())
  302. {
  303. return;
  304. }
  305. if (ServersListBox.SelectedIndex > 0)
  306. {
  307. MoveConfigItem(-1); // -1 means move backward
  308. }
  309. }
  310. private void MoveDownButton_Click(object sender, EventArgs e)
  311. {
  312. if (!SaveOldSelectedServer())
  313. {
  314. return;
  315. }
  316. if (ServersListBox.SelectedIndex < ServersListBox.Items.Count - 1)
  317. {
  318. MoveConfigItem(+1); // +1 means move forward
  319. }
  320. }
  321. }
  322. }