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

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