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

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