WindowsApp.cs 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  1. using System;
  2. using MediaBrowser.Common.Net;
  3. using MediaBrowser.Model.Logging;
  4. using MediaBrowser.Server.Startup.Common;
  5. using MediaBrowser.ServerApplication.Networking;
  6. using System.Collections.Generic;
  7. using System.Diagnostics;
  8. using System.IO;
  9. using System.Reflection;
  10. using System.Windows.Forms;
  11. using CommonIO;
  12. using MediaBrowser.Controller.Power;
  13. using MediaBrowser.Model.System;
  14. using MediaBrowser.Server.Implementations.Persistence;
  15. using MediaBrowser.Server.Startup.Common.FFMpeg;
  16. using OperatingSystem = MediaBrowser.Server.Startup.Common.OperatingSystem;
  17. namespace MediaBrowser.ServerApplication.Native
  18. {
  19. public class WindowsApp : INativeApp
  20. {
  21. private readonly IFileSystem _fileSystem;
  22. private readonly ILogger _logger;
  23. public WindowsApp(IFileSystem fileSystem, ILogger logger)
  24. {
  25. _fileSystem = fileSystem;
  26. _logger = logger;
  27. }
  28. public List<Assembly> GetAssembliesWithParts()
  29. {
  30. var list = new List<Assembly>();
  31. if (!System.Environment.Is64BitProcess)
  32. {
  33. //list.Add(typeof(PismoIsoManager).Assembly);
  34. }
  35. list.Add(GetType().Assembly);
  36. return list;
  37. }
  38. public void AuthorizeServer(int udpPort, int httpServerPort, int httpsPort, string applicationPath, string tempDirectory)
  39. {
  40. ServerAuthorization.AuthorizeServer(udpPort, httpServerPort, httpsPort, applicationPath, tempDirectory);
  41. }
  42. public NativeEnvironment Environment
  43. {
  44. get
  45. {
  46. return new NativeEnvironment
  47. {
  48. OperatingSystem = OperatingSystem.Windows,
  49. SystemArchitecture = System.Environment.Is64BitOperatingSystem ? Architecture.X64 : Architecture.X86,
  50. OperatingSystemVersionString = System.Environment.OSVersion.VersionString
  51. };
  52. }
  53. }
  54. public bool SupportsLibraryMonitor
  55. {
  56. get { return true; }
  57. }
  58. public bool SupportsRunningAsService
  59. {
  60. get
  61. {
  62. return true;
  63. }
  64. }
  65. public bool IsRunningAsService
  66. {
  67. get;
  68. set;
  69. }
  70. public bool CanSelfRestart
  71. {
  72. get
  73. {
  74. return MainStartup.CanSelfRestart;
  75. }
  76. }
  77. public bool SupportsAutoRunAtStartup
  78. {
  79. get
  80. {
  81. return true;
  82. }
  83. }
  84. public bool CanSelfUpdate
  85. {
  86. get
  87. {
  88. return MainStartup.CanSelfUpdate;
  89. }
  90. }
  91. public void Shutdown()
  92. {
  93. MainStartup.Shutdown();
  94. }
  95. public void Restart(StartupOptions startupOptions)
  96. {
  97. MainStartup.Restart();
  98. }
  99. public void ConfigureAutoRun(bool autorun)
  100. {
  101. var shortcutPath = Path.Combine(System.Environment.GetFolderPath(System.Environment.SpecialFolder.StartMenu), "Emby", "Emby Server.lnk");
  102. var startupPath = System.Environment.GetFolderPath(System.Environment.SpecialFolder.Startup);
  103. if (autorun)
  104. {
  105. //Copy our shortut into the startup folder for this user
  106. var targetPath = Path.Combine(startupPath, Path.GetFileName(shortcutPath) ?? "Emby Server.lnk");
  107. _fileSystem.CreateDirectory(Path.GetDirectoryName(targetPath));
  108. File.Copy(shortcutPath, targetPath, true);
  109. }
  110. else
  111. {
  112. //Remove our shortcut from the startup folder for this user
  113. _fileSystem.DeleteFile(Path.Combine(startupPath, Path.GetFileName(shortcutPath) ?? "Emby Server.lnk"));
  114. }
  115. }
  116. public INetworkManager CreateNetworkManager(ILogger logger)
  117. {
  118. return new NetworkManager(logger);
  119. }
  120. public void PreventSystemStandby()
  121. {
  122. MainStartup.Invoke(Standby.PreventSleep);
  123. }
  124. public void AllowSystemStandby()
  125. {
  126. MainStartup.Invoke(Standby.AllowSleep);
  127. }
  128. public IPowerManagement GetPowerManagement()
  129. {
  130. return new WindowsPowerManagement(_logger);
  131. }
  132. public FFMpegInstallInfo GetFfmpegInstallInfo()
  133. {
  134. var info = new FFMpegInstallInfo();
  135. info.FFMpegFilename = "ffmpeg.exe";
  136. info.FFProbeFilename = "ffprobe.exe";
  137. info.Version = "0";
  138. return info;
  139. }
  140. public void LaunchUrl(string url)
  141. {
  142. var process = new Process
  143. {
  144. StartInfo = new ProcessStartInfo
  145. {
  146. FileName = url
  147. },
  148. EnableRaisingEvents = true,
  149. };
  150. process.Exited += ProcessExited;
  151. try
  152. {
  153. process.Start();
  154. }
  155. catch (Exception ex)
  156. {
  157. _logger.ErrorException("Error launching url: {0}", ex, url);
  158. throw;
  159. }
  160. }
  161. public IDbConnector GetDbConnector()
  162. {
  163. return new DbConnector(_logger);
  164. }
  165. /// <summary>
  166. /// Processes the exited.
  167. /// </summary>
  168. /// <param name="sender">The sender.</param>
  169. /// <param name="e">The <see cref="EventArgs" /> instance containing the event data.</param>
  170. private static void ProcessExited(object sender, EventArgs e)
  171. {
  172. ((Process)sender).Dispose();
  173. }
  174. public void EnableLoopback(string appName)
  175. {
  176. LoopUtil.Run(appName);
  177. }
  178. private bool Confirm()
  179. {
  180. if (MainStartup._splash == null)
  181. {
  182. return false;
  183. }
  184. return MessageBox.Show(MainStartup._splash, "Emby has detected that a rule has been added to Windows Firewall that may prevent your other devices from accessing Emby Server. Click OK to remove this rule, or cancel to proceed anyway.", "Windows Firewall", MessageBoxButtons.OKCancel) == DialogResult.OK;
  185. }
  186. public bool PortsRequireAuthorization(string applicationPath)
  187. {
  188. var appNameSrch = Path.GetFileName(applicationPath);
  189. var startInfo = new ProcessStartInfo
  190. {
  191. FileName = "netsh",
  192. Arguments = "advfirewall firewall show rule \"" + appNameSrch + "\"",
  193. CreateNoWindow = true,
  194. UseShellExecute = false,
  195. WindowStyle = ProcessWindowStyle.Hidden,
  196. ErrorDialog = false,
  197. RedirectStandardOutput = true
  198. };
  199. using (var process = Process.Start(startInfo))
  200. {
  201. process.Start();
  202. try
  203. {
  204. var data = process.StandardOutput.ReadToEnd() ?? string.Empty;
  205. if (data.IndexOf("Block", StringComparison.OrdinalIgnoreCase) != -1)
  206. {
  207. _logger.Info("Found potential windows firewall rule blocking Emby Server: " + data);
  208. return Confirm();
  209. }
  210. //var parts = data.Split('\n');
  211. //return parts.Length > 4;
  212. //return Confirm();
  213. return false;
  214. }
  215. catch (Exception ex)
  216. {
  217. _logger.ErrorException("Error querying windows firewall", ex);
  218. // Hate having to do this
  219. try
  220. {
  221. process.Kill();
  222. }
  223. catch (Exception ex1)
  224. {
  225. _logger.ErrorException("Error killing process", ex1);
  226. }
  227. throw;
  228. }
  229. }
  230. }
  231. }
  232. }