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