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 7.6 kB

10 years ago
10 years ago
10 years ago
12 years ago
12 years ago
10 years ago
12 years ago
8 years ago
12 years ago
10 years ago
10 years ago
10 years ago
8 years ago
10 years ago
12 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
12 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  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.Controller.Hotkeys;
  9. using Shadowsocks.Util;
  10. using Shadowsocks.View;
  11. namespace Shadowsocks
  12. {
  13. static class Program
  14. {
  15. public static ShadowsocksController MainController { get; private set; }
  16. public static MenuViewController MenuController { get; private set; }
  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. // Check .NET Framework version
  31. if (!Utils.IsSupportedRuntimeVersion())
  32. {
  33. MessageBox.Show(I18N.GetString("Unsupported .NET Framework, please update to 4.6.2 or later."),
  34. "Shadowsocks Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
  35. Process.Start(
  36. "http://dotnetsocial.cloudapp.net/GetDotnet?tfm=.NETFramework,Version=v4.6.2");
  37. return;
  38. }
  39. Utils.ReleaseMemory(true);
  40. using (Mutex mutex = new Mutex(false, $"Global\\Shadowsocks_{Application.StartupPath.GetHashCode()}"))
  41. {
  42. Application.SetUnhandledExceptionMode(UnhandledExceptionMode.CatchException);
  43. // handle UI exceptions
  44. Application.ThreadException += Application_ThreadException;
  45. // handle non-UI exceptions
  46. AppDomain.CurrentDomain.UnhandledException += CurrentDomain_UnhandledException;
  47. Application.ApplicationExit += Application_ApplicationExit;
  48. SystemEvents.PowerModeChanged += SystemEvents_PowerModeChanged;
  49. Application.EnableVisualStyles();
  50. Application.SetCompatibleTextRenderingDefault(false);
  51. if (!mutex.WaitOne(0, false))
  52. {
  53. Process[] oldProcesses = Process.GetProcessesByName("Shadowsocks");
  54. if (oldProcesses.Length > 0)
  55. {
  56. Process oldProcess = oldProcesses[0];
  57. }
  58. MessageBox.Show(I18N.GetString("Find Shadowsocks icon in your notify tray.")
  59. + Environment.NewLine
  60. + I18N.GetString("If you want to start multiple Shadowsocks, make a copy in another directory."),
  61. I18N.GetString("Shadowsocks is already running."));
  62. return;
  63. }
  64. Directory.SetCurrentDirectory(Application.StartupPath);
  65. #if DEBUG
  66. Logging.OpenLogFile();
  67. // truncate privoxy log file while debugging
  68. string privoxyLogFilename = Utils.GetTempPath("privoxy.log");
  69. if (File.Exists(privoxyLogFilename))
  70. using (new FileStream(privoxyLogFilename, FileMode.Truncate)) { }
  71. #else
  72. Logging.OpenLogFile();
  73. #endif
  74. MainController = new ShadowsocksController();
  75. MenuController = new MenuViewController(MainController);
  76. HotKeys.Init(MainController);
  77. MainController.Start();
  78. Application.Run();
  79. }
  80. }
  81. private static int exited = 0;
  82. private static void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
  83. {
  84. if (Interlocked.Increment(ref exited) == 1)
  85. {
  86. string errMsg = e.ExceptionObject.ToString();
  87. Logging.Error(errMsg);
  88. MessageBox.Show(
  89. $"{I18N.GetString("Unexpected error, shadowsocks will exit. Please report to")} https://github.com/shadowsocks/shadowsocks-windows/issues {Environment.NewLine}{errMsg}",
  90. "Shadowsocks non-UI Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
  91. Application.Exit();
  92. }
  93. }
  94. private static void Application_ThreadException(object sender, ThreadExceptionEventArgs e)
  95. {
  96. if (Interlocked.Increment(ref exited) == 1)
  97. {
  98. string errorMsg = $"Exception Detail: {Environment.NewLine}{e.Exception}";
  99. Logging.Error(errorMsg);
  100. MessageBox.Show(
  101. $"{I18N.GetString("Unexpected error, shadowsocks will exit. Please report to")} https://github.com/shadowsocks/shadowsocks-windows/issues {Environment.NewLine}{errorMsg}",
  102. "Shadowsocks UI Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
  103. Application.Exit();
  104. }
  105. }
  106. private static void SystemEvents_PowerModeChanged(object sender, PowerModeChangedEventArgs e)
  107. {
  108. switch (e.Mode)
  109. {
  110. case PowerModes.Resume:
  111. Logging.Info("os wake up");
  112. if (MainController != null)
  113. {
  114. System.Timers.Timer timer = new System.Timers.Timer(10 * 1000);
  115. timer.Elapsed += Timer_Elapsed;
  116. timer.AutoReset = false;
  117. timer.Enabled = true;
  118. timer.Start();
  119. }
  120. break;
  121. case PowerModes.Suspend:
  122. if (MainController != null)
  123. {
  124. MainController.Stop();
  125. Logging.Info("controller stopped");
  126. }
  127. Logging.Info("os suspend");
  128. break;
  129. }
  130. }
  131. private static void Timer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
  132. {
  133. try
  134. {
  135. if (MainController != null)
  136. {
  137. MainController.Start();
  138. Logging.Info("controller started");
  139. }
  140. }
  141. catch (Exception ex)
  142. {
  143. Logging.LogUsefulException(ex);
  144. }
  145. finally
  146. {
  147. try
  148. {
  149. System.Timers.Timer timer = (System.Timers.Timer)sender;
  150. timer.Enabled = false;
  151. timer.Stop();
  152. timer.Dispose();
  153. }
  154. catch (Exception ex)
  155. {
  156. Logging.LogUsefulException(ex);
  157. }
  158. }
  159. }
  160. private static void Application_ApplicationExit(object sender, EventArgs e)
  161. {
  162. // detach static event handlers
  163. Application.ApplicationExit -= Application_ApplicationExit;
  164. SystemEvents.PowerModeChanged -= SystemEvents_PowerModeChanged;
  165. Application.ThreadException -= Application_ThreadException;
  166. HotKeys.Destroy();
  167. if (MainController != null)
  168. {
  169. MainController.Stop();
  170. MainController = null;
  171. }
  172. }
  173. }
  174. }