WindowsApp.cs 7.3 KB

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