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