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