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

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