WindowsApp.cs 7.4 KB

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