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.

Program.cs 4.5 kB

10 years ago
10 years ago
10 years ago
12 years ago
12 years ago
10 years ago
12 years ago
12 years ago
10 years ago
10 years ago
10 years ago
10 years ago
12 years ago
12 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. using System;
  2. using System.Diagnostics;
  3. using System.IO;
  4. using System.Threading;
  5. using System.Windows.Forms;
  6. using Microsoft.Win32;
  7. using Shadowsocks.Controller;
  8. using Shadowsocks.Util;
  9. using Shadowsocks.View;
  10. namespace Shadowsocks
  11. {
  12. static class Program
  13. {
  14. private static ShadowsocksController _controller;
  15. // XXX: Don't change this name
  16. private static MenuViewController _viewController;
  17. /// <summary>
  18. /// 应用程序的主入口点。
  19. /// </summary>
  20. [STAThread]
  21. static void Main()
  22. {
  23. // Check OS since we are using dual-mode socket
  24. if (!Utils.IsWinVistaOrHigher())
  25. {
  26. MessageBox.Show(I18N.GetString("Unsupported operating system, use Windows Vista at least."),
  27. "Shadowsocks Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
  28. return;
  29. }
  30. Utils.ReleaseMemory(true);
  31. using (Mutex mutex = new Mutex(false, "Global\\Shadowsocks_" + Application.StartupPath.GetHashCode()))
  32. {
  33. AppDomain.CurrentDomain.UnhandledException += CurrentDomain_UnhandledException;
  34. Application.ApplicationExit += Application_ApplicationExit;
  35. SystemEvents.PowerModeChanged += SystemEvents_PowerModeChanged;
  36. Application.EnableVisualStyles();
  37. Application.SetCompatibleTextRenderingDefault(false);
  38. Application.ApplicationExit += (sender, args) => HotKeys.Destroy();
  39. if (!mutex.WaitOne(0, false))
  40. {
  41. Process[] oldProcesses = Process.GetProcessesByName("Shadowsocks");
  42. if (oldProcesses.Length > 0)
  43. {
  44. Process oldProcess = oldProcesses[0];
  45. }
  46. MessageBox.Show(I18N.GetString("Find Shadowsocks icon in your notify tray.") + "\n" +
  47. I18N.GetString("If you want to start multiple Shadowsocks, make a copy in another directory."),
  48. I18N.GetString("Shadowsocks is already running."));
  49. return;
  50. }
  51. Directory.SetCurrentDirectory(Application.StartupPath);
  52. #if DEBUG
  53. Logging.OpenLogFile();
  54. // truncate privoxy log file while debugging
  55. string privoxyLogFilename = Utils.GetTempPath("privoxy.log");
  56. if (File.Exists(privoxyLogFilename))
  57. using (new FileStream(privoxyLogFilename, FileMode.Truncate)) { }
  58. #else
  59. Logging.OpenLogFile();
  60. #endif
  61. _controller = new ShadowsocksController();
  62. _viewController = new MenuViewController(_controller);
  63. HotKeys.Init();
  64. _controller.Start();
  65. Application.Run();
  66. }
  67. }
  68. private static int exited = 0;
  69. private static void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
  70. {
  71. if (Interlocked.Increment(ref exited) == 1)
  72. {
  73. Logging.Error(e.ExceptionObject?.ToString());
  74. MessageBox.Show(I18N.GetString("Unexpected error, shadowsocks will exit. Please report to") +
  75. " https://github.com/shadowsocks/shadowsocks-windows/issues " +
  76. Environment.NewLine + (e.ExceptionObject?.ToString()),
  77. "Shadowsocks Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
  78. Application.Exit();
  79. }
  80. }
  81. private static void SystemEvents_PowerModeChanged(object sender, PowerModeChangedEventArgs e)
  82. {
  83. switch (e.Mode)
  84. {
  85. case PowerModes.Resume:
  86. Logging.Info("os wake up");
  87. _controller?.Start();
  88. break;
  89. case PowerModes.Suspend:
  90. _controller?.Stop();
  91. Logging.Info("os suspend");
  92. break;
  93. }
  94. }
  95. private static void Application_ApplicationExit(object sender, EventArgs e)
  96. {
  97. if (_controller != null)
  98. {
  99. _controller.Stop();
  100. _controller = null;
  101. }
  102. }
  103. }
  104. }