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.

MenuViewController.cs 40 kB

10 years ago
10 years ago
10 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
10 years ago
10 years ago
10 years ago
10 years ago
5 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
6 years ago
7 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
5 years ago
5 years ago
10 years ago
10 years ago
5 years ago
10 years ago
10 years ago
5 years ago
10 years ago
5 years ago
5 years ago
5 years ago
5 years ago
10 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
5 years ago
5 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
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001
  1. using NLog;
  2. using Shadowsocks.Controller;
  3. using Shadowsocks.Localization;
  4. using Shadowsocks.Model;
  5. using Shadowsocks.Properties;
  6. using Shadowsocks.Util;
  7. using Shadowsocks.Util.SystemProxy;
  8. using Shadowsocks.Views;
  9. using System;
  10. using System.Diagnostics;
  11. using System.Drawing;
  12. using System.IO;
  13. using System.Linq;
  14. using System.Text;
  15. using System.Windows.Forms;
  16. using System.Windows.Forms.Integration;
  17. using System.Windows.Threading;
  18. using ZXing;
  19. using ZXing.Common;
  20. using ZXing.QrCode;
  21. namespace Shadowsocks.View
  22. {
  23. public class MenuViewController
  24. {
  25. private readonly Logger logger = LogManager.GetCurrentClassLogger();
  26. private ShadowsocksController controller;
  27. public UpdateChecker updateChecker;
  28. private NotifyIcon _notifyIcon;
  29. private Icon icon, icon_in, icon_out, icon_both, previousIcon;
  30. private bool _isStartupCheck;
  31. private string _urlToOpen;
  32. private ContextMenuStrip contextMenu1;
  33. private ToolStripMenuItem disableItem;
  34. private ToolStripMenuItem AutoStartupItem;
  35. private ToolStripMenuItem ShareOverLANItem;
  36. private ToolStripSeparator SeperatorItem;
  37. private ToolStripMenuItem ConfigItem;
  38. private ToolStripMenuItem ProtocolHandlerItem;
  39. private ToolStripMenuItem ServersItem;
  40. private ToolStripMenuItem globalModeItem;
  41. private ToolStripMenuItem PACModeItem;
  42. private ToolStripMenuItem localPACItem;
  43. private ToolStripMenuItem onlinePACItem;
  44. private ToolStripMenuItem editLocalPACItem;
  45. private ToolStripMenuItem updateFromGeositeItem;
  46. private ToolStripMenuItem editGFWUserRuleItem;
  47. private ToolStripMenuItem editOnlinePACItem;
  48. private ToolStripMenuItem secureLocalPacUrlToggleItem;
  49. private ToolStripMenuItem regenerateLocalPacOnUpdateItem;
  50. private ToolStripMenuItem autoCheckUpdatesToggleItem;
  51. private ToolStripMenuItem checkPreReleaseToggleItem;
  52. private ToolStripMenuItem proxyItem;
  53. private ToolStripMenuItem hotKeyItem;
  54. private ToolStripMenuItem VerboseLoggingToggleItem;
  55. private ToolStripMenuItem ShowPluginOutputToggleItem;
  56. private ToolStripMenuItem WriteI18NFileItem;
  57. private ToolStripMenuItem onlineConfigItem;
  58. private ConfigForm configForm;
  59. private LogForm logForm;
  60. private System.Windows.Window serverSharingWindow;
  61. private System.Windows.Window hotkeysWindow;
  62. private System.Windows.Window forwardProxyWindow;
  63. private System.Windows.Window onlineConfigWindow;
  64. // color definition for icon color transformation
  65. private readonly Color colorMaskBlue = Color.FromArgb(255, 25, 125, 191);
  66. private readonly Color colorMaskDarkSilver = Color.FromArgb(128, 192, 192, 192);
  67. private readonly Color colorMaskLightSilver = Color.FromArgb(192, 192, 192);
  68. private readonly Color colorMaskEclipse = Color.FromArgb(192, 64, 64, 64);
  69. public MenuViewController(ShadowsocksController controller)
  70. {
  71. this.controller = controller;
  72. LoadMenu();
  73. controller.EnableStatusChanged += controller_EnableStatusChanged;
  74. controller.ConfigChanged += controller_ConfigChanged;
  75. controller.PACFileReadyToOpen += controller_FileReadyToOpen;
  76. controller.UserRuleFileReadyToOpen += controller_FileReadyToOpen;
  77. controller.ShareOverLANStatusChanged += controller_ShareOverLANStatusChanged;
  78. controller.VerboseLoggingStatusChanged += controller_VerboseLoggingStatusChanged;
  79. controller.ShowPluginOutputChanged += controller_ShowPluginOutputChanged;
  80. controller.EnableGlobalChanged += controller_EnableGlobalChanged;
  81. controller.Errored += controller_Errored;
  82. controller.UpdatePACFromGeositeCompleted += controller_UpdatePACFromGeositeCompleted;
  83. controller.UpdatePACFromGeositeError += controller_UpdatePACFromGeositeError;
  84. _notifyIcon = new NotifyIcon();
  85. UpdateTrayIconAndNotifyText();
  86. _notifyIcon.Visible = true;
  87. _notifyIcon.ContextMenuStrip = contextMenu1;
  88. _notifyIcon.BalloonTipClicked += notifyIcon1_BalloonTipClicked;
  89. _notifyIcon.MouseClick += notifyIcon1_Click;
  90. _notifyIcon.MouseDoubleClick += notifyIcon1_DoubleClick;
  91. _notifyIcon.BalloonTipClosed += _notifyIcon_BalloonTipClosed;
  92. controller.TrafficChanged += controller_TrafficChanged;
  93. updateChecker = new UpdateChecker();
  94. updateChecker.CheckUpdateCompleted += updateChecker_CheckUpdateCompleted;
  95. LoadCurrentConfiguration();
  96. Configuration config = controller.GetCurrentConfiguration();
  97. if (config.firstRun)
  98. {
  99. ShowConfigForm();
  100. }
  101. else if (config.autoCheckUpdate)
  102. {
  103. _isStartupCheck = true;
  104. Dispatcher.CurrentDispatcher.Invoke(() => updateChecker.CheckForVersionUpdate(3000));
  105. }
  106. }
  107. #region Tray Icon
  108. private void UpdateTrayIconAndNotifyText()
  109. {
  110. Configuration config = controller.GetCurrentConfiguration();
  111. bool enabled = config.enabled;
  112. bool global = config.global;
  113. Color colorMask = SelectColorMask(enabled, global);
  114. Size iconSize = SelectIconSize();
  115. UpdateIconSet(colorMask, iconSize, out icon, out icon_in, out icon_out, out icon_both);
  116. previousIcon = icon;
  117. _notifyIcon.Icon = previousIcon;
  118. string serverInfo = null;
  119. if (controller.GetCurrentStrategy() != null)
  120. {
  121. serverInfo = controller.GetCurrentStrategy().Name;
  122. }
  123. else
  124. {
  125. serverInfo = config.GetCurrentServer().ToString();
  126. }
  127. // show more info by hacking the P/Invoke declaration for NOTIFYICONDATA inside Windows Forms
  128. string text = I18N.GetString("Shadowsocks") + " " + UpdateChecker.Version + "\n" +
  129. (enabled ?
  130. I18N.GetString("System Proxy On: ") + (global ? I18N.GetString("Global") : I18N.GetString("PAC")) :
  131. I18N.GetString("Running: Port {0}", config.localPort)) // this feedback is very important because they need to know Shadowsocks is running
  132. + "\n" + serverInfo;
  133. if (text.Length > 127)
  134. {
  135. text = text.Substring(0, 126 - 3) + "...";
  136. }
  137. ViewUtils.SetNotifyIconText(_notifyIcon, text);
  138. }
  139. /// <summary>
  140. /// Determine the icon size based on the screen DPI.
  141. /// </summary>
  142. /// <returns></returns>
  143. /// https://stackoverflow.com/a/40851713/2075611
  144. private Size SelectIconSize()
  145. {
  146. Size size = new Size(32, 32);
  147. int dpi = ViewUtils.GetScreenDpi();
  148. if (dpi < 97)
  149. {
  150. // dpi = 96;
  151. size = new Size(16, 16);
  152. }
  153. else if (dpi < 121)
  154. {
  155. // dpi = 120;
  156. size = new Size(20, 20);
  157. }
  158. else if (dpi < 145)
  159. {
  160. // dpi = 144;
  161. size = new Size(24, 24);
  162. }
  163. else
  164. {
  165. // dpi = 168;
  166. size = new Size(28, 28);
  167. }
  168. return size;
  169. }
  170. private Color SelectColorMask(bool isProxyEnabled, bool isGlobalProxy)
  171. {
  172. Color colorMask = Color.White;
  173. Utils.WindowsThemeMode currentWindowsThemeMode = Utils.GetWindows10SystemThemeSetting();
  174. if (isProxyEnabled)
  175. {
  176. if (isGlobalProxy) // global
  177. {
  178. colorMask = colorMaskBlue;
  179. }
  180. else // PAC
  181. {
  182. if (currentWindowsThemeMode == Utils.WindowsThemeMode.Light)
  183. {
  184. colorMask = colorMaskEclipse;
  185. }
  186. }
  187. }
  188. else // disabled
  189. {
  190. if (currentWindowsThemeMode == Utils.WindowsThemeMode.Light)
  191. {
  192. colorMask = colorMaskDarkSilver;
  193. }
  194. else
  195. {
  196. colorMask = colorMaskLightSilver;
  197. }
  198. }
  199. return colorMask;
  200. }
  201. private void UpdateIconSet(Color colorMask, Size size,
  202. out Icon icon, out Icon icon_in, out Icon icon_out, out Icon icon_both)
  203. {
  204. Bitmap iconBitmap;
  205. // generate the base icon
  206. iconBitmap = ViewUtils.ChangeBitmapColor(Resources.ss32Fill, colorMask);
  207. iconBitmap = ViewUtils.AddBitmapOverlay(iconBitmap, Resources.ss32Outline);
  208. icon = Icon.FromHandle(ViewUtils.ResizeBitmap(iconBitmap, size.Width, size.Height).GetHicon());
  209. icon_in = Icon.FromHandle(ViewUtils.ResizeBitmap(ViewUtils.AddBitmapOverlay(iconBitmap, Resources.ss32In), size.Width, size.Height).GetHicon());
  210. icon_out = Icon.FromHandle(ViewUtils.ResizeBitmap(ViewUtils.AddBitmapOverlay(iconBitmap, Resources.ss32In), size.Width, size.Height).GetHicon());
  211. icon_both = Icon.FromHandle(ViewUtils.ResizeBitmap(ViewUtils.AddBitmapOverlay(iconBitmap, Resources.ss32In, Resources.ss32Out), size.Width, size.Height).GetHicon());
  212. }
  213. #endregion
  214. #region ToolStripMenuItems and MenuGroups
  215. private ToolStripMenuItem CreateToolStripMenuItem(string text, EventHandler click)
  216. {
  217. return new ToolStripMenuItem(I18N.GetString(text), null, click);
  218. }
  219. private ToolStripMenuItem CreateMenuGroup(string text, ToolStripItem[] items)
  220. {
  221. return new ToolStripMenuItem(I18N.GetString(text), null, items);
  222. }
  223. private void LoadMenu()
  224. {
  225. this.contextMenu1 = new ContextMenuStrip();
  226. contextMenu1.Items.AddRange(new ToolStripItem[]{
  227. CreateMenuGroup("System Proxy", new ToolStripMenuItem[] {
  228. this.disableItem = CreateToolStripMenuItem("Disable", new EventHandler(this.EnableItem_Click)),
  229. this.PACModeItem = CreateToolStripMenuItem("PAC", new EventHandler(this.PACModeItem_Click)),
  230. this.globalModeItem = CreateToolStripMenuItem("Global", new EventHandler(this.GlobalModeItem_Click))
  231. }),
  232. this.ServersItem = CreateMenuGroup("Servers", new ToolStripItem [] {
  233. this.SeperatorItem = new ToolStripSeparator(),
  234. this.ConfigItem = CreateToolStripMenuItem("Edit Servers...", new EventHandler(this.Config_Click)),
  235. new ToolStripSeparator(),
  236. CreateToolStripMenuItem("Share Server Config...", new EventHandler(this.QRCodeItem_Click)),
  237. CreateToolStripMenuItem("Scan QRCode from Screen...", new EventHandler(this.ScanQRCodeItem_Click)),
  238. CreateToolStripMenuItem("Import URL from Clipboard...", new EventHandler(this.ImportURLItem_Click))
  239. }),
  240. CreateMenuGroup("PAC ", new ToolStripItem[] {
  241. this.localPACItem = CreateToolStripMenuItem("Local PAC", new EventHandler(this.LocalPACItem_Click)),
  242. this.onlinePACItem = CreateToolStripMenuItem("Online PAC", new EventHandler(this.OnlinePACItem_Click)),
  243. new ToolStripSeparator(),
  244. this.editLocalPACItem = CreateToolStripMenuItem("Edit Local PAC File...", new EventHandler(this.EditPACFileItem_Click)),
  245. this.updateFromGeositeItem = CreateToolStripMenuItem("Update Local PAC from Geosite", new EventHandler(this.UpdatePACFromGeositeItem_Click)),
  246. this.editGFWUserRuleItem = CreateToolStripMenuItem("Edit User Rule for Geosite...", new EventHandler(this.EditUserRuleFileForGeositeItem_Click)),
  247. this.secureLocalPacUrlToggleItem = CreateToolStripMenuItem("Secure Local PAC", new EventHandler(this.SecureLocalPacUrlToggleItem_Click)),
  248. this.regenerateLocalPacOnUpdateItem = CreateToolStripMenuItem("Regenerate local PAC on version update", new EventHandler(this.RegenerateLocalPacOnUpdateItem_Click)),
  249. CreateToolStripMenuItem("Copy Local PAC URL", new EventHandler(this.CopyLocalPacUrlItem_Click)),
  250. this.editOnlinePACItem = CreateToolStripMenuItem("Edit Online PAC URL...", new EventHandler(this.UpdateOnlinePACURLItem_Click)),
  251. }),
  252. this.proxyItem = CreateToolStripMenuItem("Forward Proxy...", new EventHandler(this.proxyItem_Click)),
  253. this.onlineConfigItem = CreateToolStripMenuItem("Online Config...", new EventHandler(this.OnlineConfig_Click)),
  254. new ToolStripSeparator( ),
  255. this.AutoStartupItem = CreateToolStripMenuItem("Start on Boot", new EventHandler(this.AutoStartupItem_Click)),
  256. this.ProtocolHandlerItem = CreateToolStripMenuItem("Associate ss:// Links", new EventHandler(this.ProtocolHandlerItem_Click)),
  257. this.ShareOverLANItem = CreateToolStripMenuItem("Allow other Devices to connect", new EventHandler(this.ShareOverLANItem_Click)),
  258. new ToolStripSeparator( ),
  259. this.hotKeyItem = CreateToolStripMenuItem("Edit Hotkeys...", new EventHandler(this.hotKeyItem_Click)),
  260. CreateMenuGroup("Help", new ToolStripItem[] {
  261. CreateToolStripMenuItem("Show Logs...", new EventHandler(this.ShowLogItem_Click)),
  262. this.VerboseLoggingToggleItem = CreateToolStripMenuItem( "Verbose Logging", new EventHandler(this.VerboseLoggingToggleItem_Click) ),
  263. this.ShowPluginOutputToggleItem = CreateToolStripMenuItem("Show Plugin Output", new EventHandler(this.ShowPluginOutputToggleItem_Click)),
  264. this.WriteI18NFileItem = CreateToolStripMenuItem("Write translation template",new EventHandler(WriteI18NFileItem_Click)),
  265. CreateMenuGroup("Updates...", new ToolStripItem[] {
  266. CreateToolStripMenuItem("Check for Updates...", new EventHandler(this.checkUpdatesItem_Click)),
  267. new ToolStripSeparator(),
  268. this.autoCheckUpdatesToggleItem = CreateToolStripMenuItem("Check for Updates at Startup", new EventHandler(this.autoCheckUpdatesToggleItem_Click)),
  269. this.checkPreReleaseToggleItem = CreateToolStripMenuItem("Check Pre-release Version", new EventHandler(this.checkPreReleaseToggleItem_Click)),
  270. }),
  271. CreateToolStripMenuItem("About...", new EventHandler(this.AboutItem_Click)),
  272. }),
  273. new ToolStripSeparator(),
  274. CreateToolStripMenuItem("Quit", new EventHandler(this.Quit_Click))
  275. });
  276. }
  277. #endregion
  278. private void controller_TrafficChanged(object sender, EventArgs e)
  279. {
  280. if (icon == null)
  281. return;
  282. Icon newIcon;
  283. bool hasInbound = controller.trafficPerSecondQueue.Last().inboundIncreasement > 0;
  284. bool hasOutbound = controller.trafficPerSecondQueue.Last().outboundIncreasement > 0;
  285. if (hasInbound && hasOutbound)
  286. newIcon = icon_both;
  287. else if (hasInbound)
  288. newIcon = icon_in;
  289. else if (hasOutbound)
  290. newIcon = icon_out;
  291. else
  292. newIcon = icon;
  293. if (newIcon != this.previousIcon)
  294. {
  295. this.previousIcon = newIcon;
  296. _notifyIcon.Icon = newIcon;
  297. }
  298. }
  299. void controller_Errored(object sender, ErrorEventArgs e)
  300. {
  301. MessageBox.Show(e.GetException().ToString(), I18N.GetString("Shadowsocks Error: {0}", e.GetException().Message));
  302. }
  303. private void controller_ConfigChanged(object sender, EventArgs e)
  304. {
  305. LoadCurrentConfiguration();
  306. UpdateTrayIconAndNotifyText();
  307. }
  308. private void LoadCurrentConfiguration()
  309. {
  310. Configuration config = controller.GetCurrentConfiguration();
  311. UpdateServersMenu();
  312. UpdateSystemProxyItemsEnabledStatus(config);
  313. ShareOverLANItem.Checked = config.shareOverLan;
  314. VerboseLoggingToggleItem.Checked = config.isVerboseLogging;
  315. ShowPluginOutputToggleItem.Checked = config.showPluginOutput;
  316. AutoStartupItem.Checked = AutoStartup.Check();
  317. ProtocolHandlerItem.Checked = ProtocolHandler.Check();
  318. onlinePACItem.Checked = onlinePACItem.Enabled && config.useOnlinePac;
  319. localPACItem.Checked = !onlinePACItem.Checked;
  320. secureLocalPacUrlToggleItem.Checked = config.secureLocalPac;
  321. regenerateLocalPacOnUpdateItem.Checked = config.regeneratePacOnUpdate;
  322. UpdatePACItemsEnabledStatus();
  323. UpdateUpdateMenu();
  324. }
  325. #region Forms
  326. private void ShowConfigForm()
  327. {
  328. if (configForm != null)
  329. {
  330. configForm.Activate();
  331. }
  332. else
  333. {
  334. configForm = new ConfigForm(controller);
  335. configForm.Show();
  336. configForm.Activate();
  337. configForm.FormClosed += configForm_FormClosed;
  338. }
  339. }
  340. private void ShowLogForm()
  341. {
  342. if (logForm != null)
  343. {
  344. logForm.Activate();
  345. }
  346. else
  347. {
  348. logForm = new LogForm(controller);
  349. logForm.Show();
  350. logForm.Activate();
  351. logForm.FormClosed += logForm_FormClosed;
  352. }
  353. }
  354. void logForm_FormClosed(object sender, FormClosedEventArgs e)
  355. {
  356. logForm.Dispose();
  357. logForm = null;
  358. }
  359. void configForm_FormClosed(object sender, FormClosedEventArgs e)
  360. {
  361. configForm.Dispose();
  362. configForm = null;
  363. var config = controller.GetCurrentConfiguration();
  364. if (config.firstRun)
  365. {
  366. CheckUpdateForFirstRun();
  367. ShowBalloonTip(
  368. I18N.GetString("Shadowsocks is here"),
  369. I18N.GetString("You can turn on/off Shadowsocks in the context menu"),
  370. ToolTipIcon.Info,
  371. 0
  372. );
  373. config.firstRun = false;
  374. }
  375. }
  376. #endregion
  377. #region Misc
  378. void ShowBalloonTip(string title, string content, ToolTipIcon icon, int timeout)
  379. {
  380. _notifyIcon.BalloonTipTitle = title;
  381. _notifyIcon.BalloonTipText = content;
  382. _notifyIcon.BalloonTipIcon = icon;
  383. _notifyIcon.ShowBalloonTip(timeout);
  384. }
  385. void notifyIcon1_BalloonTipClicked(object sender, EventArgs e)
  386. {
  387. }
  388. private void _notifyIcon_BalloonTipClosed(object sender, EventArgs e)
  389. {
  390. }
  391. private void notifyIcon1_Click(object sender, MouseEventArgs e)
  392. {
  393. UpdateTrayIconAndNotifyText();
  394. if (e.Button == MouseButtons.Middle)
  395. {
  396. ShowLogForm();
  397. }
  398. }
  399. private void notifyIcon1_DoubleClick(object sender, MouseEventArgs e)
  400. {
  401. if (e.Button == MouseButtons.Left)
  402. {
  403. ShowConfigForm();
  404. }
  405. }
  406. private void CheckUpdateForFirstRun()
  407. {
  408. Configuration config = controller.GetCurrentConfiguration();
  409. if (config.firstRun)
  410. return;
  411. _isStartupCheck = true;
  412. Dispatcher.CurrentDispatcher.Invoke(() => updateChecker.CheckForVersionUpdate(3000));
  413. }
  414. public void ShowLogForm_HotKey()
  415. {
  416. ShowLogForm();
  417. }
  418. #endregion
  419. #region Main menu
  420. void controller_ShareOverLANStatusChanged(object sender, EventArgs e)
  421. {
  422. ShareOverLANItem.Checked = controller.GetCurrentConfiguration().shareOverLan;
  423. }
  424. private void proxyItem_Click(object sender, EventArgs e)
  425. {
  426. if (forwardProxyWindow == null)
  427. {
  428. forwardProxyWindow = new System.Windows.Window()
  429. {
  430. Title = LocalizationProvider.GetLocalizedValue<string>("ForwardProxy"),
  431. Height = 400,
  432. Width = 280,
  433. MinHeight = 400,
  434. MinWidth = 280,
  435. Content = new ForwardProxyView()
  436. };
  437. forwardProxyWindow.Closed += ForwardProxyWindow_Closed;
  438. ElementHost.EnableModelessKeyboardInterop(forwardProxyWindow);
  439. forwardProxyWindow.Show();
  440. }
  441. forwardProxyWindow.Activate();
  442. }
  443. private void ForwardProxyWindow_Closed(object sender, EventArgs e)
  444. {
  445. forwardProxyWindow = null;
  446. }
  447. public void CloseForwardProxyWindow() => forwardProxyWindow.Close();
  448. private void OnlineConfig_Click(object sender, EventArgs e)
  449. {
  450. if (onlineConfigWindow == null)
  451. {
  452. onlineConfigWindow = new System.Windows.Window()
  453. {
  454. Title = LocalizationProvider.GetLocalizedValue<string>("OnlineConfigDelivery"),
  455. Height = 510,
  456. Width = 480,
  457. MinHeight = 510,
  458. MinWidth = 480,
  459. Content = new OnlineConfigView()
  460. };
  461. onlineConfigWindow.Closed += OnlineConfigWindow_Closed;
  462. ElementHost.EnableModelessKeyboardInterop(onlineConfigWindow);
  463. onlineConfigWindow.Show();
  464. }
  465. onlineConfigWindow.Activate();
  466. }
  467. private void OnlineConfigWindow_Closed(object sender, EventArgs e)
  468. {
  469. onlineConfigWindow = null;
  470. }
  471. private void hotKeyItem_Click(object sender, EventArgs e)
  472. {
  473. if (hotkeysWindow == null)
  474. {
  475. hotkeysWindow = new System.Windows.Window()
  476. {
  477. Title = LocalizationProvider.GetLocalizedValue<string>("Hotkeys"),
  478. Height = 260,
  479. Width = 320,
  480. MinHeight = 260,
  481. MinWidth = 320,
  482. Content = new HotkeysView()
  483. };
  484. hotkeysWindow.Closed += HotkeysWindow_Closed;
  485. ElementHost.EnableModelessKeyboardInterop(hotkeysWindow);
  486. hotkeysWindow.Show();
  487. }
  488. hotkeysWindow.Activate();
  489. }
  490. private void HotkeysWindow_Closed(object sender, EventArgs e)
  491. {
  492. hotkeysWindow = null;
  493. }
  494. public void CloseHotkeysWindow() => hotkeysWindow.Close();
  495. private void ShareOverLANItem_Click(object sender, EventArgs e)
  496. {
  497. ShareOverLANItem.Checked = !ShareOverLANItem.Checked;
  498. controller.ToggleShareOverLAN(ShareOverLANItem.Checked);
  499. }
  500. private void AutoStartupItem_Click(object sender, EventArgs e)
  501. {
  502. AutoStartupItem.Checked = !AutoStartupItem.Checked;
  503. if (!AutoStartup.Set(AutoStartupItem.Checked))
  504. {
  505. MessageBox.Show(I18N.GetString("Failed to update registry"));
  506. }
  507. LoadCurrentConfiguration();
  508. }
  509. private void ProtocolHandlerItem_Click(object sender, EventArgs e)
  510. {
  511. ProtocolHandlerItem.Checked = !ProtocolHandlerItem.Checked;
  512. if (!ProtocolHandler.Set(ProtocolHandlerItem.Checked))
  513. {
  514. MessageBox.Show(I18N.GetString("Failed to update registry"));
  515. }
  516. LoadCurrentConfiguration();
  517. }
  518. private void Quit_Click(object sender, EventArgs e)
  519. {
  520. controller.Stop();
  521. _notifyIcon.Visible = false;
  522. Application.Exit();
  523. }
  524. #endregion
  525. #region System proxy
  526. private void controller_EnableStatusChanged(object sender, EventArgs e)
  527. {
  528. disableItem.Checked = !controller.GetCurrentConfiguration().enabled;
  529. }
  530. private void EnableItem_Click(object sender, EventArgs e)
  531. {
  532. controller.ToggleEnable(false);
  533. Configuration config = controller.GetCurrentConfiguration();
  534. UpdateSystemProxyItemsEnabledStatus(config);
  535. }
  536. void controller_EnableGlobalChanged(object sender, EventArgs e)
  537. {
  538. globalModeItem.Checked = controller.GetCurrentConfiguration().global;
  539. PACModeItem.Checked = !globalModeItem.Checked;
  540. }
  541. private void UpdateSystemProxyItemsEnabledStatus(Configuration config)
  542. {
  543. disableItem.Checked = !config.enabled;
  544. if (!config.enabled)
  545. {
  546. globalModeItem.Checked = false;
  547. PACModeItem.Checked = false;
  548. }
  549. else
  550. {
  551. globalModeItem.Checked = config.global;
  552. PACModeItem.Checked = !config.global;
  553. }
  554. globalModeItem.Enabled = WinINet.operational;
  555. PACModeItem.Enabled = WinINet.operational;
  556. }
  557. private void GlobalModeItem_Click(object sender, EventArgs e)
  558. {
  559. controller.ToggleEnable(true);
  560. controller.ToggleGlobal(true);
  561. Configuration config = controller.GetCurrentConfiguration();
  562. UpdateSystemProxyItemsEnabledStatus(config);
  563. }
  564. private void PACModeItem_Click(object sender, EventArgs e)
  565. {
  566. controller.ToggleEnable(true);
  567. controller.ToggleGlobal(false);
  568. Configuration config = controller.GetCurrentConfiguration();
  569. UpdateSystemProxyItemsEnabledStatus(config);
  570. }
  571. #endregion
  572. #region Server
  573. private void UpdateServersMenu()
  574. {
  575. var items = ServersItem.DropDownItems;
  576. while (items[0] != SeperatorItem)
  577. {
  578. items.RemoveAt(0);
  579. }
  580. int strategyCount = 0;
  581. foreach (var strategy in controller.GetStrategies())
  582. {
  583. if (!items.OfType<ToolStripItem>().Any(ts => ts.Text == strategy.Name))
  584. {
  585. ToolStripMenuItem item = new ToolStripMenuItem(strategy.Name);
  586. item.Tag = strategy.ID;
  587. item.Click += AStrategyItem_Click;
  588. items.Add(item);
  589. strategyCount++;
  590. }
  591. }
  592. if (!items.OfType<ToolStripSeparator>().Any(ts => ts.Tag?.ToString() == "-server-"))
  593. {
  594. // user wants a seperator item between strategy and servers menugroup
  595. items.Add(new ToolStripSeparator() { Tag = "-server-" });
  596. }
  597. int serverCount = 0;
  598. Configuration configuration = controller.GetCurrentConfiguration();
  599. foreach (var server in configuration.configs)
  600. {
  601. try
  602. {
  603. Configuration.CheckServer(server);
  604. var name = server.ToString();
  605. if (!items.OfType<ToolStripMenuItem>().Any(ts => ts.Text == name))
  606. {
  607. ToolStripMenuItem item = new ToolStripMenuItem(name);
  608. item.Tag = configuration.configs.FindIndex(s => s == server);
  609. item.Click += AServerItem_Click;
  610. items.Add(item);
  611. serverCount++;
  612. }
  613. }
  614. catch
  615. {
  616. }
  617. }
  618. foreach (var item in items)
  619. {
  620. var menuItem = item as ToolStripMenuItem;
  621. if (menuItem == null || menuItem.Tag == null) continue;
  622. if (
  623. menuItem.Tag.ToString() == configuration.index.ToString()
  624. || menuItem.Tag.ToString() == configuration.strategy
  625. )
  626. {
  627. menuItem.Checked = true;
  628. }
  629. else
  630. {
  631. menuItem.Checked = false;
  632. }
  633. }
  634. }
  635. private void AServerItem_Click(object sender, EventArgs e)
  636. {
  637. ToolStripMenuItem item = (ToolStripMenuItem)sender;
  638. controller.SelectServerIndex((int)item.Tag);
  639. }
  640. private void AStrategyItem_Click(object sender, EventArgs e)
  641. {
  642. ToolStripMenuItem item = (ToolStripMenuItem)sender;
  643. controller.SelectStrategy((string)item.Tag);
  644. }
  645. private void Config_Click(object sender, EventArgs e)
  646. {
  647. ShowConfigForm();
  648. }
  649. void openURLFromQRCode()
  650. {
  651. Utils.OpenInBrowser(_urlToOpen);
  652. }
  653. private void QRCodeItem_Click(object sender, EventArgs e)
  654. {
  655. if (serverSharingWindow == null)
  656. {
  657. serverSharingWindow = new System.Windows.Window()
  658. {
  659. Title = LocalizationProvider.GetLocalizedValue<string>("ServerSharing"),
  660. Height = 400,
  661. Width = 660,
  662. MinHeight = 400,
  663. MinWidth = 660,
  664. Content = new ServerSharingView()
  665. };
  666. serverSharingWindow.Closed += ServerSharingWindow_Closed;
  667. ElementHost.EnableModelessKeyboardInterop(serverSharingWindow);
  668. serverSharingWindow.Show();
  669. }
  670. serverSharingWindow.Activate();
  671. }
  672. private void ServerSharingWindow_Closed(object sender, EventArgs e)
  673. {
  674. serverSharingWindow = null;
  675. }
  676. private void ScanQRCodeItem_Click(object sender, EventArgs e)
  677. {
  678. var result = Utils.ScanQRCodeFromScreen();
  679. if (result != null)
  680. {
  681. if (result.ToLowerInvariant().StartsWith("http://") || result.ToLowerInvariant().StartsWith("https://"))
  682. {
  683. _urlToOpen = result;
  684. openURLFromQRCode();
  685. }
  686. else if (controller.AddServerBySSURL(result))
  687. {
  688. ShowConfigForm();
  689. }
  690. else
  691. {
  692. MessageBox.Show(I18N.GetString("Invalid QR Code content: {0}", result));
  693. }
  694. return;
  695. }
  696. else
  697. MessageBox.Show(I18N.GetString("No QRCode found. Try to zoom in or move it to the center of the screen."));
  698. }
  699. private void ImportURLItem_Click(object sender, EventArgs e)
  700. {
  701. if (controller.AskAddServerBySSURL(Clipboard.GetText(TextDataFormat.Text)))
  702. {
  703. ShowConfigForm();
  704. }
  705. }
  706. #endregion
  707. #region PAC
  708. private void LocalPACItem_Click(object sender, EventArgs e)
  709. {
  710. if (!localPACItem.Checked)
  711. {
  712. localPACItem.Checked = true;
  713. onlinePACItem.Checked = false;
  714. controller.UseOnlinePAC(false);
  715. UpdatePACItemsEnabledStatus();
  716. }
  717. }
  718. private void OnlinePACItem_Click(object sender, EventArgs e)
  719. {
  720. if (!onlinePACItem.Checked)
  721. {
  722. if (string.IsNullOrEmpty(controller.GetCurrentConfiguration().pacUrl))
  723. {
  724. UpdateOnlinePACURLItem_Click(sender, e);
  725. }
  726. if (!string.IsNullOrEmpty(controller.GetCurrentConfiguration().pacUrl))
  727. {
  728. localPACItem.Checked = false;
  729. onlinePACItem.Checked = true;
  730. controller.UseOnlinePAC(true);
  731. }
  732. UpdatePACItemsEnabledStatus();
  733. }
  734. }
  735. private void UpdateOnlinePACURLItem_Click(object sender, EventArgs e)
  736. {
  737. string origPacUrl = controller.GetCurrentConfiguration().pacUrl;
  738. string pacUrl = ViewUtils.InputBox(
  739. I18N.GetString("Please input PAC Url"),
  740. I18N.GetString("Edit Online PAC URL"),
  741. origPacUrl, -1, -1);
  742. if (!string.IsNullOrEmpty(pacUrl) && pacUrl != origPacUrl)
  743. {
  744. controller.SavePACUrl(pacUrl);
  745. }
  746. }
  747. private void SecureLocalPacUrlToggleItem_Click(object sender, EventArgs e)
  748. {
  749. Configuration configuration = controller.GetCurrentConfiguration();
  750. controller.ToggleSecureLocalPac(!configuration.secureLocalPac);
  751. }
  752. private void RegenerateLocalPacOnUpdateItem_Click(object sender, EventArgs e)
  753. {
  754. var config = controller.GetCurrentConfiguration();
  755. controller.ToggleRegeneratePacOnUpdate(!config.regeneratePacOnUpdate);
  756. }
  757. private void CopyLocalPacUrlItem_Click(object sender, EventArgs e)
  758. {
  759. controller.CopyPacUrl();
  760. }
  761. private void UpdatePACItemsEnabledStatus()
  762. {
  763. if (this.localPACItem.Checked)
  764. {
  765. this.editLocalPACItem.Enabled = true;
  766. this.updateFromGeositeItem.Enabled = true;
  767. this.editGFWUserRuleItem.Enabled = true;
  768. this.editOnlinePACItem.Enabled = false;
  769. }
  770. else
  771. {
  772. this.editLocalPACItem.Enabled = false;
  773. this.updateFromGeositeItem.Enabled = false;
  774. this.editGFWUserRuleItem.Enabled = false;
  775. this.editOnlinePACItem.Enabled = true;
  776. }
  777. }
  778. private void EditPACFileItem_Click(object sender, EventArgs e)
  779. {
  780. controller.TouchPACFile();
  781. }
  782. private async void UpdatePACFromGeositeItem_Click(object sender, EventArgs e)
  783. {
  784. await GeositeUpdater.UpdatePACFromGeosite();
  785. }
  786. private void EditUserRuleFileForGeositeItem_Click(object sender, EventArgs e)
  787. {
  788. controller.TouchUserRuleFile();
  789. }
  790. void controller_FileReadyToOpen(object sender, ShadowsocksController.PathEventArgs e)
  791. {
  792. string argument = @"/select, " + e.Path;
  793. Process.Start("explorer.exe", argument);
  794. }
  795. void controller_UpdatePACFromGeositeError(object sender, System.IO.ErrorEventArgs e)
  796. {
  797. ShowBalloonTip(I18N.GetString("Failed to update PAC file"), e.GetException().Message, ToolTipIcon.Error, 5000);
  798. logger.LogUsefulException(e.GetException());
  799. }
  800. void controller_UpdatePACFromGeositeCompleted(object sender, GeositeResultEventArgs e)
  801. {
  802. string result = e.Success
  803. ? I18N.GetString("PAC updated")
  804. : I18N.GetString("No updates found. Please report to Geosite if you have problems with it.");
  805. ShowBalloonTip(I18N.GetString("Shadowsocks"), result, ToolTipIcon.Info, 1000);
  806. }
  807. #endregion
  808. #region Help
  809. void controller_VerboseLoggingStatusChanged(object sender, EventArgs e)
  810. {
  811. VerboseLoggingToggleItem.Checked = controller.GetCurrentConfiguration().isVerboseLogging;
  812. }
  813. void controller_ShowPluginOutputChanged(object sender, EventArgs e)
  814. {
  815. ShowPluginOutputToggleItem.Checked = controller.GetCurrentConfiguration().showPluginOutput;
  816. }
  817. private void VerboseLoggingToggleItem_Click(object sender, EventArgs e)
  818. {
  819. VerboseLoggingToggleItem.Checked = !VerboseLoggingToggleItem.Checked;
  820. controller.ToggleVerboseLogging(VerboseLoggingToggleItem.Checked);
  821. }
  822. private void ShowLogItem_Click(object sender, EventArgs e)
  823. {
  824. ShowLogForm();
  825. }
  826. private void ShowPluginOutputToggleItem_Click(object sender, EventArgs e)
  827. {
  828. ShowPluginOutputToggleItem.Checked = !ShowPluginOutputToggleItem.Checked;
  829. controller.ToggleShowPluginOutput(ShowPluginOutputToggleItem.Checked);
  830. }
  831. private void WriteI18NFileItem_Click(object sender, EventArgs e)
  832. {
  833. File.WriteAllText(I18N.I18N_FILE, Resources.i18n_csv, Encoding.UTF8);
  834. }
  835. #endregion
  836. #region Update
  837. void updateChecker_CheckUpdateCompleted(object sender, EventArgs e)
  838. {
  839. if (!_isStartupCheck && updateChecker.NewReleaseZipFilename == null)
  840. {
  841. ShowBalloonTip(I18N.GetString("Shadowsocks"), I18N.GetString("No update is available"), ToolTipIcon.Info, 5000);
  842. }
  843. _isStartupCheck = false;
  844. }
  845. private void UpdateUpdateMenu()
  846. {
  847. Configuration configuration = controller.GetCurrentConfiguration();
  848. autoCheckUpdatesToggleItem.Checked = configuration.autoCheckUpdate;
  849. checkPreReleaseToggleItem.Checked = configuration.checkPreRelease;
  850. }
  851. private void autoCheckUpdatesToggleItem_Click(object sender, EventArgs e)
  852. {
  853. Configuration configuration = controller.GetCurrentConfiguration();
  854. controller.ToggleCheckingUpdate(!configuration.autoCheckUpdate);
  855. UpdateUpdateMenu();
  856. }
  857. private void checkPreReleaseToggleItem_Click(object sender, EventArgs e)
  858. {
  859. Configuration configuration = controller.GetCurrentConfiguration();
  860. controller.ToggleCheckingPreRelease(!configuration.checkPreRelease);
  861. UpdateUpdateMenu();
  862. }
  863. private async void checkUpdatesItem_Click(object sender, EventArgs e)
  864. {
  865. await updateChecker.CheckForVersionUpdate();
  866. }
  867. private void AboutItem_Click(object sender, EventArgs e)
  868. {
  869. Utils.OpenInBrowser("https://github.com/shadowsocks/shadowsocks-windows");
  870. }
  871. #endregion
  872. }
  873. }