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

13 years ago
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
13 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570
  1. using System;
  2. using System.Drawing;
  3. using System.Runtime.CompilerServices;
  4. using System.Windows.Forms;
  5. using Shadowsocks.Controller;
  6. using Shadowsocks.Model;
  7. using Shadowsocks.Properties;
  8. namespace Shadowsocks.View
  9. {
  10. public partial class ConfigForm : Form
  11. {
  12. private ShadowsocksController controller;
  13. // this is a copy of configuration that we are working on
  14. private Configuration _modifiedConfiguration;
  15. private int _lastSelectedIndex = -1;
  16. public ConfigForm(ShadowsocksController controller)
  17. {
  18. Font = SystemFonts.MessageBoxFont;
  19. InitializeComponent();
  20. // a dirty hack
  21. ServersListBox.Dock = DockStyle.Fill;
  22. tableLayoutPanel5.Dock = DockStyle.Fill;
  23. PerformLayout();
  24. UpdateTexts();
  25. SetupValueChangedListeners();
  26. Icon = Icon.FromHandle(Resources.ssw128.GetHicon());
  27. this.controller = controller;
  28. controller.ConfigChanged += Controller_ConfigChanged;
  29. LoadCurrentConfiguration();
  30. }
  31. private void UpdateTexts()
  32. {
  33. AddButton.Text = I18N.GetString("&Add");
  34. DeleteButton.Text = I18N.GetString("&Delete");
  35. DuplicateButton.Text = I18N.GetString("Dupli&cate");
  36. IPLabel.Text = I18N.GetString("Server Addr");
  37. ServerPortLabel.Text = I18N.GetString("Server Port");
  38. PasswordLabel.Text = I18N.GetString("Password");
  39. ShowPasswdCheckBox.Text = I18N.GetString("Show Password");
  40. EncryptionLabel.Text = I18N.GetString("Encryption");
  41. PluginLabel.Text = I18N.GetString("Plugin Program");
  42. PluginOptionsLabel.Text = I18N.GetString("Plugin Options");
  43. PluginArgumentsLabel.Text = I18N.GetString("Plugin Arguments");
  44. NeedPluginArgCheckBox.Text = I18N.GetString("Need Plugin Argument");
  45. ProxyPortLabel.Text = I18N.GetString("Proxy Port");
  46. PortableModeCheckBox.Text = I18N.GetString("Portable Mode");
  47. toolTip1.SetToolTip(PortableModeCheckBox, I18N.GetString("Restart required"));
  48. RemarksLabel.Text = I18N.GetString("Remarks");
  49. TimeoutLabel.Text = I18N.GetString("Timeout(Sec)");
  50. ServerGroupBox.Text = I18N.GetString("Server");
  51. OKButton.Text = I18N.GetString("OK");
  52. MyCancelButton.Text = I18N.GetString("Cancel");
  53. ApplyButton.Text = I18N.GetString("Apply");
  54. MoveUpButton.Text = I18N.GetString("Move &Up");
  55. MoveDownButton.Text = I18N.GetString("Move D&own");
  56. Text = I18N.GetString("Edit Servers");
  57. }
  58. private void SetupValueChangedListeners()
  59. {
  60. IPTextBox.TextChanged += ConfigValueChanged;
  61. ProxyPortTextBox.TextChanged += ConfigValueChanged;
  62. PasswordTextBox.TextChanged += ConfigValueChanged;
  63. EncryptionSelect.SelectedIndexChanged += ConfigValueChanged;
  64. PluginTextBox.TextChanged += ConfigValueChanged;
  65. PluginArgumentsTextBox.TextChanged += ConfigValueChanged;
  66. PluginOptionsTextBox.TextChanged += ConfigValueChanged;
  67. RemarksTextBox.TextChanged += ConfigValueChanged;
  68. TimeoutTextBox.TextChanged += ConfigValueChanged;
  69. PortableModeCheckBox.CheckedChanged += ConfigValueChanged;
  70. ServerPortTextBox.TextChanged += ConfigValueChanged;
  71. }
  72. private void Controller_ConfigChanged(object sender, EventArgs e)
  73. {
  74. LoadCurrentConfiguration();
  75. }
  76. private void ConfigValueChanged(object sender, EventArgs e)
  77. {
  78. ApplyButton.Enabled = true;
  79. }
  80. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  81. private void UpdateIndexToEnd()
  82. {
  83. _lastSelectedIndex = (ServersListBox.SelectedIndex = (_modifiedConfiguration.configs.Count - 1));
  84. }
  85. private bool ValidateAndSaveSelectedServerDetails(bool isSave = false, bool isCopy = false)
  86. {
  87. try
  88. {
  89. if (_lastSelectedIndex == -1 || _lastSelectedIndex >= _modifiedConfiguration.configs.Count)
  90. {
  91. return true;
  92. }
  93. bool verify = GetServerDetailsFromUI(out Server server, isSave, isCopy);
  94. if (server != null)
  95. {
  96. if (isSave || isCopy)
  97. Configuration.CheckServer(server);
  98. _modifiedConfiguration.configs[_lastSelectedIndex] = server;
  99. }
  100. return verify;
  101. }
  102. catch (Exception ex)
  103. {
  104. MessageBox.Show(ex.Message);
  105. }
  106. return false;
  107. }
  108. private bool GetServerDetailsFromUI(out Server server, bool isSave = false, bool isCopy = false)
  109. {
  110. server = null;
  111. bool? checkIP = false;
  112. bool? checkPort = false;
  113. bool? checkPassword = false;
  114. bool? checkTimeout = false;
  115. if ((checkIP = CheckIPTextBox(out string address, isSave, isCopy)).GetValueOrDefault(false) && address != null
  116. && (checkPort = CheckServerPortTextBox(out int? addressPort, isSave, isCopy)).GetValueOrDefault(false) && addressPort.HasValue
  117. && (checkPassword = CheckPasswordTextBox(out string serverPassword, isSave, isCopy)).GetValueOrDefault(false) && serverPassword != null
  118. && (checkTimeout = CheckTimeoutTextBox(out int? timeout, isSave, isCopy)).GetValueOrDefault(false) && timeout.HasValue)
  119. {
  120. server = new Server()
  121. {
  122. server = address,
  123. server_port = addressPort.Value,
  124. password = serverPassword,
  125. method = EncryptionSelect.Text,
  126. plugin = PluginTextBox.Text,
  127. plugin_opts = PluginOptionsTextBox.Text,
  128. plugin_args = PluginArgumentsTextBox.Text,
  129. remarks = RemarksTextBox.Text,
  130. timeout = timeout.Value,
  131. };
  132. return true;
  133. }
  134. if (checkIP == null || checkPort == null || checkTimeout == null)
  135. {
  136. _modifiedConfiguration.configs.RemoveAt(_lastSelectedIndex);
  137. ServersListBox.SelectedIndexChanged -= ServersListBox_SelectedIndexChanged;
  138. LoadServerNameListToUI(_modifiedConfiguration);
  139. UpdateIndexToEnd();
  140. ServersListBox.SelectedIndexChanged += ServersListBox_SelectedIndexChanged;
  141. return true;
  142. }
  143. else
  144. return false;
  145. }
  146. #region GetServerDetailsFromUI Check
  147. private bool? CheckIPTextBox(out string address, bool isSave, bool isCopy)
  148. {
  149. address = null;
  150. string outAddress;
  151. if (Uri.CheckHostName(outAddress = IPTextBox.Text.Trim()) == UriHostNameType.Unknown)
  152. {
  153. if (!isSave && !isCopy && ServersListBox.Items.Count > 1 && I18N.GetString("New server").Equals(ServersListBox.Items[_lastSelectedIndex].ToString()))
  154. {
  155. DialogResult result = MessageBox.Show(I18N.GetString("Whether to discard unconfigured servers"), I18N.GetString("Operation failure"), MessageBoxButtons.OKCancel);
  156. if (result == DialogResult.OK)
  157. return null;
  158. }
  159. else if (ApplyButton.Enabled && !isSave && !isCopy)
  160. {
  161. var result = MessageBox.Show(I18N.GetString("Invalid server address, Cannot automatically save or discard changes"), I18N.GetString("Auto save failed"), MessageBoxButtons.OKCancel);
  162. if (result == DialogResult.Cancel)
  163. return false;
  164. else
  165. {
  166. address = _modifiedConfiguration.configs[_lastSelectedIndex].server;
  167. return true;
  168. }
  169. }
  170. else
  171. {
  172. MessageBox.Show(I18N.GetString("Invalid server address"), I18N.GetString("Operation failure"));
  173. IPTextBox.Focus();
  174. }
  175. return false;
  176. }
  177. else
  178. {
  179. address = outAddress;
  180. }
  181. return true;
  182. }
  183. private bool? CheckServerPortTextBox(out int? addressPort, bool isSave, bool isCopy)
  184. {
  185. addressPort = null;
  186. if (!int.TryParse(ServerPortTextBox.Text, out int outaddressPort))
  187. {
  188. if (!isSave && !isCopy && ServersListBox.Items.Count > 1 && I18N.GetString("New server").Equals(ServersListBox.Items[_lastSelectedIndex].ToString()))
  189. {
  190. DialogResult result = MessageBox.Show(I18N.GetString("Whether to discard unconfigured servers"), I18N.GetString("Operation failure"), MessageBoxButtons.OKCancel);
  191. if (result == DialogResult.OK)
  192. return null;
  193. }
  194. else if (!isSave && !isCopy)
  195. {
  196. var result = MessageBox.Show(I18N.GetString("Illegal port number format, Cannot automatically save or discard changes"), I18N.GetString("Auto save failed"), MessageBoxButtons.OKCancel);
  197. if (result == DialogResult.Cancel)
  198. return false;
  199. else
  200. {
  201. addressPort = _modifiedConfiguration.configs[_lastSelectedIndex].server_port;
  202. return true;
  203. }
  204. }
  205. else
  206. {
  207. MessageBox.Show(I18N.GetString("Illegal port number format"), I18N.GetString("Operation failure"));
  208. ServerPortTextBox.Focus();
  209. }
  210. return false;
  211. }
  212. else
  213. {
  214. addressPort = outaddressPort;
  215. }
  216. return true;
  217. }
  218. private bool? CheckPasswordTextBox(out string password, bool isSave, bool isCopy)
  219. {
  220. password = null;
  221. string outPassword;
  222. if ((outPassword = PasswordTextBox.Text).IsNullOrWhiteSpace())
  223. {
  224. if (!isSave && !isCopy && ServersListBox.Items.Count > 1 && I18N.GetString("New server").Equals(ServersListBox.Items[_lastSelectedIndex].ToString()))
  225. {
  226. DialogResult result = MessageBox.Show(I18N.GetString("Whether to discard unconfigured servers"), I18N.GetString("Operation failure"), MessageBoxButtons.OKCancel);
  227. if (result == DialogResult.OK)
  228. return null;
  229. }
  230. else if (ApplyButton.Enabled && !isSave && !isCopy)
  231. {
  232. var result = MessageBox.Show(I18N.GetString("Password can not be blank, Cannot automatically save or discard changes"), I18N.GetString("Auto save failed"), MessageBoxButtons.OKCancel);
  233. if (result == DialogResult.Cancel)
  234. return false;
  235. else
  236. {
  237. password = _modifiedConfiguration.configs[_lastSelectedIndex].password;
  238. return true;
  239. }
  240. }
  241. else
  242. {
  243. MessageBox.Show(I18N.GetString("Password can not be blank"), I18N.GetString("Operation failure"));
  244. PasswordTextBox.Focus();
  245. }
  246. return false;
  247. }
  248. else
  249. {
  250. password = outPassword;
  251. }
  252. return true;
  253. }
  254. private bool? CheckTimeoutTextBox(out int? timeout, bool isSave, bool isCopy)
  255. {
  256. timeout = null;
  257. if (!int.TryParse(TimeoutTextBox.Text, out int outTimeout))
  258. {
  259. if (!isSave && !isCopy && ServersListBox.Items.Count > 1 && I18N.GetString("New server").Equals(ServersListBox.Items[_lastSelectedIndex].ToString()))
  260. {
  261. DialogResult result = MessageBox.Show(I18N.GetString("Whether to discard unconfigured servers"), I18N.GetString("Operation failure"), MessageBoxButtons.OKCancel);
  262. if (result == DialogResult.OK)
  263. return null;
  264. }
  265. else if (ApplyButton.Enabled && !isSave && !isCopy)
  266. {
  267. var result = MessageBox.Show(I18N.GetString("Illegal timeout format, Cannot automatically save or discard changes"), I18N.GetString("Auto save failed"), MessageBoxButtons.OKCancel);
  268. if (result == DialogResult.Cancel)
  269. return false;
  270. else
  271. {
  272. timeout = _modifiedConfiguration.configs[_lastSelectedIndex].timeout;
  273. return true;
  274. }
  275. }
  276. else
  277. {
  278. MessageBox.Show(I18N.GetString("Illegal timeout format"), I18N.GetString("Operation failure"));
  279. TimeoutTextBox.Focus();
  280. }
  281. return false;
  282. }
  283. else
  284. {
  285. timeout = outTimeout;
  286. }
  287. return true;
  288. }
  289. #endregion
  290. private void LoadSelectedServerDetails()
  291. {
  292. if (ServersListBox.SelectedIndex >= 0 && ServersListBox.SelectedIndex < _modifiedConfiguration.configs.Count)
  293. {
  294. Server server = _modifiedConfiguration.configs[ServersListBox.SelectedIndex];
  295. SetServerDetailsToUI(server);
  296. }
  297. }
  298. private void SetServerDetailsToUI(Server server)
  299. {
  300. IPTextBox.Text = server.server;
  301. ServerPortTextBox.Text = server.server_port.ToString();
  302. PasswordTextBox.Text = server.password;
  303. EncryptionSelect.Text = server.method ?? "aes-256-cfb";
  304. PluginTextBox.Text = server.plugin;
  305. PluginOptionsTextBox.Text = server.plugin_opts;
  306. PluginArgumentsTextBox.Text = server.plugin_args;
  307. bool showPluginArgInput = !string.IsNullOrEmpty(server.plugin_args);
  308. NeedPluginArgCheckBox.Checked = showPluginArgInput;
  309. ShowHidePluginArgInput(showPluginArgInput);
  310. RemarksTextBox.Text = server.remarks;
  311. TimeoutTextBox.Text = server.timeout.ToString();
  312. ApplyButton.Enabled = false;
  313. }
  314. private void ShowHidePluginArgInput(bool show)
  315. {
  316. PluginArgumentsTextBox.Visible = show;
  317. PluginArgumentsLabel.Visible = show;
  318. }
  319. private void LoadServerNameListToUI(Configuration configuration)
  320. {
  321. ServersListBox.Items.Clear();
  322. foreach (Server server in configuration.configs)
  323. {
  324. ServersListBox.Items.Add(server.FriendlyName());
  325. }
  326. }
  327. private void LoadCurrentConfiguration()
  328. {
  329. _modifiedConfiguration = controller.GetConfigurationCopy();
  330. LoadServerNameListToUI(_modifiedConfiguration);
  331. _lastSelectedIndex = _modifiedConfiguration.index;
  332. if (_lastSelectedIndex < 0 || _lastSelectedIndex >= ServersListBox.Items.Count)
  333. {
  334. _lastSelectedIndex = 0;
  335. }
  336. ServersListBox.SelectedIndex = _lastSelectedIndex;
  337. UpdateButtons();
  338. LoadSelectedServerDetails();
  339. ProxyPortTextBox.Text = _modifiedConfiguration.localPort.ToString();
  340. PortableModeCheckBox.Checked = _modifiedConfiguration.portableMode;
  341. ApplyButton.Enabled = false;
  342. }
  343. private bool SaveValidConfiguration()
  344. {
  345. if (!ValidateAndSaveSelectedServerDetails(isSave: true))
  346. {
  347. return false;
  348. }
  349. int localPort = int.Parse(ProxyPortTextBox.Text);
  350. Configuration.CheckLocalPort(localPort);
  351. _modifiedConfiguration.localPort = localPort;
  352. _modifiedConfiguration.portableMode = PortableModeCheckBox.Checked;
  353. controller.SaveServers(_modifiedConfiguration.configs, _modifiedConfiguration.localPort, _modifiedConfiguration.portableMode);
  354. // SelectedIndex remains valid
  355. // We handled this in event handlers, e.g. Add/DeleteButton, SelectedIndexChanged
  356. // and move operations
  357. controller.SelectServerIndex(ServersListBox.SelectedIndex);
  358. return true;
  359. }
  360. private void ConfigForm_KeyDown(object sender, KeyEventArgs e)
  361. {
  362. // Sometimes the users may hit enter key by mistake, and the form will close without saving entries.
  363. if (e.KeyCode == Keys.Enter)
  364. {
  365. SaveValidConfiguration();
  366. }
  367. }
  368. private void ServersListBox_SelectedIndexChanged(object sender, EventArgs e)
  369. {
  370. if (!ServersListBox.CanSelect)
  371. {
  372. return;
  373. }
  374. if (_lastSelectedIndex == ServersListBox.SelectedIndex)
  375. {
  376. // we are moving back to oldSelectedIndex or doing a force move
  377. return;
  378. }
  379. if (!ValidateAndSaveSelectedServerDetails())
  380. {
  381. // why this won't cause stack overflow?
  382. ServersListBox.SelectedIndex = _lastSelectedIndex;
  383. return;
  384. }
  385. if (_lastSelectedIndex >= 0 && _lastSelectedIndex < _modifiedConfiguration.configs.Count)
  386. {
  387. ServersListBox.Items[_lastSelectedIndex] = _modifiedConfiguration.configs[_lastSelectedIndex].FriendlyName();
  388. }
  389. UpdateButtons();
  390. LoadSelectedServerDetails();
  391. _lastSelectedIndex = ServersListBox.SelectedIndex;
  392. }
  393. private void AddButton_Click(object sender, EventArgs e)
  394. {
  395. if (ValidateAndSaveSelectedServerDetails(isSave: true))
  396. {
  397. Configuration.AddDefaultServerOrServer(_modifiedConfiguration);
  398. LoadServerNameListToUI(_modifiedConfiguration);
  399. UpdateIndexToEnd();
  400. }
  401. }
  402. private void DuplicateButton_Click(object sender, EventArgs e)
  403. {
  404. if (ValidateAndSaveSelectedServerDetails(isCopy: true))
  405. {
  406. Server currServer = _modifiedConfiguration.configs[_lastSelectedIndex];
  407. Configuration.AddDefaultServerOrServer(_modifiedConfiguration, currServer);
  408. LoadServerNameListToUI(_modifiedConfiguration);
  409. UpdateIndexToEnd();
  410. }
  411. }
  412. private void DeleteButton_Click(object sender, EventArgs e)
  413. {
  414. _modifiedConfiguration.configs.RemoveAt(_lastSelectedIndex);
  415. if (_modifiedConfiguration.configs.Count == 0)
  416. {
  417. Configuration.AddDefaultServerOrServer(_modifiedConfiguration);
  418. }
  419. LoadServerNameListToUI(_modifiedConfiguration);
  420. UpdateIndexToEnd();
  421. LoadSelectedServerDetails();
  422. UpdateButtons();
  423. }
  424. private void UpdateButtons()
  425. {
  426. DeleteButton.Enabled = (ServersListBox.Items.Count > 0);
  427. MoveUpButton.Enabled = (ServersListBox.SelectedIndex > 0);
  428. MoveDownButton.Enabled = (ServersListBox.SelectedIndex < ServersListBox.Items.Count - 1);
  429. }
  430. private void MoveUpButton_Click(object sender, EventArgs e)
  431. {
  432. if (ServersListBox.SelectedIndex > 0)
  433. {
  434. MoveConfigItem(-1); // -1 means move backward
  435. }
  436. }
  437. private void MoveDownButton_Click(object sender, EventArgs e)
  438. {
  439. if (ServersListBox.SelectedIndex < ServersListBox.Items.Count - 1)
  440. {
  441. MoveConfigItem(+1); // +1 means move forward
  442. }
  443. }
  444. private void MoveConfigItem(int step)
  445. {
  446. int index = ServersListBox.SelectedIndex;
  447. Server server = _modifiedConfiguration.configs[index];
  448. object item = ServersListBox.Items[index];
  449. _modifiedConfiguration.configs.Remove(server);
  450. _modifiedConfiguration.configs.Insert(index + step, server);
  451. _modifiedConfiguration.index += step;
  452. ServersListBox.BeginUpdate();
  453. ServersListBox.Enabled = false;
  454. _lastSelectedIndex = index + step;
  455. ServersListBox.Items.Remove(item);
  456. ServersListBox.Items.Insert(index + step, item);
  457. ServersListBox.Enabled = true;
  458. ServersListBox.SelectedIndex = index + step;
  459. ServersListBox.EndUpdate();
  460. UpdateButtons();
  461. }
  462. private void OKButton_Click(object sender, EventArgs e)
  463. {
  464. if (SaveValidConfiguration())
  465. {
  466. Close();
  467. }
  468. }
  469. private void CancelButton_Click(object sender, EventArgs e)
  470. {
  471. Close();
  472. }
  473. private void ApplyButton_Click(object sender, EventArgs e)
  474. {
  475. SaveValidConfiguration();
  476. }
  477. private void ConfigForm_Shown(object sender, EventArgs e)
  478. {
  479. IPTextBox.Focus();
  480. }
  481. private void ConfigForm_FormClosed(object sender, FormClosedEventArgs e)
  482. {
  483. controller.ConfigChanged -= Controller_ConfigChanged;
  484. }
  485. private void ShowPasswdCheckBox_CheckedChanged(object sender, EventArgs e)
  486. {
  487. PasswordTextBox.UseSystemPasswordChar = !ShowPasswdCheckBox.Checked;
  488. }
  489. private void UsePluginArgCheckBox_CheckedChanged(object sender, EventArgs e)
  490. {
  491. ShowHidePluginArgInput(NeedPluginArgCheckBox.Checked);
  492. }
  493. }
  494. }