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 11 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
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
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
12 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
12 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Drawing;
  5. using System.Text;
  6. using System.Windows.Forms;
  7. using System.Diagnostics;
  8. using Shadowsocks.Controller;
  9. using Shadowsocks.Model;
  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 oldSelectedIndex = -1;
  18. private bool isFirstRun;
  19. public ConfigForm(ShadowsocksController controller)
  20. {
  21. InitializeComponent();
  22. notifyIcon1.ContextMenu = contextMenu1;
  23. this.controller = controller;
  24. controller.EnableStatusChanged += controller_EnableStatusChanged;
  25. controller.ConfigChanged += controller_ConfigChanged;
  26. controller.PACFileReadyToOpen += controller_PACFileReadyToOpen;
  27. LoadCurrentConfiguration();
  28. }
  29. private void controller_ConfigChanged(object sender, EventArgs e)
  30. {
  31. LoadCurrentConfiguration();
  32. }
  33. private void controller_EnableStatusChanged(object sender, EventArgs e)
  34. {
  35. enableItem.Checked = controller.GetConfiguration().enabled;
  36. }
  37. void controller_PACFileReadyToOpen(object sender, ShadowsocksController.PathEventArgs e)
  38. {
  39. string argument = @"/select, " + e.Path;
  40. System.Diagnostics.Process.Start("explorer.exe", argument);
  41. }
  42. private void ShowWindow()
  43. {
  44. this.Opacity = 1;
  45. this.Show();
  46. IPTextBox.Focus();
  47. }
  48. private bool SaveOldSelectedServer()
  49. {
  50. try
  51. {
  52. if (oldSelectedIndex == -1 || oldSelectedIndex >= modifiedConfiguration.configs.Count)
  53. {
  54. return true;
  55. }
  56. Server server = new Server
  57. {
  58. server = IPTextBox.Text,
  59. server_port = int.Parse(ServerPortTextBox.Text),
  60. password = PasswordTextBox.Text,
  61. local_port = int.Parse(ProxyPortTextBox.Text),
  62. method = EncryptionSelect.Text,
  63. remarks = RemarksTextBox.Text
  64. };
  65. Configuration.CheckServer(server);
  66. modifiedConfiguration.configs[oldSelectedIndex] = server;
  67. return true;
  68. }
  69. catch (FormatException)
  70. {
  71. MessageBox.Show("illegal port number format");
  72. }
  73. catch (Exception ex)
  74. {
  75. MessageBox.Show(ex.Message);
  76. }
  77. return false;
  78. }
  79. private void LoadSelectedServer()
  80. {
  81. if (ServersListBox.SelectedIndex >= 0 && ServersListBox.SelectedIndex < modifiedConfiguration.configs.Count)
  82. {
  83. Server server = modifiedConfiguration.configs[ServersListBox.SelectedIndex];
  84. IPTextBox.Text = server.server;
  85. ServerPortTextBox.Text = server.server_port.ToString();
  86. PasswordTextBox.Text = server.password;
  87. ProxyPortTextBox.Text = server.local_port.ToString();
  88. EncryptionSelect.Text = server.method == null ? "aes-256-cfb" : server.method;
  89. RemarksTextBox.Text = server.remarks;
  90. ServerGroupBox.Visible = true;
  91. //IPTextBox.Focus();
  92. }
  93. else
  94. {
  95. ServerGroupBox.Visible = false;
  96. }
  97. }
  98. private void LoadConfiguration(Configuration configuration)
  99. {
  100. ServersListBox.Items.Clear();
  101. foreach (Server server in modifiedConfiguration.configs)
  102. {
  103. ServersListBox.Items.Add(string.IsNullOrEmpty(server.server) ? "New server" : string.IsNullOrEmpty(server.remarks)? server.server + ":" + server.server_port : server.server + ":" + server.server_port + " (" + server.remarks + ")");
  104. }
  105. }
  106. private void LoadCurrentConfiguration()
  107. {
  108. modifiedConfiguration = controller.GetConfiguration();
  109. LoadConfiguration(modifiedConfiguration);
  110. oldSelectedIndex = modifiedConfiguration.index;
  111. ServersListBox.SelectedIndex = modifiedConfiguration.index;
  112. LoadSelectedServer();
  113. UpdateServersMenu();
  114. enableItem.Checked = modifiedConfiguration.enabled;
  115. }
  116. private void UpdateServersMenu()
  117. {
  118. var items = ServersItem.MenuItems;
  119. items.Clear();
  120. Configuration configuration = controller.GetConfiguration();
  121. for (int i = 0; i < configuration.configs.Count; i++)
  122. {
  123. Server server = configuration.configs[i];
  124. MenuItem item = new MenuItem(string.IsNullOrEmpty(server.remarks) ? server.server + ":" + server.server_port : server.server + ":" + server.server_port + " (" + server.remarks + ")");
  125. item.Tag = i;
  126. item.Click += AServerItem_Click;
  127. items.Add(item);
  128. }
  129. items.Add(SeperatorItem);
  130. items.Add(ConfigItem);
  131. if (configuration.index >= 0 && configuration.index < configuration.configs.Count)
  132. {
  133. items[configuration.index].Checked = true;
  134. }
  135. }
  136. private void ConfigForm_Load(object sender, EventArgs e)
  137. {
  138. if (!controller.GetConfiguration().isDefault)
  139. {
  140. this.Opacity = 0;
  141. BeginInvoke(new MethodInvoker(delegate
  142. {
  143. this.Hide();
  144. }));
  145. }
  146. else
  147. {
  148. isFirstRun = true;
  149. }
  150. }
  151. private void ServersListBox_SelectedIndexChanged(object sender, EventArgs e)
  152. {
  153. if (oldSelectedIndex == ServersListBox.SelectedIndex)
  154. {
  155. // we are moving back to oldSelectedIndex or doing a force move
  156. return;
  157. }
  158. if (!SaveOldSelectedServer())
  159. {
  160. // why this won't cause stack overflow?
  161. ServersListBox.SelectedIndex = oldSelectedIndex;
  162. return;
  163. }
  164. LoadSelectedServer();
  165. oldSelectedIndex = ServersListBox.SelectedIndex;
  166. }
  167. private void AddButton_Click(object sender, EventArgs e)
  168. {
  169. if (!SaveOldSelectedServer())
  170. {
  171. return;
  172. }
  173. Server server = Configuration.GetDefaultServer();
  174. modifiedConfiguration.configs.Add(server);
  175. LoadConfiguration(modifiedConfiguration);
  176. ServersListBox.SelectedIndex = modifiedConfiguration.configs.Count - 1;
  177. oldSelectedIndex = ServersListBox.SelectedIndex;
  178. }
  179. private void DeleteButton_Click(object sender, EventArgs e)
  180. {
  181. oldSelectedIndex = ServersListBox.SelectedIndex;
  182. if (oldSelectedIndex >= 0 && oldSelectedIndex < modifiedConfiguration.configs.Count)
  183. {
  184. modifiedConfiguration.configs.RemoveAt(oldSelectedIndex);
  185. }
  186. if (oldSelectedIndex >= modifiedConfiguration.configs.Count)
  187. {
  188. // can be -1
  189. oldSelectedIndex = modifiedConfiguration.configs.Count - 1;
  190. }
  191. ServersListBox.SelectedIndex = oldSelectedIndex;
  192. LoadConfiguration(modifiedConfiguration);
  193. ServersListBox.SelectedIndex = oldSelectedIndex;
  194. LoadSelectedServer();
  195. }
  196. private void Config_Click(object sender, EventArgs e)
  197. {
  198. ShowWindow();
  199. }
  200. private void Quit_Click(object sender, EventArgs e)
  201. {
  202. this.Close();
  203. }
  204. private void ShowFirstTimeBalloon()
  205. {
  206. if (isFirstRun)
  207. {
  208. notifyIcon1.BalloonTipTitle = "Shadowsocks is here";
  209. notifyIcon1.BalloonTipText = "You can turn on/off Shadowsocks in the context menu";
  210. notifyIcon1.ShowBalloonTip(0);
  211. isFirstRun = false;
  212. }
  213. }
  214. private void OKButton_Click(object sender, EventArgs e)
  215. {
  216. if (!SaveOldSelectedServer())
  217. {
  218. return;
  219. }
  220. if (modifiedConfiguration.configs.Count == 0)
  221. {
  222. MessageBox.Show("Please add at least one server");
  223. return;
  224. }
  225. controller.SaveConfig(modifiedConfiguration);
  226. this.Hide();
  227. ShowFirstTimeBalloon();
  228. }
  229. private void CancelButton_Click(object sender, EventArgs e)
  230. {
  231. this.Hide();
  232. LoadCurrentConfiguration();
  233. ShowFirstTimeBalloon();
  234. }
  235. private void ConfigForm_FormClosed(object sender, FormClosedEventArgs e)
  236. {
  237. controller.Stop();
  238. }
  239. private void AboutItem_Click(object sender, EventArgs e)
  240. {
  241. Process.Start("https://github.com/clowwindy/shadowsocks-csharp");
  242. }
  243. private void notifyIcon1_DoubleClick(object sender, EventArgs e)
  244. {
  245. ShowWindow();
  246. }
  247. private void EnableItem_Click(object sender, EventArgs e)
  248. {
  249. enableItem.Checked = !enableItem.Checked;
  250. controller.ToggleEnable(enableItem.Checked);
  251. }
  252. private void EditPACFileItem_Click(object sender, EventArgs e)
  253. {
  254. controller.TouchPACFile();
  255. }
  256. private void AServerItem_Click(object sender, EventArgs e)
  257. {
  258. MenuItem item = (MenuItem)sender;
  259. Configuration configuration = controller.GetConfiguration();
  260. configuration.index = (int)item.Tag;
  261. controller.SaveConfig(configuration);
  262. }
  263. private void ConfigForm_Shown(object sender, EventArgs e)
  264. {
  265. IPTextBox.Focus();
  266. }
  267. private void QRCodeItem_Click(object sender, EventArgs e)
  268. {
  269. new QRCodeForm(controller.GetQRCodeForCurrentServer()).Show();
  270. }
  271. }
  272. }