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

10 years ago
12 years ago
12 years ago
8 years ago
12 years ago
12 years ago
12 years ago
12 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
12 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. using System;
  2. using System.Diagnostics;
  3. using System.IO;
  4. using System.IO.Pipes;
  5. using System.Net;
  6. using System.Reflection;
  7. using System.Text;
  8. using System.Threading;
  9. using System.Threading.Tasks;
  10. using System.Windows.Forms;
  11. using CommandLine;
  12. using Microsoft.Win32;
  13. using NLog;
  14. using ReactiveUI;
  15. using Shadowsocks.Controller;
  16. using Shadowsocks.Controller.Hotkeys;
  17. using Shadowsocks.Util;
  18. using Shadowsocks.View;
  19. using Splat;
  20. using WPFLocalizeExtension.Engine;
  21. namespace Shadowsocks
  22. {
  23. internal static class Program
  24. {
  25. private static readonly Logger logger = LogManager.GetCurrentClassLogger();
  26. public static ShadowsocksController MainController { get; private set; }
  27. public static MenuViewController MenuController { get; private set; }
  28. public static CommandLineOption Options { get; private set; }
  29. public static string[] Args { get; private set; }
  30. // https://github.com/dotnet/runtime/issues/13051#issuecomment-510267727
  31. public static readonly string ExecutablePath = Process.GetCurrentProcess().MainModule?.FileName;
  32. public static readonly string WorkingDirectory = Path.GetDirectoryName(ExecutablePath);
  33. private static readonly Mutex mutex = new Mutex(true, $"Shadowsocks_{ExecutablePath.GetHashCode()}");
  34. /// <summary>
  35. /// 应用程序的主入口点。
  36. /// </summary>
  37. /// </summary>
  38. [STAThread]
  39. private static void Main(string[] args)
  40. {
  41. #region Single Instance and IPC
  42. bool hasAnotherInstance = !mutex.WaitOne(TimeSpan.Zero, true);
  43. // store args for further use
  44. Args = args;
  45. Parser.Default.ParseArguments<CommandLineOption>(args)
  46. .WithParsed(opt => Options = opt)
  47. .WithNotParsed(e => e.Output());
  48. if (hasAnotherInstance)
  49. {
  50. if (!string.IsNullOrWhiteSpace(Options.OpenUrl))
  51. {
  52. IPCService.RequestOpenUrl(Options.OpenUrl);
  53. }
  54. else
  55. {
  56. MessageBox.Show(I18N.GetString("Find Shadowsocks icon in your notify tray.")
  57. + Environment.NewLine
  58. + I18N.GetString("If you want to start multiple Shadowsocks, make a copy in another directory."),
  59. I18N.GetString("Shadowsocks is already running."));
  60. }
  61. return;
  62. }
  63. #endregion
  64. #region Enviroment Setup
  65. Directory.SetCurrentDirectory(WorkingDirectory);
  66. // todo: initialize the NLog configuartion
  67. Model.NLogConfig.TouchAndApplyNLogConfig();
  68. // .NET Framework 4.7.2 on Win7 compatibility
  69. ServicePointManager.SecurityProtocol |=
  70. SecurityProtocolType.Tls | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls12;
  71. #endregion
  72. #region Compactibility Check
  73. // Check OS since we are using dual-mode socket
  74. if (!Utils.IsWinVistaOrHigher())
  75. {
  76. MessageBox.Show(I18N.GetString("Unsupported operating system, use Windows Vista at least."),
  77. "Shadowsocks Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
  78. return;
  79. }
  80. // Check .NET Framework version
  81. if (!Utils.IsSupportedRuntimeVersion())
  82. {
  83. if (DialogResult.OK == MessageBox.Show(I18N.GetString("Unsupported .NET Framework, please update to {0} or later.", "4.7.2"),
  84. "Shadowsocks Error", MessageBoxButtons.OKCancel, MessageBoxIcon.Error))
  85. {
  86. Process.Start("https://dotnet.microsoft.com/download/dotnet-framework/net472");
  87. }
  88. return;
  89. }
  90. #endregion
  91. #region Event Handlers Setup
  92. Application.SetUnhandledExceptionMode(UnhandledExceptionMode.CatchException);
  93. // handle UI exceptions
  94. Application.ThreadException += Application_ThreadException;
  95. // handle non-UI exceptions
  96. AppDomain.CurrentDomain.UnhandledException += CurrentDomain_UnhandledException;
  97. Application.ApplicationExit += Application_ApplicationExit;
  98. SystemEvents.PowerModeChanged += SystemEvents_PowerModeChanged;
  99. Application.EnableVisualStyles();
  100. Application.SetCompatibleTextRenderingDefault(false);
  101. AutoStartup.RegisterForRestart(true);
  102. #endregion
  103. // We would use this in v5.
  104. // Parameters would have to be dropped from views' constructors (VersionUpdatePromptView)
  105. //Locator.CurrentMutable.RegisterViewsForViewModels(Assembly.GetCallingAssembly());
  106. // Workaround for hosting WPF controls in a WinForms app.
  107. // We have to manually set the culture for the LocalizeDictionary instance.
  108. // https://stackoverflow.com/questions/374518/localizing-a-winforms-application-with-embedded-wpf-user-controls
  109. // https://stackoverflow.com/questions/14668640/wpf-localize-extension-translate-window-at-run-time
  110. LocalizeDictionary.Instance.Culture = Thread.CurrentThread.CurrentCulture;
  111. #if DEBUG
  112. // truncate privoxy log file while debugging
  113. string privoxyLogFilename = Utils.GetTempPath("privoxy.log");
  114. if (File.Exists(privoxyLogFilename))
  115. using (new FileStream(privoxyLogFilename, FileMode.Truncate)) { }
  116. #endif
  117. MainController = new ShadowsocksController();
  118. MenuController = new MenuViewController(MainController);
  119. HotKeys.Init(MainController);
  120. MainController.Start();
  121. // Update online config
  122. Task.Run(async () =>
  123. {
  124. await Task.Delay(10 * 1000);
  125. await MainController.UpdateAllOnlineConfig();
  126. });
  127. #region IPC Handler and Arguement Process
  128. IPCService ipcService = new IPCService();
  129. Task.Run(() => ipcService.RunServer());
  130. ipcService.OpenUrlRequested += (_1, e) => MainController.AskAddServerBySSURL(e.Url);
  131. if (!string.IsNullOrWhiteSpace(Options.OpenUrl))
  132. {
  133. MainController.AskAddServerBySSURL(Options.OpenUrl);
  134. }
  135. #endregion
  136. Application.Run();
  137. }
  138. private static int exited = 0;
  139. private static void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
  140. {
  141. if (Interlocked.Increment(ref exited) == 1)
  142. {
  143. string errMsg = e.ExceptionObject.ToString();
  144. logger.Error(errMsg);
  145. MessageBox.Show(
  146. $"{I18N.GetString("Unexpected error, shadowsocks will exit. Please report to")} https://github.com/shadowsocks/shadowsocks-windows/issues {Environment.NewLine}{errMsg}",
  147. "Shadowsocks non-UI Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
  148. Application.Exit();
  149. }
  150. }
  151. private static void Application_ThreadException(object sender, ThreadExceptionEventArgs e)
  152. {
  153. if (Interlocked.Increment(ref exited) == 1)
  154. {
  155. string errorMsg = $"Exception Detail: {Environment.NewLine}{e.Exception}";
  156. logger.Error(errorMsg);
  157. MessageBox.Show(
  158. $"{I18N.GetString("Unexpected error, shadowsocks will exit. Please report to")} https://github.com/shadowsocks/shadowsocks-windows/issues {Environment.NewLine}{errorMsg}",
  159. "Shadowsocks UI Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
  160. Application.Exit();
  161. }
  162. }
  163. private static void SystemEvents_PowerModeChanged(object sender, PowerModeChangedEventArgs e)
  164. {
  165. switch (e.Mode)
  166. {
  167. case PowerModes.Resume:
  168. logger.Info("os wake up");
  169. if (MainController != null)
  170. {
  171. Task.Factory.StartNew(() =>
  172. {
  173. Thread.Sleep(10 * 1000);
  174. try
  175. {
  176. MainController.Start(true);
  177. logger.Info("controller started");
  178. }
  179. catch (Exception ex)
  180. {
  181. logger.LogUsefulException(ex);
  182. }
  183. });
  184. }
  185. break;
  186. case PowerModes.Suspend:
  187. if (MainController != null)
  188. {
  189. MainController.Stop();
  190. logger.Info("controller stopped");
  191. }
  192. logger.Info("os suspend");
  193. break;
  194. }
  195. }
  196. private static void Application_ApplicationExit(object sender, EventArgs e)
  197. {
  198. // detach static event handlers
  199. Application.ApplicationExit -= Application_ApplicationExit;
  200. SystemEvents.PowerModeChanged -= SystemEvents_PowerModeChanged;
  201. Application.ThreadException -= Application_ThreadException;
  202. HotKeys.Destroy();
  203. if (MainController != null)
  204. {
  205. MainController.Stop();
  206. MainController = null;
  207. }
  208. }
  209. }
  210. }