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

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