WindowsApp.cs 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  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 FFMpegInstallInfo GetFfmpegInstallInfo()
  122. {
  123. var info = new FFMpegInstallInfo();
  124. info.FFMpegFilename = "ffmpeg.exe";
  125. info.FFProbeFilename = "ffprobe.exe";
  126. info.Version = "0";
  127. return info;
  128. }
  129. public void LaunchUrl(string url)
  130. {
  131. var process = new Process
  132. {
  133. StartInfo = new ProcessStartInfo
  134. {
  135. FileName = url
  136. },
  137. EnableRaisingEvents = true,
  138. };
  139. process.Exited += ProcessExited;
  140. try
  141. {
  142. process.Start();
  143. }
  144. catch (Exception ex)
  145. {
  146. _logger.ErrorException("Error launching url: {0}", ex, url);
  147. throw;
  148. }
  149. }
  150. public IDbConnector GetDbConnector()
  151. {
  152. return new DbConnector(_logger);
  153. }
  154. /// <summary>
  155. /// Processes the exited.
  156. /// </summary>
  157. /// <param name="sender">The sender.</param>
  158. /// <param name="e">The <see cref="EventArgs" /> instance containing the event data.</param>
  159. private static void ProcessExited(object sender, EventArgs e)
  160. {
  161. ((Process)sender).Dispose();
  162. }
  163. public void EnableLoopback(string appName)
  164. {
  165. LoopUtil.Run(appName);
  166. }
  167. public bool PortsRequireAuthorization(string applicationPath)
  168. {
  169. var appNameSrch = Path.GetFileName(applicationPath);
  170. var startInfo = new ProcessStartInfo
  171. {
  172. FileName = "netsh",
  173. Arguments = "advfirewall firewall show rule \"" + appNameSrch + "\"",
  174. CreateNoWindow = true,
  175. UseShellExecute = false,
  176. WindowStyle = ProcessWindowStyle.Hidden,
  177. ErrorDialog = false,
  178. RedirectStandardOutput = true
  179. };
  180. using (var process = Process.Start(startInfo))
  181. {
  182. process.Start();
  183. try
  184. {
  185. var data = process.StandardOutput.ReadToEnd() ?? string.Empty;
  186. if (data.IndexOf("Block", StringComparison.OrdinalIgnoreCase) != -1)
  187. {
  188. _logger.Info("Found potential windows firewall rule blocking Emby Server: " + data);
  189. }
  190. //var parts = data.Split('\n');
  191. //return parts.Length > 4;
  192. //return Confirm();
  193. return false;
  194. }
  195. catch (Exception ex)
  196. {
  197. _logger.ErrorException("Error querying windows firewall", ex);
  198. // Hate having to do this
  199. try
  200. {
  201. process.Kill();
  202. }
  203. catch (Exception ex1)
  204. {
  205. _logger.ErrorException("Error killing process", ex1);
  206. }
  207. throw;
  208. }
  209. }
  210. }
  211. }
  212. }